-
Notifications
You must be signed in to change notification settings - Fork 4
/
mkr2wmkr-complete-manual.js
128 lines (97 loc) · 3.55 KB
/
mkr2wmkr-complete-manual.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
const WanX = require('../');
const Web3 = require('web3');
const keythereum = require('keythereum');
const utils = require('./utils');
/**
* Requirements:
* - Ethereum account has enough token to cover the value defined in `opts`
* - Ethereum account has enough to cover gas for lock
* - Wanchain account has enough to cover gas for redeem
*/
const config = {
wanchain: { url: 'http://localhost:18545' },
ethereum: { url: 'http://localhost:28545'},
};
const web3wan = new Web3(new Web3.providers.HttpProvider(config.wanchain.url));
const web3eth = new Web3(new Web3.providers.HttpProvider(config.ethereum.url));
const wanx = new WanX('testnet', config);
// New crosschain transaction
// ethereum, inbound
const cctx = wanx.newChain('erc20', true);
// Generate a new redeemKey
const redeemKey = wanx.newRedeemKey();
// Define the transaction opts
const opts = {
token: {
eth: '0x54950025d1854808b09277fe082b54682b11a50b', // MKR
wan: '0x67f3de547c7f3bc77095686a9e7fe49397e59cdf', // WMKR
},
from: '0x026a6301477c59ab17d11cade5fd00e5c8c6fa90',
to: '0x017ab346a4bb19f46c99bf19b6592828435540b0',
value: '2242000000000000',
storeman: {
wan: '0x06daa9379cbe241a84a65b217a11b38fe3b4b063',
eth: '0x41623962c5d44565de623d53eb677e0f300467d2',
},
redeemKey,
};
// Get Ethereum private keys
const ethDatadir = '/home/user/.ethereum/testnet/';
const ethKeyObject = keythereum.importFromFile(opts.from, ethDatadir);
const ethPrivateKey = keythereum.recover('mypassword', ethKeyObject);
// Get Wanchain private keys
const wanDatadir = '/home/user/.wanchain/testnet/';
const wanKeyObject = keythereum.importFromFile(opts.to, wanDatadir);
const wanPrivateKey = keythereum.recover('mypassword', wanKeyObject);
// Do inbound MKR to WMKR transaction
Promise.resolve([])
.then(sendApprove)
.then(sendLock)
.then(confirmLock)
.then(sendRedeem)
.then(confirmRedeem)
.catch(err => {
console.log(err);
});
async function sendApprove() {
console.log('Starting eth inbound lock', opts);
// Get the raw approve tx
const approveTx = cctx.buildApproveTx(opts);
// Send the approve transaction on Ethereum
const receipt = await utils.sendRawEthTx(web3eth, approveTx, opts.from, ethPrivateKey);
console.log('Token approved for transfer');
console.log(receipt);
}
async function sendLock() {
// Get the raw lock tx
const lockTx = cctx.buildLockTx(opts);
// Send the lock transaction on Ethereum
const receipt = await utils.sendRawEthTx(web3eth, lockTx, opts.from, ethPrivateKey);
console.log('Lock submitted and now pending on storeman');
console.log(receipt);
}
async function confirmLock() {
// Get the current block number on Wanchain
const blockNumber = await web3wan.eth.getBlockNumber();
// Scan for the lock confirmation from the storeman
const log = await cctx.listenLock(opts, blockNumber);
console.log('Lock confirmed by storeman');
console.log(log);
}
async function sendRedeem() {
// Get the raw redeem tx
const redeemTx = cctx.buildRedeemTx(opts);
// Send the redeem transaction on Wanchain
const receipt = await utils.sendRawWanTx(web3wan, redeemTx, opts.to, wanPrivateKey);
console.log('Redeem confirmed and is now pending on storeman');
console.log(receipt);
}
async function confirmRedeem() {
// Get the current block number on Ethereum
const blockNumber = await web3eth.eth.getBlockNumber();
// Scan for the redeem confirmation from the storeman
const log = await cctx.listenRedeem(opts, blockNumber);
console.log('Redeem confirmed by storeman');
console.log(log);
console.log('COMPLETE!!!');
}