Skip to content

Commit

Permalink
🐛 fix fee undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
yuichiroaoki committed Feb 13, 2022
1 parent 08de0ff commit f971061
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 8 deletions.
17 changes: 13 additions & 4 deletions src/price/uniswap/v3/fee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,31 @@ type FeeMap = {
};
};

const uniswapV3Fee: FeeMap = {
export const uniswapV3Fee: FeeMap = {
DAI: {
USDC: 500,
USDT: 500,
WETH: 3000,
WMATIC: 500,
WBTC: 3000,
},
USDC: {
DAI: 500, // https://info.uniswap.org/#/polygon/pools/0x5f69c2ec01c22843f8273838d570243fd1963014
USDT: 500,
WETH: 500,
WMATIC: 500,
WBTC: 3000,
},
USDT: {
DAI: 500, // https://info.uniswap.org/#/polygon/pools/0x42f0530351471dab7ec968476d19bd36af9ec52d
USDC: 500, // https://info.uniswap.org/#/polygon/pools/0x3f5228d0e7d75467366be7de2c31d0d098ba2c23
WETH: 3000,
WMATIC: 500,
},
WBTC: {
WMATIC: 500,
WETH: 500,
},
WETH: {
DAI: 3000, // https://info.uniswap.org/#/polygon/pools/0x6bad0f9a89ca403bb91d253d385cec1a2b6eca97
USDC: 500, // https://info.uniswap.org/#/polygon/pools/0x45dda9cb7c25131df268515131f647d726f50608
Expand All @@ -40,10 +46,13 @@ const uniswapV3Fee: FeeMap = {
};

export const getUniswapV3PoolFee = (tokenAddresses: string[]): number => {
let feeArray = [];
const tokens = tokenAddresses.map(findToken);
const tokens = tokenAddresses.map(findToken).sort();
try {
return uniswapV3Fee[tokens[0]][tokens[1]];
const fee = uniswapV3Fee[tokens[0]][tokens[1]];
if (!fee) {
throw new Error("No fee found");
}
return fee;
} catch (error) {
// set default as 0.3%
return 3000;
Expand Down
71 changes: 67 additions & 4 deletions test/fee.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { ERC20Token } from "../src/constants/addresses";
import { getUniswapV3PoolFeeArray } from "../src/price/uniswap/v3/fee";
import { ethers } from "ethers";
import { ERC20Token, uniswapRouter } from "../src/constants/addresses";
import {
getUniswapV3PoolFee,
getUniswapV3PoolFeeArray,
uniswapV3Fee,
} from "../src/price/uniswap/v3/fee";
import { findToken } from "../src/utils";

describe("UniswapV3 pool fee test", () => {
describe("Check if it returns the correct a pool fee.", () => {
Expand All @@ -12,6 +18,12 @@ describe("UniswapV3 pool fee test", () => {
).toStrictEqual([500]);
});

test("DAI - USDC", () => {
expect(
getUniswapV3PoolFee([ERC20Token.DAI.address, ERC20Token.USDC.address])
).toStrictEqual(500);
});

test("DAI - WMATIC", () => {
expect(
getUniswapV3PoolFeeArray([
Expand All @@ -21,13 +33,27 @@ describe("UniswapV3 pool fee test", () => {
).toStrictEqual([500]);
});

test("DAI - WMATIC", () => {
expect(
getUniswapV3PoolFee([ERC20Token.DAI.address, ERC20Token.WMATIC.address])
).toStrictEqual(500);
});

test("UNI - WMATIC", () => {
const UNI = "0xb33eaad8d922b1083446dc23f610c2567fb5180f";
expect(
getUniswapV3PoolFeeArray([UNI, ERC20Token.USDC.address])
getUniswapV3PoolFeeArray([
ERC20Token.UNI.address,
ERC20Token.USDC.address,
])
).toStrictEqual([3000]);
});

test("UNI - WMATIC", () => {
expect(
getUniswapV3PoolFee([ERC20Token.UNI.address, ERC20Token.USDC.address])
).toStrictEqual(3000);
});

test("WETH - USDT", () => {
expect(
getUniswapV3PoolFeeArray([
Expand All @@ -47,4 +73,41 @@ describe("UniswapV3 pool fee test", () => {
).toStrictEqual([500, 500]);
});
});

describe("pool fee error check", () => {
test("invalid BigNumber value", () => {
expect(() => {
ethers.utils.defaultAbiCoder.encode(
["address", "uint24"],
[uniswapRouter.POLYGON_UNISWAP_V3, undefined]
);
}).toThrowError("invalid BigNumber value");
});

test("find WBTC", () => {
expect(findToken(ERC20Token.WBTC.address)).toStrictEqual("WBTC");
});

test("USDC - WBTC", () => {
expect(
getUniswapV3PoolFee([ERC20Token.DAI.address, ERC20Token.WBTC.address])
).not.toStrictEqual(undefined);
});

test("USDC - undefined", () => {
expect(
getUniswapV3PoolFee([ERC20Token.DAI.address, ""])
).not.toStrictEqual(undefined);
});

test("USDC - LINK", () => {
expect(
getUniswapV3PoolFee([ERC20Token.DAI.address, ERC20Token.LINK.address])
).not.toStrictEqual(undefined);
});

test("undefined fee", () => {
expect(uniswapV3Fee["USDC"]["WBTC"]).not.toStrictEqual(undefined);
});
});
});

0 comments on commit f971061

Please sign in to comment.