{"id":2269,"date":"2025-06-01T13:40:09","date_gmt":"2025-06-04T06:02:09","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=2269"},"modified":"2025-06-04T13:40:09","modified_gmt":"2025-06-04T06:02:09","slug":"python-web3%e5%91%bc%e5%8f%abcontract-%e6%99%ba%e8%83%bd%e5%90%88%e7%b4%84-abi%e5%91%bc%e5%8f%ab-%e5%af%ab%e5%85%a5-abi-read-write","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/uncategorized\/python-web3%e5%91%bc%e5%8f%abcontract-%e6%99%ba%e8%83%bd%e5%90%88%e7%b4%84-abi%e5%91%bc%e5%8f%ab-%e5%af%ab%e5%85%a5-abi-read-write\/","title":{"rendered":"\u6df1\u5165\u6dfa\u51fa Python Web3\uff1a\u667a\u80fd\u5408\u7d04 ABI \u7684\u8b80\u53d6\u8207\u5beb\u5165\u5be6\u4f5c"},"content":{"rendered":"<style>\n    .text {\n        font-size: 20px;\n    }\n    .a > img {\n        max-width: 100%;\n        max-height: 60vh;\n        object-fit: contain;\n        object-position: center;\n    }\n<\/style>\n<p>\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528 Python \u7684 Web3 \u5eab\u4f86\u8207\u667a\u80fd\u5408\u7d04\u9032\u884c\u4e92\u52d5\u3002\u6211\u5011\u5c07\u6db5\u84cb\u5982\u4f55\u8b80\u53d6\u548c\u5beb\u5165\u667a\u80fd\u5408\u7d04\u7684 ABI\uff08\u61c9\u7528\u7a0b\u5f0f\u4e8c\u9032\u4f4d\u4ecb\u9762\uff09\uff0c\u4e26\u63d0\u4f9b\u5be6\u969b\u7684\u7a0b\u5f0f\u78bc\u7bc4\u4f8b\uff0c\u5e6b\u52a9\u4f60\u5feb\u901f\u4e0a\u624b\u3002<\/p>\n<p>\u9996\u5148\uff0c\u78ba\u4fdd\u4f60\u5df2\u7d93\u5b89\u88dd\u4e86 `web3` \u5eab\u3002\u5982\u679c\u5c1a\u672a\u5b89\u88dd\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<p>&#8220;`bash<br \/>\npip install web3<br \/>\n&#8220;`<\/p>\n<p>\u63a5\u4e0b\u4f86\uff0c\u8b93\u6211\u5011\u958b\u59cb\u8207 Binance Smart Chain\uff08BSC\uff09\u9032\u884c\u9023\u63a5\u3002<\/p>\n<p>&#8220;`python<br \/>\nfrom web3 import Web3<\/p>\n<p># \u9023\u63a5\u5230 BSC \u6e2c\u8a66\u7db2<br \/>\nbscLink = &#8216;https:\/\/data-seed-prebsc-2-s2.binance.org:8545&#8217;<br \/>\nbscWeb3 = Web3(Web3.HTTPProvider(bscLink))<\/p>\n<p># \u78ba\u8a8d\u662f\u5426\u6210\u529f\u9023\u63a5<br \/>\nif bscWeb3.isConnected():<br \/>\n    print(&#8220;\u6210\u529f\u9023\u63a5\u5230 BSC \u6e2c\u8a66\u7db2&#8221;)<br \/>\nelse:<br \/>\n    print(&#8220;\u7121\u6cd5\u9023\u63a5\u5230 BSC \u6e2c\u8a66\u7db2&#8221;)<br \/>\n&#8220;`<\/p>\n<p>\u4f60\u53ef\u4ee5\u5728 [Chainlist](https:\/\/chainlist.org\/zh) \u4e0a\u627e\u5230\u5404\u7a2e\u5340\u584a\u93c8\u7684\u9023\u63a5\u8cc7\u8a0a\u3002<\/p>\n<div class=\"a\">\n    <img decoding=\"async\" alt=\"Chainlist\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/08\/wp_editor_md_c8438922e6b7822ca82113b102f5e618.jpg\" title=\"Chainlist\">\n<\/div>\n<p>\u63a5\u4e0b\u4f86\uff0c\u6211\u5011\u9700\u8981\u667a\u80fd\u5408\u7d04\u7684 ABI \u548c\u5730\u5740\u3002\u9019\u88e1\u6211\u5011\u4ee5\u4e00\u500b\u793a\u7bc4\u5408\u7d04\u70ba\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\naddress = &#8220;0xB1ba07780285D26d2E90E69527dE2493a84e6C3a&#8221;<br \/>\nabi = &#8216;[{&#8220;inputs&#8221;:[],&#8221;stateMutability&#8221;:&#8221;nonpayable&#8221;,&#8221;type&#8221;:&#8221;constructor&#8221;}, &#8230; ]&#8217;  # \u78ba\u4fdd\u66ff\u63db\u70ba\u5be6\u969b\u7684 ABI<br \/>\ncontract = bscWeb3.eth.contract(address=address, abi=abi)<br \/>\n&#8220;`<\/p>\n<p>### Web3 \u5408\u7d04\u8b80\u53d6<\/p>\n<p>\u6211\u5011\u53ef\u4ee5\u900f\u904e\u5408\u7d04\u7684\u51fd\u6578\u4f86\u8b80\u53d6\u8cc7\u6599\u3002\u4f8b\u5982\uff0c\u547c\u53eb `getMyRobot` \u51fd\u6578\uff1a<\/p>\n<p>&#8220;`python<br \/>\nreward = contract.functions.getMyRobot(&#8216;0x01010129b1FcB9b01b3f0B345681C3rA8aa0c48A5&#8217;).call()<br \/>\nprint(&#8220;reward = &#8220;, reward)<br \/>\n&#8220;`<\/p>\n<div class=\"a\">\n    <img decoding=\"async\" alt=\"\u5408\u7d04\u8b80\u53d6\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/08\/wp_editor_md_228b11aff4b982afae5019671dbf17e8.jpg\" title=\"\u5408\u7d04\u8b80\u53d6\">\n<\/div>\n<p>### Web3 \u5408\u7d04\u5beb\u5165<\/p>\n<p>\u82e5\u8981\u5beb\u5165\u8cc7\u6599\u5230\u667a\u80fd\u5408\u7d04\uff0c\u6211\u5011\u9700\u8981\u63d0\u4f9b\u4e00\u4e9b\u984d\u5916\u7684\u8cc7\u8a0a\uff0c\u5982\u4f60\u7684\u5730\u5740\u3001\u79c1\u9470\u4ee5\u53ca nonce\u3002\u9019\u88e1\u6211\u5011\u5c07\u547c\u53eb `createRobot` \u51fd\u6578\uff1a<\/p>\n<p>&#8220;`python<br \/>\nmyAddress = &#8220;0x01010129b1FcB9b01b3f0B345681C3A8aa0c48A5&#8221;<br \/>\nprivateKey = &#8220;0x3e80675a40058c7efb369c510c197e22&#8221;<br \/>\nnonce = bscWeb3.eth.getTransactionCount(myAddress)<\/p>\n<p># \u5efa\u7acb\u4ea4\u6613<br \/>\ntransaction = contract.functions.createRobot(_ref=myAddress, _amount=10000000000000000000).buildTransaction({<br \/>\n    &#8220;chainId&#8221;: 97,<br \/>\n    &#8220;from&#8221;: myAddress,<br \/>\n    &#8220;nonce&#8221;: nonce<br \/>\n})<\/p>\n<p># \u7c3d\u540d\u4ea4\u6613<br \/>\nsignedTxn = bscWeb3.eth.account.sign_transaction(transaction, privateKey)<br \/>\n# \u767c\u9001\u7c3d\u540d\u4ea4\u6613<br \/>\ntxHash = bscWeb3.eth.send_raw_transaction(signedTxn.rawTransaction)<br \/>\n# \u7b49\u5f85\u4ea4\u6613\u56de\u57f7<br \/>\ntxReceipt = bscWeb3.eth.wait_for_transaction_receipt(txHash)<br \/>\nprint(txReceipt)<br \/>\n&#8220;`<\/p>\n<div class=\"a\">\n    <img decoding=\"async\" alt=\"\u5408\u7d04\u5beb\u5165\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/08\/wp_editor_md_69f3e3692c5021905c80451d05704861.jpg\" title=\"\u5408\u7d04\u5beb\u5165\">\n<\/div>\n<p>### \u5b8c\u6574\u7bc4\u4f8b\u7a0b\u5f0f\u78bc<\/p>\n<p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u7a0b\u5f0f\u78bc\u7bc4\u4f8b\uff0c\u4f9b\u4f60\u53c3\u8003\uff1a<\/p>\n<p>&#8220;`python<br \/>\nfrom web3 import Web3<\/p>\n<p># \u9023\u63a5\u93c8<br \/>\nbscLink = &#8216;https:\/\/data-seed-prebsc-2-s2.binance.org:8545&#8217;<br \/>\nbscWeb3 = Web3(Web3.HTTPProvider(bscLink))<\/p>\n<p># \u9023\u63a5\u667a\u80fd\u5408\u7d04<br \/>\nabi = &#8216;[{&#8220;inputs&#8221;:[],&#8221;stateMutability&#8221;:&#8221;nonpayable&#8221;,&#8221;type&#8221;:&#8221;constructor&#8221;}, &#8230; ]&#8217;  # \u78ba\u4fdd\u66ff\u63db\u70ba\u5be6\u969b\u7684 ABI<br \/>\naddress = &#8220;0xB1ba07780285D26d2E90E69527dE2493a84e6C3a&#8221;<br \/>\ncontract = bscWeb3.eth.contract(address=address, abi=abi)<\/p>\n<p># \u8b80\u53d6\u667a\u80fd\u5408\u7d04<br \/>\nreward = contract.functions.getMyRobot(&#8216;0x01010129b1FcB9b01b3f0B345681C3rA8aa0c48A5&#8217;).call()<br \/>\nprint(&#8220;reward = &#8220;, reward)<\/p>\n<p># \u5beb\u5165\u667a\u80fd\u5408\u7d04<br \/>\nmyAddress = &#8220;0x01010129b1FcB9b01b3f0B345681C3A8aa0c48A5&#8221;<br \/>\nprivateKey = &#8220;0x3e80675a40058c7efb369c510c197e22&#8221;<br \/>\nnonce = bscWeb3.eth.getTransactionCount(myAddress)<\/p>\n<p>transaction = contract.functions.createRobot(_ref=myAddress, _amount=10000000000000000000).buildTransaction({<br \/>\n    &#8220;chainId&#8221;: 97,<br \/>\n    &#8220;from&#8221;: myAddress,<br \/>\n    &#8220;nonce&#8221;: nonce<br \/>\n})<\/p>\n<p>signedTxn = bscWeb3.eth.account.sign_transaction(transaction, privateKey)<br \/>\ntxHash = bscWeb3.eth.send_raw_transaction(signedTxn.rawTransaction)<br \/>\ntxReceipt = bscWeb3.eth.wait_for_transaction_receipt(txHash)<br \/>\nprint(txReceipt)<br \/>\n&#8220;`<\/p>\n<p>\u9019\u7bc7\u6587\u7ae0\u63d0\u4f9b\u4e86\u4e00\u500b\u7c21\u55ae\u7684\u5165\u9580\u6307\u5357\uff0c\u5e6b\u52a9\u4f60\u5feb\u901f\u4e0a\u624b Python \u8207 Web3 \u7684\u7d50\u5408\u3002\u5e0c\u671b\u5c0d\u4f60\u6709\u6240\u5e6b\u52a9\uff0c\u6b61\u8fce\u5728\u4e0b\u65b9\u7559\u8a00\u5206\u4eab\u4f60\u7684\u60f3\u6cd5\u6216\u4efb\u4f55\u554f\u984c\uff01<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u9019\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u5011\u5c07\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528 Python \u7684&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-2269","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-AB","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=2269"}],"version-history":[{"count":8,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2269\/revisions"}],"predecessor-version":[{"id":2408,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/2269\/revisions\/2408"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=2269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=2269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=2269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}