Skip to content

Commit

Permalink
fix: add 1bps in quoted fee
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff-CCH committed Aug 16, 2024
1 parent e7e50fd commit 1ad28b7
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 131 deletions.
2 changes: 1 addition & 1 deletion .env.bnb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
CHAIN_ID=56
HTTP_RPC_URL=https://bsc-dataseed.binance.org
HTTP_RPC_URL=https://rpc.ankr.com/bsc
2 changes: 2 additions & 0 deletions .env.polygonZkevm
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CHAIN_ID=1101
HTTP_RPC_URL=https://zkevm-rpc.com
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,21 @@
"lint": "eslint --fix src",
"prepublishOnly": "yarn build",
"test": "mocha",
"test:e2e:latest": "yarn run test:e2e:mainnet && yarn run test:e2e:metis",
"test:e2e:latest": "yarn run test:e2e:mainnet && yarn run test:e2e:metis && yarn run test:e2e:bnb && yarn run test:e2e:polygonZkevm",
"test:e2e:mainnet": "env-cmd -f .env.mainnet hardhat test --grep 'mainnet:'",
"test:e2e:mainnet:pb": "env-cmd -f .env.mainnet.pb hardhat test --grep 'mainnet-pb:'",
"test:e2e:optimism:pb": "env-cmd -f .env.optimism.pb hardhat test --grep 'optimism-pb:'",
"test:e2e:iota:pb": "env-cmd -f .env.iota.pb hardhat test --grep 'iota-pb:'",
"test:e2e:metis": "env-cmd -f .env.metis hardhat test --grep 'metis:'",
"test:e2e:bnb": "env-cmd -f .env.bnb hardhat test --grep 'bnb:'",
"test:e2e:polygonZkevm": "env-cmd -f .env.polygonZkevm hardhat test --grep 'polygonZkevm:'",
"test:unit": "mocha --recursive src"
},
"dependencies": {
"@paraswap/sdk": "^6.6.0",
"@protocolink/common": "^0.5.3",
"@protocolink/core": "^0.6.2",
"@protocolink/smart-accounts": "^0.1.6",
"@protocolink/common": "^0.5.4",
"@protocolink/core": "^0.6.3",
"@protocolink/smart-accounts": "^0.1.7",
"@types/lodash": "^4.14.195",
"@uniswap/sdk": "^3.0.3",
"@uniswap/sdk-core": "^3.2.6",
Expand All @@ -50,7 +52,7 @@
"@nomicfoundation/hardhat-chai-matchers": "^1.0.6",
"@nomicfoundation/hardhat-network-helpers": "^1.0.8",
"@nomiclabs/hardhat-ethers": "^2.2.3",
"@protocolink/test-helpers": "^0.4.4",
"@protocolink/test-helpers": "^0.4.5",
"@typechain/ethers-v5": "^11.0.0",
"@types/chai": "^4.3.5",
"@types/fs-extra": "^11.0.1",
Expand Down
95 changes: 47 additions & 48 deletions src/logics/stargate-v2/configs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { bnbTokens, mainnetTokens, metisTokens, polygonZkevmTokens } from './tokens';
import * as common from '@protocolink/common';

export enum StargateChainId {
Expand Down Expand Up @@ -51,51 +50,51 @@ export const configs: Config[] = [
// eid: EndpointId.ETHEREUM,
// chainId: common.ChainId.mainnet,
// pools: [
// // {
// // id: PoolId.ETH,
// // token: common.mainnetTokens.ETH,
// // address: '0x77b2043768d28E9C9aB44E1aBfC95944bcE57931',
// // destinations: [
// // { chainId: common.ChainId.optimism, endpointId: EndpointId.OPTIMISM },
// // { chainId: common.ChainId.metis, endpointId: EndpointId.METIS },
// // { chainId: common.ChainId.base, endpointId: EndpointId.BASE },
// // { chainId: common.ChainId.iota, endpointId: EndpointId.IOTA },
// // { chainId: common.ChainId.arbitrum, endpointId: EndpointId.ARBITRUM },
// // ],
// // },
// // {
// // id: PoolId.USDC,
// // token: common.mainnetTokens.USDC,
// // address: '0xc026395860Db2d07ee33e05fE50ed7bD583189C7',
// // destinations: [
// // { chainId: common.ChainId.optimism, endpointId: EndpointId.OPTIMISM },
// // { chainId: common.ChainId.polygon, endpointId: EndpointId.POLYGON },
// // { chainId: common.ChainId.base, endpointId: EndpointId.BASE },
// // { chainId: common.ChainId.iota, endpointId: EndpointId.IOTA },
// // { chainId: common.ChainId.arbitrum, endpointId: EndpointId.ARBITRUM },
// // { chainId: common.ChainId.avalanche, endpointId: EndpointId.AVALANCHE },
// // ],
// // },
// // {
// // id: PoolId.USDT,
// // token: common.mainnetTokens.USDT,
// // address: '0x933597a323Eb81cAe705C5bC29985172fd5A3973',
// // destinations: [
// // { chainId: common.ChainId.optimism, endpointId: EndpointId.OPTIMISM },
// // { chainId: common.ChainId.bnb, endpointId: EndpointId.BNB },
// // { chainId: common.ChainId.polygon, endpointId: EndpointId.POLYGON },
// // { chainId: common.ChainId.metis, endpointId: EndpointId.METIS },
// // { chainId: common.ChainId.iota, endpointId: EndpointId.IOTA },
// // { chainId: common.ChainId.arbitrum, endpointId: EndpointId.ARBITRUM },
// // { chainId: common.ChainId.avalanche, endpointId: EndpointId.AVALANCHE },
// // ],
// // },
// // {
// // id: PoolId.METIS,
// // token: mainnetTokens.Metis,
// // address: '0xcDafB1b2dB43f366E48e6F614b8DCCBFeeFEEcD3',
// // destinations: [{ chainId: common.ChainId.metis, endpointId: EndpointId.METIS }],
// // },
// {
// id: PoolId.ETH,
// token: common.mainnetTokens.ETH,
// address: '0x77b2043768d28E9C9aB44E1aBfC95944bcE57931',
// destinations: [
// { chainId: common.ChainId.optimism, endpointId: EndpointId.OPTIMISM },
// { chainId: common.ChainId.metis, endpointId: EndpointId.METIS },
// { chainId: common.ChainId.base, endpointId: EndpointId.BASE },
// { chainId: common.ChainId.iota, endpointId: EndpointId.IOTA },
// { chainId: common.ChainId.arbitrum, endpointId: EndpointId.ARBITRUM },
// ],
// },
// {
// id: PoolId.USDC,
// token: common.mainnetTokens.USDC,
// address: '0xc026395860Db2d07ee33e05fE50ed7bD583189C7',
// destinations: [
// { chainId: common.ChainId.optimism, endpointId: EndpointId.OPTIMISM },
// { chainId: common.ChainId.polygon, endpointId: EndpointId.POLYGON },
// { chainId: common.ChainId.base, endpointId: EndpointId.BASE },
// { chainId: common.ChainId.iota, endpointId: EndpointId.IOTA },
// { chainId: common.ChainId.arbitrum, endpointId: EndpointId.ARBITRUM },
// { chainId: common.ChainId.avalanche, endpointId: EndpointId.AVALANCHE },
// ],
// },
// {
// id: PoolId.USDT,
// token: common.mainnetTokens.USDT,
// address: '0x933597a323Eb81cAe705C5bC29985172fd5A3973',
// destinations: [
// { chainId: common.ChainId.optimism, endpointId: EndpointId.OPTIMISM },
// { chainId: common.ChainId.bnb, endpointId: EndpointId.BNB },
// { chainId: common.ChainId.polygon, endpointId: EndpointId.POLYGON },
// { chainId: common.ChainId.metis, endpointId: EndpointId.METIS },
// { chainId: common.ChainId.iota, endpointId: EndpointId.IOTA },
// { chainId: common.ChainId.arbitrum, endpointId: EndpointId.ARBITRUM },
// { chainId: common.ChainId.avalanche, endpointId: EndpointId.AVALANCHE },
// ],
// },
// {
// id: PoolId.METIS,
// token: mainnetTokens.Metis,
// address: '0xcDafB1b2dB43f366E48e6F614b8DCCBFeeFEEcD3',
// destinations: [{ chainId: common.ChainId.metis, endpointId: EndpointId.METIS }],
// },
// ],
// },
{
Expand All @@ -105,7 +104,7 @@ export const configs: Config[] = [
pools: [
{
id: PoolId.OFT,
token: bnbTokens.CAKE,
token: common.bnbTokens.Cake,
address: '0x86355F02119bdBC28ED6A4D5E0cA327Ca7730fFF', // OFTWrapper
proxyOFT: '0xb274202daBA6AE180c665B4fbE59857b7c3a8091',
destinations: [{ chainId: common.ChainId.polygonZkevm, endpointId: EndpointId.OFT }],
Expand Down Expand Up @@ -185,7 +184,7 @@ export const configs: Config[] = [
pools: [
{
id: PoolId.OFT,
token: polygonZkevmTokens.CAKE,
token: common.polygonZkevmTokens.Cake,
address: '0x86355F02119bdBC28ED6A4D5E0cA327Ca7730fFF', // OFTWrapper
destinations: [{ chainId: common.ChainId.bnb, endpointId: EndpointId.OFT }],
},
Expand Down
15 changes: 7 additions & 8 deletions src/logics/stargate-v2/logic.swap-token.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { LogicTestCase } from 'test/types';
import { OFTWrapper__factory, StargatePool__factory } from './contracts';
import { OFTWrapper__factory } from './contracts';
import { SwapTokenLogic, SwapTokenLogicFields, SwapTokenLogicOptions } from './logic.swap-token';
import { bnbTokens, polygonZkevmTokens } from './tokens';
import * as common from '@protocolink/common';
import { constants, utils } from 'ethers';
import * as core from '@protocolink/core';
Expand All @@ -26,26 +25,26 @@ describe('StargateV2 SwapTokenLogic', function () {
const testCases: LogicTestCase<SwapTokenLogicFields, SwapTokenLogicOptions>[] = [
{
fields: {
input: new common.TokenAmount(bnbTokens.CAKE, '1'),
output: new common.TokenAmount(polygonZkevmTokens.CAKE, '1'),
input: new common.TokenAmount(common.bnbTokens.Cake, '1'),
output: new common.TokenAmount(common.polygonZkevmTokens.Cake, '1'),
receiver: account,
fee: '0',
},
options: { account },
},
{
fields: {
input: new common.TokenAmount(polygonZkevmTokens.CAKE, '1'),
output: new common.TokenAmount(bnbTokens.CAKE, '1'),
input: new common.TokenAmount(common.polygonZkevmTokens.Cake, '1'),
output: new common.TokenAmount(common.bnbTokens.Cake, '1'),
receiver: account,
fee: '0.1',
},
options: { account },
},
{
fields: {
input: new common.TokenAmount(polygonZkevmTokens.CAKE, '1'),
output: new common.TokenAmount(bnbTokens.CAKE, '1'),
input: new common.TokenAmount(common.polygonZkevmTokens.Cake, '1'),
output: new common.TokenAmount(common.bnbTokens.Cake, '1'),
receiver: account,
fee: '0.1',
balanceBps: 5000,
Expand Down
14 changes: 8 additions & 6 deletions src/logics/stargate-v2/logic.swap-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,18 @@ export class SwapTokenLogic extends core.Logic implements core.LogicBuilderInter
adapterParameters,
feeObj
);
fee = common.calcSlippage(fee, -1); // slightly higher than the quoted fee

const [amount] = await oftWrapper.getAmountAndFees(oft, srcAmount, feeObj.callerBps);
output = output.setWei(amount);
}

return {
input,
output,
fee: common.toBigUnit(fee, common.getNativeToken(this.chainId).decimals),
receiver,
};
// } else {

// // check if tokenOut is legit
Expand Down Expand Up @@ -126,12 +134,6 @@ export class SwapTokenLogic extends core.Logic implements core.LogicBuilderInter
// const lzTokenFee = common.toBigUnit(messagingFee.lzTokenFee, getNativeToken(this.chainId).decimals);
// const output = new common.TokenAmount(tokenOut).setWei(receipt.amountReceivedLD);
// }
return {
input,
output,
fee: common.toBigUnit(fee, common.getNativeToken(this.chainId).decimals),
receiver,
};
}

async build(fields: SwapTokenLogicFields, options: SwapTokenLogicOptions) {
Expand Down
9 changes: 0 additions & 9 deletions src/logics/stargate-v2/tokens/data/bnb.json

This file was deleted.

7 changes: 0 additions & 7 deletions src/logics/stargate-v2/tokens/data/mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,5 @@
"decimals": 18,
"symbol": "Metis",
"name": "Metis Token"
},
"CAKE": {
"chainId": 1,
"address": "0x152649ea73beab28c5b49b26eb48f7ead6d4c898",
"decimals": 18,
"symbol": "CAKE",
"name": "PancakeSwap Token"
}
}
9 changes: 0 additions & 9 deletions src/logics/stargate-v2/tokens/data/polygonZkevm.json

This file was deleted.

10 changes: 0 additions & 10 deletions src/logics/stargate-v2/tokens/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
import bnbTokensJSON from './data/bnb.json';
import * as common from '@protocolink/common';
import mainnetTokensJSON from './data/mainnet.json';
import metisTokensJSON from './data/metis.json';
import polygonZkevmTokensJSON from './data/polygonZkevm.json';

type MainnetTokenSymbols = keyof typeof mainnetTokensJSON;

export const mainnetTokens = common.toTokenMap<MainnetTokenSymbols>(mainnetTokensJSON);

type BnbTokenSymbols = keyof typeof bnbTokensJSON;

export const bnbTokens = common.toTokenMap<BnbTokenSymbols>(bnbTokensJSON);

type MetisTokenSymbols = keyof typeof metisTokensJSON;

export const metisTokens = common.toTokenMap<MetisTokenSymbols>(metisTokensJSON);

type PolygonZkevmTokenSymbols = keyof typeof polygonZkevmTokensJSON;

export const polygonZkevmTokens = common.toTokenMap<PolygonZkevmTokenSymbols>(polygonZkevmTokensJSON);
7 changes: 3 additions & 4 deletions test/logics/stargate-v2/swap-token-bnb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import * as core from '@protocolink/core';
import { expect } from 'chai';
import { getNativeToken } from '@protocolink/common';
import hre from 'hardhat';
import { polygonZkevmTokens } from 'src/logics/stargate-v2/tokens';
import * as stargate from 'src/logics/stargate-v2';
import * as utils from 'test/utils';

Expand All @@ -25,13 +24,13 @@ describe('bnb: Test StargateV2 SwapToken Logic', function () {
{
params: {
input: new common.TokenAmount(common.bnbTokens.Cake, '1'),
tokenOut: polygonZkevmTokens.CAKE,
tokenOut: common.polygonZkevmTokens.Cake,
},
},
{
params: {
input: new common.TokenAmount(common.bnbTokens.Cake, '1'),
tokenOut: polygonZkevmTokens.CAKE,
tokenOut: common.polygonZkevmTokens.Cake,
},
balanceBps: 5000,
},
Expand Down Expand Up @@ -78,7 +77,7 @@ describe('bnb: Test StargateV2 SwapToken Logic', function () {
await expect(user.address).to.changeBalance(getNativeToken(chainId), -input.add(fee).amount, 1);
} else {
await expect(user.address).to.changeBalance(input.token, -input.amount);
await expect(user.address).to.changeBalance(getNativeToken(chainId), -fee);
await expect(user.address).to.changeBalance(getNativeToken(chainId), -fee, 1);
}
});
});
Expand Down
9 changes: 4 additions & 5 deletions test/logics/stargate-v2/swap-token-polygonZkevm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import * as core from '@protocolink/core';
import { expect } from 'chai';
import { getNativeToken } from '@protocolink/common';
import hre from 'hardhat';
import { polygonZkevmTokens } from 'src/logics/stargate-v2/tokens';
import * as stargate from 'src/logics/stargate-v2';
import * as utils from 'test/utils';

Expand All @@ -19,7 +18,7 @@ describe('polygonZkevm: Test StargateV2 SwapToken Logic', function () {
await claimToken(
chainId,
user.address,
polygonZkevmTokens.CAKE,
common.polygonZkevmTokens.Cake,
'10',
'0xb4BAB40e5a869eF1b5ff440a170A57d9feb228e9'
);
Expand All @@ -30,13 +29,13 @@ describe('polygonZkevm: Test StargateV2 SwapToken Logic', function () {
const testCases = [
{
params: {
input: new common.TokenAmount(polygonZkevmTokens.CAKE, '1'),
input: new common.TokenAmount(common.polygonZkevmTokens.Cake, '1'),
tokenOut: common.bnbTokens.Cake,
},
},
{
params: {
input: new common.TokenAmount(polygonZkevmTokens.CAKE, '1'),
input: new common.TokenAmount(common.polygonZkevmTokens.Cake, '1'),
tokenOut: common.bnbTokens.Cake,
},
balanceBps: 5000,
Expand Down Expand Up @@ -84,7 +83,7 @@ describe('polygonZkevm: Test StargateV2 SwapToken Logic', function () {
await expect(user.address).to.changeBalance(getNativeToken(chainId), -input.add(fee).amount, 1);
} else {
await expect(user.address).to.changeBalance(input.token, -input.amount);
await expect(user.address).to.changeBalance(getNativeToken(chainId), -fee);
await expect(user.address).to.changeBalance(getNativeToken(chainId), -fee, 1);
}
});
});
Expand Down
Loading

0 comments on commit 1ad28b7

Please sign in to comment.