@aurigami/sdk 1.4.8 → 1.5.0-dummy-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/MoneyMarket.d.ts +2 -2
- package/dist/Papermill.d.ts +31 -0
- package/dist/SDK.d.ts +6 -7
- package/dist/airdrop-lottery.d.ts +4 -4
- package/dist/constants.d.ts +6 -2
- package/dist/helpers.d.ts +3 -3
- package/dist/index.d.ts +1 -0
- package/dist/priceFetcher.d.ts +6 -4
- package/dist/sdk.cjs.development.js +700 -270
- package/dist/sdk.cjs.development.js.map +1 -1
- package/dist/sdk.cjs.production.min.js +1 -1
- package/dist/sdk.cjs.production.min.js.map +1 -1
- package/dist/sdk.esm.js +690 -268
- package/dist/sdk.esm.js.map +1 -1
- package/dist/token.d.ts +1 -1
- package/dist/tokenAmount.d.ts +1 -1
- package/dist/transfer-event-query.d.ts +4 -4
- package/dist/types.d.ts +13 -9
- package/package.json +5 -11
- package/src/BlockchainEntity.ts +6 -2
- package/src/MoneyMarket.ts +317 -105
- package/src/Papermill.ts +243 -0
- package/src/SDK.ts +307 -147
- package/src/airdrop-lottery.ts +293 -248
- package/src/aurora-api-helpers.ts +11 -6
- package/src/constants.ts +82 -60
- package/src/decimals.ts +5 -3
- package/src/dummy.ts +17 -23
- package/src/helpers.ts +45 -26
- package/src/index.ts +2 -1
- package/src/priceFetcher.ts +256 -103
- package/src/token.ts +16 -16
- package/src/tokenAmount.ts +21 -21
- package/src/transfer-event-query.ts +64 -54
- package/src/types.ts +39 -19
package/src/priceFetcher.ts
CHANGED
|
@@ -1,145 +1,298 @@
|
|
|
1
|
-
import BigNumber from
|
|
2
|
-
import { Contract, providers, BigNumber as BN, ethers } from
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
import
|
|
1
|
+
import BigNumber from 'bignumber.js';
|
|
2
|
+
import { Contract, providers, BigNumber as BN, ethers } from 'ethers';
|
|
3
|
+
import {
|
|
4
|
+
DECIMAL_PRECISION,
|
|
5
|
+
networkAddresses,
|
|
6
|
+
PRICE_WHITELISTED,
|
|
7
|
+
} from './constants';
|
|
8
|
+
import OracleABI from './abis/Oracle.json';
|
|
9
|
+
import { isSameAddress, decimalFactor, getDecimal } from './helpers';
|
|
10
|
+
import { IUniswapV2Pair, PriceOracle } from '@aurigami/contracts/typechain';
|
|
11
|
+
import { TokenAmount } from './tokenAmount';
|
|
12
|
+
import { Address, TokenValuation } from './types';
|
|
9
13
|
import IUniswapV2PairABI from './abis/IUniswapV2Pair.json';
|
|
10
14
|
import AuriLensABI from '@aurigami/contracts/artifacts/contracts/AuriLens.sol/AuriLens.json';
|
|
11
15
|
import axios from 'axios';
|
|
16
|
+
import assert from 'assert';
|
|
12
17
|
|
|
13
18
|
const hardcodePLYPrice = true;
|
|
14
|
-
export async function fetchPriceFromCoingeckoAPI(
|
|
15
|
-
|
|
19
|
+
export async function fetchPriceFromCoingeckoAPI(
|
|
20
|
+
id: string
|
|
21
|
+
): Promise<BigNumber> {
|
|
22
|
+
const price = await axios
|
|
23
|
+
.get(
|
|
16
24
|
`https://api.coingecko.com/api/v3/simple/price?ids=${id}&vs_currencies=usd`
|
|
17
|
-
)
|
|
18
|
-
|
|
19
|
-
|
|
25
|
+
)
|
|
26
|
+
.then((res: any) => {
|
|
27
|
+
return res.data;
|
|
28
|
+
});
|
|
20
29
|
|
|
21
|
-
|
|
30
|
+
return new BigNumber(price[id].usd);
|
|
22
31
|
}
|
|
23
32
|
|
|
24
33
|
export async function fetchPriceFromCoingecko(id: Address): Promise<BigNumber> {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
34
|
+
switch (id.toLowerCase()) {
|
|
35
|
+
case networkAddresses.tokens.AURORA:
|
|
36
|
+
return fetchPriceFromCoingeckoAPI('aurora-near');
|
|
28
37
|
|
|
29
|
-
|
|
30
|
-
|
|
38
|
+
case networkAddresses.tokens.TRI:
|
|
39
|
+
return fetchPriceFromCoingeckoAPI('trisolaris');
|
|
31
40
|
|
|
32
|
-
|
|
33
|
-
|
|
41
|
+
case networkAddresses.tokens.META:
|
|
42
|
+
return fetchPriceFromCoingeckoAPI('meta-pool');
|
|
34
43
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
44
|
+
default:
|
|
45
|
+
throw Error(`Unknown token ${id} in fetchPriceFromCoingecko`);
|
|
46
|
+
}
|
|
38
47
|
}
|
|
39
48
|
|
|
40
|
-
export async function fetchLPPositions(
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
49
|
+
export async function fetchLPPositions(
|
|
50
|
+
LPAddress: Address,
|
|
51
|
+
provider: providers.Provider
|
|
52
|
+
): Promise<{
|
|
53
|
+
token0: Address;
|
|
54
|
+
token1: Address;
|
|
55
|
+
reserve0: BN;
|
|
56
|
+
reserve1: BN;
|
|
57
|
+
totalSupply: BN;
|
|
46
58
|
}> {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
const LPContract = new Contract(
|
|
60
|
+
LPAddress,
|
|
61
|
+
IUniswapV2PairABI.abi,
|
|
62
|
+
provider
|
|
63
|
+
) as IUniswapV2Pair;
|
|
64
|
+
var promises: any[] = [];
|
|
65
|
+
promises.push(LPContract.token0());
|
|
66
|
+
promises.push(LPContract.token1());
|
|
67
|
+
promises.push(LPContract.getReserves());
|
|
68
|
+
promises.push(LPContract.totalSupply());
|
|
69
|
+
const values: any[] = await Promise.all(promises);
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
token0: values[0] as string,
|
|
73
|
+
token1: values[1] as string,
|
|
74
|
+
reserve0: (values[2] as { reserve0: BN; reserve1: BN }).reserve0,
|
|
75
|
+
reserve1: (values[2] as { reserve0: BN; reserve1: BN }).reserve1,
|
|
76
|
+
totalSupply: values[3] as BN,
|
|
77
|
+
};
|
|
61
78
|
}
|
|
62
79
|
|
|
63
|
-
export async function fetchLPPrice(
|
|
64
|
-
|
|
80
|
+
export async function fetchLPPrice(
|
|
81
|
+
address: Address,
|
|
82
|
+
provider: providers.Provider
|
|
83
|
+
): Promise<BigNumber> {
|
|
84
|
+
var LPInfo = await fetchLPPositions(address, provider);
|
|
85
|
+
const LPDecimal = getDecimal(address);
|
|
86
|
+
|
|
87
|
+
async function fetchLPPriceBy(tokenReverse: {
|
|
88
|
+
address: string;
|
|
89
|
+
reserve: BN;
|
|
90
|
+
}): Promise<BigNumber> {
|
|
91
|
+
if (tokenReverse.reserve.isZero()) return new BigNumber(0);
|
|
92
|
+
|
|
93
|
+
// Should NOT call `fetchPrice`, it will cause an infinite loop
|
|
94
|
+
// because the token can be PLY. To calculate PLY price, we need to
|
|
95
|
+
// call `fetchPrice` again...
|
|
96
|
+
const tokenPrice: BigNumber = await _fetchPrice(
|
|
97
|
+
tokenReverse.address,
|
|
98
|
+
provider
|
|
99
|
+
);
|
|
100
|
+
const tokenDecimal = getDecimal(tokenReverse.address);
|
|
101
|
+
|
|
102
|
+
// reserveUSD will be divided by 10^tokenDecimal later for more precision
|
|
103
|
+
const reserveUSD: BigNumber = tokenPrice
|
|
104
|
+
.multipliedBy(tokenReverse.reserve.toString())
|
|
105
|
+
.multipliedBy(2);
|
|
106
|
+
return reserveUSD
|
|
107
|
+
.multipliedBy(decimalFactor(LPDecimal))
|
|
108
|
+
.dividedBy(new BigNumber(LPInfo.totalSupply.toString()))
|
|
109
|
+
.dividedBy(decimalFactor(tokenDecimal));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
try {
|
|
113
|
+
// MUST await here
|
|
114
|
+
return await fetchLPPriceBy({
|
|
115
|
+
address: LPInfo.token0,
|
|
116
|
+
reserve: LPInfo.reserve0,
|
|
117
|
+
});
|
|
118
|
+
} catch {
|
|
65
119
|
try {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
120
|
+
// MUST await here
|
|
121
|
+
return await fetchLPPriceBy({
|
|
122
|
+
address: LPInfo.token1,
|
|
123
|
+
reserve: LPInfo.reserve1,
|
|
124
|
+
});
|
|
69
125
|
} catch {
|
|
70
|
-
|
|
71
|
-
const token1Price: BigNumber = await fetchPrice(LPInfo.token1, provider);
|
|
72
|
-
const reserveUSD: BigNumber = token1Price.multipliedBy(LPInfo.reserve1.toString()).multipliedBy(2);
|
|
73
|
-
return reserveUSD.multipliedBy(decimalFactor(18)).dividedBy(new BigNumber(LPInfo.totalSupply.toString()));
|
|
74
|
-
} catch {
|
|
75
|
-
throw Error(`Unable to fetch price for both tokens of LP ${address}`);
|
|
76
|
-
}
|
|
126
|
+
throw Error(`Unable to fetch price for both tokens of LP ${address}`);
|
|
77
127
|
}
|
|
78
128
|
}
|
|
129
|
+
}
|
|
79
130
|
|
|
80
131
|
function processPriceFromOracle(rawPrice: BN, underlyingDecimal: number) {
|
|
81
|
-
|
|
132
|
+
return new BigNumber(rawPrice.toString()).div(
|
|
133
|
+
decimalFactor(36 - underlyingDecimal)
|
|
134
|
+
);
|
|
82
135
|
}
|
|
83
|
-
export async function fetchPriceFromOracle(
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
136
|
+
export async function fetchPriceFromOracle(
|
|
137
|
+
auTokenAddress: Address,
|
|
138
|
+
underlyingDecimal: number,
|
|
139
|
+
provider: providers.Provider
|
|
140
|
+
): Promise<BigNumber> {
|
|
141
|
+
const oracleContract: PriceOracle = new Contract(
|
|
142
|
+
networkAddresses.misc.oracle,
|
|
143
|
+
OracleABI.abi,
|
|
144
|
+
provider
|
|
145
|
+
) as PriceOracle;
|
|
146
|
+
const rawPrice = await oracleContract.getUnderlyingPrice(auTokenAddress);
|
|
147
|
+
return processPriceFromOracle(rawPrice, underlyingDecimal);
|
|
87
148
|
}
|
|
88
149
|
|
|
89
|
-
export async function fetchUnderlyingTokensPrices(
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
150
|
+
export async function fetchUnderlyingTokensPrices(
|
|
151
|
+
provider: providers.Provider
|
|
152
|
+
): Promise<
|
|
153
|
+
{
|
|
154
|
+
auToken: string;
|
|
155
|
+
underlyingPrice: BigNumber;
|
|
156
|
+
}[]
|
|
157
|
+
> {
|
|
158
|
+
const auriLens: Contract = new Contract(
|
|
159
|
+
networkAddresses.misc.AURILENS,
|
|
160
|
+
AuriLensABI.abi,
|
|
161
|
+
provider
|
|
162
|
+
);
|
|
163
|
+
const auTokenAddresses = networkAddresses.auTokens.map(
|
|
164
|
+
(auToken) => auToken.address
|
|
165
|
+
);
|
|
166
|
+
const underlyingDecimals = networkAddresses.auTokens.map((auToken) =>
|
|
167
|
+
getDecimal(auToken.underlying)
|
|
168
|
+
);
|
|
169
|
+
const rawPrices = await auriLens.auTokenUnderlyingPriceAll(auTokenAddresses);
|
|
170
|
+
return rawPrices.map((res: any, ind: number) => {
|
|
171
|
+
return {
|
|
172
|
+
auToken: res.auToken,
|
|
173
|
+
underlyingPrice: processPriceFromOracle(
|
|
174
|
+
res.underlyingPrice,
|
|
175
|
+
underlyingDecimals[ind]
|
|
176
|
+
),
|
|
177
|
+
};
|
|
178
|
+
});
|
|
103
179
|
}
|
|
104
180
|
|
|
105
181
|
export async function fetchStNEARPrice(): Promise<BigNumber> {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
182
|
+
const ratioPromise = axios
|
|
183
|
+
.get('https://validators.narwallets.com/metrics_json')
|
|
184
|
+
.then((res) => res.data);
|
|
185
|
+
const nearPricePromise = fetchPriceFromCoingeckoAPI('near');
|
|
186
|
+
const [ratioRes, nearPrice]: [any, BigNumber] = await Promise.all([
|
|
187
|
+
ratioPromise,
|
|
188
|
+
nearPricePromise,
|
|
189
|
+
]);
|
|
190
|
+
const ratio = new BigNumber(ratioRes.st_near_price);
|
|
191
|
+
return ratio.multipliedBy(nearPrice);
|
|
111
192
|
}
|
|
112
193
|
|
|
113
194
|
function shouldFetchFromCoingecko(address: string) {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
195
|
+
address = address.toLowerCase();
|
|
196
|
+
return (
|
|
197
|
+
address == networkAddresses.tokens.AURORA ||
|
|
198
|
+
address == networkAddresses.tokens.TRI ||
|
|
199
|
+
address == networkAddresses.tokens.META
|
|
200
|
+
);
|
|
118
201
|
}
|
|
119
202
|
|
|
120
|
-
|
|
121
|
-
|
|
203
|
+
/**
|
|
204
|
+
* Fetch price from Oracle or Coingecko
|
|
205
|
+
*/
|
|
206
|
+
async function _fetchPrice(
|
|
207
|
+
address: Address,
|
|
208
|
+
provider: providers.Provider
|
|
209
|
+
): Promise<BigNumber> {
|
|
210
|
+
assert(PRICE_WHITELISTED.has(address.toLocaleLowerCase()));
|
|
122
211
|
|
|
123
|
-
|
|
124
|
-
|
|
212
|
+
if (isSameAddress(address, networkAddresses.tokens.stNEAR))
|
|
213
|
+
return fetchStNEARPrice();
|
|
214
|
+
if (shouldFetchFromCoingecko(address))
|
|
215
|
+
return fetchPriceFromCoingecko(address);
|
|
125
216
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
217
|
+
const matchingAuToken = networkAddresses.auTokens.find((auToken) => {
|
|
218
|
+
return isSameAddress(auToken.underlying, address);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
if (matchingAuToken !== undefined) {
|
|
222
|
+
return fetchPriceFromOracle(
|
|
223
|
+
matchingAuToken.address,
|
|
224
|
+
getDecimal(address),
|
|
225
|
+
provider
|
|
226
|
+
);
|
|
227
|
+
} else {
|
|
228
|
+
throw Error(`Unable to fetch price for ${address}`);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export async function fetchPrice(
|
|
233
|
+
address: Address,
|
|
234
|
+
provider: providers.Provider
|
|
235
|
+
): Promise<BigNumber> {
|
|
236
|
+
if (isSameAddress(address, networkAddresses.tokens.PLYUSDC)) {
|
|
237
|
+
return fetchLPPrice(address, provider);
|
|
238
|
+
} else if (isSameAddress(address, networkAddresses.tokens.PLY)) {
|
|
239
|
+
return fetchPLYPrice(provider);
|
|
240
|
+
}
|
|
241
|
+
return _fetchPrice(address, provider);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export async function fetchPLYPrice(
|
|
245
|
+
provider: providers.Provider
|
|
246
|
+
): Promise<BigNumber> {
|
|
247
|
+
const LPInfo = await fetchLPPositions(
|
|
248
|
+
networkAddresses.tokens.PLYUSDC,
|
|
249
|
+
provider
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
if (LPInfo.reserve1.isZero()) {
|
|
253
|
+
return new BigNumber(0);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const plyAddress = LPInfo.token0;
|
|
257
|
+
assert(isSameAddress(plyAddress, networkAddresses.tokens.PLY));
|
|
258
|
+
const plyDecimal = getDecimal(plyAddress);
|
|
259
|
+
|
|
260
|
+
const usdcAddress = LPInfo.token1;
|
|
261
|
+
assert(isSameAddress(usdcAddress, networkAddresses.tokens.USDC));
|
|
262
|
+
const usdcDecimal = getDecimal(usdcAddress);
|
|
263
|
+
|
|
264
|
+
// (token0Price * reserve0) / 10^decimal0 = (token1Price * reserve1) / 10^decimal1
|
|
265
|
+
// token0Price = (token1Price * reserve1) * 10^decimal0 / 10^decimal1 / reserve0
|
|
266
|
+
return (await fetchPrice(usdcAddress, provider))
|
|
267
|
+
.multipliedBy(LPInfo.reserve1.toString())
|
|
268
|
+
.multipliedBy(decimalFactor(plyDecimal))
|
|
269
|
+
.dividedBy(decimalFactor(usdcDecimal))
|
|
270
|
+
.dividedBy(LPInfo.reserve0.toString());
|
|
136
271
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
272
|
+
|
|
273
|
+
export async function fetchValuation(
|
|
274
|
+
tokenAmount: TokenAmount,
|
|
275
|
+
provider: providers.Provider
|
|
276
|
+
): Promise<BigNumber> {
|
|
277
|
+
if (tokenAmount.rawAmount() == '0') return new BigNumber(0);
|
|
278
|
+
var price = await fetchPrice(tokenAmount.token.address, provider);
|
|
279
|
+
return calcValuation(tokenAmount, price);
|
|
141
280
|
}
|
|
142
281
|
|
|
143
282
|
export function calcValuation(tokenAmount: TokenAmount, price: BigNumber) {
|
|
144
|
-
|
|
283
|
+
return new BigNumber(tokenAmount.formattedAmount()).multipliedBy(price);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export async function valuateToken(
|
|
287
|
+
tokenAmount: TokenAmount,
|
|
288
|
+
provider: providers.Provider
|
|
289
|
+
): Promise<TokenValuation> {
|
|
290
|
+
let tokenValuation = await fetchValuation(tokenAmount, provider);
|
|
291
|
+
return {
|
|
292
|
+
tokenAmount: tokenAmount,
|
|
293
|
+
currencyAmount: {
|
|
294
|
+
currency: 'USD',
|
|
295
|
+
amount: tokenValuation.toFixed(DECIMAL_PRECISION),
|
|
296
|
+
},
|
|
297
|
+
};
|
|
145
298
|
}
|
package/src/token.ts
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import { getDecimal } from
|
|
2
|
-
import { networkAddresses } from
|
|
1
|
+
import { getDecimal } from './helpers';
|
|
2
|
+
import { networkAddresses } from './constants';
|
|
3
3
|
|
|
4
4
|
export class Token {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
public readonly address: string;
|
|
6
|
+
public readonly decimals: number;
|
|
7
|
+
public constructor(address: string, decimals: number) {
|
|
8
|
+
this.address = address.toLowerCase();
|
|
9
|
+
this.decimals = decimals;
|
|
10
|
+
}
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
export const PLYToken = new Token(
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
)
|
|
17
|
-
|
|
18
|
-
export const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
)
|
|
14
|
+
networkAddresses.tokens.PLY,
|
|
15
|
+
getDecimal(networkAddresses.tokens.PLY)
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
export const PLYUSDCToken = new Token(
|
|
19
|
+
networkAddresses.tokens.PLYUSDC,
|
|
20
|
+
getDecimal(networkAddresses.tokens.PLYUSDC)
|
|
21
|
+
);
|
package/src/tokenAmount.ts
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import { Token } from
|
|
1
|
+
import { Token } from './token';
|
|
2
2
|
import BigNumber from 'bignumber.js';
|
|
3
|
-
import { decimalFactor } from
|
|
3
|
+
import { decimalFactor } from './helpers';
|
|
4
4
|
|
|
5
5
|
export class TokenAmount {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
public readonly token: Token;
|
|
7
|
+
private rawAmnt: string;
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
this.token = token;
|
|
9
|
+
public constructor(token: Token, amount: string, isRaw: boolean = true) {
|
|
10
|
+
if (isRaw) {
|
|
11
|
+
this.rawAmnt = amount;
|
|
12
|
+
} else {
|
|
13
|
+
this.rawAmnt = new BigNumber(amount)
|
|
14
|
+
.times(decimalFactor(token.decimals))
|
|
15
|
+
.toFixed(0);
|
|
18
16
|
}
|
|
17
|
+
this.token = token;
|
|
18
|
+
}
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
public formattedAmount(): string {
|
|
21
|
+
return new BigNumber(this.rawAmnt)
|
|
22
|
+
.div(decimalFactor(this.token.decimals))
|
|
23
|
+
.toString();
|
|
24
|
+
}
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
public rawAmount(): string {
|
|
27
|
+
return this.rawAmnt;
|
|
28
|
+
}
|
|
29
29
|
}
|
|
@@ -1,60 +1,70 @@
|
|
|
1
|
-
import { BlockchainEntityRead } from
|
|
2
|
-
import { NetworkConnection } from
|
|
3
|
-
import { AuToken } from
|
|
1
|
+
import { BlockchainEntityRead } from './BlockchainEntity';
|
|
2
|
+
import { NetworkConnection } from './types';
|
|
3
|
+
import { AuToken } from '@aurigami/contracts/typechain';
|
|
4
4
|
import AuTokenABI from '@aurigami/contracts/artifacts/contracts/AuToken.sol/AuToken.json';
|
|
5
|
-
import { BigNumber as BN, Contract } from
|
|
5
|
+
import { BigNumber as BN, Contract } from 'ethers';
|
|
6
6
|
|
|
7
7
|
type SimpleTransfer = {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
|
|
8
|
+
from: string;
|
|
9
|
+
to: string;
|
|
10
|
+
amount: BN;
|
|
11
|
+
blockNumber: number;
|
|
12
|
+
};
|
|
14
13
|
|
|
15
14
|
export class TransferEventQuery extends BlockchainEntityRead {
|
|
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
|
-
|
|
15
|
+
auToken: AuToken;
|
|
16
|
+
|
|
17
|
+
constructor(tokenAddr: string, networkConnection: NetworkConnection) {
|
|
18
|
+
super(networkConnection);
|
|
19
|
+
this.auToken = new Contract(
|
|
20
|
+
tokenAddr,
|
|
21
|
+
AuTokenABI.abi,
|
|
22
|
+
this._networkConnection.provider
|
|
23
|
+
) as AuToken;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* query ERC20 transfers from/to an address & auto convert them into SimpleTransfer.
|
|
28
|
+
* The result will be sorted by blocknumber.
|
|
29
|
+
*/
|
|
30
|
+
async queryERC20TransfersOf(
|
|
31
|
+
address: string,
|
|
32
|
+
fromBlock: number,
|
|
33
|
+
toBlock: number
|
|
34
|
+
): Promise<SimpleTransfer[]> {
|
|
35
|
+
const filters = [
|
|
36
|
+
this.auToken.filters.Transfer(address, null),
|
|
37
|
+
this.auToken.filters.Transfer(null, address),
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
const events = (
|
|
41
|
+
await Promise.all(
|
|
42
|
+
filters.map((filter) =>
|
|
43
|
+
this.auToken.queryFilter(filter, fromBlock, toBlock)
|
|
44
|
+
)
|
|
45
|
+
)
|
|
46
|
+
).flat();
|
|
47
|
+
|
|
48
|
+
let results: SimpleTransfer[] = events.map((event) => {
|
|
49
|
+
return {
|
|
50
|
+
from: event.args![0],
|
|
51
|
+
to: event.args![1],
|
|
52
|
+
amount: event.args![2],
|
|
53
|
+
blockNumber: event.blockNumber,
|
|
54
|
+
};
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
results.sort((a, b) => a.blockNumber - b.blockNumber);
|
|
58
|
+
|
|
59
|
+
return results;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Get the balance of an address at a given block number.
|
|
64
|
+
*
|
|
65
|
+
* Note that this will return the balance after the block get mined.
|
|
66
|
+
*/
|
|
67
|
+
async queryERC20BalanceAt(address: string, endBlock: number): Promise<BN> {
|
|
68
|
+
return this.auToken.balanceOf(address, { blockTag: endBlock + 1 });
|
|
69
|
+
}
|
|
60
70
|
}
|