Skip to content

Commit 1712d1d

Browse files
committed
add deployer with enhancedStepsReserve
1 parent 1d8ef4d commit 1712d1d

File tree

6 files changed

+326
-39
lines changed

6 files changed

+326
-39
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@ jspm_packages
4343
.DS_Store
4444

4545
report
46+
47+
.env

buidler.config.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,17 @@ module.exports = {
151151
mocha: {
152152
enableTimeouts: false
153153
}
154-
};
154+
};
155+
156+
require('dotenv').config()
157+
158+
const INFURA_API_KEY = process.env.INFURA_API_KEY
159+
const PRIVATE_KEY = process.env.PRIVATE_KEY
160+
161+
if (INFURA_API_KEY != undefined && PRIVATE_KEY != undefined) {
162+
module.exports.networks.ropsten = {
163+
url: `https://ropsten.infura.io/v3/${INFURA_API_KEY}`,
164+
accounts: [PRIVATE_KEY],
165+
timeout: 20000
166+
}
167+
}

package-lock.json

Lines changed: 43 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"chai-as-promised": "7.1.1",
2424
"compare-versions": "3.5.1",
2525
"ethers": "5.0.4",
26+
"dotenv": "^8.2.0",
2627
"ganache-cli": "6.8.2",
2728
"mathjs": "4.4.2",
2829
"rlp": "2.2.4",
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
const artifacts = require('@nomiclabs/buidler').artifacts
2+
3+
const KyberReserveHighRate = artifacts.require('KyberReserveHighRate.sol')
4+
const ConversionRateEnhancedSteps = artifacts.require(
5+
'ConversionRateEnhancedSteps.sol'
6+
)
7+
const WrapConversionRateEnhancedSteps = artifacts.require(
8+
'WrapConversionRateEnhancedSteps.sol'
9+
)
10+
11+
const Web3 = require('web3')
12+
const fs = require('fs')
13+
const RLP = require('rlp')
14+
const { fromUtf8 } = require('web3-utils')
15+
const BN = Web3.utils.BN
16+
17+
const {
18+
gasPriceGwei,
19+
rpcUrl,
20+
chainId: chainIdInput,
21+
privateKey
22+
} = require('yargs')
23+
.usage(
24+
'Usage: PRIVATE_KEY=xxxx $0 --gas-price-gwei [gwei] --rpc-url [url] --chain-id'
25+
)
26+
.demandOption(['gasPriceGwei', 'rpcUrl', 'privateKey'])
27+
.env(true)
28+
.boolean('dontSendTx').argv
29+
30+
console.log(rpcUrl)
31+
32+
let web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl))
33+
34+
let reserve
35+
let reserveAddr //= "0xc4b5C2B1d8922B324024A8e7CA1216d1460376Bd"
36+
let conversionRate
37+
let conversionRateAddr // = "0x80a932DFd1DA618d1f6d446CC676F68Aac1b9f6C"
38+
let wrapper
39+
let wrapperAddr //= "0xc3Caa675F718E84e05EA0Da649987d3AafE17b96"
40+
41+
let networkAddr = '0x9cb7bb6d4795a281860b9bfb7b1441361cc9a794'
42+
let admin = '0xf3D872b9E8d314820dc8E99DAfBe1A3FeEDc27D5'
43+
// let deployer
44+
45+
const account = web3.eth.accounts.privateKeyToAccount(privateKey)
46+
const sender = account.address
47+
const gasPrice = new BN(gasPriceGwei).mul(new BN(10).pow(new BN(9)))
48+
const signedTxs = []
49+
let nonce
50+
let chainId = chainIdInput
51+
52+
process.on('unhandledRejection', console.error.bind(console))
53+
54+
console.log('from', sender)
55+
56+
async function sendTx (txObject, gasLimit) {
57+
const txTo = txObject._parent.options.address
58+
59+
try {
60+
gasLimit = gasLimit == undefined ? await txObject.estimateGas() : gasLimit
61+
gasLimit = Math.round(1.1 * gasLimit)
62+
} catch (e) {
63+
gasLimit = 800000
64+
}
65+
66+
if (txTo !== null) {
67+
gasLimit = 800000
68+
}
69+
70+
const txData = txObject.encodeABI()
71+
const txFrom = account.address
72+
const txKey = account.privateKey
73+
74+
const tx = {
75+
from: txFrom,
76+
to: txTo,
77+
nonce: nonce,
78+
data: txData,
79+
gas: gasLimit,
80+
chainId,
81+
gasPrice
82+
}
83+
84+
const signedTx = await web3.eth.accounts.signTransaction(tx, txKey)
85+
nonce++
86+
// don't wait for confirmation
87+
signedTxs.push(signedTx.rawTransaction)
88+
web3.eth.sendSignedTransaction(signedTx.rawTransaction, {
89+
from: sender
90+
})
91+
}
92+
93+
async function deployContract (contract, ctorArgs) {
94+
const bytecode = contract.bytecode
95+
const abi = contract.abi
96+
const myContract = new web3.eth.Contract(abi)
97+
98+
const deploy = myContract.deploy({ data: bytecode, arguments: ctorArgs })
99+
let address =
100+
'0x' +
101+
web3.utils
102+
.sha3(RLP.encode([sender, nonce]))
103+
.slice(12)
104+
.substring(14)
105+
address = web3.utils.toChecksumAddress(address)
106+
107+
await sendTx(deploy, 6500000)
108+
109+
myContract.options.address = address
110+
111+
return [address, myContract]
112+
}
113+
114+
const keypress = async () => {
115+
process.stdin.setRawMode(true)
116+
return new Promise(resolve =>
117+
process.stdin.once('data', data => {
118+
const byteArray = [...data]
119+
if (byteArray.length > 0 && byteArray[0] === 3) {
120+
console.log('^C')
121+
process.exit(1)
122+
}
123+
process.stdin.setRawMode(false)
124+
resolve()
125+
})
126+
)
127+
}
128+
129+
async function main () {
130+
nonce = await web3.eth.getTransactionCount(sender)
131+
console.log('nonce', nonce)
132+
chainId = chainId || (await web3.eth.net.getId())
133+
console.log('chainId', chainId)
134+
135+
web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl))
136+
137+
if (conversionRateAddr == undefined) {
138+
[conversionRateAddr, conversionRate] = await deployContract(
139+
ConversionRateEnhancedSteps,
140+
[sender]
141+
)
142+
console.log(`deploy conversionRate at ${conversionRateAddr}`)
143+
} else {
144+
conversionRate = new web3.eth.Contract(
145+
ConversionRateEnhancedSteps.abi,
146+
conversionRateAddr
147+
)
148+
}
149+
150+
if (reserveAddr == undefined) {
151+
[reserveAddr, reserve] = await deployContract(KyberReserveHighRate, [
152+
networkAddr,
153+
conversionRateAddr,
154+
sender
155+
])
156+
console.log(`deploy reserve at ${reserveAddr}`)
157+
} else {
158+
reserve = new web3.eth.Contract(KyberReserveHighRate.abi, reserveAddr)
159+
}
160+
161+
if (wrapperAddr == undefined) {
162+
[wrapperAddr, wrapper] = await deployContract(
163+
WrapConversionRateEnhancedSteps,
164+
[conversionRateAddr]
165+
)
166+
console.log(`deploy wrapper at ${wrapperAddr}`)
167+
} else {
168+
wrapper = new web3.eth.Contract(
169+
WrapConversionRateEnhancedSteps.abi,
170+
wrapperAddr
171+
)
172+
}
173+
174+
console.log(`set conversionRate admin to wrapper contract`)
175+
await sendTx(conversionRate.methods.transferAdmin(wrapperAddr))
176+
await sendTx(wrapper.methods.claimWrappedContractAdmin())
177+
178+
console.log(`set admin to new admin`)
179+
await sendTx(wrapper.methods.transferAdminQuickly(admin))
180+
await sendTx(reserve.methods.transferAdminQuickly(admin))
181+
182+
await keypress()
183+
}
184+
185+
main()
186+
.then(() => process.exit(0))
187+
.catch(error => {
188+
console.error(error)
189+
process.exit(1)
190+
})

0 commit comments

Comments
 (0)