|
| 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