-
Notifications
You must be signed in to change notification settings - Fork 20
/
handleMultiSwap.ts
97 lines (92 loc) · 2.92 KB
/
handleMultiSwap.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 {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_FNAME_ONLY_SWAP} from '../../../utils/web3/utils';
import {IContractSharkTx} from '../../../models/TransactionSchema';
import {getV3InternalSwap} from './getV3InternalSwap';
import {models} from '../../../utils/mongo/config';
const {g} = models;
const {hashes} = g;
const pName = 'qnPending_v3_multiswap';
export const handleMultiSwap = async (
tx: IContractSharkTx,
dexSpace: string,
directConfirm: boolean,
providers: Array<any>
) => {
try {
const {decodedData} = tx.mempoolData;
let fromTokenAddress = '';
let toTokenAddress = '';
const multiSwapCalls = [];
for (let i = 0; i < decodedData.length; i++) {
const internalCall = await getV3InternalSwap(decodedData[i]);
if (internalCall) {
const {decodedInnerData, checkedPath, innerMethod, fee} = internalCall;
if (_V3_FNAME_ONLY_SWAP.indexOf(innerMethod) >= 0) {
if (fromTokenAddress === '') {
fromTokenAddress = checkedPath[0];
}
toTokenAddress = checkedPath[checkedPath.length - 1];
const tks = await getTokens(checkedPath, pName, dexSpace, providers);
if (tks && tks.length === checkedPath.length) {
const innerCall = await getMempoolData(
{
...tx,
mempoolData: {
txMethod: innerMethod,
decodedData: decodedInnerData,
fee,
},
},
tks,
dexSpace
);
if (innerCall) {
multiSwapCalls.push(innerCall);
}
} else {
_log.warn(
'handleMultiSwap',
dexSpace,
tx.hash,
'not tks or tks.length !== checkedPath.length'
);
}
}
}
}
if (multiSwapCalls.length > 0) {
for (const swapCall of multiSwapCalls) {
const toCreate = {
...tx,
fromTokenAddress: swapCall.input.address,
toTokenAddress: swapCall.output.address,
mempoolData: {
...tx.mempoolData,
multiSwapCalls,
...swapCall,
},
};
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);
}
}
}
} catch (e: any) {
_log.error('handleMultiswap catch', dexSpace, e.message, tx.hash);
}
return;
};