From 66aa290f4e55b16eac976c273dec15fcddf7123c Mon Sep 17 00:00:00 2001 From: wanglonghong Date: Wed, 22 Mar 2023 09:55:54 +0800 Subject: [PATCH] refactor: fix incorrect comparison --- .../optimism/connectors/connext/events.sol | 3 +- .../optimism/connectors/connext/helpers.sol | 36 +------------------ .../optimism/connectors/connext/main.sol | 26 +++++++++++--- test/optimism/connext/connext.test.ts | 28 ++++++++++----- 4 files changed, 45 insertions(+), 48 deletions(-) diff --git a/contracts/optimism/connectors/connext/events.sol b/contracts/optimism/connectors/connext/events.sol index a1d7e441..63676a17 100644 --- a/contracts/optimism/connectors/connext/events.sol +++ b/contracts/optimism/connectors/connext/events.sol @@ -9,6 +9,7 @@ contract Events { address delegate, uint256 amount, uint256 slippage, - uint256 getId + uint256 getId, + uint256 setId ); } diff --git a/contracts/optimism/connectors/connext/helpers.sol b/contracts/optimism/connectors/connext/helpers.sol index 5adddd7a..ccce031b 100644 --- a/contracts/optimism/connectors/connext/helpers.sol +++ b/contracts/optimism/connectors/connext/helpers.sol @@ -15,14 +15,6 @@ contract Helpers is DSMath, Basic { 0x8f7492DE823025b4CfaAB1D34c58963F2af5DEDA; IConnext internal constant connext = IConnext(connextAddr); - /** - * @dev InstaReceiver Address - */ - address internal constant instaReceiverAddr = - 0x0000000000000000000000000000000000000000; // TODO: Add InstaReceiver address - IInstaReceiver internal constant instaReceiver = - IInstaReceiver(instaReceiverAddr); - /** * @param destination The destination domain ID. * @param asset The address of token to be bridged. @@ -44,33 +36,7 @@ contract Helpers is DSMath, Basic { } function _xcall(XCallParams memory params) internal { - uint256 nativeTokenAmt; - bool isNative = params.asset == ethAddr; - - if (isNative) { - params.amount = params.amount == uint256(-1) - ? address(this).balance - : params.amount; - - // xcall does not take native asset, must wrap - TokenInterface tokenContract = TokenInterface(wethAddr); - convertEthToWeth(true, tokenContract, params.amount); - - nativeTokenAmt = params.amount; - } else { - TokenInterface tokenContract = TokenInterface(params.asset); - params.amount = params.amount == uint256(-1) - ? tokenContract.balanceOf(address(this)) - : params.amount; - - if (params.amount > 0) { - tokenContract.approve(connextAddr, params.amount); - } - - nativeTokenAmt = 0; - } - - connext.xcall{ value: params.relayerFee + nativeTokenAmt }( + connext.xcall{ value: params.relayerFee }( params.destination, params.to, params.asset, diff --git a/contracts/optimism/connectors/connext/main.sol b/contracts/optimism/connectors/connext/main.sol index 64b14aca..d289005a 100644 --- a/contracts/optimism/connectors/connext/main.sol +++ b/contracts/optimism/connectors/connext/main.sol @@ -21,16 +21,33 @@ abstract contract ConnextResolver is Helpers { * @notice Call xcall on Connext. * @param params XCallParams struct. * @param getId ID to retrieve amount from last spell. + * @param setId ID stores the amount of tokens deposited. */ - function xcall(XCallParams memory params, uint256 getId) + function xcall(XCallParams memory params, uint256 getId, uint256 setId) external payable returns (string memory _eventName, bytes memory _eventParam) { - params.amount = getUint(getId, params.amount); + uint256 _amount = getUint(getId, params.amount); TokenInterface tokenContract = TokenInterface(params.asset); + bool isNative = params.asset == wethAddr; + + if (isNative) { + _amount = _amount == uint256(-1) ? sub(address(this).balance, params.relayerFee) : _amount; + + // xcall does not take native asset, must wrap + convertEthToWeth(true, tokenContract, _amount); + + } else { + _amount = _amount == uint256(-1) ? tokenContract.balanceOf(address(this)) : _amount; + } + + params.amount = _amount; + approve(tokenContract, connextAddr, _amount); _xcall(params); - _eventName = "LogXCall(uint32,address,address,address,uint256,uint256,uint256)"; + + setUint(setId, _amount); + _eventName = "LogXCall(uint32,address,address,address,uint256,uint256,uint256,uint256)"; _eventParam = abi.encode( params.destination, params.to, @@ -38,7 +55,8 @@ abstract contract ConnextResolver is Helpers { params.delegate, params.amount, params.slippage, - getId + getId, + setId ); } } diff --git a/test/optimism/connext/connext.test.ts b/test/optimism/connext/connext.test.ts index e6fb6ed7..32aa281d 100644 --- a/test/optimism/connext/connext.test.ts +++ b/test/optimism/connext/connext.test.ts @@ -12,6 +12,7 @@ import { abis } from "../../../scripts/constant/abis"; import { ConnectV2ConnextOptimism__factory } from "../../../typechain"; import { Signer, Contract } from "ethers"; + describe("Connext Connector [Optimism]", function () { const connectorName = "CONNEXT-TEST-A"; @@ -19,11 +20,14 @@ describe("Connext Connector [Optimism]", function () { let masterSigner: Signer; let instaConnectorsV2: Contract; let connector: Contract; - + let usdcContract: Contract; + let signer: any; + const connextAddr = "0x8f7492DE823025b4CfaAB1D34c58963F2af5DEDA"; const usdcAddr = "0x7F5c764cBc14f9669B88837ca1490cCa17c31607"; const ethAddr = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"; const wethAddr = "0x4200000000000000000000000000000000000006"; + const account = "0xebe80f029b1c02862b9e8a70a7e5317c06f62cae"; const wallets = provider.getWallets(); const [wallet0, wallet1] = wallets; @@ -39,7 +43,7 @@ describe("Connext Connector [Optimism]", function () { forking: { // @ts-ignore jsonRpcUrl: hre.config.networks.hardhat.forking.url, - blockNumber: 80768349 + blockNumber: 82686991 } } ] @@ -53,8 +57,19 @@ describe("Connext Connector [Optimism]", function () { signer: masterSigner, connectors: instaConnectorsV2 }); + usdcContract = await ethers.getContractAt(abis.basic.erc20, usdcAddr); + signer = await ethers.getSigner(account); - console.log("Connector address", connector.address); + await hre.network.provider.send("hardhat_setBalance", [account, ethers.utils.parseEther("10").toHexString()]); + + await hre.network.provider.request({ + method: "hardhat_impersonateAccount", + params: [account] + }); + + await usdcContract.connect(signer).transfer(wallet0.address, ethers.utils.parseUnits("10000", 6)); + + console.log("deployed connector: ", connector.address); }); it("Should have contracts deployed.", async function () { @@ -76,11 +91,8 @@ describe("Connext Connector [Optimism]", function () { }); expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10")); - // await addLiquidity( - // "usdc", - // dsaWallet0.address, - // ethers.utils.parseEther("100000") - // ); + await usdcContract.connect(wallet0).transfer(dsaWallet0.address, ethers.utils.parseUnits("10", 6)); + expect(await usdcContract.balanceOf(dsaWallet0.address)).to.be.gte(ethers.utils.parseUnits("10", 6)); }); });