@instadapp/interop-x 0.0.0-dev.f78418c → 0.0.0-dev.fd7fd6f
Sign up to get free protection for your applications and to get access to all the features.
- package/.env.example +2 -1
- package/bin/interop-x +1 -1
- package/dist/package.json +72 -0
- package/dist/{abi → src/abi}/erc20.json +0 -0
- package/dist/{abi → src/abi}/gnosisSafe.json +0 -0
- package/dist/{abi → src/abi}/index.js +0 -0
- package/dist/{abi → src/abi}/interopBridgeToken.json +0 -0
- package/dist/{abi → src/abi}/interopXGateway.json +0 -0
- package/dist/src/api/index.js +33 -0
- package/dist/{config → src/config}/index.js +5 -0
- package/dist/{constants → src/constants}/addresses.js +0 -8
- package/dist/{constants → src/constants}/index.js +1 -0
- package/dist/src/constants/itokens.js +13 -0
- package/dist/{constants → src/constants}/tokens.js +0 -0
- package/dist/{db → src/db}/index.js +0 -0
- package/dist/{db → src/db}/models/index.js +0 -0
- package/dist/{db → src/db}/models/transaction.js +3 -1
- package/dist/{db → src/db}/sequelize.js +2 -1
- package/dist/src/index.js +103 -0
- package/dist/{logger → src/logger}/index.js +0 -0
- package/dist/{net → src/net}/index.js +0 -0
- package/dist/{net → src/net}/peer/index.js +6 -2
- package/dist/{net → src/net}/pool/index.js +27 -9
- package/dist/{net → src/net}/protocol/dial/BaseDialProtocol.js +0 -0
- package/dist/{net → src/net}/protocol/dial/SignatureDialProtocol.js +22 -12
- package/dist/{net → src/net}/protocol/index.js +30 -1
- package/dist/src/tasks/AutoUpdateTask.js +44 -0
- package/dist/{tasks → src/tasks}/BaseTask.js +1 -1
- package/dist/src/tasks/InteropBridge/ProcessWithdrawEvents.js +147 -0
- package/dist/src/tasks/InteropBridge/SyncWithdrawEvents.js +70 -0
- package/dist/src/tasks/InteropXGateway/ProcessDepositEvents.js +150 -0
- package/dist/{tasks → src/tasks}/InteropXGateway/SyncDepositEvents.js +16 -20
- package/dist/{tasks → src/tasks}/index.js +15 -0
- package/dist/{typechain → src/typechain}/Erc20.js +0 -0
- package/dist/{typechain → src/typechain}/GnosisSafe.js +0 -0
- package/dist/{typechain → src/typechain}/InteropBridgeToken.js +0 -0
- package/dist/{typechain → src/typechain}/InteropXGateway.js +0 -0
- package/dist/{typechain → src/typechain}/common.js +0 -0
- package/dist/{typechain → src/typechain}/factories/Erc20__factory.js +0 -0
- package/dist/{typechain → src/typechain}/factories/GnosisSafe__factory.js +0 -0
- package/dist/{typechain → src/typechain}/factories/InteropBridgeToken__factory.js +0 -0
- package/dist/{typechain → src/typechain}/factories/InteropXGateway__factory.js +0 -0
- package/dist/{typechain → src/typechain}/factories/index.js +0 -0
- package/dist/{typechain → src/typechain}/index.js +0 -0
- package/dist/{types.js → src/types.js} +0 -0
- package/dist/src/utils/index.js +228 -0
- package/package.json +10 -17
- package/patches/@ethersproject+properties+5.6.0.patch +13 -0
- package/src/api/index.ts +33 -0
- package/src/config/index.ts +8 -0
- package/src/constants/addresses.ts +0 -8
- package/src/constants/index.ts +1 -0
- package/src/constants/itokens.ts +10 -0
- package/src/db/models/transaction.ts +8 -4
- package/src/db/sequelize.ts +2 -1
- package/src/index.ts +79 -6
- package/src/net/peer/index.ts +7 -6
- package/src/net/pool/index.ts +37 -11
- package/src/net/protocol/dial/SignatureDialProtocol.ts +25 -13
- package/src/net/protocol/index.ts +45 -1
- package/src/tasks/AutoUpdateTask.ts +53 -0
- package/src/tasks/BaseTask.ts +1 -1
- package/src/tasks/InteropBridge/ProcessWithdrawEvents.ts +233 -0
- package/src/tasks/InteropBridge/SyncWithdrawEvents.ts +121 -0
- package/src/tasks/InteropXGateway/ProcessDepositEvents.ts +245 -0
- package/src/tasks/InteropXGateway/SyncDepositEvents.ts +23 -11
- package/src/tasks/index.ts +22 -2
- package/src/utils/index.ts +182 -4
- package/dist/index.js +0 -43
- package/dist/utils/index.js +0 -101
@@ -0,0 +1,245 @@
|
|
1
|
+
import { BaseTask } from "../BaseTask";
|
2
|
+
import Logger from '@/logger';
|
3
|
+
import { BigNumber, ethers } from "ethers";
|
4
|
+
import abi from "@/abi";
|
5
|
+
import { Transaction } from "@/db";
|
6
|
+
import { buildDataForTransaction, buildSignatureBytes, getContract, getRpcProviderUrl, Signature } from "@/utils";
|
7
|
+
import { addresses } from "@/constants";
|
8
|
+
import { ChainId } from "@/types";
|
9
|
+
import config from "@/config";
|
10
|
+
import { GnosisSafe, InteropXGateway } from "@/typechain";
|
11
|
+
import { Op } from "sequelize";
|
12
|
+
import wait from "waait";
|
13
|
+
import { peerPool, protocol } from "@/net";
|
14
|
+
import { LogDescription } from "ethers/lib/utils";
|
15
|
+
|
16
|
+
const generateGnosisTransaction = async (transactionData: any, safeContract: GnosisSafe) => {
|
17
|
+
console.log(transactionData);
|
18
|
+
|
19
|
+
let isExecuted = await safeContract.dataHashes(
|
20
|
+
await safeContract.getTransactionHash(
|
21
|
+
transactionData.to,
|
22
|
+
transactionData.value,
|
23
|
+
transactionData.data,
|
24
|
+
transactionData.operation,
|
25
|
+
transactionData.safeTxGas,
|
26
|
+
transactionData.baseGas,
|
27
|
+
transactionData.gasPrice,
|
28
|
+
transactionData.gasToken,
|
29
|
+
transactionData.refundReceiver,
|
30
|
+
transactionData.nonce
|
31
|
+
)
|
32
|
+
)
|
33
|
+
|
34
|
+
while (isExecuted == 1) {
|
35
|
+
transactionData.safeTxGas = BigNumber.from(String(transactionData.safeTxGas)).add(1).toString()
|
36
|
+
|
37
|
+
isExecuted = await safeContract.dataHashes(
|
38
|
+
await safeContract.getTransactionHash(
|
39
|
+
transactionData.to,
|
40
|
+
transactionData.value,
|
41
|
+
transactionData.data,
|
42
|
+
transactionData.operation,
|
43
|
+
transactionData.safeTxGas,
|
44
|
+
transactionData.baseGas,
|
45
|
+
transactionData.gasPrice,
|
46
|
+
transactionData.gasToken,
|
47
|
+
transactionData.refundReceiver,
|
48
|
+
transactionData.nonce
|
49
|
+
)
|
50
|
+
)
|
51
|
+
}
|
52
|
+
|
53
|
+
return transactionData
|
54
|
+
}
|
55
|
+
|
56
|
+
class ProcessDepositEvents extends BaseTask {
|
57
|
+
contractAddress: string;
|
58
|
+
provider: ethers.providers.JsonRpcProvider;
|
59
|
+
contract: InteropXGateway;
|
60
|
+
chainId: ChainId;
|
61
|
+
leadNodeOnly = true
|
62
|
+
|
63
|
+
constructor({ chainId }: { chainId: ChainId }) {
|
64
|
+
super({
|
65
|
+
logger: new Logger("InteropXGateway::ProcessDepositEvents"),
|
66
|
+
})
|
67
|
+
this.chainId = chainId;
|
68
|
+
}
|
69
|
+
|
70
|
+
async pollHandler() {
|
71
|
+
const blockNumber = await this.provider.getBlockNumber()
|
72
|
+
|
73
|
+
const transaction = await Transaction.findOne({
|
74
|
+
where: {
|
75
|
+
status: 'pending',
|
76
|
+
sourceStatus: 'success',
|
77
|
+
targetStatus: 'uninitialised',
|
78
|
+
action: 'deposit',
|
79
|
+
sourceCreatedAt: {
|
80
|
+
[Op.gte]: new Date(Date.now() - 12 * 60 * 60 * 1000),
|
81
|
+
},
|
82
|
+
targetDelayUntil: {
|
83
|
+
[Op.or]: {
|
84
|
+
[Op.is]: null,
|
85
|
+
[Op.lt]: new Date(),
|
86
|
+
}
|
87
|
+
},
|
88
|
+
sourceBlockNumber: {
|
89
|
+
[Op.lt]: blockNumber - 12,
|
90
|
+
},
|
91
|
+
sourceChainId: this.chainId,
|
92
|
+
}
|
93
|
+
})
|
94
|
+
|
95
|
+
if (!transaction) {
|
96
|
+
return;
|
97
|
+
}
|
98
|
+
|
99
|
+
console.log(`Processing transaction ${transaction.transactionHash}`);
|
100
|
+
|
101
|
+
transaction.targetStatus = 'pending';
|
102
|
+
await transaction.save();
|
103
|
+
|
104
|
+
// refresh event data?
|
105
|
+
|
106
|
+
const targetChainProvider = new ethers.providers.JsonRpcProvider(
|
107
|
+
getRpcProviderUrl(transaction.targetChainId as ChainId)
|
108
|
+
);
|
109
|
+
|
110
|
+
const targetWallet = new ethers.Wallet(config.privateKey!, targetChainProvider);
|
111
|
+
|
112
|
+
const safeAddress = addresses[transaction.targetChainId].gnosisSafe;
|
113
|
+
|
114
|
+
|
115
|
+
const safeContract = getContract<GnosisSafe>(
|
116
|
+
safeAddress,
|
117
|
+
abi.gnosisSafe,
|
118
|
+
targetWallet
|
119
|
+
)
|
120
|
+
|
121
|
+
const ownersThreshold = await safeContract.getThreshold();
|
122
|
+
await wait(10000);
|
123
|
+
|
124
|
+
let gnosisTx = await generateGnosisTransaction({
|
125
|
+
baseGas: "0",
|
126
|
+
data: await buildDataForTransaction(transaction),
|
127
|
+
gasPrice: "0",
|
128
|
+
gasToken: "0x0000000000000000000000000000000000000000",
|
129
|
+
nonce: '0',
|
130
|
+
operation: "1",
|
131
|
+
refundReceiver: "0x0000000000000000000000000000000000000000",
|
132
|
+
safeAddress: safeAddress,
|
133
|
+
safeTxGas: "79668",
|
134
|
+
to: addresses[transaction.targetChainId].multisend,
|
135
|
+
value: "0",
|
136
|
+
}, safeContract);
|
137
|
+
|
138
|
+
const owners = await safeContract.getOwners().then(owners => owners.map(owner => owner.toLowerCase()));
|
139
|
+
|
140
|
+
const ownerPeerIds = peerPool.activePeers.filter(peer => owners.includes(peer.publicAddress.toLowerCase())).map(peer => peer.id)
|
141
|
+
|
142
|
+
console.log(`Collecting signatures for execution ${transaction.transactionHash}`)
|
143
|
+
|
144
|
+
console.log(ownerPeerIds);
|
145
|
+
|
146
|
+
const signatures = await protocol.requestSignatures({
|
147
|
+
type: 'source',
|
148
|
+
transactionHash: transaction.transactionHash,
|
149
|
+
safeTxGas: gnosisTx.safeTxGas,
|
150
|
+
safeNonce: gnosisTx.nonce
|
151
|
+
}, ownerPeerIds)
|
152
|
+
|
153
|
+
|
154
|
+
const validSignatures = signatures.filter(s => !!s.data && s.data !== '0x') as Signature[];
|
155
|
+
|
156
|
+
console.log({ signatures, validSignatures, ownersThreshold: ownersThreshold.toString() });
|
157
|
+
|
158
|
+
if (validSignatures.length === 0 || ownersThreshold.gt(validSignatures.length)) {
|
159
|
+
await transaction.save();
|
160
|
+
transaction.targetDelayUntil = new Date(Date.now() + 30 * 1000);
|
161
|
+
transaction.targetStatus = 'uninitialised'
|
162
|
+
|
163
|
+
await transaction.save();
|
164
|
+
const errorMessage = signatures.find(s => !!s.error)?.error;
|
165
|
+
throw new Error(`Not enough signatures` + (errorMessage ? `: ${errorMessage}` : ''));
|
166
|
+
}
|
167
|
+
|
168
|
+
|
169
|
+
console.log(`Executing transaction for execution ${transaction.transactionHash}`)
|
170
|
+
|
171
|
+
const { data: txData } = await safeContract.populateTransaction.execTransaction(
|
172
|
+
gnosisTx.to,
|
173
|
+
gnosisTx.value,
|
174
|
+
gnosisTx.data,
|
175
|
+
gnosisTx.operation,
|
176
|
+
gnosisTx.safeTxGas,
|
177
|
+
gnosisTx.baseGas,
|
178
|
+
gnosisTx.gasPrice,
|
179
|
+
gnosisTx.gasToken,
|
180
|
+
gnosisTx.refundReceiver,
|
181
|
+
buildSignatureBytes(validSignatures)
|
182
|
+
);
|
183
|
+
|
184
|
+
console.log({
|
185
|
+
from: targetWallet.address,
|
186
|
+
gasPrice: BigNumber.from(120 * 10 ** 9).toString(),
|
187
|
+
to: safeAddress,
|
188
|
+
data: txData,
|
189
|
+
})
|
190
|
+
|
191
|
+
|
192
|
+
const txSent = await targetWallet.sendTransaction({
|
193
|
+
from: targetWallet.address,
|
194
|
+
gasPrice: BigNumber.from(120 * 10 ** 9),
|
195
|
+
to: safeAddress,
|
196
|
+
data: txData,
|
197
|
+
})
|
198
|
+
|
199
|
+
const receipt = await txSent.wait();
|
200
|
+
|
201
|
+
const parsedLogs: LogDescription[] = [];
|
202
|
+
|
203
|
+
receipt.logs.forEach((log) => {
|
204
|
+
try {
|
205
|
+
parsedLogs.push(safeContract.interface.parseLog(log));
|
206
|
+
} catch (e) { }
|
207
|
+
});
|
208
|
+
|
209
|
+
if (parsedLogs.find(e => e.name === 'ExecutionSuccess')) {
|
210
|
+
console.log('ExecutionSuccess')
|
211
|
+
transaction.targetStatus = 'success'
|
212
|
+
transaction.targetTransactionHash = txSent.hash
|
213
|
+
transaction.status = 'success'
|
214
|
+
await transaction.save();
|
215
|
+
} else {
|
216
|
+
console.log('ExecutionFailure')
|
217
|
+
transaction.targetStatus = 'failed'
|
218
|
+
transaction.targetTransactionHash = txSent.hash
|
219
|
+
transaction.status = 'failed'
|
220
|
+
await transaction.save();
|
221
|
+
}
|
222
|
+
|
223
|
+
protocol.sendTransaction(transaction)
|
224
|
+
}
|
225
|
+
|
226
|
+
async start(): Promise<void> {
|
227
|
+
this.logger.info(`Starting execution watcher on interop chain`);
|
228
|
+
|
229
|
+
this.contractAddress = addresses[this.chainId].interopXGateway;
|
230
|
+
|
231
|
+
this.provider = new ethers.providers.JsonRpcProvider(
|
232
|
+
getRpcProviderUrl(this.chainId)
|
233
|
+
);
|
234
|
+
|
235
|
+
this.contract = getContract<InteropXGateway>(
|
236
|
+
this.contractAddress,
|
237
|
+
abi.interopXGateway,
|
238
|
+
new ethers.Wallet(config.privateKey!, this.provider)
|
239
|
+
);
|
240
|
+
|
241
|
+
await super.start()
|
242
|
+
}
|
243
|
+
}
|
244
|
+
|
245
|
+
export default ProcessDepositEvents;
|
@@ -3,7 +3,7 @@ import Logger from '@/logger';
|
|
3
3
|
import { ethers } from "ethers";
|
4
4
|
import abi from "@/abi";
|
5
5
|
import { Transaction } from "@/db";
|
6
|
-
import { generateInteropTransactionHash, getRpcProviderUrl } from "@/utils";
|
6
|
+
import { generateInteropTransactionHash, getContract, getRpcProviderUrl } from "@/utils";
|
7
7
|
import { addresses } from "@/constants";
|
8
8
|
import { ChainId } from "@/types";
|
9
9
|
import config from "@/config";
|
@@ -43,8 +43,8 @@ class SyncDepositEvents extends BaseTask {
|
|
43
43
|
const { sourceChainId, targetChainId, user, vnonce, amount, token } = event.args;
|
44
44
|
|
45
45
|
const uniqueIdentifier = {
|
46
|
-
|
47
|
-
|
46
|
+
action: 'deposit',
|
47
|
+
submitTransactionHash: event.transactionHash,
|
48
48
|
sourceChainId: sourceChainId.toNumber(),
|
49
49
|
targetChainId: targetChainId.toNumber(),
|
50
50
|
}
|
@@ -56,17 +56,20 @@ class SyncDepositEvents extends BaseTask {
|
|
56
56
|
const tx = await event.getTransaction()
|
57
57
|
|
58
58
|
await Transaction.create({
|
59
|
+
...uniqueIdentifier,
|
59
60
|
transactionHash: generateInteropTransactionHash(uniqueIdentifier),
|
60
|
-
type: 'deposit',
|
61
61
|
from: tx.from,
|
62
62
|
to: user,
|
63
63
|
|
64
|
-
|
64
|
+
|
65
|
+
submitTransactionHash: event.transactionHash,
|
66
|
+
submitBlockNumber: event.blockNumber,
|
67
|
+
|
68
|
+
// submit & source are the same
|
65
69
|
sourceTransactionHash: event.transactionHash,
|
66
70
|
sourceBlockNumber: event.blockNumber,
|
67
|
-
sourceStatus: "
|
71
|
+
sourceStatus: "success",
|
68
72
|
|
69
|
-
targetChainId: targetChainId.toNumber(),
|
70
73
|
targetStatus: "uninitialised",
|
71
74
|
|
72
75
|
submitEvent: {
|
@@ -74,14 +77,23 @@ class SyncDepositEvents extends BaseTask {
|
|
74
77
|
sourceChainId: sourceChainId.toString(),
|
75
78
|
targetChainId: targetChainId.toString(),
|
76
79
|
token: token,
|
77
|
-
|
80
|
+
amount: amount.toString(),
|
81
|
+
vnonce: vnonce.toString(),
|
82
|
+
},
|
83
|
+
|
84
|
+
sourceEvent: {
|
85
|
+
user,
|
86
|
+
sourceChainId: sourceChainId.toString(),
|
87
|
+
targetChainId: targetChainId.toString(),
|
88
|
+
token: token,
|
89
|
+
amount: amount.toString(),
|
78
90
|
vnonce: vnonce.toString(),
|
79
91
|
},
|
80
92
|
status: "pending",
|
81
93
|
})
|
82
94
|
|
83
95
|
this.logger.info(
|
84
|
-
`
|
96
|
+
`Deposit queued: ${event.transactionHash} ${event.blockNumber}`
|
85
97
|
);
|
86
98
|
} catch (error) {
|
87
99
|
this.logger.error(error);
|
@@ -101,11 +113,11 @@ class SyncDepositEvents extends BaseTask {
|
|
101
113
|
getRpcProviderUrl(this.chainId)
|
102
114
|
);
|
103
115
|
|
104
|
-
this.contract =
|
116
|
+
this.contract = getContract<InteropXGateway>(
|
105
117
|
this.contractAddress,
|
106
118
|
abi.interopXGateway,
|
107
119
|
new ethers.Wallet(config.privateKey!, this.provider)
|
108
|
-
)
|
120
|
+
);
|
109
121
|
|
110
122
|
await super.start()
|
111
123
|
}
|
package/src/tasks/index.ts
CHANGED
@@ -1,11 +1,31 @@
|
|
1
1
|
import { BaseTask } from "./BaseTask";
|
2
|
-
import
|
2
|
+
import InteropXGatewayProcessDepositEvents from "./InteropXGateway/ProcessDepositEvents";
|
3
|
+
import InteropXGatewaySyncDepositEvents from "./InteropXGateway/SyncDepositEvents";
|
4
|
+
|
5
|
+
import InteropBridgeSyncWithdrawEvents from "./InteropBridge/SyncWithdrawEvents";
|
6
|
+
import InteropBridgeProcessWithdrawEvents from "./InteropBridge/ProcessWithdrawEvents";
|
7
|
+
import AutoUpdateTask from "./AutoUpdateTask";
|
3
8
|
|
4
9
|
export class Tasks {
|
5
10
|
|
6
11
|
tasks: BaseTask[] = [
|
7
|
-
new
|
12
|
+
new AutoUpdateTask(),
|
13
|
+
|
14
|
+
new InteropXGatewaySyncDepositEvents({
|
15
|
+
chainId: 43114
|
16
|
+
}),
|
17
|
+
|
18
|
+
new InteropXGatewayProcessDepositEvents({
|
8
19
|
chainId: 43114
|
20
|
+
}),
|
21
|
+
|
22
|
+
new InteropBridgeSyncWithdrawEvents({
|
23
|
+
chainId: 137,
|
24
|
+
itokenAddress: '0xEab02fe1F016eE3e4106c1C6aad35FeEe657268E',
|
25
|
+
}),
|
26
|
+
|
27
|
+
new InteropBridgeProcessWithdrawEvents({
|
28
|
+
chainId: 137,
|
9
29
|
})
|
10
30
|
];
|
11
31
|
|
package/src/utils/index.ts
CHANGED
@@ -3,9 +3,14 @@
|
|
3
3
|
*/
|
4
4
|
import axios from 'axios'
|
5
5
|
import axiosRetry from "axios-retry";
|
6
|
-
import { addresses } from '@/constants';
|
6
|
+
import { addresses, itokens, tokens } from '@/constants';
|
7
7
|
import { ChainId } from '@/types'
|
8
8
|
import { ethers } from 'ethers';
|
9
|
+
import { encodeMulti, MetaTransaction, OperationType } from 'ethers-multisend';
|
10
|
+
import { Transaction } from '@/db';
|
11
|
+
import config from '@/config';
|
12
|
+
import abi from '@/abi';
|
13
|
+
import { InteropBridgeToken, InteropXGateway } from '@/typechain';
|
9
14
|
|
10
15
|
export const http = axios.create();
|
11
16
|
|
@@ -119,11 +124,184 @@ export const asyncCallWithTimeout = async <T>(asyncPromise: Promise<T>, timeout:
|
|
119
124
|
}
|
120
125
|
|
121
126
|
|
122
|
-
export const generateInteropTransactionHash = (data: {
|
127
|
+
export const generateInteropTransactionHash = (data: { action: string, submitTransactionHash: string, sourceChainId: string | number, targetChainId: string | number }) => {
|
123
128
|
return ethers.utils.solidityKeccak256(['string', 'string', 'string', 'string'], [
|
124
|
-
String(data.
|
125
|
-
String(data.
|
129
|
+
String(data.action),
|
130
|
+
String(data.submitTransactionHash),
|
126
131
|
String(data.sourceChainId),
|
127
132
|
String(data.targetChainId),
|
128
133
|
]);
|
134
|
+
}
|
135
|
+
|
136
|
+
export const buildDataForTransaction = async (transaction: Transaction, type?: 'source' | 'target') => {
|
137
|
+
type = type || transaction.sourceStatus === 'pending' ? 'source' : 'target';
|
138
|
+
|
139
|
+
switch (transaction.action) {
|
140
|
+
case "deposit":
|
141
|
+
return await buildDepositDataForTransaction(transaction, type);
|
142
|
+
case "withdraw":
|
143
|
+
return await buildWithdrawDataForTransaction(transaction, type);
|
144
|
+
default:
|
145
|
+
throw new Error(`Unknown action: ${transaction.action}`);
|
146
|
+
}
|
147
|
+
}
|
148
|
+
|
149
|
+
export const buildDepositDataForTransaction = async (transaction: Transaction, type: 'source' | 'target') => {
|
150
|
+
const transactions: MetaTransaction[] = [];
|
151
|
+
|
152
|
+
if (transaction.action !== 'deposit') {
|
153
|
+
throw new Error(`Invalid action: ${transaction.action}`)
|
154
|
+
}
|
155
|
+
|
156
|
+
if (transaction.action === 'deposit' && transaction.sourceStatus === 'pending') {
|
157
|
+
throw Error('Cannot build data for pending deposit transaction');
|
158
|
+
}
|
159
|
+
|
160
|
+
if (!transaction.submitEvent) {
|
161
|
+
throw Error('Cannot build data for transaction without submitEvent');
|
162
|
+
}
|
163
|
+
|
164
|
+
|
165
|
+
const token = tokens[transaction.sourceChainId].find(token => token.address.toLowerCase() === transaction.submitEvent.token.toLowerCase());
|
166
|
+
|
167
|
+
if (!token) {
|
168
|
+
throw Error('Cannot build data for transaction without token');
|
169
|
+
}
|
170
|
+
|
171
|
+
const itoken = itokens[transaction.targetChainId].find(itoken => itoken.symbol.toLowerCase() === token.symbol.toLowerCase());
|
172
|
+
|
173
|
+
if (!itoken) {
|
174
|
+
throw Error('Cannot build data for transaction without itoken');
|
175
|
+
}
|
176
|
+
|
177
|
+
const targetChainProvider = new ethers.providers.JsonRpcProvider(getRpcProviderUrl(transaction.targetChainId as ChainId));
|
178
|
+
const targetWallet = new ethers.Wallet(config.privateKey, targetChainProvider);
|
179
|
+
const interopBridgeContract = getContract<InteropBridgeToken>(itoken.address, abi.interopBridgeToken, targetWallet);
|
180
|
+
|
181
|
+
const { data } = await interopBridgeContract.populateTransaction.mint(
|
182
|
+
transaction.submitEvent.user,
|
183
|
+
ethers.BigNumber.from(transaction.submitEvent.amount.toString()),
|
184
|
+
ethers.BigNumber.from(transaction.submitEvent.sourceChainId.toString()),
|
185
|
+
transaction.submitTransactionHash,
|
186
|
+
);
|
187
|
+
|
188
|
+
transactions.push({
|
189
|
+
to: itoken.address,
|
190
|
+
data: data!,
|
191
|
+
value: '0',
|
192
|
+
operation: OperationType.Call,
|
193
|
+
});
|
194
|
+
|
195
|
+
return encodeMulti(transactions).data
|
196
|
+
}
|
197
|
+
|
198
|
+
export const buildWithdrawDataForTransaction = async (transaction: Transaction, type: 'source' | 'target') => {
|
199
|
+
const transactions: MetaTransaction[] = [];
|
200
|
+
|
201
|
+
if (transaction.action !== 'withdraw') {
|
202
|
+
throw new Error(`Invalid action: ${transaction.action}`)
|
203
|
+
}
|
204
|
+
|
205
|
+
if (transaction.action === 'withdraw' && transaction.sourceStatus === 'pending') {
|
206
|
+
throw Error('Cannot build data for pending withdraw transaction');
|
207
|
+
}
|
208
|
+
|
209
|
+
if (!transaction.submitEvent) {
|
210
|
+
throw Error('Cannot build data for transaction without submitEvent');
|
211
|
+
}
|
212
|
+
|
213
|
+
const { to, amount, chainId, itoken: itokenAddress } = transaction.submitEvent;
|
214
|
+
|
215
|
+
const itoken = itokens[transaction.sourceChainId].find(token => token.address.toLowerCase() === itokenAddress.toLowerCase());
|
216
|
+
|
217
|
+
if (!itoken) {
|
218
|
+
throw Error('Cannot build data for transaction without itoken');
|
219
|
+
}
|
220
|
+
|
221
|
+
const token = tokens[chainId].find(t => t.symbol.toLowerCase() === itoken.symbol.toLowerCase());
|
222
|
+
|
223
|
+
if (!token) {
|
224
|
+
throw Error('Cannot build data for transaction without token');
|
225
|
+
}
|
226
|
+
|
227
|
+
const targetChainProvider = new ethers.providers.JsonRpcProvider(getRpcProviderUrl(transaction.targetChainId as ChainId));
|
228
|
+
const targetWallet = new ethers.Wallet(config.privateKey, targetChainProvider);
|
229
|
+
const gatewayAddress = addresses[chainId].interopXGateway;
|
230
|
+
const interopBridgeContract = getContract<InteropXGateway>(gatewayAddress, abi.interopXGateway, targetWallet);
|
231
|
+
|
232
|
+
const { data } = await interopBridgeContract.populateTransaction.systemWithdraw(
|
233
|
+
ethers.BigNumber.from(amount.toString()),
|
234
|
+
to,
|
235
|
+
token.address,
|
236
|
+
ethers.BigNumber.from(transaction.sourceChainId.toString()),
|
237
|
+
transaction.submitTransactionHash,
|
238
|
+
);
|
239
|
+
|
240
|
+
transactions.push({
|
241
|
+
to: gatewayAddress,
|
242
|
+
data: data!,
|
243
|
+
value: '0',
|
244
|
+
operation: OperationType.Call,
|
245
|
+
});
|
246
|
+
|
247
|
+
return encodeMulti(transactions).data
|
248
|
+
}
|
249
|
+
|
250
|
+
|
251
|
+
export function getContract<TContract extends ethers.Contract>(address: string, contractInterface: ethers.ContractInterface | any, signerOrProvider?: ethers.Signer | ethers.providers.Provider) {
|
252
|
+
if (!ethers.utils.getAddress(address) || address === ethers.constants.AddressZero) {
|
253
|
+
throw Error(`Invalid 'address' parameter '${address}'.`)
|
254
|
+
}
|
255
|
+
|
256
|
+
const contract = new ethers.Contract(
|
257
|
+
address,
|
258
|
+
contractInterface,
|
259
|
+
signerOrProvider
|
260
|
+
) as TContract
|
261
|
+
|
262
|
+
// Make sure the contract properties is writable
|
263
|
+
const desc = Object.getOwnPropertyDescriptor(contract, 'functions');
|
264
|
+
|
265
|
+
if (!desc || desc.writable !== true) {
|
266
|
+
return contract
|
267
|
+
}
|
268
|
+
|
269
|
+
return new Proxy(contract, {
|
270
|
+
get(target, prop, receiver) {
|
271
|
+
const value = Reflect.get(target, prop, receiver);
|
272
|
+
|
273
|
+
if (typeof value === 'function' && (contract.functions.hasOwnProperty(prop) || ['queryFilter'].includes(String(prop)))) {
|
274
|
+
return async (...args: any[]) => {
|
275
|
+
try {
|
276
|
+
return await value.bind(contract)(...args);
|
277
|
+
} catch (error) {
|
278
|
+
throw new Error(`Error calling "${String(prop)}" on "${address}": ${error.reason || error.message}`)
|
279
|
+
}
|
280
|
+
}
|
281
|
+
}
|
282
|
+
|
283
|
+
|
284
|
+
if (typeof value === 'object' && ['populateTransaction', 'estimateGas', 'functions', 'callStatic'].includes(String(prop))) {
|
285
|
+
const parentProp = String(prop);
|
286
|
+
|
287
|
+
return new Proxy(value, {
|
288
|
+
get(target, prop, receiver) {
|
289
|
+
const value = Reflect.get(target, prop, receiver);
|
290
|
+
|
291
|
+
if (typeof value === 'function') {
|
292
|
+
return async (...args: any[]) => {
|
293
|
+
try {
|
294
|
+
return await value.bind(contract)(...args);
|
295
|
+
} catch (error) {
|
296
|
+
throw new Error(`Error calling "${String(prop)}" using "${parentProp}" on "${address}": ${error.reason || error.message}`)
|
297
|
+
}
|
298
|
+
}
|
299
|
+
}
|
300
|
+
}
|
301
|
+
})
|
302
|
+
}
|
303
|
+
|
304
|
+
return value;
|
305
|
+
},
|
306
|
+
});
|
129
307
|
}
|
package/dist/index.js
DELETED
@@ -1,43 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
-
};
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
require("module-alias/register");
|
7
|
-
const assert_1 = __importDefault(require("assert"));
|
8
|
-
const dotenv_1 = __importDefault(require("dotenv"));
|
9
|
-
const ethers_1 = require("ethers");
|
10
|
-
dotenv_1.default.config();
|
11
|
-
const logger_1 = __importDefault(require("@/logger"));
|
12
|
-
const logger = new logger_1.default('Process');
|
13
|
-
(0, assert_1.default)(process.env.PRIVATE_KEY, "PRIVATE_KEY is not defined");
|
14
|
-
try {
|
15
|
-
new ethers_1.ethers.Wallet(process.env.PRIVATE_KEY);
|
16
|
-
}
|
17
|
-
catch (e) {
|
18
|
-
logger.error('Invalid private key');
|
19
|
-
process.exit(1);
|
20
|
-
}
|
21
|
-
const tasks_1 = require("@/tasks");
|
22
|
-
const net_1 = require("@/net");
|
23
|
-
async function main() {
|
24
|
-
(0, net_1.startPeer)({});
|
25
|
-
const tasks = new tasks_1.Tasks();
|
26
|
-
tasks.start();
|
27
|
-
}
|
28
|
-
main()
|
29
|
-
.then(() => {
|
30
|
-
}).catch(err => {
|
31
|
-
console.error(err);
|
32
|
-
});
|
33
|
-
process.on('SIGINT', () => {
|
34
|
-
logger.debug('received SIGINT signal. exiting.');
|
35
|
-
process.exit(0);
|
36
|
-
});
|
37
|
-
process.on('SIGTERM', () => {
|
38
|
-
logger.debug('received SIGTERM signal. exiting.');
|
39
|
-
process.exit(0);
|
40
|
-
});
|
41
|
-
process.on('unhandledRejection', (reason, p) => {
|
42
|
-
logger.error('unhandled rejection: promise:', p, 'reason:', reason);
|
43
|
-
});
|