我对合约内的一个方法做了权限管理,指定地址才可以调用此方法,ethers中在创建合约是直接在最后传入钱包对象就可以了,web3.py我试了很多次问了GPT都无法解决,无论是call还是build_transaction都不行,麻烦大佬们了
contract = w3.eth.contract(address=GAME_ADDRESS, abi=GAME_ABI)
private_key = os.getenv("KEY")
caller_address = w3.to_checksum_address("0x12345678912345678912345678")
nonce = w3.eth.get_transaction_count(caller_address) gas_price = w3.eth.gas_price gas_limit = 100000 # 根据合约方法的复杂性和资源消耗进行调整 chain_id = w3.eth.chain_id
transaction = contract.functions.addBalance(user_address, amount).build_transaction({ 'chainId': chain_id, 'gas': gas_limit, 'gasPrice': gas_price, 'nonce': nonce, 'from': caller_address })
signed_transaction = w3.eth.account.sign_transaction(transaction, private_key=private_key)
transaction_hash = w3.eth.send_raw_transaction(signed_transaction.rawTransaction)
transaction_receipt = w3.eth.wait_for_transaction_receipt(transaction_hash)