/
home.js
139 lines (126 loc) · 4.48 KB
/
home.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
const assert = require('assert')
const Web3Utils = require('web3-utils')
const { web3Home, HOME_RPC_URL, deploymentPrivateKey } = require('../web3')
const {
deployContract,
privateKeyToAddress,
upgradeProxy,
setBridgeContract,
transferOwnership,
sendRawTxHome,
assertStateWithRetry
} = require('../deploymentUtils')
const {
BRIDGEABLE_TOKEN_NAME,
BRIDGEABLE_TOKEN_SYMBOL,
BRIDGEABLE_TOKEN_DECIMALS,
DEPLOYMENT_ACCOUNT_PRIVATE_KEY,
DEPLOY_REWARDABLE_TOKEN,
BLOCK_REWARD_ADDRESS,
DPOS_STAKING_ADDRESS
} = require('../loadEnv')
const {
homeContracts: {
EternalStorageProxy,
HomeAMBErc677ToErc677: HomeBridge,
ERC677BridgeToken,
ERC677BridgeTokenRewardable
}
} = require('../loadContracts')
const DEPLOYMENT_ACCOUNT_ADDRESS = privateKeyToAddress(DEPLOYMENT_ACCOUNT_PRIVATE_KEY)
async function deployHome() {
let nonce = await web3Home.eth.getTransactionCount(DEPLOYMENT_ACCOUNT_ADDRESS)
console.log('\n[Home] Deploying Bridge Mediator storage\n')
const homeBridgeStorage = await deployContract(EternalStorageProxy, [], {
from: DEPLOYMENT_ACCOUNT_ADDRESS,
nonce
})
nonce++
console.log('[Home] Bridge Mediator Storage: ', homeBridgeStorage.options.address)
console.log('\n[Home] Deploying Bridge Mediator implementation\n')
const homeBridgeImplementation = await deployContract(HomeBridge, [], {
from: DEPLOYMENT_ACCOUNT_ADDRESS,
nonce
})
nonce++
console.log('[Home] Bridge Mediator Implementation: ', homeBridgeImplementation.options.address)
console.log('\n[Home] Hooking up Mediator storage to Mediator implementation')
await upgradeProxy({
proxy: homeBridgeStorage,
implementationAddress: homeBridgeImplementation.options.address,
version: '1',
nonce,
url: HOME_RPC_URL
})
nonce++
console.log('\n[Home] deploying Bridgeable token')
const erc677Contract = DEPLOY_REWARDABLE_TOKEN ? ERC677BridgeTokenRewardable : ERC677BridgeToken
let args = [BRIDGEABLE_TOKEN_NAME, BRIDGEABLE_TOKEN_SYMBOL, BRIDGEABLE_TOKEN_DECIMALS]
if (DEPLOY_REWARDABLE_TOKEN) {
const chainId = await web3Home.eth.net.getId()
assert.strictEqual(chainId > 0, true, 'Invalid chain ID')
args.push(chainId)
}
const erc677token = await deployContract(
erc677Contract,
args,
{ from: DEPLOYMENT_ACCOUNT_ADDRESS, network: 'home', nonce }
)
nonce++
console.log('[Home] Bridgeable Token: ', erc677token.options.address)
console.log('\n[Home] Set Bridge Mediator contract on Bridgeable token')
await setBridgeContract({
contract: erc677token,
bridgeAddress: homeBridgeStorage.options.address,
nonce,
url: HOME_RPC_URL
})
nonce++
if (DEPLOY_REWARDABLE_TOKEN) {
console.log('\n[Home] Set BlockReward contract on Bridgeable token contract')
const setBlockRewardContractData = await erc677token.methods
.setBlockRewardContract(BLOCK_REWARD_ADDRESS)
.encodeABI()
const setBlockRewardContract = await sendRawTxHome({
data: setBlockRewardContractData,
nonce,
to: erc677token.options.address,
privateKey: deploymentPrivateKey,
url: HOME_RPC_URL
})
if (setBlockRewardContract.status) {
assert.strictEqual(Web3Utils.hexToNumber(setBlockRewardContract.status), 1, 'Transaction Failed')
} else {
await assertStateWithRetry(erc677token.methods.blockRewardContract().call, BLOCK_REWARD_ADDRESS)
}
nonce++
console.log('\n[Home] Set Staking contract on Bridgeable token contract')
const setStakingContractData = await erc677token.methods.setStakingContract(DPOS_STAKING_ADDRESS).encodeABI()
const setStakingContract = await sendRawTxHome({
data: setStakingContractData,
nonce,
to: erc677token.options.address,
privateKey: deploymentPrivateKey,
url: HOME_RPC_URL
})
if (setStakingContract.status) {
assert.strictEqual(Web3Utils.hexToNumber(setStakingContract.status), 1, 'Transaction Failed')
} else {
await assertStateWithRetry(erc677token.methods.stakingContract().call, DPOS_STAKING_ADDRESS)
}
nonce++
}
console.log('[Home] Transferring ownership of Bridgeable token to Bridge Mediator contract')
await transferOwnership({
contract: erc677token,
newOwner: homeBridgeStorage.options.address,
nonce,
url: HOME_RPC_URL
})
console.log('\nHome part of ERC677-to-ERC677 bridge deployed\n')
return {
homeBridgeMediator: { address: homeBridgeStorage.options.address },
bridgeableErc677: { address: erc677token.options.address }
}
}
module.exports = deployHome