Skip to content

Commit

Permalink
feat: add auto transfer configs
Browse files Browse the repository at this point in the history
  • Loading branch information
hhio618 committed Jan 7, 2025
1 parent 5bffc14 commit 03fe0ee
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
35 changes: 35 additions & 0 deletions src/helpers/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,38 @@ export async function getErc20TokenSymbol(networkId: number, tokenAddress: strin
const contract = new ethers.Contract(tokenAddress, abi, provider);
return await contract.symbol();
}

/**
* Returns ERC20 token balance of the funding wallet
* @param networkId Network id
* @param tokenAddress ERC20 token address
* @param fundingWalledAddress funding wallet address
* @returns ERC20 token balance of the funding wallet
*/
export async function getFundingWalletBalance(networkId: number, tokenAddress: string, fundingWalledAddress: string) {
const abi = ["function balanceOf(address) view returns (uint256)"];

// get fastest RPC
const config: HandlerConstructorConfig = {
networkName: null,
networkRpcs: null,
proxySettings: {
retryCount: 5,
retryDelay: 500,
logTier: null,
logger: null,
strictLogs: false,
},
runtimeRpcs: null,
networkId: String(networkId) as NetworkId,
rpcTimeout: 1500,
autoStorage: false,
cacheRefreshCycles: 10,
};
const handler = new RPCHandler(config);
const provider = await handler.getFastestRpcProvider();

// fetch token symbol
const contract = new ethers.Contract(tokenAddress, abi, provider);
return await contract.balanceOf(fundingWalledAddress);
}
28 changes: 28 additions & 0 deletions src/parser/permit-generation-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { EnvConfig } from "../types/env-type";
import { BaseModule } from "../types/module";
import { Result } from "../types/results";
import { isAdmin, isCollaborative } from "../helpers/checkers";
import { getFundingWalletBalance } from "../helpers/web3";

interface Payload {
evmNetworkId: number;
Expand All @@ -36,6 +37,10 @@ interface Payload {

export class PermitGenerationModule extends BaseModule {
readonly _configuration: PermitGenerationConfiguration | null = this.context.config.incentives.permitGeneration;
readonly _autoTransferMode: boolean = this.context.config.automaticTransferMode;
readonly _fudningWalletAddress: string = this.context.config.fundingWalletAddress;
readonly _evmNetworkId: number = this.context.config.evmNetworkId;
readonly _erc20RewardToken: string = this.context.config.erc20RewardToken;
readonly _supabase = createClient<Database>(this.context.env.SUPABASE_URL, this.context.env.SUPABASE_KEY);

async transform(data: Readonly<IssueActivity>, result: Result): Promise<Result> {
Expand All @@ -45,6 +50,21 @@ export class PermitGenerationModule extends BaseModule {
this.context.logger.error("[PermitGenerationModule] Non collaborative issue detected, skipping.");
return Promise.resolve(result);
}

const sumPayouts = await this._sumPayouts(result);
const fundingWalletBalance = await getFundingWalletBalance(
this._evmNetworkId,
this._erc20RewardToken,
this._fudningWalletAddress
);

if (this._autoTransferMode && sumPayouts < fundingWalletBalance) {
this.context.logger.debug(
"[PermitGenerationModule] AutoTransforMode is enabled and there are enough funds, skipping."
);
return Promise.resolve(result);
}

const payload: Context["payload"] & Payload = {
...context.payload.inputs,
issueUrl: this.context.payload.issue.html_url,
Expand Down Expand Up @@ -207,6 +227,14 @@ export class PermitGenerationModule extends BaseModule {
return isCollaborative(data);
}

async _sumPayouts(result: Result) {
let sumPayouts = 0;
for (const value of Object.values(result)) {
sumPayouts += value.total;
}
return sumPayouts;
}

_deductFeeFromReward(
result: Result,
treasuryGithubData: RestEndpointMethodTypes["users"]["getByUsername"]["response"]["data"]
Expand Down
18 changes: 17 additions & 1 deletion src/types/plugin-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,29 @@ export const pluginSettingsSchema = T.Object(
* The encrypted key to use for permit generation
*/
evmPrivateEncrypted: T.String({
description: "The encrypted key to use for permit generation",
description: "The encrypted key to use for permit generation and auto transfers",
examples: ["0x000..."],
}),
/**
* Funding wallet address (corresponding to evmPrivateEncrypted)
*/
fundingWalletAddress: T.String({
description: "The funding wallet address",
examples: ["0x000..."],
}),
/**
* Reward token for ERC20 permits, default WXDAI for gnosis chain
*/
erc20RewardToken: T.String({ default: "0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d" }),
/**
* If set to false or if there are insufficient funds to settle the payment,
* permits will be generated instead of processing direct payouts.
*/
automaticTransferMode: T.Boolean({
default: true,
description:
"If set to false or if there are insufficient funds to settle the payment, permits will be generated instead of processing direct payouts.",
}),
incentives: T.Object(
{
/**
Expand Down

0 comments on commit 03fe0ee

Please sign in to comment.