-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
238 lines (215 loc) · 6.54 KB
/
main.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import {
CHAIN_ID_ARBITRUM,
CHAIN_ID_AVAX,
CHAIN_ID_BASE,
CHAIN_ID_BSC,
CHAIN_ID_ETH,
CHAIN_ID_OPTIMISM,
CHAIN_ID_POLYGON,
ChainId,
} from '@certusone/wormhole-sdk';
import { ethers } from 'ethers';
import { forwarderAbi } from './abis/forwarder';
import { MctpEvmRegistry } from './mctp';
import { SwiftEvmRegistry } from './swift';
import { MCTP_EVM, MCTP_V2_EVM, SWIFT_EVM, WH_EVM } from './utils/const';
import { makeEvmProviders } from './utils/evm-providers';
import { Swap } from './utils/swap.dto';
import { getTokenDataGeneral } from './utils/token.util';
import { WhEvmRegistery } from './wh';
const LogForwardedEthSig = 'ForwardedEth(address,bytes)';
const LogForwardedERC20Sig = 'ForwardedERC20(address,uint256,address,bytes)';
const LogSwapAndForwardedEthSig =
'SwapAndForwardedEth(uint256,address,address,uint256,address,bytes)';
const LogSwapAndForwardedERC20Sig =
'SwapAndForwardedERC20(address,uint256,address,address,uint256,address,bytes)';
const evmProviders = makeEvmProviders([
CHAIN_ID_ARBITRUM,
CHAIN_ID_BASE,
CHAIN_ID_AVAX,
CHAIN_ID_OPTIMISM,
CHAIN_ID_ETH,
CHAIN_ID_POLYGON,
CHAIN_ID_BSC,
]);
export class Parser {
private readonly mayanForwarderInterface = new ethers.utils.Interface(
forwarderAbi,
);
private readonly mctpEvmRegistry = new MctpEvmRegistry();
private readonly swiftEvmRegistry = new SwiftEvmRegistry(evmProviders);
private readonly whEvmRegistery = new WhEvmRegistery();
private acceptedForwarderAddresses: Set<string> = new Set([
'0x0654874eb7F59C6f5b39931FC45dC45337c967c3',
]);
private allAvailableWhProtocols: Set<string> = new Set([
...WH_EVM,
...WH_EVM.map((a) => a.toLowerCase()),
]);
private allAvailableMctpProtocols: Set<string> = new Set([
MCTP_EVM,
MCTP_EVM.toLowerCase(),
MCTP_V2_EVM,
MCTP_V2_EVM.toLowerCase(),
]);
private allAvailableSwiftProtocols: Set<string> = new Set([
SWIFT_EVM,
SWIFT_EVM.toLowerCase(),
]);
async processEventLog(
chainId: ChainId,
txReceipt: ethers.providers.TransactionReceipt,
) {
const forwarderV2log = txReceipt.logs.find((log) =>
this.acceptedForwarderAddresses.has(log.address),
);
if (!forwarderV2log) {
return null; // not a forwarder transaction
}
const receiptStatus = txReceipt.status as any;
if (receiptStatus !== 1 && receiptStatus !== '0x1') {
console.error(
`failed transaction: ${txReceipt.transactionHash} for forwarder parse`,
);
return null; // failed transaction
}
const innerParams = this.getInnerParamsFromEventLog(
chainId,
forwarderV2log,
);
let extractedType: 'MCTP' | 'SWIFT' | 'WH';
if (this.allAvailableMctpProtocols.has(innerParams.mayanProtocol)) {
extractedType = 'MCTP';
} else if (
this.allAvailableSwiftProtocols.has(innerParams.mayanProtocol)
) {
extractedType = 'SWIFT';
} else if (
this.allAvailableWhProtocols.has(innerParams.mayanProtocol)
) {
extractedType = 'WH';
} else {
return null; // did not forward mctp/swift/wh related stuff (should not happen)
}
let baseSwapData: Swap | null | undefined;
switch (extractedType) {
case 'MCTP':
baseSwapData = await this.mctpEvmRegistry.processEventLog(
chainId,
txReceipt,
{ data: innerParams.mayanCallData },
innerParams.swappedAmount,
);
break;
case 'SWIFT':
baseSwapData = await this.swiftEvmRegistry.processEventLog(
chainId,
txReceipt,
{ data: innerParams.mayanCallData },
innerParams.swappedAmount,
);
break;
case 'WH':
baseSwapData = await this.whEvmRegistery.processTxFromChain(
txReceipt,
+chainId,
);
break;
}
if (!baseSwapData) {
return null;
}
if (innerParams.realTokenInAddr) {
let realTokenIn = await getTokenDataGeneral(
chainId,
innerParams.realTokenInAddr,
);
baseSwapData!.forwardedTokenSymbol = realTokenIn.symbol;
baseSwapData!.forwardedTokenAddress = innerParams.realTokenInAddr;
baseSwapData!.forwardedFromAmount = ethers.utils.formatUnits(
innerParams.realAmountIn,
realTokenIn.decimals,
) as any;
}
return baseSwapData
}
private getInnerParamsFromEventLog(
chainId: ChainId,
log: ethers.providers.Log,
): innerParams {
let realTokenInAddr: string = '';
let realAmountIn;
let mayanProtocol;
let mayanCallData;
let swappedAmount;
if (log.topics.includes(ethers.utils.id(LogSwapAndForwardedERC20Sig))) {
const forwardEventData =
this.mayanForwarderInterface.decodeEventLog(
LogSwapAndForwardedERC20Sig,
log.data,
log.topics,
);
mayanCallData = forwardEventData.mayanData;
mayanProtocol = forwardEventData.mayanProtocol;
realTokenInAddr = forwardEventData.tokenIn;
realAmountIn = forwardEventData.amountIn;
swappedAmount = forwardEventData.middleAmount;
} else if (
log.topics.includes(ethers.utils.id(LogSwapAndForwardedEthSig))
) {
const forwardEventData =
this.mayanForwarderInterface.decodeEventLog(
LogSwapAndForwardedEthSig,
log.data,
log.topics,
);
mayanCallData = forwardEventData.mayanData;
mayanProtocol = forwardEventData.mayanProtocol;
realTokenInAddr = '0x0000000000000000000000000000000000000000';
realAmountIn = forwardEventData.amountIn;
swappedAmount = forwardEventData.middleAmount;
} else if (log.topics.includes(ethers.utils.id(LogForwardedERC20Sig))) {
const forwardEventData =
this.mayanForwarderInterface.decodeEventLog(
LogForwardedERC20Sig,
log.data,
log.topics,
);
mayanCallData = forwardEventData.protocolData;
mayanProtocol = forwardEventData.mayanProtocol;
} else if (log.topics.includes(ethers.utils.id(LogForwardedEthSig))) {
const forwardEventData =
this.mayanForwarderInterface.decodeEventLog(
LogForwardedEthSig,
log.data,
log.topics,
);
mayanCallData = forwardEventData.protocolData;
mayanProtocol = forwardEventData.mayanProtocol;
} else {
throw new Error(`can not decode this log for forwarder`);
}
return {
mayanProtocol: mayanProtocol,
mayanCallData: mayanCallData,
realAmountIn: realAmountIn,
realTokenInAddr: realTokenInAddr,
posAddress: ethers.utils.getAddress(log.address),
swappedAmount: swappedAmount,
};
}
}
type innerParams = {
realTokenInAddr: string;
realAmountIn: ethers.BigNumber;
mayanProtocol: string;
mayanCallData: Buffer;
posAddress: string;
swappedAmount: bigint;
};
(async () => {
const p = new Parser();
const chainId = CHAIN_ID_ETH; // ethereum
const rct = await evmProviders[chainId].getTransactionReceipt('0x96777a8044f05ea7262c6cce45505be5e7699593df8cad0362482455adbf2b61');
console.log(await p.processEventLog(chainId, rct));
})();