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

fix: Prevent ferry from transfering native funds to contract in case ERC20 withdrawal #437

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion ferry-withdrawal/src/CloserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ class CloserService {
}) !== undefined;

return (
(shouldBeClosed || await this.stash.shouldBeClosed(request.hash)) &&
(shouldBeClosed ||
(await this.stash.shouldBeClosed(request.hash))) &&
!(await this.l1.isClosed(request.hash))
);
} else {
Expand Down
6 changes: 4 additions & 2 deletions ferry-withdrawal/src/l1/L1Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,9 @@ class L1Api implements L1Interface {
});

const native_addr = await this.getNativeTokenAddress();
if (u8aToHex(withdrawal.tokenAddress) !== u8aToHex(native_addr)) {
const isNativeToken =
u8aToHex(withdrawal.tokenAddress) === u8aToHex(native_addr);
if (!isNativeToken) {
// TODO: submit as a batch
const approveRequest = await this.client.simulateContract({
account: acc,
Expand Down Expand Up @@ -357,7 +359,7 @@ class L1Api implements L1Interface {
args: [withdrawalToViemFormat(withdrawal)],
maxFeePerGas: maxFeeInWei,
maxPriorityFeePerGas: maxPriorityFeePerGasInWei,
value: withdrawal.amount - withdrawal.ferryTip,
value: isNativeToken ? withdrawal.amount - withdrawal.ferryTip : 0n,
});

const ferrytxHash = await wc.writeContract(ferryRequest.request);
Expand Down
25 changes: 25 additions & 0 deletions ferry-withdrawal/tests/L1Interface.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,31 @@ describe('L1Interface', () => {
expect(await l1Api.isFerried(hashWithdrawal(withdrawal))).toBeTruthy();
}, {timeout: 10000});

it('non native ferryWithdrawal works doest not transfer native token to contract', async () => {
const publicClient = createPublicClient({
transport: webSocket(WS_URI, { retryCount: 5 }),
});
const randomAddress = getRandomUintArray(20);

const contactBalanceBefore = await publicClient.getBalance({ address: MANGATA_CONTRACT_ADDRESS });
const withdrawal: Withdrawal = {
requestId: 1n,
withdrawalRecipient: randomAddress,
tokenAddress: TOKEN_ADDRESS,
amount: 1n,
ferryTip: 0n,
hash: hexToU8a("0x0000000000000000000000000000000000000000000000000000000000000000", 32),
};

const privateKey = hexToU8a("0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80");
expect(await l1Api.isFerried(hashWithdrawal(withdrawal))).toBeFalsy();
await l1Api.ferry(withdrawal, privateKey);
const contactBalanceAfter = await publicClient.getBalance({ address: MANGATA_CONTRACT_ADDRESS });
expect(await l1Api.isFerried(hashWithdrawal(withdrawal))).toBeTruthy();
expect(contactBalanceBefore).toEqual(contactBalanceAfter);
}, {timeout: 10000});


it('native ferryWithdrawal works', async () => {
const randomAddress = getRandomUintArray(20);

Expand Down
Loading