/web3js@4-dapp-backend.js Secret
Last active
June 20, 2023 19:19
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Web3 from 'web3' // import web3 | |
import fs from 'fs' // fs for saving file | |
import dotenv from 'dotenv' // parse envs | |
// bytecodes and abi artifacts | |
const abi = [...] as const | |
const GOLD = '0x...'; | |
const SILVER = '0x...'; | |
const USDT = '0x...' | |
const USDC = '0x...' | |
const SOME = '0x...' | |
const bytecodes = [GOLD, SILVER, USDT, USDC, SOME] | |
dotenv.config() | |
const init = async () => { | |
const web3 = new Web3(process.env.REACT_APP_PROVIDER) | |
// make account from private key | |
const acc = web3.eth.accounts.privateKeyToAccount(String(process.env.REACT_APP_MAIN_PK)) | |
const myAcc = web3.eth.accounts.privateKeyToAccount(String(process.env.REACT_APP_PK)) | |
web3.eth.accounts.wallet.add(acc); // add account to a wallet | |
const contractAddresses = [] | |
const contract = new web3.eth.Contract(abi) | |
// generate ERC20 tokens | |
for (const bytecode of bytecodes) { | |
const transaction = contract.deploy({ | |
data: bytecode, | |
arguments: ['1000000000000000000000'], | |
}) | |
const gas = await transaction.estimateGas() | |
const deployed = await transaction.send({from: acc.address, gas: gas.toString()}) | |
contractAddresses.push(deployed.options.address) | |
// add some tokens to our account | |
await deployed.methods.transfer(myAcc.address, String(web3.utils.toWei(Math.random() * 1000, 'ether'))).send({ | |
from: acc.address, | |
gas: '2000000' | |
}) | |
} | |
// save results | |
const fileName = './src/web3/addresses.json' | |
if (fs.existsSync(fileName)) { | |
fs.unlinkSync(fileName) | |
} | |
fs.writeFileSync(fileName, JSON.stringify(contractAddresses)) | |
} | |
init().catch(console.error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment