-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbridge.js
136 lines (121 loc) · 4.07 KB
/
bridge.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
const ethers = require('ethers')
const request = require('request')
const urljoin = require('url-join');
const BigNumber = require('bignumber.js')
const WebSocket = require('ws')
const path = require('path')
const fs = require('fs')
const solc = require('solc')
function createContract () {
try {
const p = path.resolve(__dirname, './contracts', 'TomoBridgeWrapToken.sol')
const contractCode = fs.readFileSync(p, 'UTF-8')
return contractCode
} catch (error) {
throw error
}
}
function compileContract (contractCode) {
try {
const compiledContract = solc.compile(contractCode, 1)
const contract = compiledContract.contracts['TomoBridgeWrapToken'] ||
compiledContract.contracts[':' + 'TomoBridgeWrapToken']
return contract
} catch (error) {
throw error
}
}
async function getABI () {
let p = path.resolve(__dirname, './abis', 'TomoBridgeWrapToken.json')
const data = fs.readFileSync(p, 'UTF-8')
if (data) {
return JSON.parse(data).abi
}
}
class Bridge {
constructor ({
endpoint,
pkey,
chainId,
}) {
this.gasLimit = 4000000
this.endpoint = endpoint
this.chainId = chainId ? Number(chainId) : (this.endpoint === 'https://rpc.tomochain.com' ? 88 : 89)
if (!pkey) {
let randomWallet = ethers.Wallet.createRandom()
pkey = randomWallet.privateKey
}
if (endpoint.endsWith('.ipc')) {
this.provider = new ethers.providers.IpcProvider(endpoint)
} else {
this.provider = new ethers.providers.JsonRpcProvider(endpoint)
}
this.wallet = new ethers.Wallet(pkey, this.provider)
this.coinbase = this.wallet.address
}
async issueWrapToken ({
name,
symbol,
totalSupply,
decimals,
minFee,
depositFee,
withdrawFee,
nonce
}) {
try {
console.log('Creating contract...')
const contractCode = createContract()
console.log('Compiling contract...')
const compliedContract = compileContract(contractCode)
const bytecode = compliedContract.bytecode
const abi = JSON.parse(compliedContract.interface)
const trcContract = {
abi, bytecode
}
console.log('Deploying contract...')
const factory = new ethers.ContractFactory(
trcContract.abi,
trcContract.bytecode,
this.wallet
)
nonce = nonce || await this.provider.getTransactionCount(this.coinbase)
const txParams = {
gasLimit: ethers.utils.hexlify(this.gasLimit),
gasPrice: ethers.utils.hexlify(10000000000000),
chainId: this.chainId,
nonce: nonce
}
const contract = await factory.deploy(
[
'0x538E9F8D20B37A4a5b8Bd726d7753a3143f21353',
'0x8397712b0Ea16AC6f685E8fB061981988e2F598f',
'0xcaFC432009EE987604674a43B5cF47125d50B8a6'
], 2,
name,
symbol,
decimals,
(new BigNumber(totalSupply).multipliedBy(10 ** decimals)).toString(10),
(new BigNumber(minFee).multipliedBy(10 ** decimals)).toString(10),
(new BigNumber(depositFee).multipliedBy(10 ** decimals)).toString(10),
(new BigNumber(withdrawFee).multipliedBy(10 ** decimals)).toString(10),
txParams
)
await contract.deployed()
return {
name,
symbol,
totalSupply,
minFee,
withdrawFee,
depositFee,
decimals,
contractAddress: contract.address,
transactionHash: contract.deployTransaction.hash
}
} catch (error) {
throw error
}
}
}
module.exports = Bridge