import Web3

from web3 import Web3

bscLink 可以去 chainList找
正式區/測試區都有連結
https://chainlist.org/zh

bscLink = 'https://data-seed-prebsc-2-s2.binance.org:8545'
bscWeb3 = Web3(Web3.HTTPProvider(bscLink))
bscWeb3.isConnected()

abi要去智能合約複製
https://testnet.bscscan.com/address/0xB1ba07780285D26d2E90E69527dE2493a84e6C3a#code

address是智能合約地址
0xB1ba07780285D26d2E90E69527dE2493a84e6C3a

abi = '[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Cleam","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"uint256","name":"time","type":"uint256"}],"name":"Clone","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Create","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"uint256","name":"time","type":"uint256"}],"name":"ParticipateNanottery","type":"event"},{"inputs":[{"internalType":"uint256","name":"robot","type":"uint256"}],"name":"calculateRobotCleam","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"cleamRobot","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_ref","type":"address"}],"name":"cloneRobot","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_ref","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"createRobot","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_invasionTime","type":"uint256"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"dispatchMarket","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"}],"name":"getCleamRobot","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"}],"name":"getLastWorking","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"}],"name":"getMyRobot","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getPoolAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"}],"name":"getReferralAllInvitee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"}],"name":"getReferralAllRebate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"}],"name":"getReferralData","outputs":[{"components":[{"internalType":"address[]","name":"invitees","type":"address[]"},{"internalType":"uint256","name":"rebates","type":"uint256"}],"internalType":"struct NanoMinions.ReferralData","name":"","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"}],"name":"getRobotSinceLastClone","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"}],"name":"getTempCleamRobot","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"}],"name":"getWorkingWorkers","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"invasionTime","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"marketRobot","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"nanotteryPrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_ticketCount","type":"uint256"}],"name":"participateNanottery","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"}],"name":"robotRewards","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_contractAddress","type":"address"}],"name":"setNanotteryContact","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_nanotteryPrice","type":"uint256"}],"name":"setNanotteryPrice","outputs":[],"stateMutability":"nonpayable","type":"function"}]'
address = "0xB1ba07780285D26d2E90E69527dE2493a84e6C3a"
contract = bscWeb3.eth.contract(address=address, abi=abi)

Web3 Contract Read

contract.functions.getMyRobot
是呼叫智能合約getMyRobot的func

 reward = contract.functions.getMyRobot('0x01010129b1FcB9b01b3f0B345681C3rA8aa0c48A5').call()
 print("reward = ", reward)

Web3 Contract Write

myAddress你的錢包地址
privateKey你的私鑰
nonce隨機數

contract.functions.createRobot
是呼叫智能合約createRobot的func
裡面有兩個參數_ref,_amount

myAddress = "0x01010129b1FcB9b01b3f0B345681C3A8aa0c48A5"
privateKey = "0x3e80675a40058c7efb369c510c197e22"
nonce = bscWeb3.eth.getTransactionCount(myAddress)
transaction = contract.functions.createRobot(_ref=myAddress, _amount=10000000000000000000).buildTransaction({"chainId": 97, "from": myAddress, "nonce": nonce})
print(transaction)
signedTxn = bscWeb3.eth.account.sign_transaction(transaction,privateKey)
txHash = bscWeb3.eth.send_raw_transaction(signedTxn.rawTransaction)
txReceipt = bscWeb3.eth.wait_for_transaction_receipt(txHash)
print(txReceipt)

完整Code

from web3 import Web3

# 連接鏈
bscLink = 'https://data-seed-prebsc-2-s2.binance.org:8545'
bscWeb3 = Web3(Web3.HTTPProvider(bscLink))
bscWeb3.isConnected()

# 連接智能合約
abi = '[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Cleam","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"uint256","name":"time","type":"uint256"}],"name":"Clone","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Create","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":true,"internalType":"uint256","name":"time","type":"uint256"}],"name":"ParticipateNanottery","type":"event"},{"inputs":[{"internalType":"uint256","name":"robot","type":"uint256"}],"name":"calculateRobotCleam","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"cleamRobot","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_ref","type":"address"}],"name":"cloneRobot","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_ref","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"createRobot","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_invasionTime","type":"uint256"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"dispatchMarket","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"}],"name":"getCleamRobot","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"}],"name":"getLastWorking","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"}],"name":"getMyRobot","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getPoolAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"}],"name":"getReferralAllInvitee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"}],"name":"getReferralAllRebate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"}],"name":"getReferralData","outputs":[{"components":[{"internalType":"address[]","name":"invitees","type":"address[]"},{"internalType":"uint256","name":"rebates","type":"uint256"}],"internalType":"struct NanoMinions.ReferralData","name":"","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"}],"name":"getRobotSinceLastClone","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"}],"name":"getTempCleamRobot","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"}],"name":"getWorkingWorkers","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"invasionTime","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"marketRobot","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"nanotteryPrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_ticketCount","type":"uint256"}],"name":"participateNanottery","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_address","type":"address"}],"name":"robotRewards","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_contractAddress","type":"address"}],"name":"setNanotteryContact","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_nanotteryPrice","type":"uint256"}],"name":"setNanotteryPrice","outputs":[],"stateMutability":"nonpayable","type":"function"}]'
address = "0xB1ba07780285D26d2E90E69527dE2493a84e6C3a"
contract = bscWeb3.eth.contract(address=address, abi=abi)

# 讀取智能合約getMyRobot func
reward = contract.functions.getMyRobot('0x01010129b1FcB9b01b3f0B345681C3rA8aa0c48A5').call()
print("reward = ", reward)

# 呼叫智能合約的參數
myAddress = "0x01010129b1FcB9b01b3f0B345681C3A8aa0c48A5"
privateKey = "0x3e80675a58c7e69c510c197e22"
nonce = bscWeb3.eth.getTransactionCount(myAddress)

# 寫入智能合約createRobot func
# 這會計算出一些Gas Fee之類的
transaction = contract.functions.createRobot(_ref=myAddress, _amount=10000000000000000000).buildTransaction({"chainId": 97, "from": myAddress, "nonce": nonce})
print(transaction)
# 拿私鑰去簽名
signedTxn = bscWeb3.eth.account.sign_transaction(transaction,privateKey)
# 簽名後送給交易
txHash = bscWeb3.eth.send_raw_transaction(signedTxn.rawTransaction)
# 等待交易回來的hash 完成
txReceipt = bscWeb3.eth.wait_for_transaction_receipt(txHash)
print(txReceipt)

Categorized in: