UNPKG

allo-monad-ray

Version:

Monad version of Allo v2 SDK

359 lines (358 loc) 12.1 kB
import { encodeAbiParameters, encodeFunctionData, extractChain, getContract, parseAbiParameters, } from "viem"; import { Allo } from "../../Allo/Allo"; import { abi as alloAbi } from "../../Allo/allo.config"; import { create } from "../../Client/Client"; import { supportedChains } from "../../chains.config"; import { ZERO_ADDRESS, } from "../../types"; import { abi as directGrantsAbi, bytecode as directGrantsBytecode, } from "./directGrants.config"; export class DirectGrantsStrategy { constructor({ chain, rpc, address, poolId }) { const usedChain = extractChain({ chains: supportedChains, id: chain, }); this.client = create(usedChain, rpc); this.allo = new Allo({ chain, rpc }); if (address) { this.contract = getContract({ address: address, abi: directGrantsAbi, client: { public: this.client, } }); this.strategy = address; } this.poolId = poolId || BigInt(-1); } // Get the DirectGrants strategy InitializeData getInitializeData(params) { const encoded = encodeAbiParameters(parseAbiParameters("bool,bool,bool,uint128,uint128"), [ params.registryGating, params.metadataRequired, params.grantAmountRequired, params.registrationStartTime, params.registrationEndTime, ]); return encoded; } getDeployParams() { const constructorArgs = encodeAbiParameters(parseAbiParameters("address,string"), [this.allo.address(), "DirectGrantsSimpleStrategy1.1"]); const constructorArgsNo0x = constructorArgs.slice(2); return { abi: directGrantsAbi, bytecode: (directGrantsBytecode + constructorArgsNo0x), }; } async setPoolId(poolId) { this.poolId = poolId; const strategyAddress = await this.allo.getStrategy(poolId); this.setContract(strategyAddress); } setContract(address) { this.contract = getContract({ address: address, abi: directGrantsAbi, client: { public: this.client, } }); this.strategy = address; } checkPoolId() { if (this.poolId === BigInt(-1)) throw new Error("DirectGrantsStrategy: No poolId provided. Please call `setPoolId` first."); } checkStrategy() { if (!this.strategy) throw new Error("DirectGrantsStrategy: No strategy address provided. Please call `setContract` first."); } async getNative() { this.checkStrategy(); const native = await this.contract.read.NATIVE(); return native; } async getAllocatedGrantAmount() { this.checkStrategy(); const amount = await this.contract.read.allocatedGrantAmount(); return amount; } async getGrantAmountRequired() { this.checkStrategy(); const required = await this.contract.read.grantAmountRequired(); return required; } async getMetadataRequired() { this.checkStrategy(); const required = await this.contract.read.metadataRequired(); return required; } async getAllo() { return this.contract.read.getAllo(); } async getPoolAmount() { this.checkStrategy(); const amount = await this.contract.read.getPoolAmount(); return amount; } async getPoolId() { this.checkStrategy(); const poolId = await this.contract.read.getPoolId(); return poolId; } async getRecipient(recipientId) { this.checkStrategy(); const recipient = await this.contract.read.getRecipient([recipientId]); return recipient; } async getRecipientStatus(recipientId) { this.checkStrategy(); const status = await this.contract.read.getRecipientStatus([recipientId]); return status; } async getStrategyId() { this.checkStrategy(); const id = await this.contract.read.getStrategyId(); return id; } async isPoolActive() { this.checkStrategy(); const active = await this.contract.read.isPoolActive(); return active; } async isValidAllocator(allocatorAddress) { this.checkStrategy(); const valid = await this.contract.read.isValidAllocator([allocatorAddress]); return valid; } async useRegistryAnchor() { this.checkStrategy(); const useRegistryAnchor = await this.contract.read.useRegistryAnchor(); return useRegistryAnchor; } async getMilestoneStatus(recipientId, milestoneId) { this.checkStrategy(); const status = await this.contract.read.getMilestoneStatus([ recipientId, BigInt(milestoneId), ]); return status; } async getMilestones(recipientAddress) { this.checkStrategy(); const milestones = await this.contract.read.getMilestones([ recipientAddress, ]); return milestones; } async getUpcomingMilestone(recipientAddress) { this.checkStrategy(); const milestone = await this.contract.read.upcomingMilestone([ recipientAddress, ]); return milestone; } async getPayouts(recipientIds) { this.checkStrategy(); const emptyData = Array(recipientIds.length).fill("0x"); const payouts = await this.contract.read.getPayouts([ recipientIds, emptyData, ]); const payoutSummary = payouts.map((payout) => { this.checkStrategy(); return { address: payout.recipientAddress, amount: payout.amount, }; }); return payoutSummary; } // Write methods getSetMilestonesData(recipientId, milestones) { this.checkPoolId(); const encodedData = encodeFunctionData({ abi: directGrantsAbi, functionName: "setMilestones", args: [recipientId, milestones], }); return { to: this.strategy, data: encodedData, value: "0", }; } getReviewSetMilestonesData(recipientId, status, milestoneHash) { this.checkPoolId(); // todo: add milestone hash logic const encoded = encodeFunctionData({ abi: directGrantsAbi, functionName: "reviewSetMilestones", args: [recipientId, status, milestoneHash], }); return { to: this.strategy, data: encoded, value: "0", }; } getSubmitMilestonesData(recipientId, milestoneId, metadata) { this.checkPoolId(); const encodedData = encodeFunctionData({ abi: directGrantsAbi, functionName: "submitMilestone", args: [recipientId, BigInt(milestoneId), metadata], }); return { to: this.strategy, data: encodedData, value: "0", }; } getRejectMilestoneData(recipientId, milestoneId) { this.checkPoolId(); const encodedData = encodeFunctionData({ abi: directGrantsAbi, functionName: "rejectMilestone", args: [recipientId, BigInt(milestoneId)], }); return { to: this.strategy, data: encodedData, value: "0", }; } getSetRecipientStatusToInReviewData(recipientIds) { this.checkPoolId(); const encodedData = encodeFunctionData({ abi: directGrantsAbi, functionName: "setRecipientStatusToInReview", args: [recipientIds], }); return { to: this.strategy, data: encodedData, value: "0", }; } getSetPoolActiveData(flag) { this.checkPoolId(); const encodedData = encodeFunctionData({ abi: directGrantsAbi, functionName: "setPoolActive", args: [flag], }); return { to: this.strategy, data: encodedData, value: "0", }; } getRegisterRecipientData(data) { this.checkPoolId(); const encoded = encodeAbiParameters(parseAbiParameters("address,address,uint256,(uint256,string)"), [ data.registryAnchor || ZERO_ADDRESS, data.recipientAddress, data.grantAmount, [data.metadata.protocol, data.metadata.pointer], ]); const encodedData = encodeFunctionData({ abi: alloAbi, functionName: "registerRecipient", args: [this.poolId, encoded], }); return { to: this.allo.address(), data: encodedData, value: "0", }; } getBatchRegisterRecipientData(data) { this.checkPoolId(); const encodedParams = []; data.forEach((registerData) => { const encoded = encodeAbiParameters(parseAbiParameters("address,address,uint256,(uint256,string)"), [ registerData.registryAnchor || ZERO_ADDRESS, registerData.recipientAddress, registerData.grantAmount, [registerData.metadata.protocol, registerData.metadata.pointer], ]); encodedParams.push(encoded); }); const poolIds = Array(encodedParams.length).fill(this.poolId); const encodedData = encodeFunctionData({ abi: alloAbi, functionName: "batchRegisterRecipient", args: [poolIds, encodedParams], }); return { to: this.allo.address(), data: encodedData, value: "0", }; } getAllocationData(recipientId, status, grantAmount) { this.checkPoolId(); const encoded = encodeAbiParameters(parseAbiParameters("address,uint8,uint256"), [recipientId, status, grantAmount]); const encodedData = encodeFunctionData({ abi: alloAbi, functionName: "allocate", args: [this.poolId, encoded], }); return { to: this.allo.address(), data: encodedData, value: "0", }; } getBatchAllocationData(allocations) { this.checkPoolId(); const encodedParams = []; allocations.forEach((allocation) => { const encoded = encodeAbiParameters(parseAbiParameters("address,uint8,uint256"), [ allocation.recipientId, allocation.status, BigInt(allocation.grantAmount), ]); encodedParams.push(encoded); }); const poolIds = Array(encodedParams.length).fill(this.poolId); const encodedData = encodeFunctionData({ abi: alloAbi, functionName: "batchAllocate", args: [poolIds, encodedParams], }); return { to: this.allo.address(), data: encodedData, value: "0", }; } getDistributeData(recipientIds) { this.checkPoolId(); const encodedData = encodeFunctionData({ abi: alloAbi, functionName: "distribute", args: [this.poolId, recipientIds, "0x"], }); return { to: this.allo.address(), data: encodedData, value: "0", }; } getUpdatePoolTimestampsData(registrationStartTime, registrationEndTime) { this.checkStrategy(); const encodedData = encodeFunctionData({ abi: directGrantsAbi, functionName: "updatePoolTimestamps", args: [registrationStartTime, registrationEndTime], }); return { to: this.strategy, data: encodedData, value: "0", }; } }