Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert allo v2 donate into an operation #3378

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/common/src/allo/allo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,14 @@ export interface Allo {
deadline: number;
nonce: bigint;
}
) => Promise<TransactionReceipt>;
) => AlloOperation<
Result<TransactionReceipt>,
{
transaction: Result<Hex>;
transactionStatus: Result<TransactionReceipt>;
indexingStatus: Result<null>;
}
>;

bulkUpdateApplicationStatus: (args: {
roundId: string;
Expand Down
167 changes: 95 additions & 72 deletions packages/common/src/allo/backends/allo-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class AlloV2 implements Allo {
});
}

async donate(
donate(
publicClient: PublicClient,
chainId: ChainId,
token: VotingToken,
Expand All @@ -128,84 +128,109 @@ export class AlloV2 implements Allo {
deadline: number;
nonce: bigint;
}
) {
let tx: Result<Hex>;
const mrcAddress = MRC_CONTRACTS[chainId];
): AlloOperation<
Result<TransactionReceipt>,
{
transaction: Result<Hex>;
transactionStatus: Result<TransactionReceipt>;
indexingStatus: Result<null>;
}
> {
return new AlloOperation(async ({ emit }) => {
let tx: Result<Hex>;
const mrcAddress = MRC_CONTRACTS[chainId];

const poolIds = Object.keys(groupedVotes).flatMap((key) => {
const count = groupedVotes[key].length;
return new Array(count).fill(key);
});
const poolIds = Object.keys(groupedVotes).flatMap((key) => {
const count = groupedVotes[key].length;
return new Array(count).fill(key);
});

const data = Object.values(groupedVotes).flat();
const data = Object.values(groupedVotes).flat();

/* decide which function to use based on whether token is native, permit-compatible or DAI */
if (token.address === zeroAddress || token.address === NATIVE) {
tx = await sendTransaction(this.transactionSender, {
address: mrcAddress,
abi: MRC_ABI,
functionName: "allocate",
args: [poolIds, Object.values(groupedAmounts), data],
value: nativeTokenAmount,
});
} else if (permit) {
if (getPermitType(token) === "dai") {
/* decide which function to use based on whether token is native, permit-compatible or DAI */
if (token.address === zeroAddress || token.address === NATIVE) {
tx = await sendTransaction(this.transactionSender, {
address: mrcAddress,
abi: MRC_ABI,
functionName: "allocateDAIPermit",
args: [
data,
poolIds,
Object.values(groupedAmounts),
Object.values(groupedAmounts).reduce((acc, b) => acc + b),
token.address as Hex,
BigInt(permit.deadline ?? Number.MAX_SAFE_INTEGER),
permit.nonce,
permit.sig.v,
permit.sig.r as Hex,
permit.sig.s as Hex,
],
functionName: "allocate",
args: [poolIds, Object.values(groupedAmounts), data],
value: nativeTokenAmount,
});
} else if (permit) {
if (getPermitType(token) === "dai") {
tx = await sendTransaction(this.transactionSender, {
address: mrcAddress,
abi: MRC_ABI,
functionName: "allocateDAIPermit",
args: [
data,
poolIds,
Object.values(groupedAmounts),
Object.values(groupedAmounts).reduce((acc, b) => acc + b),
token.address as Hex,
BigInt(permit.deadline ?? Number.MAX_SAFE_INTEGER),
permit.nonce,
permit.sig.v,
permit.sig.r as Hex,
permit.sig.s as Hex,
],
});
} else {
tx = await sendTransaction(this.transactionSender, {
address: mrcAddress,
abi: MRC_ABI,
functionName: "allocateERC20Permit",
args: [
data,
poolIds,
Object.values(groupedAmounts),
Object.values(groupedAmounts).reduce((acc, b) => acc + b),
token.address as Hex,
BigInt(permit.deadline ?? Number.MAX_SAFE_INTEGER),
permit.sig.v,
permit.sig.r as Hex,
permit.sig.s as Hex,
],
});
}
} else {
tx = await sendTransaction(this.transactionSender, {
address: mrcAddress,
abi: MRC_ABI,
functionName: "allocateERC20Permit",
args: [
data,
poolIds,
Object.values(groupedAmounts),
Object.values(groupedAmounts).reduce((acc, b) => acc + b),
token.address as Hex,
BigInt(permit.deadline ?? Number.MAX_SAFE_INTEGER),
permit.sig.v,
permit.sig.r as Hex,
permit.sig.s as Hex,
],
});
/* Tried voting using erc-20 but no permit signature provided */
throw new AlloError(
"Tried voting using erc-20 but no permit signature provided"
);
}

emit("transaction", tx);

if (tx.type === "error") {
return tx;
}

let receipt: TransactionReceipt;

try {
receipt = await this.transactionSender.wait(
tx.value,
60_000,
publicClient
);

emit("transactionStatus", success(receipt));
} catch (err) {
const result = new AlloError("Failed to create project");
emit("transactionStatus", error(result));
return error(result);
}
} else {
/* Tried voting using erc-20 but no permit signature provided */
throw new AlloError(
"Tried voting using erc-20 but no permit signature provided"
);
}

if (tx.type === "success") {
const receipt = await this.transactionSender.wait(
tx.value,
60_000,
publicClient
);
await this.waitUntilIndexerSynced({
chainId: this.chainId,
blockNumber: receipt.blockNumber,
});
return receipt;
} else {
throw tx.error;
}

emit("indexingStatus", success(null));

return success(receipt);
});
}

createProject(args: {
Expand Down Expand Up @@ -696,13 +721,11 @@ export class AlloV2 implements Allo {
throw new AlloError("DirectGrants is not supported yet!");
}

const strategyInstance = new DonationVotingMerkleDistributionStrategy(
{
chain: this.chainId,
poolId: BigInt(args.roundId),
address: args.strategyAddress,
}
);
const strategyInstance = new DonationVotingMerkleDistributionStrategy({
chain: this.chainId,
poolId: BigInt(args.roundId),
address: args.strategyAddress,
});

const totalApplications = await strategyInstance.recipientsCounter();

Expand Down
Loading