Skip to content

Commit

Permalink
throw error if can't fund the DonationBox or loadEthForL2Calls call
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaspai committed Dec 1, 2024
1 parent ffacd57 commit 83f2b62
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/dataworker/Dataworker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,21 @@ export class Dataworker {
requiredAmount,
});
if (submitExecution) {
const canFund = await balanceAllocator.requestBalanceAllocations([
{
tokens: [feeToken],
amount: requiredAmount,
holder: await signer.getAddress(),
chainId: hubPoolChainId,
},
]);
if (!canFund) {
throw new Error(
`Failed to fund ${requiredAmount.toString()} of orbit gas token ${feeToken} for message to ${getNetworkName(
leaf.chainId
)}!`
);
}
if (feeToken === ZERO_ADDRESS) {
this.clients.multiCallerClient.enqueueTransaction({
contract: this.clients.hubPoolClient.hubPool,
Expand Down
43 changes: 41 additions & 2 deletions test/Dataworker.executePoolRebalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ describe("Dataworker: Execute pool rebalances", async function () {
)
).to.equal(toBNWei("-1"));
});
it("contains arbitrum leaf", async function () {
it("funds arbitrum leaf", async function () {
// Adds one fee per net send amount + one extra if groupIndex = 0
const leaves: PoolRebalanceLeaf[] = [
{
Expand Down Expand Up @@ -598,7 +598,7 @@ describe("Dataworker: Execute pool rebalances", async function () {
expect(queuedTransactions[2].method).to.equal("executeRootBundle");
expect(queuedTransactions[3].method).to.equal("executeRootBundle");
});
it("contains custom gas token orbit leaf", async function () {
it("funds custom gas token orbit leaf", async function () {
// Replicate custom gas token setups:
const azero = await smock.fake(ERC20.abi, {
address: TOKEN_SYMBOLS_MAP.AZERO.addresses[CHAIN_IDs.MAINNET],
Expand Down Expand Up @@ -634,6 +634,9 @@ describe("Dataworker: Execute pool rebalances", async function () {
const expectedFee = toBNWei("0.49");
const expectedFeeLeaf1 = expectedFee.mul(2).add(expectedFee);
const expectedFeeLeaf2 = expectedFee.mul(2);
azero.balanceOf
.whenCalledWith(await hubPoolClient.hubPool.signer.getAddress())
.returns(expectedFeeLeaf1.add(expectedFeeLeaf2));
await dataworkerInstance._executePoolRebalanceLeaves(
spokePoolClients,
leaves,
Expand All @@ -652,5 +655,41 @@ describe("Dataworker: Execute pool rebalances", async function () {
expect(queuedTransactions[2].method).to.equal("executeRootBundle");
expect(queuedTransactions[3].method).to.equal("executeRootBundle");
});
it("fails to fund custom gas token orbit leaf", async function () {
// Replicate custom gas token setups, but this time do not set a balance for the custom gas token funder.
const azero = await smock.fake(ERC20.abi, {
address: TOKEN_SYMBOLS_MAP.AZERO.addresses[CHAIN_IDs.MAINNET],
provider: hubPoolClient.hubPool.signer.provider,
});
// Custom gas token funder for AZERO
const customGasTokenFunder = "0x0d57392895Db5aF3280e9223323e20F3951E81B1";
azero.balanceOf.whenCalledWith(customGasTokenFunder).returns(0);
expect(await balanceAllocator.getBalance(hubPoolClient.chainId, azero.address, customGasTokenFunder)).to.equal(0);

// Adds one fee per net send amount + one extra if groupIndex = 0
const leaves: PoolRebalanceLeaf[] = [
{
chainId: 41455,
groupIndex: 0,
bundleLpFees: [toBNWei("1"), toBNWei("1")],
netSendAmounts: [toBNWei("1"), toBNWei("1")],
runningBalances: [toBNWei("1"), toBNWei("1")],
leafId: 0,
l1Tokens: [randomAddress(), randomAddress()],
},
];
// Should throw an error if caller doesn't have enough custom gas token to fund
// DonationBox.
await assertPromiseError(
dataworkerInstance._executePoolRebalanceLeaves(
spokePoolClients,
leaves,
balanceAllocator,
buildPoolRebalanceLeafTree(leaves),
true
),
"Failed to fund"
);
});
});
});

0 comments on commit 83f2b62

Please sign in to comment.