UNPKG

allo-monad-ray

Version:

Monad version of Allo v2 SDK

459 lines (458 loc) 16 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 { abi as microGrantsAbi, bytecode as microGrantsBytecode, } from "./microGrants.config"; import { abi as microGrantsGovAbi, bytecode as microGrantsGovBytecode, } from "./microGrantsGov.config"; import { abi as microGrantsHatsAbi, bytecode as microGrantsHatsBytecode, } from "./microGrantsHats.config"; import { ZERO_ADDRESS, } from "../../Common/types"; import { supportedChains } from "../../chains.config"; import { StrategyType, } from "./types"; export class MicroGrantsStrategy { constructor({ chain, rpc, address, poolId }) { const usedChain = extractChain({ chains: supportedChains, id: chain, }); this.client = create(usedChain, rpc); this.allo = new Allo({ chain, rpc }); // to call allocate if (address) { this.contract = getContract({ address: address, abi: microGrantsAbi, client: { public: this.client, } }); this.strategy = address; } this.poolId = poolId || BigInt(-1); } async setPoolId(poolId) { this.poolId = poolId; const strategyAddress = await this.allo.getStrategy(poolId); this.setContract(strategyAddress); } setContract(address) { this.contract = getContract({ address: address, abi: microGrantsAbi, client: { public: this.client, } }); this.strategy = address; } checkPoolId() { if (this.poolId === BigInt(-1)) throw new Error("MicroGrantsStrategy: No poolId provided. Please call `setPoolId` first."); } checkStrategy() { if (!this.strategy) throw new Error("MicroGrantsStrategy: No strategy address provided. Please call `setContract` first."); } async getNative() { this.checkStrategy(); const native = await this.contract.read.NATIVE(); return native; } async allocator(allocatorAddress) { this.checkStrategy(); const allocator = await this.contract.read.allocators([allocatorAddress]); return allocator; } async allocated(allocatorAddress, recipientAddress) { this.checkStrategy(); const allocated = await this.contract.read.allocated([ allocatorAddress, recipientAddress, ]); return allocated; } async allocationEndTime() { this.checkStrategy(); const endTime = await this.contract.read.allocationEndTime(); return endTime; } async allocationStartTime() { this.checkStrategy(); const startTime = await this.contract.read.allocationStartTime(); return startTime; } async approvalThreshold() { this.checkStrategy(); const threshold = await this.contract.read.approvalThreshold(); return threshold; } async getAllo() { return this.allo; } 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; } 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 recipientAllocations(recipientId, status) { this.checkStrategy(); const allocations = await this.contract.read.recipientAllocations([ recipientId, status, ]); return allocations; } async maxRequestedAmount() { this.checkStrategy(); const maxRequestedAmount = await this.contract.read.maxRequestedAmount(); return maxRequestedAmount; } async useRegistryAnchor() { this.checkStrategy(); const useRegistryAnchor = await this.contract.read.useRegistryAnchor(); return useRegistryAnchor; } async getHatsAddress() { this.checkStrategy(); const contractReader = getContract({ address: this.strategy, abi: microGrantsHatsAbi, client: { public: this.client, } }); const hatsAddress = (await contractReader.read.hats()); return hatsAddress; } async getHatId() { this.checkStrategy(); const contractReader = getContract({ address: this.strategy, abi: microGrantsHatsAbi, client: { public: this.client, } }); const hatId = (await contractReader.read.hatId()); return hatId; } async getGovAddress() { this.checkStrategy(); const contractReader = getContract({ address: this.strategy, abi: microGrantsGovAbi, client: { public: this.client, } }); const govAddress = (await contractReader.read.gov()); return govAddress; } async getSnapshotReference() { this.checkStrategy(); const contractReader = getContract({ address: this.strategy, abi: microGrantsGovAbi, client: { public: this.client, } }); const reference = (await contractReader.read.snapshotReference()); return reference; } async getMinimumVotePower() { this.checkStrategy(); const contractReader = getContract({ address: this.strategy, abi: microGrantsGovAbi, client: { public: this.client, } }); const votePower = (await contractReader.read.minVotePower()); return votePower; } async getStrategyContractId(strategyContract) { const contractReader = getContract({ address: strategyContract, abi: microGrantsGovAbi, client: { public: this.client, } }); const strategyId = await contractReader.read.getStrategyId(); switch (strategyId) { case "0x697f0592ebd05466d2d24454477e11d69c475d7a7c4134f15ddc1ea9811bb16f": return { strategyId: strategyId, }; case "0x741ac1e2f387d83f219f6b5349d35ec34902cf94019d117335e0045d2e0ed912": return { strategyId: strategyId, }; case "0x5aa24dcfcd55a1e059a172e987b3456736b4856c71e57aaf52e9a965897318dd": return { strategyId: strategyId, }; case "0xb87f34c0968bd74d43a6a5b72831a5ea733a4783a026b9fc9b1d17adf51214d2": return { strategyId: strategyId, }; case "0x414f2ea9b91b8ee2e35a380fa0af0e14079832cc93530a61a4893b3dbf0a9aba": return { strategyId: strategyId, }; case "0xed28ce0387d1786c1a38404047e9eecc4d1dcaeff695b867e912483e36c3d770": return { strategyId: strategyId, }; case "0xc5263e972c91d7ff40708bc71239a2b6cbc8768704e210ca3069e2e11fc195df": return { strategyId: strategyId, }; case "0xecc48557f4826bd1181a4495232d6d07f248ef9cc0a650e64520f6c9f7458a8c": return { strategyId: strategyId, }; default: return { strategyId: "0x", }; } } async getInitializeData(params) { const encoded = encodeAbiParameters(parseAbiParameters("bool, uint64, uint64, uint256, uint256"), [ params.useRegistryAnchor, params.allocationStartTime, params.allocationEndTime, params.approvalThreshold, params.maxRequestedAmount, ]); return encoded; } async getInitializeDataHats(params) { const encoded = encodeAbiParameters(parseAbiParameters("(bool, uint64, uint64, uint256, uint256), address, uint256"), [ [ params.useRegistryAnchor, params.allocationStartTime, params.allocationEndTime, params.approvalThreshold, params.maxRequestedAmount, ], params.hats, params.hatId, ]); return encoded; } async getInitializeDataGov(params) { const encoded = encodeAbiParameters(parseAbiParameters("bool, uint64, uint64, uint256, uint256, address, uint256, uint256"), [ params.useRegistryAnchor, params.allocationStartTime, params.allocationEndTime, params.approvalThreshold, params.maxRequestedAmount, params.gov, params.snapshotReference, params.minVotePower, ]); console.log(params); return encoded; } getDeployParams(strategyType) { const constructorArgs = encodeAbiParameters(parseAbiParameters("address, string"), [this.allo.address(), strategyType]); const constructorArgsNo0x = constructorArgs.slice(2); // create the proper bytecode const bytecode = strategyType == StrategyType.Gov ? microGrantsGovBytecode : strategyType == StrategyType.Hats ? microGrantsHatsBytecode : microGrantsBytecode; const abi = strategyType == StrategyType.Gov ? microGrantsGovAbi : strategyType == StrategyType.Hats ? microGrantsHatsAbi : microGrantsAbi; return { abi: abi, bytecode: (bytecode + constructorArgsNo0x), }; } getBatchAllocationData(allocations) { this.checkPoolId(); const encodedParams = []; allocations.forEach((allocation) => { const encoded = encodeAbiParameters(parseAbiParameters("address, uint8"), [allocation.recipientId, allocation.status]); 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", }; } getAllocationData(recipientId, status) { this.checkPoolId(); const encoded = encodeAbiParameters(parseAbiParameters("address, uint8"), [recipientId, status]); const encodedData = encodeFunctionData({ abi: alloAbi, functionName: "allocate", args: [this.poolId, encoded], }); return { to: this.allo.address(), data: encodedData, value: "0", }; } getRegisterRecipientData(data) { this.checkPoolId(); const encoded = encodeAbiParameters(parseAbiParameters("address, address, uint256, (uint256, string)"), [ data.registryAnchor || ZERO_ADDRESS, data.recipientAddress, data.requestedAmount, [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.requestedAmount, [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", }; } getIncreasemaxRequestedAmountData(amount) { this.checkStrategy(); const encodedData = encodeFunctionData({ abi: microGrantsAbi, functionName: "increasemaxRequestedAmountAllowed", args: [amount], }); return { to: this.strategy, data: encodedData, value: "0", }; } getSetAllocatorData(data) { this.checkStrategy(); const encodedData = encodeFunctionData({ abi: microGrantsAbi, functionName: "setAllocator", args: [data.allocatorAddress, data.flag], }); return { to: this.strategy, data: encodedData, value: "0", }; } getBatchSetAllocatorData(data) { this.checkStrategy(); const allocatorArray = []; const flagArray = []; data.map((allocator) => { allocatorArray.push(allocator.allocatorAddress); flagArray.push(allocator.flag); }); const encodedData = encodeFunctionData({ abi: microGrantsAbi, functionName: "batchSetAllocator", args: [allocatorArray, flagArray], }); return { to: this.strategy, data: encodedData, value: "0", }; } getUpdatePoolTimestampsData(allocationStartTime, allocationEndTime) { this.checkStrategy(); const encodedData = encodeFunctionData({ abi: microGrantsAbi, functionName: "updatePoolTimestamps", args: [allocationStartTime, allocationEndTime], }); return { to: this.strategy, data: encodedData, value: "0", }; } }