Copyright © 2022-2024 aizws.net · 网站版本: v1.2.6·内部版本: v1.23.3·
页面加载耗时 0.00 毫秒·物理内存 63.6MB ·虚拟内存 1300.0MB
欢迎来到 AI 中文社区(简称 AI 中文社),这里是学习交流 AI 人工智能技术的中文社区。 为了更好的体验,本站推荐使用 Chrome 浏览器。
执行部署,需要签署交易、广播交易,就像前面章节中一样。
完整代码如下所示:
var Tx = require('ethereumjs-tx').Transaction const Web3 = require('web3') const web3 = new Web3('https://ropsten.infura.io/v3/YOUR_INFURA_API_KEY') const account1 = '0xf4Ab5314ee8d7AA0eB00b366c52cEEccC62d6B4B' // Your account address 1 const account2 = '0xff96B8B43ECd6C49805747F94747bFfa3A960b69' // Your account address 2 const pk1 = 'b75e2bcaec74857cf9bb6636d66a04784d4c0fcfd908f4a2bc213428eba5af0d' // 实际项目中应该从process.env.PRIVATE_KEY_1中读取 const pk2 = 'ac0adfdbaeb0770a479e79aac78779d82fdc2f9262e0c8f751ae70fb63ef6196' // 实际项目中应该从process.env.PRIVATE_KEY_2中读取 const privateKey1 = Buffer.from(pk1, 'hex') const privateKey2 = Buffer.from(pk2, 'hex') web3.eth.getTransactionCount(account1, (err, txCount) => { const data = "0x608060405234801561001057600080fd5b506040518060400160405280600781526020017f6d7956616c7565000000000000000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b61030f806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80634ed3885e1461003b5780636d4ce63c146100f6575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610179565b005b6100fe610193565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b806000908051906020019061018f929190610235565b5050565b606060008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561022b5780601f106102005761010080835404028352916020019161022b565b820191906000526020600020905b81548152906001019060200180831161020e57829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea265627a7a72315820f5df86185a4d1d6fa207ed96829d0ff774b214b849e6af605bfc345c5f769ae864736f6c634300050b0032" // 创建交易对象 const txObject = { nonce: web3.utils.toHex(txCount), gasLimit: web3.utils.toHex(1000000), gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')), data: data } // 签署交易 const tx = new Tx(txObject, { chain: 'ropsten', hardfork: 'petersburg' }) tx.sign(privateKey1) const serializedTx = tx.serialize() const raw = '0x' + serializedTx.toString('hex') // 广播交易 web3.eth.sendSignedTransaction(raw, (err, txHash) => { console.log('txHash:', txHash) // 可以去ropsten.etherscan.io查看交易详情 }) })
运行app.js:
$ node app
输出
txHash: 0xc6b30a882cd6998326b7b7e30b6e64bc87887608e39602a38713f7ebd282a934
可以去etherscan,查看交易详情。
调用智能合约写函数,会改变区块链状态,需要消耗Gas,被视为一种交易操作。本章将使用web3.js – 部署智能合约 章节中部署好的合约,调用此合约的set()函数。调用智能合约写函数的执行步骤与前面章节 ...