-
Notifications
You must be signed in to change notification settings - Fork 15
/
logic.swap-token.ts
60 lines (50 loc) · 2 KB
/
logic.swap-token.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { FeeAmount, computePoolAddress } from '@uniswap/v3-sdk';
import { Token } from '@uniswap/sdk-core';
import { TokenList } from '@uniswap/token-lists';
import { axios } from 'src/utils';
import * as common from '@protocolink/common';
import * as core from '@protocolink/core';
import { getConfig, supportedChainIds } from './configs';
import { providers } from 'ethers';
import * as univ3 from 'src/modules/univ3';
export type SwapTokenLogicTokenList = common.Token[];
export type SwapTokenLogicParams = univ3.SwapTokenLogicParams;
export type SwapTokenLogicFields = univ3.SwapTokenLogicFields;
export type SwapTokenLogicOptions = univ3.SwapTokenLogicOptions;
export class SwapTokenLogic
extends univ3.SwapTokenLogic
implements core.LogicTokenListInterface, core.LogicOracleInterface, core.LogicBuilderInterface
{
static id = 'swap-token';
static protocolId = 'uniswap-v3';
static readonly supportedChainIds = supportedChainIds;
constructor(chainId: number, provider?: providers.Provider) {
super({ chainId, provider, config: getConfig(chainId) });
}
async getTokenList() {
const { data } = await axios.get<TokenList>('https://gateway.ipfs.io/ipns/tokens.uniswap.org');
const tmp: Record<string, boolean> = { [this.nativeToken.address]: true };
const tokenList: SwapTokenLogicTokenList = [this.nativeToken];
for (const { chainId, address, decimals, symbol, name, logoURI } of data.tokens) {
if (tmp[address] || chainId !== this.chainId) continue;
tokenList.push(new common.Token(chainId, address, decimals, symbol, name, logoURI));
tmp[address] = true;
}
return tokenList;
}
public async computePoolAddress({
factoryAddress,
tokenA,
tokenB,
fee,
initCodeHashManualOverride,
}: {
factoryAddress: string;
tokenA: Token;
tokenB: Token;
fee: FeeAmount;
initCodeHashManualOverride?: string | undefined;
}): Promise<string> {
return computePoolAddress({ factoryAddress, tokenA, tokenB, fee, initCodeHashManualOverride });
}
}