Skip to content

Commit

Permalink
4087 no need to swap in case of from and to asset is same in chain ab…
Browse files Browse the repository at this point in the history
…straction sdk (#4088)

* feat: update swap and xcall contract

* feat: check if from and to is same

* fix: unit tests
  • Loading branch information
liu-zhipeng authored May 2, 2023
1 parent c804362 commit 574041e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
4 changes: 2 additions & 2 deletions packages/agents/chain-abstraction/src/helpers/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ export const DEPLOYED_ADDRESSES: Record<string, Record<string, string>> = {
swapandxcall: {
"6648936": "", // ETH mainnet
"1869640809": "", // Optimism
"6450786": "0x6840600Bc9B74fA6B3BDF1503cb31A7AdF061c1D", // BNB Chain
"6450786": "0xB7E327b0484641Aa59b3F9ffDbEa4b4872a701fD", // BNB Chain
"6778479": "", // Gnosis Chain
"1886350457": "0x697075f4A3Ce358d125281134e98d594D8Bb472e", // Polygon
"1886350457": "0x56D087121E5CC320002A4D826B08832FA9E729Bc", // Polygon
"1634886255": "", // Arbitrum One
"2053862243": "", // zkSync2 mainnet
"1887071085": "", // Polygon zkEVM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SwapAndXCallParams } from "../../types";
import { getSwapAndXCallInterface } from "../../interfaces";
import { DEPLOYED_ADDRESSES } from "../../helpers/address";
import { OriginSwapDataFns, OriginSwapperPerDomain } from "../../helpers";
import { getAddress } from "ethers/lib/utils";

/**
* Prepares `SwapAndXCall` inputs and encodes the calldata. Returns `providers.TransactionRequest` object to be sent to the RPC provider.
Expand Down Expand Up @@ -63,8 +64,10 @@ export const prepareSwapAndXCall = async (
return txRequest;
}

const originRoute =
_route ?? (await calculateRouteForSwapAndXCall(originDomain, fromAsset, toAsset, amountIn, swapAndXCallAddress));
const isSameAsset = getAddress(toAsset) === getAddress(fromAsset);
const originRoute = !isSameAsset
? _route ?? (await calculateRouteForSwapAndXCall(originDomain, fromAsset, toAsset, amountIn, swapAndXCallAddress))
: null;

const feeInNativeAsset = relayerFeeInTransactingAsset.eq(0) ?? false;
let swapAndXCallData: string;
Expand All @@ -81,8 +84,8 @@ export const prepareSwapAndXCall = async (
fromAsset,
toAsset,
BigNumber.from(amountIn),
originRoute.swapper,
originRoute.swapData,
originRoute ? originRoute.swapper : constants.AddressZero,
originRoute ? originRoute.swapData : "0x",
destinationDomain,
to,
delegate,
Expand Down Expand Up @@ -111,8 +114,8 @@ export const prepareSwapAndXCall = async (
fromAsset,
toAsset,
BigNumber.from(amountIn),
originRoute.swapper,
originRoute.swapData,
originRoute ? originRoute.swapper : constants.AddressZero,
originRoute ? originRoute.swapData : "0x",
destinationDomain,
to,
delegate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe("Libs:origin", () => {
axiosGetStub.resolves({ data: { tx: { data: "0x1a1a1a" } } });
const res = await prepareSwapAndXCall(mockSwapAndXCallParams, mkAddress("0x123"));
expect(res).to.be.deep.eq({
to: "0x697075f4A3Ce358d125281134e98d594D8Bb472e",
to: "0x56D087121E5CC320002A4D826B08832FA9E729Bc",
value: BigNumber.from(0),
data: "0x2bb679640000000000000000000000007ceb23fd6bc0add59e62ac25578270cff1b9f6190000000000000000000000002791bca1f2de4661ed88a30c99a7a9449aa8417400000000000000000000000000000000000000000000000000038d7ea4c680000000000000000000000000001111111254eeb25477b68fb85ed929f73a96058200000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000626e6200000000000000000000000012300000000000000000000000000000000000000000000000000000000000001230000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012c000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000000031a1a1a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
from: "0x1230000000000000000000000000000000000000",
Expand All @@ -84,7 +84,7 @@ describe("Libs:origin", () => {
};
const res = await prepareSwapAndXCall(swapAndXCallParams, mkAddress("0x123"));
expect(res).to.be.deep.eq({
to: "0x697075f4A3Ce358d125281134e98d594D8Bb472e",
to: "0x56D087121E5CC320002A4D826B08832FA9E729Bc",
value: BigNumber.from(1000),
data: "0x0c949d490000000000000000000000007ceb23fd6bc0add59e62ac25578270cff1b9f6190000000000000000000000002791bca1f2de4661ed88a30c99a7a9449aa8417400000000000000000000000000000000000000000000000000038d7ea4c680000000000000000000000000001111111254eeb25477b68fb85ed929f73a96058200000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000626e620000000000000000000000001230000000000000000000000000000000000000000000000000000000000000aaa0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012c00000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000003e800000000000000000000000000000000000000000000000000000000000000031a1a1a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
from: "0x1230000000000000000000000000000000000000",
Expand Down

0 comments on commit 574041e

Please sign in to comment.