This repository has been archived by the owner on Oct 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfrom-near.js
507 lines (480 loc) · 15.7 KB
/
from-near.js
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
const Web3 = require('web3')
const nearlib = require('near-api-js')
const BN = require('bn.js')
const fs = require('fs')
//const assert = require('bsert')
const bs58 = require('bs58')
const { toBuffer } = require('eth-util-lite')
const { tokenAddressParam, tokenAccountParam } = require('./deploy-token')
const { verifyAccount } = require('../rainbow/helpers')
const { NearMintableToken } = require('../near-mintable-token')
const { RainbowConfig } = require('../config')
const { borshifyOutcomeProof } = require('../rainbow/borsh')
const { sleep, RobustWeb3 } = require('../rainbow/robust')
const {
normalizeEthKey,
backoff,
nearJsonContractFunctionCall,
} = require('../rainbow/robust')
let initialCmd
class TransferEthERC20FromNear {
static showRetryAndExit() {
console.log('Retry with command:')
console.log(initialCmd)
process.exit(1)
}
static parseBuffer(obj) {
for (let i in obj) {
if (obj[i] && obj[i].type === 'Buffer') {
obj[i] = Buffer.from(obj[i].data)
} else if (obj[i] && typeof obj[i] === 'object') {
obj[i] = TransferEthERC20FromNear.parseBuffer(obj[i])
}
}
return obj
}
static loadTransferLog() {
try {
let log =
JSON.parse(
fs.readFileSync('transfer-eth-erc20-from-near.log.json').toString()
) || {}
return TransferEthERC20FromNear.parseBuffer(log)
} catch (e) {
return {}
}
}
static deleteTransferLog() {
try {
fs.unlinkSync('transfer-eth-erc20-from-near.log.json')
} catch (e) {
console.log('Warning: failed to remove tranfer log')
}
}
static recordTransferLog(obj) {
fs.writeFileSync(
'transfer-eth-erc20-from-near.log.json',
JSON.stringify(obj)
)
}
static async withdraw({
nearTokenContract,
nearSenderAccountId,
tokenAccount,
amount,
ethReceiverAddress,
nearSenderAccount,
}) {
// Withdraw the token on Near side.
try {
const old_balance = await backoff(10, () =>
nearTokenContract.get_balance({
owner_id: nearSenderAccountId,
})
)
console.log(
`Balance of ${nearSenderAccountId} before withdrawing: ${old_balance}`
)
console.log(
`Withdrawing ${amount} tokens on NEAR blockchain in favor of ${ethReceiverAddress}.`
)
const txWithdraw = await nearJsonContractFunctionCall(
tokenAccount,
nearSenderAccount,
'withdraw',
{ amount: amount, recipient: ethReceiverAddress },
new BN('300000000000000'),
new BN(0)
)
console.log(`tx withdraw: ${JSON.stringify(txWithdraw)}`)
TransferEthERC20FromNear.recordTransferLog({
finished: 'withdraw',
txWithdraw,
})
} catch (txRevertMessage) {
console.log('Failed to withdraw.')
console.log(txRevertMessage.toString())
TransferEthERC20FromNear.showRetryAndExit()
}
}
static async findWithdrawInBlock({ txWithdraw, nearSenderAccountId, near }) {
try {
let txReceiptId
let txReceiptBlockHash
let idType
/*assert(
RainbowConfig.getParam('near-token-factory-account') !== nearSenderAccountId
)*/
// Getting 1st tx
const receipts = txWithdraw.transaction_outcome.outcome.receipt_ids
if (receipts.length === 1) {
txReceiptId = receipts[0]
idType = 'receipt'
} else {
throw new Error(
`Fungible token transaction call is expected to produce only one receipt, but produced: ${JSON.stringify(
txWithdraw
)}`
)
}
// Getting 2nd tx
try {
txReceiptId = txWithdraw.receipts_outcome.find(
(el) => el.id == txReceiptId
).outcome.status.SuccessReceiptId
txReceiptBlockHash = txWithdraw.receipts_outcome.find(
(el) => el.id == txReceiptId
).block_hash
} catch (e) {
throw new Error(`Invalid tx withdraw: ${JSON.stringify(txWithdraw)}`, e)
}
// Get block in which the receipt was processed.
const receiptBlock = await backoff(10, () =>
near.connection.provider.block({
blockId: txReceiptBlockHash,
})
)
// Now wait for a final block with a strictly greater height. This block (or one of its ancestors) should hold the outcome, although this is not guaranteed if there are multiple shards.
const outcomeBlock = await backoff(10, async () => {
while (true) {
let block = await near.connection.provider.block({
finality: "final"
})
if (Number(block.header.height) <= Number(receiptBlock.header.height)) {
await sleep(1000)
continue
}
return block
}
})
TransferEthERC20FromNear.recordTransferLog({
finished: 'find-withdraw',
txReceiptBlockHash,
txReceiptId,
outcomeBlock,
idType,
})
} catch (txRevertMessage) {
console.log('Failed to find withdraw in block.')
console.log(txRevertMessage.toString())
TransferEthERC20FromNear.showRetryAndExit()
}
}
static async waitBlock({
clientContract,
outcomeBlock,
robustWeb3,
nearSenderAccountId,
nearTokenContract,
amount,
idType,
txReceiptId,
}) {
// Wait for the block with the given receipt/transaction in Near2EthClient.
try {
const outcomeBlockHeight = Number(outcomeBlock.header.height)
let clientBlockHeight
let clientBlockHash
while (true) {
let clientState = await clientContract.methods.bridgeState().call()
clientBlockHeight = Number(clientState.currentHeight)
let clientBlockValidAfter = Number(clientState.nextValidAt)
clientBlockHash = bs58.encode(toBuffer(await clientContract.methods.blockHashes(clientBlockHeight).call()))
console.log(`Current light client head is: hash=${clientBlockHash}, height=${clientBlockHeight}`)
if (clientBlockHeight > outcomeBlockHeight) {
console.log(`The block at height ${outcomeBlockHeight} is already available to the client.`)
break
} else {
let delay = clientBlockValidAfter == 0
? await clientContract.methods.lockDuration().call()
: clientBlockValidAfter - (await robustWeb3.getBlock('latest')).timestamp
delay = Math.max(delay, 1)
console.log(`Block ${outcomeBlockHeight} is not yet available. Sleeping for ${delay} seconds.`)
await sleep(delay * 1000)
}
}
console.log(`Withdrawn ${JSON.stringify(amount)}`)
const new_balance = await backoff(10, () =>
nearTokenContract.get_balance({
owner_id: nearSenderAccountId,
})
)
console.log(
`Balance of ${nearSenderAccountId} after withdrawing: ${new_balance}`
)
TransferEthERC20FromNear.recordTransferLog({
finished: 'wait-block',
clientBlockHashB58: clientBlockHash,
idType,
txReceiptId,
clientBlockHeight,
})
} catch (txRevertMessage) {
console.log('Failed to wait for block occur in near on eth contract')
console.log(txRevertMessage.toString())
TransferEthERC20FromNear.showRetryAndExit()
}
}
static async getProof({
idType,
near,
txReceiptId,
nearSenderAccountId,
clientBlockHashB58,
clientBlockHeight,
}) {
try {
// Get the outcome proof only use block merkle root that we know is available on the Near2EthClient.
let proofRes
if (idType === 'transaction') {
proofRes = await near.connection.provider.sendJsonRpc(
'light_client_proof',
{
type: 'transaction',
transaction_hash: txReceiptId,
// TODO: Use proper sender.
receiver_id: nearSenderAccountId,
light_client_head: clientBlockHashB58,
}
)
} else if (idType === 'receipt') {
proofRes = await near.connection.provider.sendJsonRpc(
'light_client_proof',
{
type: 'receipt',
receipt_id: txReceiptId,
// TODO: Use proper sender.
receiver_id: nearSenderAccountId,
light_client_head: clientBlockHashB58,
}
)
} else {
throw new Error('Unreachable')
}
TransferEthERC20FromNear.recordTransferLog({
finished: 'get-proof',
proofRes,
clientBlockHeight,
})
} catch (txRevertMessage) {
console.log('Failed to get proof.')
console.log(txRevertMessage.toString())
TransferEthERC20FromNear.showRetryAndExit()
}
}
static async unlock({
proverContract,
proofRes,
clientBlockHeight,
ethERC20Contract,
ethReceiverAddress,
ethTokenLockerContract,
ethMasterAccount,
robustWeb3,
}) {
try {
// Check that the proof is correct.
const borshProofRes = borshifyOutcomeProof(proofRes)
clientBlockHeight = new BN(clientBlockHeight)
// Debugging output, uncomment for debugging.
// console.log(`proof: ${JSON.stringify(proofRes)}`);
// console.log(`client height: ${clientBlockHeight.toString()}`);
// console.log(`root: ${clientBlockMerkleRoot}`);
await proverContract.methods
.proveOutcome(borshProofRes, clientBlockHeight)
.call()
const oldBalance = await ethERC20Contract.methods
.balanceOf(ethReceiverAddress)
.call()
console.log(
`ERC20 balance of ${ethReceiverAddress} before the transfer: ${oldBalance}`
)
await robustWeb3.callContract(
ethTokenLockerContract,
'unlockToken',
[borshProofRes, clientBlockHeight],
{
from: ethMasterAccount,
gas: 5000000,
handleRevert: true,
gasPrice: new BN(await robustWeb3.web3.eth.getGasPrice()).mul(
new BN(RainbowConfig.getParam('eth-gas-multiplier'))
),
}
)
/*await ethTokenLockerContract.methods
.unlockToken(borshProofRes, clientBlockHeight)
.send({
from: ethMasterAccount,
gas: 5000000,
handleRevert: true,
gasPrice: new BN(await robustWeb3.web3.eth.getGasPrice()).mul(
new BN(RainbowConfig.getParam('eth-gas-multiplier'))
),
})*/
const newBalance = await ethERC20Contract.methods
.balanceOf(ethReceiverAddress)
.call()
console.log(
`ERC20 balance of ${ethReceiverAddress} after the transfer: ${newBalance}`
)
TransferEthERC20FromNear.deleteTransferLog()
} catch (txRevertMessage) {
console.log('Failed to unlock.')
console.log(txRevertMessage.toString())
TransferEthERC20FromNear.showRetryAndExit()
}
}
static async execute(command) {
initialCmd = command.parent.rawArgs.join(' ')
const nearSenderAccountId = command.nearSenderAccount
let amount = command.amount
const ethReceiverAddress = command.ethReceiverAddress.startsWith('0x')
? command.ethReceiverAddress.substr(2)
: command.ethReceiverAddress
const tokenAddress = command.tokenName
? RainbowConfig.getParam(tokenAddressParam(command.tokenName))
: RainbowConfig.getParam('eth-erc20-address')
const tokenAccount = command.tokenName
? RainbowConfig.getParam(tokenAccountParam(command.tokenName))
: RainbowConfig.getParam('near-erc20-account')
const keyStore = new nearlib.keyStores.InMemoryKeyStore()
await keyStore.setKey(
RainbowConfig.getParam('near-network-id'),
nearSenderAccountId,
nearlib.KeyPair.fromString(command.nearSenderSk)
)
const near = await nearlib.connect({
nodeUrl: RainbowConfig.getParam('near-node-url'),
networkId: RainbowConfig.getParam('near-network-id'),
masterAccount: nearSenderAccountId,
deps: { keyStore: keyStore },
})
const nearSenderAccount = new nearlib.Account(
near.connection,
nearSenderAccountId
)
await verifyAccount(near, nearSenderAccountId)
const nearTokenContract = new nearlib.Contract(
nearSenderAccount,
tokenAccount,
{
changeMethods: ['new', 'withdraw'],
viewMethods: ['get_balance'],
}
)
const nearTokenContractBorsh = new NearMintableToken(
nearSenderAccount,
tokenAccount
)
await nearTokenContractBorsh.accessKeyInit()
let robustWeb3 = new RobustWeb3(RainbowConfig.getParam('eth-node-url'))
const web3 = robustWeb3.web3
let ethMasterAccount = web3.eth.accounts.privateKeyToAccount(
normalizeEthKey(RainbowConfig.getParam('eth-master-sk'))
)
web3.eth.accounts.wallet.add(ethMasterAccount)
web3.eth.defaultAccount = ethMasterAccount.address
ethMasterAccount = ethMasterAccount.address
const clientContract = new web3.eth.Contract(
// @ts-ignore
JSON.parse(
fs.readFileSync(RainbowConfig.getParam('eth-client-abi-path'))
),
RainbowConfig.getParam('eth-client-address'),
{
from: ethMasterAccount,
handleRevert: true,
}
)
const proverContract = new web3.eth.Contract(
// @ts-ignore
JSON.parse(
fs.readFileSync(RainbowConfig.getParam('eth-prover-abi-path'))
),
RainbowConfig.getParam('eth-prover-address'),
{
from: ethMasterAccount,
handleRevert: true,
}
)
const ethTokenLockerContract = new web3.eth.Contract(
// @ts-ignore
JSON.parse(
fs.readFileSync(RainbowConfig.getParam('eth-locker-abi-path'))
),
RainbowConfig.getParam('eth-locker-address'),
{
from: ethMasterAccount,
handleRevert: true,
}
)
const ethERC20Contract = new web3.eth.Contract(
// @ts-ignore
JSON.parse(fs.readFileSync(RainbowConfig.getParam('eth-erc20-abi-path'))),
tokenAddress,
{
from: ethMasterAccount,
handleRevert: true,
}
)
let transferLog = TransferEthERC20FromNear.loadTransferLog()
if (transferLog.finished === undefined) {
await TransferEthERC20FromNear.withdraw({
nearTokenContract,
nearSenderAccountId,
tokenAccount,
amount,
ethReceiverAddress,
nearSenderAccount,
})
transferLog = TransferEthERC20FromNear.loadTransferLog()
}
if (transferLog.finished === 'withdraw') {
await TransferEthERC20FromNear.findWithdrawInBlock({
txWithdraw: transferLog.txWithdraw,
nearSenderAccountId,
near,
})
transferLog = TransferEthERC20FromNear.loadTransferLog()
}
if (transferLog.finished === 'find-withdraw') {
await TransferEthERC20FromNear.waitBlock({
clientContract,
robustWeb3,
outcomeBlock: transferLog.outcomeBlock,
nearSenderAccountId,
nearTokenContract,
amount,
idType: transferLog.idType,
txReceiptId: transferLog.txReceiptId,
})
transferLog = TransferEthERC20FromNear.loadTransferLog()
}
if (transferLog.finished === 'wait-block') {
await TransferEthERC20FromNear.getProof({
idType: transferLog.idType,
near,
txReceiptId: transferLog.txReceiptId,
nearSenderAccountId,
clientBlockHashB58: transferLog.clientBlockHashB58,
clientBlockHeight: transferLog.clientBlockHeight,
})
transferLog = TransferEthERC20FromNear.loadTransferLog()
}
if (transferLog.finished === 'get-proof') {
await TransferEthERC20FromNear.unlock({
proverContract,
proofRes: transferLog.proofRes,
clientBlockHeight: transferLog.clientBlockHeight,
ethERC20Contract,
ethReceiverAddress,
ethTokenLockerContract,
ethMasterAccount,
robustWeb3,
})
}
process.exit(0)
}
}
exports.TransferEthERC20FromNear = TransferEthERC20FromNear