Skip to content

Commit 4e04644

Browse files
feat: add directAllocation to allo-v2
1 parent b2a68ca commit 4e04644

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

packages/common/src/allo/allo.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,23 @@ export interface Allo {
244244
indexingStatus: Result<null>;
245245
}
246246
>;
247+
248+
directAllocation: (args: {
249+
tokenAddress: Address;
250+
poolId: string;
251+
amount: bigint;
252+
recipient: Address;
253+
nonce: bigint;
254+
requireTokenApproval?: boolean;
255+
}) => AlloOperation<
256+
Result<null>,
257+
{
258+
tokenApprovalStatus: Result<TransactionReceipt | null>;
259+
transaction: Result<Hex>;
260+
transactionStatus: Result<TransactionReceipt>;
261+
indexingStatus: Result<null>;
262+
}
263+
>;
247264
}
248265

249266
export { AlloOperation };

packages/common/src/allo/backends/allo-v1.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,28 @@ export class AlloV1 implements Allo {
13101310
return error(result);
13111311
});
13121312
}
1313+
1314+
directAllocation(args: {
1315+
tokenAddress: Address;
1316+
poolId: string;
1317+
amount: bigint;
1318+
recipient: Address;
1319+
nonce: bigint;
1320+
requireTokenApproval?: boolean;
1321+
}): AlloOperation<
1322+
Result<null>,
1323+
{
1324+
tokenApprovalStatus: Result<TransactionReceipt | null>;
1325+
transaction: Result<Hex>;
1326+
transactionStatus: Result<TransactionReceipt>;
1327+
indexingStatus: Result<null>;
1328+
}
1329+
> {
1330+
return new AlloOperation(async () => {
1331+
const result = new AlloError(`Unsupported on v1 ${args}`);
1332+
return error(result);
1333+
});
1334+
}
13131335
}
13141336
// todo: move this out?
13151337
export type CreateRoundArgs = {

packages/common/src/allo/backends/allo-v2.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,94 @@ export class AlloV2 implements Allo {
14351435
return success(null);
14361436
});
14371437
}
1438+
1439+
directAllocation(args: {
1440+
tokenAddress: Address;
1441+
poolId: string;
1442+
amount: bigint;
1443+
recipient: Address;
1444+
nonce: bigint;
1445+
requireTokenApproval?: boolean;
1446+
}): AlloOperation<
1447+
Result<null>,
1448+
{
1449+
tokenApprovalStatus: Result<TransactionReceipt | null>;
1450+
transaction: Result<Hex>;
1451+
transactionStatus: Result<TransactionReceipt>;
1452+
indexingStatus: Result<null>;
1453+
}
1454+
> {
1455+
return new AlloOperation(async ({ emit }) => {
1456+
if (isNaN(Number(args.poolId))) {
1457+
return error(new AlloError("Pool ID is not a valid Allo V2 pool ID"));
1458+
}
1459+
1460+
const poolId = BigInt(args.poolId);
1461+
1462+
if (args.tokenAddress === zeroAddress || !args.requireTokenApproval) {
1463+
emit("tokenApprovalStatus", success(null));
1464+
} else {
1465+
const approvalTx = await sendTransaction(this.transactionSender, {
1466+
address: args.tokenAddress,
1467+
abi: Erc20ABI,
1468+
functionName: "approve",
1469+
args: [this.allo.address(), args.amount],
1470+
});
1471+
1472+
if (approvalTx.type === "error") {
1473+
return approvalTx;
1474+
}
1475+
1476+
try {
1477+
const receipt = await this.transactionSender.wait(approvalTx.value);
1478+
emit("tokenApprovalStatus", success(receipt));
1479+
} catch (err) {
1480+
const result = new AlloError("Failed to approve token transfer", err);
1481+
emit("tokenApprovalStatus", error(result));
1482+
return error(result);
1483+
}
1484+
}
1485+
1486+
const encodeData = utils.defaultAbiCoder.encode(
1487+
["address", "uint256", "address", "uint256"],
1488+
[args.recipient, args.amount, args.tokenAddress, args.nonce]
1489+
);
1490+
1491+
const tx = await sendTransaction(this.transactionSender, {
1492+
address: this.allo.address(),
1493+
abi: AlloAbi,
1494+
functionName: "allocate",
1495+
args: [poolId, encodeData],
1496+
value: args.tokenAddress === zeroAddress ? args.amount : 0n,
1497+
});
1498+
1499+
emit("transaction", tx);
1500+
1501+
if (tx.type === "error") {
1502+
return tx;
1503+
}
1504+
1505+
let receipt: TransactionReceipt;
1506+
1507+
try {
1508+
receipt = await this.transactionSender.wait(tx.value);
1509+
emit("transactionStatus", success(receipt));
1510+
} catch (err) {
1511+
const result = new AlloError("Failed to fund round", err);
1512+
emit("transactionStatus", error(result));
1513+
return error(result);
1514+
}
1515+
1516+
await this.waitUntilIndexerSynced({
1517+
chainId: this.chainId,
1518+
blockNumber: receipt.blockNumber,
1519+
});
1520+
1521+
emit("indexingStatus", success(null));
1522+
1523+
return success(null);
1524+
});
1525+
}
14381526
}
14391527

14401528
export function serializeProject(project: ProjectWithMerkleProof) {

0 commit comments

Comments
 (0)