{"id":1808,"date":"2023-03-25T12:59:53","date_gmt":"2023-03-25T04:59:53","guid":{"rendered":""},"modified":"2023-03-25T12:59:53","modified_gmt":"2023-03-25T04:59:53","slug":"web3.js \u8c03\u7528\u667a\u80fd\u5408\u7ea6\u5199\u51fd\u6570","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1808.html","title":{"rendered":"web3.js \u8c03\u7528\u667a\u80fd\u5408\u7ea6\u5199\u51fd\u6570"},"content":{"rendered":"
\n
\u6784\u5efa\u4ea4\u6613\u5bf9\u8c61<\/span> <\/code>\n <\/div>\n <\/code>\n <\/div>\n to<\/b> \u2013 \u6b64\u53c2\u6570\u5c06\u662f\u5df2\u90e8\u7f72\u667a\u80fd\u5408\u7ea6\u7684\u5730\u5740\u3002\u53ef\u4ee5\u4eceetherscan\u4e2d\u83b7\u53d6\u3002<\/span> <\/code>\n <\/div>\n <\/code>\n <\/div>\n web3.js \u2013 \u6267\u884c\u5199\u51fd\u6570\u8c03\u7528<\/span>\n <\/div>\n <\/body>
\n \u7b7e\u7f72\u4ea4\u6613<\/span>
\n \u5e7f\u64ad\u4ea4\u6613<\/span> <\/p>\nvar <\/span>Tx = require('ethereumjs-tx'<\/span>).Transaction\r\nconst Web3 = require('web3'<\/span>)\r\nconst web3 = new <\/span>Web3('https:\/\/ropsten.infura.io\/YOUR_INFURA_API_KEY'<\/span>)\r\nconst account1 = ''<\/span><\/span> \/\/ Your account address 1\r\nconst account2 = '' \/\/ Your account address 2\r\nconst privateKey1 = Buffer.from<\/span>('YOUR_PRIVATE_KEY_1'<\/span>, 'hex'<\/span><\/span>)\r\nconst privateKey2 = Buffer.from<\/span>('YOUR_PRIVATE_KEY_2'<\/span>, 'hex')<\/pre>\n
\u6784\u5efa\u4ea4\u6613\u5bf9\u8c61<\/h2>\n
const txObject = {\r\n nonce: web3.utils.toHex<\/span>(txCount),\r\n gasLimit: web3.utils.toHex<\/span>(800000),\r\n gasPrice: web3.utils.toHex<\/span>(web3.utils.toWei<\/span>('10'<\/span>, 'gwei'<\/span>)),\r\n to: contractAddress,\r\n data: data\r\n}<\/pre>\n
\n gasLimit<\/b>\u3001
\n gasPrice<\/b>\u7684\u4f7f\u7528\uff0c\u4e0e\u524d\u9762\u7ae0\u8282\u57fa\u672c\u76f8\u540c\uff0c\u4e0d\u518d\u8d58\u8ff0\u3002\u5269\u4e0b2\u4e2a\u53c2\u6570\u8bf4\u660e\u5982\u4e0b\uff1a\n <\/div>\n
\n data<\/b> \u2013 \u88ab\u8c03\u7528\u7684\u667a\u80fd\u5408\u7ea6\u51fd\u6570\u7684\u5341\u516d\u8fdb\u5236\u8868\u793a\u3002<\/span> <\/p>\nconst contractAddress = '0x30951343d6d80d2c94897f1a81c53cc030aef879'<\/span>\r\nconst contractABI = [\r\n {\r\n \"constant\"<\/span><\/span>: false<\/span>,\r\n \"inputs\"<\/span><\/span><\/span>: [\r\n {\r\n \"internalType\"<\/span><\/span>: \"string\"<\/span><\/span><\/span><\/span>,\r\n \"name\"<\/span><\/span><\/span><\/span>: \"_value\"<\/span>,\r\n \"type\"<\/span><\/span><\/span><\/span><\/span>: \"string\"\r\n }\r\n ],\r\n \"name\": \"set\"<\/span>,\r\n \"outputs\"<\/span><\/span>: [],\r\n \"payable\"<\/span><\/span><\/span>: false<\/span>,\r\n \"stateMutability\"<\/span><\/span><\/span>: \"nonpayable\"<\/span><\/span>,\r\n \"type\": \"function\"<\/span><\/span>\r\n },\r\n {\r\n \"constant\": true<\/span><\/span>,\r\n \"inputs\": [],\r\n \"name\": \"get\"<\/span>,\r\n \"outputs\": [\r\n {\r\n \"internalType\": \"string\",\r\n \"name\": \"\"<\/span>,\r\n \"type\": \"string\"\r\n }\r\n ],\r\n \"payable\": false<\/span>,\r\n \"stateMutability\": \"view\"<\/span>,\r\n \"type\": \"function\"\r\n },\r\n {\r\n \"inputs\": [],\r\n \"payable\": false<\/span>,\r\n \"stateMutability\": \"nonpayable\",\r\n \"type\": \"constructor\"<\/span>\r\n }\r\n]\r\nconst contract = new <\/span>web3.eth.Contract<\/span>(contractABI, contractAddress)<\/pre>\n
\n data<\/b>\u53c2\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528web3.js\u51fd\u6570
\n encodeABI()<\/b>\uff0c\u628acontract\u5bf9\u8c61\u4e2d\u7684\u667a\u80fd\u5408\u7ea6\u51fd\u6570\u8f6c\u6362\u4e3a\u5341\u516d\u8fdb\u5236\u8868\u793a\u3002\u5982\u4e0b\u6240\u793a\uff1a\n <\/div>\nconst data = contract.methods.set<\/span>(\"qikegu\"<\/span>).encodeABI<\/span>()<\/pre>\n
\u7ae0\u8282<\/h2>\n
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"web3.js \u8c03\u7528\u667a\u80fd\u5408\u7ea6\u5199\u51fd\u6570zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[155],"tags":[],"class_list":["post-1808","post","type-post","status-publish","format-standard","hentry","category-web3js"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1808"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1808"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1808\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1808"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}