UNPKG

allo-monad-ray

Version:

Monad version of Allo v2 SDK

389 lines (388 loc) 12.9 kB
import { encodeAbiParameters, encodeFunctionData, extractChain, getContract, parseAbiParameters, } from "viem"; import { Allo } from "../../Allo/Allo"; import { supportedChains } from "../../chains.config"; import { create } from "../../Client/Client"; import { abi as superfluidAbi, bytecode as superfluidBytecode, } from "./superfluid.config"; import { abi as alloAbi } from "../../Allo/allo.config"; export class SQFSuperFluidStrategy { 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: superfluidAbi, 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: superfluidAbi, client: { public: this.client, } }); this.strategy = address; } checkPoolId() { if (this.poolId === BigInt(-1)) throw new Error("SQFSuperfluidStrategy: No poolId provided. Please call `setPoolId` first."); } checkStrategy() { if (!this.strategy) throw new Error("SQFSuperfluidStrategy: No strategy address provided. Please call `setContract` first."); } // Init and Deploy async getInitializeData(params) { const encoded = encodeAbiParameters(parseAbiParameters("bool, bool, address, address, address, uint64, uint64, uint64, uint64, uint256, uint256"), [ params.useRegistryAnchor, params.metadataRequired, params.passportDecoder, params.superfluidHost, params.allocationSuperToken, params.registrationStartTime, params.registrationEndTime, params.allocationStartTime, params.allocationEndTime, params.minPassportScore, params.initialSuperAppBalance, ]); return encoded; } getDeployParams() { const constructorArgs = encodeAbiParameters(parseAbiParameters("address, string"), [this.allo.address(), "SQFSuperfluidv1"]); const constructorArgsNo0x = constructorArgs.slice(2); // create the proper bytecode const bytecode = superfluidBytecode; const abi = superfluidAbi; return { abi: abi, bytecode: (bytecode + constructorArgsNo0x), }; } // Getters async getNative() { this.checkStrategy(); const native = await this.contract.read.NATIVE(); return native; } async getAllocationEndTime() { this.checkStrategy(); const endTime = await this.contract.read.allocationEndTime(); return endTime; } async getAllocationStartTime() { this.checkStrategy(); const startTime = await this.contract.read.allocationStartTime(); return startTime; } async getRegistrationEndTime() { this.checkStrategy(); const endTime = await this.contract.read.registrationEndTime(); return endTime; } async getRegistrationStartTime() { this.checkStrategy(); const startTime = await this.contract.read.registrationStartTime(); return startTime; } 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 useRegistryAnchor() { this.checkStrategy(); const useRegistryAnchor = await this.contract.read.useRegistryAnchor(); return useRegistryAnchor; } getInitialSuperAppBalance() { this.checkStrategy(); return this.contract.read.initialSuperAppBalance(); } getSuperfluidHost() { this.checkStrategy(); return this.contract.read.superfluidHost(); } getAllocationSuperToken() { this.checkStrategy(); return this.contract.read.allocationSuperToken(); } getPoolSuperToken() { this.checkStrategy(); return this.contract.read.poolSuperToken(); } getGdaPool() { this.checkStrategy(); return this.contract.read.gdaPool(); } getPassportDecoder() { this.checkStrategy(); return this.contract.read.passportDecoder(); } getMinPassportScore() { this.checkStrategy(); return this.contract.read.minPassportScore(); } getMetadataRequired() { this.checkStrategy(); return this.contract.read.metadataRequired(); } getRegistry() { this.checkStrategy(); return this.contract.read.registry(); } getRecipientIdBySuperApp(superApp) { this.checkStrategy(); return this.contract.read.superApps([superApp]); } getTotalUnitsByRecipient(recipientId) { this.checkStrategy(); return this.contract.read.totalUnitsByRecipient([recipientId]); } getRecipientFlowRate(recipientId) { this.checkStrategy(); return this.contract.read.recipientFlowRate([recipientId]); } getSuperApp(recipientId) { this.checkStrategy(); return this.contract.read.getSuperApp([recipientId]); } // Write functions getUpdatePoolTimestampsData(registrationStartTime, registrationEndTime, allocationStartTime, allocationEndTime) { this.checkStrategy(); const encodedData = encodeFunctionData({ abi: superfluidAbi, functionName: "updatePoolTimestamps", args: [ registrationStartTime, registrationEndTime, allocationStartTime, allocationEndTime, ], }); return { to: this.strategy, data: encodedData, value: "0", }; } getUpdateMinPassportScoreData(minPassportScore) { this.checkStrategy(); const encodedData = encodeFunctionData({ abi: superfluidAbi, functionName: "updateMinPassportScore", args: [minPassportScore], }); return { to: this.strategy, data: encodedData, value: "0", }; } getRegisterRecipientData(data) { this.checkPoolId(); const encoded = encodeAbiParameters(parseAbiParameters("address, address, (uint256, string)"), [ data.registryAnchor, data.recipientAddress, [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, string)"), [ registerData.registryAnchor, registerData.recipientAddress, [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", }; } getDistributeData(flowRate) { this.checkPoolId(); const encoded = encodeAbiParameters(parseAbiParameters("int96"), [flowRate]); const encodedData = encodeFunctionData({ abi: alloAbi, functionName: "distribute", args: [this.poolId, [], encoded], }); return { to: this.strategy, data: encodedData, value: "0", }; } getAllocationData(recipientId, flowRate) { this.checkPoolId(); const encoded = encodeAbiParameters(parseAbiParameters("address, int96"), [recipientId, flowRate]); 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, int96"), [allocation.recipientId, allocation.flowRate]); 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", }; } getReviewRecipientData(data) { this.checkStrategy(); const recipientIds = data.map((recipient) => recipient.recipientId); const statuses = data.map((recipient) => recipient.recipientStatus); const encodedData = encodeFunctionData({ abi: superfluidAbi, functionName: "reviewRecipients", args: [recipientIds, statuses], }); return { to: this.strategy, data: encodedData, value: "0", }; } getCancelRecipientsData(recipientIds) { this.checkStrategy(); const encodedData = encodeFunctionData({ abi: superfluidAbi, functionName: "cancelRecipients", args: [recipientIds], }); return { to: this.strategy, data: encodedData, value: "0", }; } getWithdrawData(token, amount) { this.checkStrategy(); const encodedData = encodeFunctionData({ abi: superfluidAbi, functionName: "withdraw", args: [token, amount], }); return { to: this.strategy, data: encodedData, value: "0", }; } getCloseStream() { this.checkStrategy(); const encodedData = encodeFunctionData({ abi: superfluidAbi, functionName: "closeStream", }); return { to: this.strategy, data: encodedData, value: "0", }; } }