From b6a8ca344955515874211097dee2cee63ff4aa03 Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Mon, 7 Oct 2024 20:28:19 +0300 Subject: [PATCH] switch to approve --- examples/hello/contracts/Hello.sol | 10 ++++++---- examples/hello/tasks/helloCall.ts | 6 +----- examples/hello/tasks/helloWithdrawAndCall.ts | 8 ++++---- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/examples/hello/contracts/Hello.sol b/examples/hello/contracts/Hello.sol index 5546f8f0..afebf156 100644 --- a/examples/hello/contracts/Hello.sol +++ b/examples/hello/contracts/Hello.sol @@ -38,6 +38,7 @@ contract Hello is UniversalContract { RevertOptions memory revertOptions ) external { (, uint256 gasFee) = IZRC20(zrc20).withdrawGasFeeWithGasLimit(gasLimit); + IZRC20(zrc20).transferFrom(msg.sender, address(this), gasFee); IZRC20(zrc20).approve(address(gateway), gasFee); gateway.call(receiver, zrc20, message, gasLimit, revertOptions); } @@ -52,10 +53,11 @@ contract Hello is UniversalContract { ) external { (address gasZRC20, uint256 gasFee) = IZRC20(zrc20) .withdrawGasFeeWithGasLimit(gasLimit); - if (zrc20 == gasZRC20) { - IZRC20(zrc20).approve(address(gateway), amount + gasFee); - } else { - IZRC20(zrc20).approve(address(gateway), amount); + uint256 total = zrc20 == gasZRC20 ? amount + gasFee : amount; + IZRC20(zrc20).transferFrom(msg.sender, address(this), total); + IZRC20(zrc20).approve(address(gateway), total); + if (zrc20 != gasZRC20) { + IZRC20(gasZRC20).transferFrom(msg.sender, address(this), gasFee); IZRC20(gasZRC20).approve(address(gateway), gasFee); } gateway.withdrawAndCall( diff --git a/examples/hello/tasks/helloCall.ts b/examples/hello/tasks/helloCall.ts index 92ca946a..ef977e98 100644 --- a/examples/hello/tasks/helloCall.ts +++ b/examples/hello/tasks/helloCall.ts @@ -58,11 +58,7 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => { const gasLimit = hre.ethers.BigNumber.from(args.txOptionsGasLimit); const zrc20 = new ethers.Contract(args.zrc20, ZRC20ABI.abi, signer); const [, gasFee] = await zrc20.withdrawGasFeeWithGasLimit(gasLimit); - const zrc20TransferTx = await zrc20.transfer( - args.contract, - gasFee, - txOptions - ); + const zrc20TransferTx = await zrc20.approve(args.contract, gasFee, txOptions); await zrc20TransferTx.wait(); diff --git a/examples/hello/tasks/helloWithdrawAndCall.ts b/examples/hello/tasks/helloWithdrawAndCall.ts index b2799483..43818bd5 100644 --- a/examples/hello/tasks/helloWithdrawAndCall.ts +++ b/examples/hello/tasks/helloWithdrawAndCall.ts @@ -62,20 +62,20 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => { const zrc20 = new ethers.Contract(args.zrc20, ZRC20ABI.abi, signer); const [gasZRC20, gasFee] = await zrc20.withdrawGasFeeWithGasLimit(gasLimit); const gasZRC20Contract = new ethers.Contract(gasZRC20, ZRC20ABI.abi, signer); - const gasFeeTransfer = await gasZRC20Contract.transfer( + const gasFeeApprove = await gasZRC20Contract.approve( args.contract, gasZRC20 == args.zrc20 ? gasFee.add(amount) : gasFee, txOptions ); - await gasFeeTransfer.wait(); + await gasFeeApprove.wait(); if (gasZRC20 !== args.zrc20) { - const targetTokenTransfer = await zrc20.transfer( + const targetTokenApprove = await zrc20.approve( args.contract, gasFee.add(amount), txOptions ); - await targetTokenTransfer.wait(); + await targetTokenApprove.wait(); } const factory = (await hre.ethers.getContractFactory(args.name)) as any;