-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.ts
66 lines (62 loc) · 2.83 KB
/
index.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
import { configDotenv } from 'dotenv';
import { mnemonicToWalletKey } from '@ton/crypto';
import { Cell, TonClient, WalletContractV4 } from '@ton/ton';
import { Evaa, FEES, getPrices, MAINNET_LP_POOL_CONFIG, PricesCollector, TESTNET_POOL_CONFIG, TON_MAINNET, TONUSDT_DEDUST_MAINNET } from '@evaafi/sdk';
async function main() {
configDotenv();
const keyPair = await mnemonicToWalletKey(process.env.WALLET_MNEMONIC!.split(' '));
const client = new TonClient({
endpoint: 'https://testnet.toncenter.com/api/v2/jsonRPC',
apiKey: process.env.RPC_API_KEY,
});
const evaa = client.open(
new Evaa({poolConfig: TESTNET_POOL_CONFIG}),
);
const wallet = client.open(
WalletContractV4.create({
workchain: 0,
publicKey: keyPair.publicKey,
}),
);
await evaa.getSync();
const pricesCollector = new PricesCollector(TESTNET_POOL_CONFIG);
const priceData = await pricesCollector.getPrices();
// get user contract that already opened by same client
// alternative: openUserContract method, which return only instance of user contract without opening
const user = evaa.getOpenedUserContract(wallet.address);
await user.getSync(evaa.data!.assetsData, evaa.data!.assetsConfig, priceData!.dict);
if (user.isLiquidable) {
const liquidationPrices = await pricesCollector.getPricesForLiquidate(user.liteData?.principals!);
const liquidationData = user.liquidationParameters!;
// if user code version is outdated, includeUserCode should be true for upgrade this contract
const includeUserCode = evaa.data!.upgradeConfig.userCodeVersion !== user.liteData!.codeVersion;
if (liquidationData.tonLiquidation) {
await evaa.sendLiquidation(wallet.sender(keyPair.secretKey), FEES.LIQUIDATION, {
queryID: 0n,
liquidatorAddress: wallet.address,
includeUserCode: includeUserCode,
priceData: liquidationPrices.dataCell,
...liquidationData,
forwardAmount: FEES.LIQUIDATION_JETTON_FWD,
payload: Cell.EMPTY,
asset: TON_MAINNET,
responseAddress: wallet.address,
payloadForwardAmount: 0n,
});
} else {
await evaa.sendLiquidation(wallet.sender(keyPair.secretKey), FEES.LIQUIDATION_JETTON, {
queryID: 0n,
liquidatorAddress: wallet.address,
includeUserCode: includeUserCode,
priceData: priceData!.dataCell,
...liquidationData,
responseAddress: wallet.address,
forwardAmount: FEES.LIQUIDATION_JETTON_FWD,
payload: Cell.EMPTY,
asset: TON_MAINNET,
payloadForwardAmount: 0n,
});
}
}
}
main();