Published: 28 Feb 2022 › Updated: 28 Feb 2022
合约的执行结果和历史的数据结构 / 学习智能合约#57
在执行智能合约时如何确定合约的正确执行结果呢?在得到正确的结果后再执行一系列的操作。比如现实中,只有你已确定付款后才能拿走商品一样,智能合约只能得到正确的结果才能进行下一步。
要起得到确定的结果,一般有两种办法:一种是轮询事件,另一种是直接分析得到的结果 。例如:let res = await instance.methods.transfer(to, amount), 第一种:可以轮询事件,直到查到想要的事件为目,事件(history)数据结构如下:
address: '0x4369bAD2aBb80a234f38xxxxxx',
blockNumber: 25102441,
transactionHash: '0x10468cdc4dfbea5f03bd26f4b6ecxxxx',
transactionIndex: 42,
blockHash: '0x5a7690d19e935cdfdf72axxx',
logIndex: 200,
removed: false,
id: 'log_d9709891',
returnValues: Result {
'0': '0x4870fd1f34xxxxxxxxxxxxxx',
'1': '0x4870fd1f34EdD28800D92c3A3F7Eb179095284aB',
'2': '11500000000000000000',
from: '0x4870fd1f34xxxxxxxxxxxxxx',
to: '0x4870fd1f34EdD28800D9xxxxxxx',
amount: '11500000000000000000'
},
event: 'Transfer',
signature: '0x417646ecea3e1b1950ce7xxxxx',
raw: {
data: '0x00000000000xxxxxx',
topics: [Array]
}
},
通过检查returnValues中的数据可以确保是否得到正确的结果。
另一种是另一种是直接分析得到的结果,从res中分析数据,如下所示:
blockHash: "0xaa02b1e9ea4fd46c1c5039a59820cxxxx"
blockNumber: 25109634
contractAddress: null
cumulativeGasUsed: 7898685
effectiveGasPrice: 35108799999
events: {0: {…}, Transfer: {…}}
from: "0x4870fd1f34edd28800d92c3xxxx"
gasUsed: 36598
logsBloom: "0x000000000001100220"
status: true
to: "0x4369bad2abb80a234f38757xxxx"
transactionHash: "0xb70ca889e155a4ee21b754d0fxxxxx"
transactionIndex: 71
type: "0x2"
[[Prototype]]: Object
可以从status= true来判断是否得到正确结果。这种方法也都是ok的。
至于采用哪一种方法,要看应用场景的需求,灵活应用。
Leave 合约的执行结果和历史的数据结构 / 学习智能合约#57 to:
Read more #smartcontract posts
Best Posts From lemooljiang
We have not curated any of lemooljiang's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From lemooljiang
- Nextjs初体验,和Nuxt的异同
- DeepTutor:AI个性化辅导平台 / ai #57
- ComfyUI:可编程和自动化的PS / ai #56
- 将Neo4j封装进Agent / ai #55
- 闲鱼省钱大法:极限0.5折!
- 对Neo4j批量导入结构化数据 / ai #54
- langchain对neo4j进行交互,写入和查询数据 / ai #53
- 知识图谱和图数据库Neo4j的作用和使用场景 / ai #52
- SpaceX、美股和比特币
- 又是币圈血洗日!
- 链股合流,当币安也开始卖股票啰
- 天涯社区重启,爷青回?
- 诺基亚在AI时代重新杀回来啦
- 《2049:未来10000天的可能》书中的镜像世界为什么不叫元宇宙
- 今天适合吃披萨
- 开通即被封,别再给Claude送人头了!
- 两根内存条引发的“官司”
- AI·Joe V12更新, 更新GPT-5.5和DeepSeek V4 等
- 中医进校园活动反响热烈
- 猪价低伤农,猪价贵伤民