/
xfai-token.ts
162 lines (145 loc) · 4.71 KB
/
xfai-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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { IAggregatorV3__factory, IERC20Metadata__factory } from '@xfai-labs/dex';
import { BigNumber, ethers, Wallet } from 'ethers';
import { multicall } from './multicall';
import { TokenInfo } from '@uniswap/token-lists';
import { Xfai, AddressType, Address, AccountAddress } from './xfai';
import { Pool } from './pool/core';
import { NATIVE_TOKEN_ADDRESS } from '../chains';
export type Token = Address<AddressType.Token> | Pick<TokenInfo, 'address' | 'decimals'>;
export const Token = (address: string): Token => Address(AddressType.Token, address);
export const isUnderlyingToken = (xfai: Xfai, token: Token) =>
xfai.underlyingToken.address === token.address;
export const isWrappedNativeToken = (xfai: Xfai, token: Token | Pool | undefined) =>
xfai.wrappedNativeToken.address === token?.address;
export const isSharedToken = (xfai: Xfai, token: Token | Pool | undefined) =>
isWrappedNativeToken(xfai, token) || isNativeToken(token);
export const isNativeToken = (token: undefined | Token | Pool) =>
NATIVE_TOKEN_ADDRESS.toLowerCase() === token?.address.toLowerCase();
export const getToken = (xfai: Xfai, token: Token | Pool) =>
IERC20Metadata__factory.connect(handleNativeToken(xfai, token).address, xfai.provider);
export function getTokenBalance(
xfai: Xfai,
wallet: Wallet | AccountAddress,
token: Token,
): Promise<BigNumber> {
if (isNativeToken(token)) {
return xfai.provider.getBalance(wallet.address);
}
return getToken(xfai, token).balanceOf(wallet.address);
}
export async function getTokenBalanceMulticall(
xfai: Xfai,
wallet: Wallet | { address: string },
tokens: Token[],
): Promise<Record<Token['address'], BigNumber | undefined>> {
const nonWeth = tokens.filter((t) => !isNativeToken(t));
const nonWethBalances = multicall(
xfai,
IERC20Metadata__factory,
nonWeth.map((token) => ({
contractAddress: token.address,
function_name: 'balanceOf',
arguments: [wallet.address],
})),
);
if (tokens.length === nonWeth.length) {
return nonWethBalances;
}
const wethBalance = await xfai.provider.getBalance(wallet.address);
return {
[xfai.nativeToken.address]: wethBalance,
...((await nonWethBalances) as Record<Token['address'], BigNumber>),
};
}
export const requestAllowance = async (xfai: Xfai, token: Token | Pool) =>
getToken(xfai, token).populateTransaction.approve(
xfai.peripheryAddress,
ethers.constants.MaxUint256,
{
from: await xfai.provider.getSigner().getAddress(),
},
);
export const requestINFTAllowance = async (xfai: Xfai, token: Token | Pool) =>
getToken(xfai, token).populateTransaction.approve(
xfai.inftPeripheryAddress,
ethers.constants.MaxUint256,
{
from: await xfai.provider.getSigner().getAddress(),
},
);
export function getPeripheryAllowance(
xfai: Xfai,
wallet: Wallet | AccountAddress,
token: Token | Pool,
) {
if (isNativeToken(token)) {
return ethers.constants.MaxUint256;
}
return getToken(xfai, token).allowance(wallet.address, xfai.peripheryAddress);
}
export function getINFTPeripheryAllowance(
xfai: Xfai,
wallet: Wallet | AccountAddress,
token: Token | Pool,
) {
if (isNativeToken(token)) {
return ethers.constants.MaxUint256;
}
return getToken(xfai, token).allowance(wallet.address, xfai.inftPeripheryAddress);
}
export async function getTokenDetails(xfai: Xfai, token: Token): Promise<TokenInfo> {
const { name, symbol, decimals } = (await multicall(
xfai,
IERC20Metadata__factory,
[
{
contractAddress: token.address,
function_name: 'name',
arguments: [],
},
{
contractAddress: token.address,
function_name: 'symbol',
arguments: [],
},
{
contractAddress: token.address,
function_name: 'decimals',
arguments: [],
},
],
{
key: (args) => args['function_name'],
},
)) as {
name: string;
symbol: string;
decimals: number;
};
return {
name,
address: token.address,
symbol,
decimals,
chainId: xfai.chain.chainId,
};
}
export function handleNativeToken(xfai: Xfai, token: Token | Pool): Token | Pool {
if (isNativeToken(token)) {
return xfai.wrappedNativeToken;
}
return token;
}
export async function getOraclePrice(xfai: Xfai, token: TokenInfo): Promise<BigNumber> {
if (isWrappedNativeToken(xfai, handleNativeToken(xfai, token))) {
return BigNumber.from(10).pow(18);
}
if (!token.extensions?.chainLinkOracleAddress) {
throw new Error('Token does not have a Chain Link oracle address');
}
const { answer } = await IAggregatorV3__factory.connect(
token.extensions.chainLinkOracleAddress as string,
xfai.provider,
).callStatic.latestRoundData();
return answer;
}