Web3.0主要可以分为几块:区块链、智能合约、密码学以及分布式存储。
区块链技术是实现Web3.0的核心前提:区块链技术是一种高级数据库机制,允许在企业网络中透明地共享信息。区块链通过链式记账法,将数据存储在链式记账中,数据库则连接到一个链条中。由于数据在链条的时间上是一致的,因此无法删除或者修改,具有一致性
Web3.py调用部署的智能合约:
完整代码如下:
from base import*
#调用deploy.py会获得contract_address
contract_address='0x5071ad6611B322647B88ACF5CBeBCA71Bead0c6f'
nonce=w3.eth.get_transaction_count(my_address)
#实例化合约对象
storage=w3.eth.contract(address=contract_address,abi=abi)
#调用addPerson方法:
transaction=storage.functions.addPerson('二两',28).buildTransaction({
"chainId":chain_id,
"from":my_address,
"nonce":nonce
})
#签名
signed_transaction=w3.eth.account.sign_transaction(transaction,private_key=private_key)
#发送交易
tx_hash=w3.eth.send_raw_transaction(signed_transaction.rawTransaction)
print('add new Person to contract...')
#等待交易完成
tx_receipt=w3.eth.wait_for_transaction_receipt(tx_hash)
#获得people数组中存储的值
result=storage.functions.people(0).call()
print(f'get person info:{result}')
因为编译后获得的智能合约的ABI中存在addPerson与people,复制compiled_code.json中abi的内容:
"abi":[
{
"inputs":[
{
"internalType":"string",
"name":"_name",
"type":"string"
},
{
"internalType":"uint256",
"name":"_age",
"type":"uint256"
}
],
"name":"addPerson",
"outputs":[],
"stateMutability":"nonpayable",
"type":"function"
},
{
"inputs":[
{
"internalType":"uint256",
"name":"",
"type":"uint256"
}
],
"name":"people",
"outputs":[
{
"internalType":"string",
"name":"name",
"type":"string"
},
{
"internalType":"uint256",
"name":"age",
"type":"uint256"
}
],
"stateMutability":"view",
"type":"function"
}
],
以addPerson函数为例,其type为function,name为addPerson,inputs表示调用该方法需传入的参数,也给出了type,通过abi,程序才知道当前的智能合约提供什么功能。