-
Notifications
You must be signed in to change notification settings - Fork 20
/
handleSwap.ts
97 lines (91 loc) · 2.9 KB
/
handleSwap.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
import {checksum, nowMs, _log} from '../../../utils/configs/utils';
import {getMempoolData} from '../_decoders/getMempoolData';
import {createPending} from '../../../utils/mongo/savePending';
import {createConfirm} from '../../../utils/mongo/saveConfirmed';
import {getTokens} from '../../../utils/web3/getTokens';
import {V3_SWAP_FNAME} from '../../../utils/web3/utils';
import {IContractSharkTx} from '../../../models/TransactionSchema';
import {models} from '../../../utils/mongo/config';
const {g} = models;
const {hashes} = g;
const {EI, EIS, EO, EOS} = V3_SWAP_FNAME;
const pName = 'qnPending_v3_swap';
export const handleSwap = async (
tx: IContractSharkTx,
dexSpace: string,
directConfirm: boolean,
providers: Array<any>
) => {
try {
const {txMethod, decodedData} = tx.mempoolData;
let fromTokenAddress = '';
let midTokenAddress = null;
let toTokenAddress = '';
let fee = null;
const checkedPath = [];
if (txMethod === EIS || txMethod === EOS) {
fromTokenAddress = checksum(decodedData['tokenIn']);
toTokenAddress = checksum(decodedData['tokenOut']);
fee = decodedData['fee'];
} else if (txMethod === EO || txMethod === EI) {
const path = decodedData['path'];
midTokenAddress = checksum('0x' + path.slice(48, 88));
fromTokenAddress = checksum(
txMethod === EO ? '0x' + path.slice(94, 134) : path.slice(0, 42)
);
toTokenAddress = checksum(
txMethod === EI ? '0x' + path.slice(94, 134) : path.slice(0, 42)
);
}
checkedPath.push(fromTokenAddress);
midTokenAddress && checkedPath.push(midTokenAddress);
checkedPath.push(toTokenAddress);
if (checkedPath.length > 0) {
const tks = await getTokens(checkedPath, pName, dexSpace, providers);
if (tks && tks.length === checkedPath.length) {
const mempoolData = await getMempoolData(
{
...tx,
mempoolData: {txMethod, decodedData, checkedPath, fee},
},
tks,
dexSpace
);
if (mempoolData) {
const toCreate = {
...tx,
fromTokenAddress,
toTokenAddress,
mempoolData: {
...tx.mempoolData,
...mempoolData,
},
};
if (directConfirm) {
new hashes({
hash: tx.hash,
txHash: tx.hash,
timestampTx: nowMs(),
}).save(async (e: any) => {
if (!e) {
createConfirm(toCreate, 'directConfirm');
}
});
} else {
createPending(toCreate, dexSpace);
}
}
} else {
_log.warn(
'handleSwap',
dexSpace,
tx.hash,
'not tks or tks.length !== checkedPath.length'
);
}
}
} catch (e: any) {
_log.error('handleSwap catch', dexSpace, e.message, tx.hash);
}
return;
};