|
| 1 | +const { usePlugin } = require("@nomiclabs/buidler/config"); |
| 2 | +const { utils } = require("ethers"); |
| 3 | +const fs = require("fs"); |
| 4 | + |
| 5 | +usePlugin("@nomiclabs/buidler-waffle"); |
| 6 | + |
| 7 | +const { isAddress, getAddress, formatUnits, parseUnits } = utils; |
| 8 | + |
| 9 | +/* |
| 10 | + 📡 This is where you configure your deploy configuration for 🏗 scaffold-eth |
| 11 | +
|
| 12 | + check out `packages/scripts/deploy.js` to customize your deployment |
| 13 | +
|
| 14 | + out of the box it will auto deploy anything in the `contracts` folder and named *.sol |
| 15 | + plus it will use *.args for constructor args |
| 16 | +*/ |
| 17 | + |
| 18 | +// |
| 19 | +// Select the network you want to deploy to here: |
| 20 | +// |
| 21 | +const defaultNetwork = "localhost"; |
| 22 | + |
| 23 | +function mnemonic() { |
| 24 | + try { |
| 25 | + return fs.readFileSync("./mnemonic.txt").toString().trim(); |
| 26 | + } catch (e) { |
| 27 | + if (defaultNetwork !== "localhost") { |
| 28 | + console.log("☢️ WARNING: No mnemonic file created for a deploy account. Try `yarn run generate` and then `yarn run account`.") |
| 29 | + } |
| 30 | + } |
| 31 | + return ""; |
| 32 | +} |
| 33 | + |
| 34 | +module.exports = { |
| 35 | + defaultNetwork, |
| 36 | + |
| 37 | + // don't forget to set your provider like: |
| 38 | + // REACT_APP_PROVIDER=https://dai.poa.network in packages/react-app/.env |
| 39 | + // (then your frontend will talk to your contracts on the live network!) |
| 40 | + // (you will need to restart the `yarn run start` dev server after editing the .env) |
| 41 | + |
| 42 | + networks: { |
| 43 | + localhost: { |
| 44 | + url: "http://localhost:8545", |
| 45 | + /* |
| 46 | + notice no mnemonic here? it will just use account 0 of the buidler node to deploy |
| 47 | + (you can put in a mnemonic here to set the deployer locally) |
| 48 | + */ |
| 49 | + }, |
| 50 | + rinkeby: { |
| 51 | + url: "https://rinkeby.infura.io/v3/c954231486fa42ccb6d132b406483d14",//<---- YOUR INFURA ID! (or it won't work) |
| 52 | + accounts: { |
| 53 | + mnemonic: mnemonic(), |
| 54 | + }, |
| 55 | + }, |
| 56 | + mainnet: { |
| 57 | + url: "https://mainnet.infura.io/v3/c954231486fa42ccb6d132b406483d14",//<---- YOUR INFURA ID! (or it won't work) |
| 58 | + accounts: { |
| 59 | + mnemonic: mnemonic(), |
| 60 | + }, |
| 61 | + }, |
| 62 | + ropsten: { |
| 63 | + url: "https://ropsten.infura.io/v3/c954231486fa42ccb6d132b406483d14",//<---- YOUR INFURA ID! (or it won't work) |
| 64 | + accounts: { |
| 65 | + mnemonic: mnemonic(), |
| 66 | + }, |
| 67 | + }, |
| 68 | + xdai: { |
| 69 | + url: 'https://dai.poa.network', |
| 70 | + gasPrice: 1000000000, |
| 71 | + accounts: { |
| 72 | + mnemonic: mnemonic(), |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + solc: { |
| 77 | + version: "0.6.6", |
| 78 | + optimizer: { |
| 79 | + enabled: true, |
| 80 | + runs: 200, |
| 81 | + }, |
| 82 | + }, |
| 83 | +}; |
| 84 | + |
| 85 | +const DEBUG = false; |
| 86 | + |
| 87 | +function debug(text) { |
| 88 | + if (DEBUG) { |
| 89 | + console.log(text); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +task("generate", "Create a mnemonic for builder deploys", async (_, { ethers }) => { |
| 94 | + const bip39 = require("bip39") |
| 95 | + const hdkey = require('ethereumjs-wallet/hdkey'); |
| 96 | + const mnemonic = bip39.generateMnemonic() |
| 97 | + if (DEBUG) console.log("mnemonic", mnemonic) |
| 98 | + const seed = await bip39.mnemonicToSeed(mnemonic) |
| 99 | + if (DEBUG) console.log("seed", seed) |
| 100 | + const hdwallet = hdkey.fromMasterSeed(seed); |
| 101 | + const wallet_hdpath = "m/44'/60'/0'/0/"; |
| 102 | + const account_index = 0 |
| 103 | + let fullPath = wallet_hdpath + account_index |
| 104 | + if (DEBUG) console.log("fullPath", fullPath) |
| 105 | + const wallet = hdwallet.derivePath(fullPath).getWallet(); |
| 106 | + const privateKey = "0x" + wallet._privKey.toString('hex'); |
| 107 | + if (DEBUG) console.log("privateKey", privateKey) |
| 108 | + var EthUtil = require('ethereumjs-util'); |
| 109 | + const address = "0x" + EthUtil.privateToAddress(wallet._privKey).toString('hex') |
| 110 | + console.log("🔐 Account Generated as " + address + ".txt and set as mnemonic in packages/buidler") |
| 111 | + console.log("💬 Use 'yarn run account' to get more information about the deployment account.") |
| 112 | + |
| 113 | + fs.writeFileSync("./" + address + ".txt", mnemonic.toString()) |
| 114 | + fs.writeFileSync("./mnemonic.txt", mnemonic.toString()) |
| 115 | +}); |
| 116 | + |
| 117 | +task("account", "Get balance informations for the deployment account.", async (_, { ethers }) => { |
| 118 | + const hdkey = require('ethereumjs-wallet/hdkey'); |
| 119 | + const bip39 = require("bip39") |
| 120 | + let mnemonic = fs.readFileSync("./mnemonic.txt").toString().trim() |
| 121 | + if (DEBUG) console.log("mnemonic", mnemonic) |
| 122 | + const seed = await bip39.mnemonicToSeed(mnemonic) |
| 123 | + if (DEBUG) console.log("seed", seed) |
| 124 | + const hdwallet = hdkey.fromMasterSeed(seed); |
| 125 | + const wallet_hdpath = "m/44'/60'/0'/0/"; |
| 126 | + const account_index = 0 |
| 127 | + let fullPath = wallet_hdpath + account_index |
| 128 | + if (DEBUG) console.log("fullPath", fullPath) |
| 129 | + const wallet = hdwallet.derivePath(fullPath).getWallet(); |
| 130 | + const privateKey = "0x" + wallet._privKey.toString('hex'); |
| 131 | + if (DEBUG) console.log("privateKey", privateKey) |
| 132 | + var EthUtil = require('ethereumjs-util'); |
| 133 | + const address = "0x" + EthUtil.privateToAddress(wallet._privKey).toString('hex') |
| 134 | + |
| 135 | + var qrcode = require('qrcode-terminal'); |
| 136 | + qrcode.generate(address); |
| 137 | + console.log("📬 Deployer Account is " + address) |
| 138 | + for (let n in config.networks) { |
| 139 | + //console.log(config.networks[n],n) |
| 140 | + try { |
| 141 | + |
| 142 | + let provider = new ethers.providers.JsonRpcProvider(config.networks[n].url) |
| 143 | + let balance = (await provider.getBalance(address)) |
| 144 | + console.log(" -- " + n + " -- -- -- 📡 ") |
| 145 | + console.log(" balance: " + ethers.utils.formatEther(balance)) |
| 146 | + console.log(" nonce: " + (await provider.getTransactionCount(address))) |
| 147 | + } catch (e) { |
| 148 | + if (DEBUG) { |
| 149 | + console.log(e) |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | +}); |
| 155 | + |
| 156 | + |
| 157 | +async function addr(ethers, addr) { |
| 158 | + if (isAddress(addr)) { |
| 159 | + return getAddress(addr); |
| 160 | + } |
| 161 | + const accounts = await ethers.provider.listAccounts(); |
| 162 | + if (accounts[addr] !== undefined) { |
| 163 | + return accounts[addr]; |
| 164 | + } |
| 165 | + throw `Could not normalize address: ${addr}`; |
| 166 | +} |
| 167 | + |
| 168 | +task("accounts", "Prints the list of accounts", async (_, { ethers }) => { |
| 169 | + const accounts = await ethers.provider.listAccounts(); |
| 170 | + accounts.forEach((account) => console.log(account)); |
| 171 | +}); |
| 172 | + |
| 173 | +task("blockNumber", "Prints the block number", async (_, { ethers }) => { |
| 174 | + const blockNumber = await ethers.provider.getBlockNumber(); |
| 175 | + console.log(blockNumber); |
| 176 | +}); |
| 177 | + |
| 178 | +task("balance", "Prints an account's balance") |
| 179 | + .addPositionalParam("account", "The account's address") |
| 180 | + .setAction(async (taskArgs, { ethers }) => { |
| 181 | + const balance = await ethers.provider.getBalance( |
| 182 | + await addr(ethers, taskArgs.account) |
| 183 | + ); |
| 184 | + console.log(formatUnits(balance, "ether"), "ETH"); |
| 185 | + }); |
| 186 | + |
| 187 | +function send(signer, txparams) { |
| 188 | + return signer.sendTransaction(txparams, (error, transactionHash) => { |
| 189 | + if (error) { |
| 190 | + debug(`Error: ${error}`); |
| 191 | + } |
| 192 | + debug(`transactionHash: ${transactionHash}`); |
| 193 | + // checkForReceipt(2, params, transactionHash, resolve) |
| 194 | + }); |
| 195 | +} |
| 196 | + |
| 197 | +task("send", "Send ETH") |
| 198 | + .addParam("from", "From address or account index") |
| 199 | + .addOptionalParam("to", "To address or account index") |
| 200 | + .addOptionalParam("amount", "Amount to send in ether") |
| 201 | + .addOptionalParam("data", "Data included in transaction") |
| 202 | + .addOptionalParam("gasPrice", "Price you are willing to pay in gwei") |
| 203 | + .addOptionalParam("gasLimit", "Limit of how much gas to spend") |
| 204 | + |
| 205 | + .setAction(async (taskArgs, { network, ethers }) => { |
| 206 | + const from = await addr(ethers, taskArgs.from); |
| 207 | + debug(`Normalized from address: ${from}`); |
| 208 | + const fromSigner = await ethers.provider.getSigner(from); |
| 209 | + |
| 210 | + let to; |
| 211 | + if (taskArgs.to) { |
| 212 | + to = await addr(ethers, taskArgs.to); |
| 213 | + debug(`Normalized to address: ${to}`); |
| 214 | + } |
| 215 | + |
| 216 | + const txRequest = { |
| 217 | + from: await fromSigner.getAddress(), |
| 218 | + to, |
| 219 | + value: parseUnits( |
| 220 | + taskArgs.amount ? taskArgs.amount : "0", |
| 221 | + "ether" |
| 222 | + ).toHexString(), |
| 223 | + nonce: await fromSigner.getTransactionCount(), |
| 224 | + gasPrice: parseUnits( |
| 225 | + taskArgs.gasPrice ? taskArgs.gasPrice : "1.001", |
| 226 | + "gwei" |
| 227 | + ).toHexString(), |
| 228 | + gasLimit: taskArgs.gasLimit ? taskArgs.gasLimit : 24000, |
| 229 | + chainId: network.config.chainId, |
| 230 | + }; |
| 231 | + |
| 232 | + if (taskArgs.data !== undefined) { |
| 233 | + txRequest.data = taskArgs.data; |
| 234 | + debug(`Adding data to payload: ${txRequest.data}`); |
| 235 | + } |
| 236 | + debug(txRequest.gasPrice / 1000000000 + " gwei"); |
| 237 | + debug(JSON.stringify(txRequest, null, 2)); |
| 238 | + |
| 239 | + return send(fromSigner, txRequest); |
| 240 | + }); |
0 commit comments