-
Notifications
You must be signed in to change notification settings - Fork 1
/
moonbeam.ts
146 lines (110 loc) · 4.34 KB
/
moonbeam.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
import { EvmApi, MoonbeamConfig } from '@zenlink-dex/sdk-api';
import { Percent, Token, TokenAmount, TradeType, StablePair, StableSwap } from '@zenlink-dex/sdk-core';
import { firstValueFrom } from 'rxjs';
import { SmartRouterV2 } from '@zenlink-dex/sdk-router';
import { ethers } from 'ethers';
import axios from 'axios';
const PRIVITE_KEY = '0x'; // YOUR PRIVITE KEY
async function main () {
const response = await axios.get('https://raw.githubusercontent.com/zenlinkpro/token-list/main/tokens/moonbeam.json');
const tokensMeta = response.data.tokens;
const provider = new ethers.providers.JsonRpcProvider(MoonbeamConfig.rpcUrls[0]);
const singer = new ethers.Wallet(
PRIVITE_KEY,
provider
);
const account = await singer.getAddress();
console.log(`account address ${account}`);
// generate Tokens
const tokens = tokensMeta.map((item) => {
return new Token(item);
});
const tokensMap: Record<string, Token> = {};
tokens.reduce((total, cur) => {
total[cur.assetId] = cur;
return total;
}, tokensMap);
// generate the dex api
const dexApi = new EvmApi(
singer,
MoonbeamConfig
);
// query the tokens balance of acoount
const tokenBalances = await firstValueFrom(dexApi.balanceOfTokens(tokens, account));
console.log('tokenBalances ', tokenBalances);
// query the standard pair of tokens
const standardPairs = await firstValueFrom(dexApi.standardPairOfTokens(tokens));
console.log('standardPairs', standardPairs);
// query the standardPools of standard pairs;
const standardPools = await firstValueFrom(dexApi.standardPoolOfPairs(standardPairs));
console.log('standardPools', standardPools);
// query the stable pair;
const stablePairs: StablePair[] = await firstValueFrom(dexApi.stablePairOf(MoonbeamConfig.stableAddress));
console.log('stablePairs', stablePairs);
// query the stable pool of stable pair;
const stablePools: StableSwap[] = await firstValueFrom(dexApi.stablePoolOfPairs(stablePairs));
console.log('stablePairs', stablePools);
const anyUSDCToken = tokensMap['300-2004-255-0x818ec0a7fe18ff94269904fced6ae3dae6d6dc0b'];
const wglmrToken = tokensMap['300-2004-255-0xacc15dc74880c9944775448304b263d191c6077f'];
// const glmrToken = tokensMap['300-2004-0-0'];
// swap from token -> toToken
const fromToken = wglmrToken;
const fromTokenAmount = new TokenAmount(fromToken, (1_0_000_000_000_000_000).toString());
const toToken = anyUSDCToken;
// use smart router to get the best trade;
const result = SmartRouterV2.swapExactTokensForTokens(
fromTokenAmount,
toToken,
standardPools,
stablePools
);
const trade = result.trade;
if (!trade) {
console.log('There is no match for this trade');
return;
}
console.log(
'The match trade is swap', `${
trade.inputAmount.toPrecision(8)} ${trade.inputAmount.token.symbol
} to ${trade.outputAmount.toPrecision(8)} ${trade.outputAmount.token.symbol}`
);
console.log('The executionPrice is',
`1 ${trade.inputAmount.token.symbol} = ${trade.executionPrice.toPrecision(8)} ${trade.outputAmount.token.symbol}`
);
console.log('The route path is ', trade.route.routePath.map((item) => item.stable).join(' -> '));
console.log('The route path is ', trade.route.tokenPath.map((item) => item.symbol).join(' -> '));
const currentBlock = await provider.getBlockNumber();
const currentBlockInfo = await provider.getBlock(currentBlock);
const timestamp = currentBlockInfo.timestamp;
const deadline = timestamp + 300; // deadleine
const gasPrice = await singer.getGasPrice();
console.log('deadline', deadline);
// set slippage of 5%
const slippageTolerance = new Percent(5, 100);
// get the extrinsics of this swap
const extrinsics = dexApi.swapExactTokensForTokens(
trade.route.routePath,
trade.inputAmount,
trade.minimumAmountOut(slippageTolerance),
account,
deadline
);
if (!extrinsics) return;
// query the estimateGas;
const estimateGas = await extrinsics.estimateGas();
try {
// submit
const tx = await extrinsics?.signAndSend({
gasPrice,
gasLimit: estimateGas.toString()
});
console.log('tx hash', tx.hash.toString());
const result = await tx.wait();
console.log('result gasUsed', result.gasUsed.toString());
} catch (error) {
console.log('tx', error);
}
}
main().then(() => {
console.log('end');
});