UNPKG

allo-monad-ray

Version:

Monad version of Allo v2 SDK

363 lines (362 loc) 12.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DirectGrantsStrategy = void 0; const viem_1 = require("viem"); const Allo_1 = require("../../Allo/Allo"); const allo_config_1 = require("../../Allo/allo.config"); const Client_1 = require("../../Client/Client"); const chains_config_1 = require("../../chains.config"); const types_1 = require("../../types"); const directGrants_config_1 = require("./directGrants.config"); class DirectGrantsStrategy { constructor({ chain, rpc, address, poolId }) { const usedChain = (0, viem_1.extractChain)({ chains: chains_config_1.supportedChains, id: chain, }); this.client = (0, Client_1.create)(usedChain, rpc); this.allo = new Allo_1.Allo({ chain, rpc }); if (address) { this.contract = (0, viem_1.getContract)({ address: address, abi: directGrants_config_1.abi, client: { public: this.client, } }); this.strategy = address; } this.poolId = poolId || BigInt(-1); } // Get the DirectGrants strategy InitializeData getInitializeData(params) { const encoded = (0, viem_1.encodeAbiParameters)((0, viem_1.parseAbiParameters)("bool,bool,bool,uint128,uint128"), [ params.registryGating, params.metadataRequired, params.grantAmountRequired, params.registrationStartTime, params.registrationEndTime, ]); return encoded; } getDeployParams() { const constructorArgs = (0, viem_1.encodeAbiParameters)((0, viem_1.parseAbiParameters)("address,string"), [this.allo.address(), "DirectGrantsSimpleStrategy1.1"]); const constructorArgsNo0x = constructorArgs.slice(2); return { abi: directGrants_config_1.abi, bytecode: (directGrants_config_1.bytecode + constructorArgsNo0x), }; } async setPoolId(poolId) { this.poolId = poolId; const strategyAddress = await this.allo.getStrategy(poolId); this.setContract(strategyAddress); } setContract(address) { this.contract = (0, viem_1.getContract)({ address: address, abi: directGrants_config_1.abi, 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 = (0, viem_1.encodeFunctionData)({ abi: directGrants_config_1.abi, 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 = (0, viem_1.encodeFunctionData)({ abi: directGrants_config_1.abi, functionName: "reviewSetMilestones", args: [recipientId, status, milestoneHash], }); return { to: this.strategy, data: encoded, value: "0", }; } getSubmitMilestonesData(recipientId, milestoneId, metadata) { this.checkPoolId(); const encodedData = (0, viem_1.encodeFunctionData)({ abi: directGrants_config_1.abi, functionName: "submitMilestone", args: [recipientId, BigInt(milestoneId), metadata], }); return { to: this.strategy, data: encodedData, value: "0", }; } getRejectMilestoneData(recipientId, milestoneId) { this.checkPoolId(); const encodedData = (0, viem_1.encodeFunctionData)({ abi: directGrants_config_1.abi, functionName: "rejectMilestone", args: [recipientId, BigInt(milestoneId)], }); return { to: this.strategy, data: encodedData, value: "0", }; } getSetRecipientStatusToInReviewData(recipientIds) { this.checkPoolId(); const encodedData = (0, viem_1.encodeFunctionData)({ abi: directGrants_config_1.abi, functionName: "setRecipientStatusToInReview", args: [recipientIds], }); return { to: this.strategy, data: encodedData, value: "0", }; } getSetPoolActiveData(flag) { this.checkPoolId(); const encodedData = (0, viem_1.encodeFunctionData)({ abi: directGrants_config_1.abi, functionName: "setPoolActive", args: [flag], }); return { to: this.strategy, data: encodedData, value: "0", }; } getRegisterRecipientData(data) { this.checkPoolId(); const encoded = (0, viem_1.encodeAbiParameters)((0, viem_1.parseAbiParameters)("address,address,uint256,(uint256,string)"), [ data.registryAnchor || types_1.ZERO_ADDRESS, data.recipientAddress, data.grantAmount, [data.metadata.protocol, data.metadata.pointer], ]); const encodedData = (0, viem_1.encodeFunctionData)({ abi: allo_config_1.abi, 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 = (0, viem_1.encodeAbiParameters)((0, viem_1.parseAbiParameters)("address,address,uint256,(uint256,string)"), [ registerData.registryAnchor || types_1.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 = (0, viem_1.encodeFunctionData)({ abi: allo_config_1.abi, functionName: "batchRegisterRecipient", args: [poolIds, encodedParams], }); return { to: this.allo.address(), data: encodedData, value: "0", }; } getAllocationData(recipientId, status, grantAmount) { this.checkPoolId(); const encoded = (0, viem_1.encodeAbiParameters)((0, viem_1.parseAbiParameters)("address,uint8,uint256"), [recipientId, status, grantAmount]); const encodedData = (0, viem_1.encodeFunctionData)({ abi: allo_config_1.abi, 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 = (0, viem_1.encodeAbiParameters)((0, viem_1.parseAbiParameters)("address,uint8,uint256"), [ allocation.recipientId, allocation.status, BigInt(allocation.grantAmount), ]); encodedParams.push(encoded); }); const poolIds = Array(encodedParams.length).fill(this.poolId); const encodedData = (0, viem_1.encodeFunctionData)({ abi: allo_config_1.abi, functionName: "batchAllocate", args: [poolIds, encodedParams], }); return { to: this.allo.address(), data: encodedData, value: "0", }; } getDistributeData(recipientIds) { this.checkPoolId(); const encodedData = (0, viem_1.encodeFunctionData)({ abi: allo_config_1.abi, functionName: "distribute", args: [this.poolId, recipientIds, "0x"], }); return { to: this.allo.address(), data: encodedData, value: "0", }; } getUpdatePoolTimestampsData(registrationStartTime, registrationEndTime) { this.checkStrategy(); const encodedData = (0, viem_1.encodeFunctionData)({ abi: directGrants_config_1.abi, functionName: "updatePoolTimestamps", args: [registrationStartTime, registrationEndTime], }); return { to: this.strategy, data: encodedData, value: "0", }; } } exports.DirectGrantsStrategy = DirectGrantsStrategy;