Solidity
如何使用 web3 部署多個編譯的solidity?
所以我有這個程式碼
const MultiSigWalletFactory = fs.readFileSync(path.resolve(__dirname, "MultiSigWalletFactory.sol"), "utf8"); const Factory = fs.readFileSync(path.resolve(__dirname, "Factory.sol"), "utf8"); const MultiSigWallet = fs.readFileSync(path.resolve(__dirname, "MultiSigWallet.sol"), "utf8"); const input = { language: "Solidity", sources: { "Factory.sol": Factory, "MultiSigWallet.sol": MultiSigWallet, "MultiSigWalletFactory.sol": MultiSigWalletFactory, }, settings: { outputSelection: { '*': { '*': ['*'], }, }, }, }; const output = solc.compile(input);
它將為每個文件輸出字節碼和 ABI。
如何使用 web3 將其部署到區塊鏈?
您可以為每個合約嘗試這個(使用 web3.js v1.2.1 測試):
const fs = require("fs"); const Web3 = require("web3"); const NODE_ADDRESS = "your ethereum node URL"; const PRIVATE_KEY = "your private key"; const CONTRACT_NAME = "your contract name (not file name)"; const CONTRACT_ARGS = ["constructor argument 1", "constructor argument 2", ...]; const ARTIFACTS_DIR = "<the relative path from this script to your abi & bin folder>"; async function scan(message) { process.stdout.write(message); return await new Promise(function(resolve, reject) { process.stdin.resume(); process.stdin.once("data", function(data) { process.stdin.pause(); resolve(data.toString().trim()); }); }); } async function getGasPrice(web3) { while (true) { const nodeGasPrice = await web3.eth.getGasPrice(); const userGasPrice = await scan(`Enter gas-price or leave empty to use ${nodeGasPrice}: `); if (/^\d+$/.test(userGasPrice)) return userGasPrice; if (userGasPrice == "") return nodeGasPrice; console.log("Illegal gas-price"); } } async function getTransactionReceipt(web3) { while (true) { const hash = await scan("Enter transaction-hash or leave empty to retry: "); if (/^0x([0-9A-Fa-f]{64})$/.test(hash)) { const receipt = await web3.eth.getTransactionReceipt(hash); if (receipt) return receipt; console.log("Invalid transaction-hash"); } else if (hash) { console.log("Illegal transaction-hash"); } else { return null; } } } async function send(web3, account, transaction) { while (true) { try { const options = { data : transaction.encodeABI(), gas : await transaction.estimateGas({from: account.address}), gasPrice: await getGasPrice(web3), }; const signed = await web3.eth.accounts.signTransaction(options, account.privateKey); const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction); return receipt; } catch (error) { console.log(error.message); const receipt = await getTransactionReceipt(web3); if (receipt) return receipt; } } } async function run() { const web3 = new Web3(NODE_ADDRESS); const account = web3.eth.accounts.privateKeyToAccount(PRIVATE_KEY); const path = __dirname + "/" + ARTIFACTS_DIR + "/" + CONTRACT_NAME; const abi = fs.readFileSync(path + ".abi", {encoding: "utf8"}); const bin = fs.readFileSync(path + ".bin", {encoding: "utf8"}); const contract = new web3.eth.Contract(JSON.parse(abi)); const options = {data: "0x" + bin, arguments: CONTRACT_ARGS}; const transaction = contract.deploy(options); const receipt = await send(web3, account, transaction); console.log(CONTRACT_NAME, "deployed at", receipt.contractAddress); if (web3.currentProvider.constructor.name == "WebsocketProvider") web3.currentProvider.connection.close(); } run();