From 2253f552da305082236b3938417547b7bdeb3b20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20=C5=BBuk?= Date: Thu, 22 Aug 2024 16:31:26 +0200 Subject: [PATCH] FIL-248 - double-check allowance with Glif --- src/bot/checkApplications.ts | 36 ++++++++++++++++++++++++++++++++---- src/config.ts | 1 + src/services/glifService.ts | 30 ++++++++++++++++++++++++++++++ src/types/types.ts | 6 ++++++ 4 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 src/services/glifService.ts diff --git a/src/bot/checkApplications.ts b/src/bot/checkApplications.ts index 4e6d9e6..9d8964e 100644 --- a/src/bot/checkApplications.ts +++ b/src/bot/checkApplications.ts @@ -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(); @@ -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); diff --git a/src/config.ts b/src/config.ts index cde03b6..20efe0a 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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", }; diff --git a/src/services/glifService.ts b/src/services/glifService.ts new file mode 100644 index 0000000..5a6ba1e --- /dev/null +++ b/src/services/glifService.ts @@ -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 => { + 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, + }; + } +}; diff --git a/src/types/types.ts b/src/types/types.ts index 4dde703..19b7ee8 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -227,3 +227,9 @@ export interface CheckApplicationReturn { newAllocationNeeded: boolean; amount?: string; } + +export interface GetVerifiedClientStatusReturn { + success: boolean; + error: string; + data: string; +}