我是一个使用python进行Blockchain开发的初学者。我有一个可靠的智能合同,有3个函数,第一个只能由一个ganache帐户A调用,其他函数可以由其他帐户调用(在ganache中有4个帐户: B、C、D和E)。我的问题是如何指定函数的调用方以及如何更改它?
import json
from web3 import Web3
# Set up web3 connection with Ganache
ganache_url = "http://127.0.0.1:7545"
web3 = Web3(Web3.HTTPProvider(ganache_url))
abi = json.loads('[{"constant":false,"inputs":[{"name":"_greeting","type":"string"}],"name":"setGreeting","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"greeting","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]')
bytecode = "6060604052341561000f57600080fd5b6040805190810160405280600581526020017f48656c6c6f000000000000000000000000000000000..."
发布于 2022-11-01 00:34:19
所以,首先,我相信你想要部署合同。这样做是可行的:
tx_hash = web3.eth.contract(
abi = abi, bytecode = "0x"+bytecode
).constructor().transact({'from': web3.eth.accounts[0]})
contractAddress = web3.eth.get_transaction_receipt(tx_hash)['contractAddress']
contract = web3.eth.contract(address=contractAddress, abi=abi)
注意上面的from
参数:我们可以指定以这种方式进行事务的人。web3.eth.accounts[0]
是加纳赫自动为我们创造的钱包(我相信总共有10个)。
用另一个钱包进行交易:
contract.functions.setGreeting("Hola").transact({'from': web3.eth.accounts[1]})
https://ethereum.stackexchange.com/questions/138501
复制相似问题