allo-monad-ray
Version:
Monad version of Allo v2 SDK
539 lines (538 loc) • 17.9 kB
JavaScript
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 { ZERO_ADDRESS, } from "../../Common/types";
import { supportedChains } from "../../chains.config";
import { NATIVE } from "../../types";
import { abi as directAbi, bytecode as directBytecode, } from "./donationVotingDirect.config";
import { abi as vaultAbi, bytecode as vaultBytecode, } from "./donationVotingVault.config";
import { StrategyType, } from "./types";
export class DonationVotingMerkleDistributionStrategy {
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: vaultAbi,
client: {
public: this.client,
}
});
this.strategy = address;
}
this.poolId = poolId || BigInt(-1);
}
async getAllo() {
return this.allo;
}
async setPoolId(poolId) {
this.poolId = poolId;
const strategyAddress = await this.allo.getStrategy(poolId);
this.setContract(strategyAddress);
}
setContract(address) {
this.contract = getContract({
address: address,
abi: vaultAbi,
client: {
public: this.client,
}
});
this.strategy = address;
}
// Validation functions
checkPoolId() {
if (this.poolId === BigInt(-1))
throw new Error("DonationVotingMerkleDistributionStrategy: No poolId provided. Please call `setPoolId` first.");
}
checkStrategy() {
if (!this.strategy)
throw new Error("DonationVotingMerkleDistributionStrategy: No strategy address provided. Please call `setContract` first.");
}
/* Read Functions */
async getNative() {
this.checkStrategy();
const native = await this.contract.read.NATIVE();
return native;
}
async getPermit2() {
this.checkStrategy();
const permit2 = await this.contract.read.PERMIT2();
return permit2;
}
/* Public Storage Variables */
async getDistributionMetadata() {
this.checkStrategy();
const metadata = await this.contract.read.distributionMetadata();
return metadata;
}
async useRegistryAnchor() {
this.checkStrategy();
const anchor = await this.contract.read.useRegistryAnchor();
return anchor;
}
async metadataRequired() {
this.checkStrategy();
const required = await this.contract.read.metadataRequired();
return required;
}
async distributionStarted() {
this.checkStrategy();
const started = await this.contract.read.distributionStarted();
return started;
}
async registrationStartTime() {
this.checkStrategy();
const startTime = await this.contract.read.registrationStartTime();
return startTime;
}
async registrationEndTime() {
this.checkStrategy();
const endTime = await this.contract.read.registrationEndTime();
return endTime;
}
async allocationStartTime() {
this.checkStrategy();
const startTime = await this.contract.read.allocationStartTime();
return startTime;
}
async allocationEndTime() {
this.checkStrategy();
const endTime = await this.contract.read.allocationEndTime();
return endTime;
}
async totalPayoutAmount() {
this.checkStrategy();
const amount = await this.contract.read.totalPayoutAmount();
return amount;
}
async recipientsCounter() {
this.checkStrategy();
const counter = await this.contract.read.recipientsCounter();
return counter;
}
async getMerkleRoot() {
this.checkStrategy();
const root = await this.contract.read.merkleRoot();
return root;
}
async statusesBitMap(index) {
this.checkStrategy();
const bitMap = await this.contract.read.statusesBitMap([index]);
return bitMap;
}
async recipientToStatusIndexes(recipient) {
this.checkStrategy();
const indexes = await this.contract.read.recipientToStatusIndexes([
recipient,
]);
return indexes;
}
async isTokenAllowed(token) {
this.checkStrategy();
const allowed = await this.contract.read.allowedTokens(token);
return allowed;
}
async getClaims(recipient, token) {
const claims = await this.contract.read.claims([recipient, token]);
return claims;
}
async getTotalClaimableAmount(recipient) {
const claims = await this.contract.read.totalClaimableAmount([recipient]);
return claims;
}
/* Public Read Functions */
// TODO: FIX FROM HERE
async getPayouts(recipientIds, data) {
this.checkStrategy();
const payouts = await this.contract.read.getPayouts([recipientIds, data]);
const payoutSummary = payouts.map((payout) => {
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 id = await this.contract.read.getPoolId();
return id;
}
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 hasBeenDistributed(index) {
this.checkStrategy();
const distributed = await this.contract.read.hasBeenDistributed([index]);
return distributed;
}
async isDistributionSet() {
this.checkStrategy();
const set = await this.contract.read.isDistributionSet();
return set;
}
async isPoolActive() {
this.checkStrategy();
const active = await this.contract.read.isPoolActive();
return active;
}
async isValidAllocator(allocator) {
this.checkStrategy();
const valid = await this.contract.read.isValidAllocator([allocator]);
return valid;
}
/**
* Write functions
*/
/**
*
* @param strategyType - StrategyType ("Vault" | "Direct")
* @returns DeployParams {abi, bytecode}
*/
getDeployParams(strategyType) {
if (strategyType !== StrategyType.Vault &&
strategyType !== StrategyType.Direct) {
throw new Error("Invalid strategy type");
}
const version = strategyType === StrategyType.Vault
? "DonationVotingMerkleDistributionVaultStrategyv2.0"
: "DonationVotingMerkleDistributionDirectTransferStrategyv2.0";
const bytecode = strategyType === StrategyType.Vault ? vaultBytecode : directBytecode;
const abi = strategyType === StrategyType.Vault ? vaultAbi : directAbi;
const constructorArgs = encodeAbiParameters(parseAbiParameters("address, string"), [this.allo.address(), version]);
const constructorArgsNo0x = constructorArgs.slice(2);
return {
abi: abi,
bytecode: (bytecode + constructorArgsNo0x),
};
}
async getInitializeData(data) {
const encodedData = encodeAbiParameters(parseAbiParameters("(bool, bool, uint64, uint64, uint64, uint64, address[])"), [
[
data.useRegistryAnchor,
data.metadataRequired,
data.registrationStartTime,
data.registrationEndTime,
data.allocationStartTime,
data.allocationEndTime,
data.allowedTokens,
],
]);
return encodedData;
}
/**
*
* @param data - Allocation: (address,(((address,uint256),uint256,uint256),bytes))
* @returns `0x${string}`
*/
getEncodedAllocation(data) {
const encoded = encodeAbiParameters(parseAbiParameters("address,uint8,(((address,uint256),uint256,uint256),bytes)"), [
data.recipientId,
data.permitType,
[
[
[
data.permit2Data.permit.permitted.token,
data.permit2Data.permit.permitted.amount,
],
data.permit2Data.permit.nonce,
data.permit2Data.permit.deadline,
],
data.permit2Data.signature,
],
]);
return encoded;
}
/**
*
* @param allocation - Allocation: (address,PermitType,(((address,uint256),uint256,uint256),bytes32))
* @returns TransactionData: {to: `0x${string}`, data: `0x${string}`, value: string}
*/
getAllocateData(allocation) {
this.checkPoolId();
const token = allocation.permit2Data.permit.permitted.token;
const amount = allocation.permit2Data.permit.permitted.amount;
const encoded = this.getEncodedAllocation(allocation);
const encodedData = encodeFunctionData({
abi: alloAbi,
functionName: "allocate",
args: [this.poolId, encoded],
});
return {
to: this.allo.address(),
data: encodedData,
value: token.toLowerCase() === NATIVE ? amount.toString() : "0",
};
}
/**
*
* @param allocations - Array of Allocation: (address,(((address,uint256),uint256,uint256),bytes32))
* @returns TransactionData: {to: `0x${string}`, data: `0x${string}`, value: string}
*/
getBatchAllocateData(allocations) {
this.checkPoolId();
const encodedParams = [];
allocations.forEach((allocation) => {
const encoded = this.getEncodedAllocation(allocation);
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",
};
}
/**
*
* @param poolIds - Array of poolIds
* @param allocations - Array of Allocation: (address,(((address,uint256),uint256,uint256),bytes32))
* @returns TransactionData: {to: `0x${string}`, data: `0x${string}`, value: string}
*/
getBatchAllocateDataMultiplePools(poolIds, allocations) {
if (poolIds.length !== allocations.length) {
throw new Error("DonationVotingMerkleDistributionStrategy: Length of poolIds and allocations must be equal");
}
const encodedParams = [];
allocations.forEach((allocation) => {
const encoded = this.getEncodedAllocation(allocation);
encodedParams.push(encoded);
});
const encodedData = encodeFunctionData({
abi: alloAbi,
functionName: "batchAllocate",
args: [poolIds, encodedParams],
});
return {
to: this.allo.address(),
data: encodedData,
value: "0",
};
}
/**
*
* @param data - (address, address, Metadata)
* @returns
*/
getRegisterRecipientData(data) {
this.checkPoolId();
const encoded = encodeAbiParameters(parseAbiParameters("address, address, (uint256, string)"), [
data.registryAnchor || ZERO_ADDRESS,
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",
};
}
/**
* Batch register recipients
*
* @param data - Array of RegisterDataDonationVoting
*
* @returns TransactionData
*/
getBatchRegisterRecipientData(data) {
this.checkPoolId();
const encodedParams = [];
data.forEach((registerData) => {
const encoded = encodeAbiParameters(parseAbiParameters("address, address, (uint256, string)"), [
registerData.registryAnchor || ZERO_ADDRESS,
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",
};
}
/**
* Fund the pool
*
* @param amount - Amount to fund the pool
*
* @returns TransactionData
*/
fundPool(amount) {
this.checkPoolId();
const encodedData = encodeFunctionData({
abi: alloAbi,
functionName: "fundPool",
args: [this.poolId, amount],
});
return {
to: this.allo.address(),
data: encodedData,
value: "0",
};
}
/**
* Distribute to the recipients
*
* @param recipientIds - Array of recipientIds
* @param data - Array of Distribution
*
* @returns TransactionData
*/
distribute(recipientIds,
// (uint256 _poolId, address[] memory _recipientIds, bytes memory _data)
data) {
this.checkPoolId();
const encodeDistribution = encodeAbiParameters(parseAbiParameters("(uint256 index, address recipientId, uint256 amount, bytes32[] merkleProof)[]"), [data]);
const encodedData = encodeFunctionData({
abi: alloAbi,
functionName: "distribute",
args: [this.poolId, recipientIds, encodeDistribution],
});
return {
to: this.allo.address(),
data: encodedData,
value: "0",
};
}
/**
* Get the claim function encoded data
*
* @param claims - Array of claims
*
* @returns - Encoded transaction data
*/
getClaimData(claims) {
this.checkPoolId();
const encodedData = encodeFunctionData({
abi: vaultAbi,
functionName: "claim",
args: [claims],
});
return {
to: this.strategy,
data: encodedData,
value: "0",
};
}
/**
* Provides a function to batch together multiple calls in a single external call
*
* @param data - Array of encoded data
*
* @returns - Encoded transaction data
*/
multicall(data) {
this.checkPoolId();
const encodedData = encodeFunctionData({
abi: vaultAbi,
functionName: "multicall",
args: [data],
});
return {
to: this.strategy,
data: encodedData,
value: "0",
};
}
/**
* Review recipients
*
* @param statuses - Array of status indexes and statusRows
*
* @returns TransactionData
*/
reviewRecipients(statuses, refRecipientsCounter) {
const data = encodeFunctionData({
abi: vaultAbi,
functionName: "reviewRecipients",
args: [statuses, refRecipientsCounter],
});
return {
to: this.strategy,
data: data,
value: "0",
};
}
updateDistribution(merkleRoot, distributionMetadata) {
const data = encodeFunctionData({
abi: vaultAbi,
functionName: "updateDistribution",
args: [merkleRoot, distributionMetadata],
});
return {
to: this.strategy,
data: data,
value: "0",
};
}
updatePoolTimestamps(registrationStartTime, registrationEndTime, allocationStartTime, allocationEndTime) {
const data = encodeFunctionData({
abi: vaultAbi,
functionName: "updatePoolTimestamps",
args: [
registrationStartTime,
registrationEndTime,
allocationStartTime,
allocationEndTime,
],
});
return {
to: this.strategy,
data: data,
value: "0",
};
}
withdraw(address) {
const data = encodeFunctionData({
abi: vaultAbi,
functionName: "withdraw",
args: [address],
});
return {
to: this.strategy,
data: data,
value: "0",
};
}
}