Skip to content

Commit

Permalink
FIL-248 - double-check allowance with Glif
Browse files Browse the repository at this point in the history
  • Loading branch information
kacperzuk-neti committed Aug 22, 2024
1 parent 82509c6 commit aa3a170
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
36 changes: 32 additions & 4 deletions src/bot/checkApplications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from "../types/types";
import { config } from "../config";
import { anyToBytes, calculateAllocationToRequest } from "../utils/utils";
import { getVerifiedClientStatus } from "../services/glifService";

const metrics = Metrics.getInstance();

Expand Down Expand Up @@ -132,19 +133,46 @@ export const checkApplication = async (
logGeneral(
`${config.logPrefix} ${
application.ID
} datacap remaining / datacap allocated: ${(margin * 100).toFixed(
} datacap remaining (DMOB) / datacap allocated: ${(margin * 100).toFixed(
2,
)}% - doesn't need more allowance`,
);
return;
}

// double check, as remainingDatacap from DMOB is often outdated
const {
data: glifRemainingDatacap,
success,
error,
} = await getVerifiedClientStatus(client?.addressId);
if (!success) {
logError(error);
return;
}

const glifMargin = computeMargin(
glifRemainingDatacap,
lastRequestAllowance["Allocation Amount"],
);

if (glifMargin > 0.25) {
logGeneral(
`${config.logPrefix} ${
application.ID
} datacap remaining (Glif) / datacap allocated: ${(
glifMargin * 100
).toFixed(2)}% - doesn't need more allowance`,
);
return;
}

logGeneral(
`${config.logPrefix} ${
application.ID
} datacap remaining / datacap allocated: ${(margin * 100).toFixed(
2,
)}% - Needs more allowance`,
} datacap remaining (Glif) / datacap allocated: ${(
glifMargin * 100
).toFixed(2)}% - Needs more allowance`,
);
const amountToRequest = calculateAmountToRequest(application);
await requestAllowance(application, owner, repo, amountToRequest);
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export const config = {
awsAccessKeyId: process.env.AWS_ACCESS_KEY_ID ?? "",
awsSecretAccessKey: process.env.AWS_SECRET_ACCESS_KEY ?? "",
networkType: process.env.NETWORK_TYPE ?? "production",
glifApi: process.env.GLIF_API_URL ?? "https://api.node.glif.io/rpc/v0",
};
30 changes: 30 additions & 0 deletions src/services/glifService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import axios from "axios";
import { config } from "../config";
import { logDebug } from "../utils/consoleLogger";
import { type GetVerifiedClientStatusReturn } from "types";

export const getVerifiedClientStatus = async (
clientId: string,
): Promise<GetVerifiedClientStatusReturn> => {
logDebug(`Requesting allocators from backend`);
try {
const response = await axios.post(config.glifApi, {
jsonrpc: "2.0",
method: "Filecoin.StateVerifiedClientStatus",
params: [clientId, null],
id: 0,
});
return {
data: response.data.result,
error: "",
success: true,
};
} catch (error) {
const errMessage = `Error accessing Glif API Filecoin.StateVerifiedClientStatus: ${error.message}`;
return {
data: "",
error: errMessage,
success: false,
};
}
};
6 changes: 6 additions & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,9 @@ export interface CheckApplicationReturn {
newAllocationNeeded: boolean;
amount?: string;
}

export interface GetVerifiedClientStatusReturn {
success: boolean;
error: string;
data: string;
}

0 comments on commit aa3a170

Please sign in to comment.