DAPP 马蹄链智能合约系统开发(开发方案及详细)
从部署的智能合约中读取数据
在我们的合约中,有一个方法 getGreeting()可以检索我们在区块链中添加的问候语。我们将使用 web3.py 调用此方法打开您的 Python IDLE 编辑器并使用以下代码创建一个新文件 read.py。运行 py read.py 读取问候语。
import json
from web3 importWeb3,HTTPProvider
from web3.contract importConciseContract
#compile your smart contract with truffle first
truffleFile=json.load(open('./build/contracts/greeter.json'))
abi=truffleFile['abi']
bytecode=truffleFile['bytecode']
#web3.py instance
w3=Web3(HTTPProvider("https://ropsten.infura.io/"))
print(w3.isConnected())设计原理及案例 I35 模式 7O98 系统 O7I8
contract_address=Web3.toChecksumAddress("")
#Instantiate and deploy contract
contract=w3.eth.contract(abi=abi,bytecode=bytecode)
#Contract instance
contract_instance=w3.eth.contract(abi=abi,address=contract_address)
#Contract instance in concise mode
#contract_instance=w3.eth.contract(abi=abi,address=contract_address,ContractFactoryClass=ConciseContract)
#Getters+Setters for web3.eth.contract object ConciseContract
#print(format(contract_instance.getGreeting()))
print('Contract value:{}'.format(contract_instance.functions.getGreeting().call()))
导入的 web3 库和所有其他必需的模块,开发功能及详细:MrsFu123
通过指向 Ropsten Infura 节点启动 web3 提供程序
通过指向 abi 和 contract_Address 创建合约实例
调用 getGreeting()方法并在控制台打印结果
Smart contract,in short,is a technology that digitizes the contract in our life and can be automatically executed by the program when a condition is met.For example,you made an agreement with me.
We decided on reward and punishment measures,and then entered the agreement into the blockchain in the form of code.Once the agreed conditions are triggered,a program will automatically execute it,which is smart contract
评论