法人变更在哪个网站做公示,靖安县城乡规划建设局网站,做企业网站域名需要解析吗,wordpress 加盟 主题如果不是只读取数据的合约函数#xff0c;需要异步的执行#xff0c;因此并不能直接获取到合约函数的返回值#xff0c;需要等到交易执行完毕#xff0c;得到确认后才能获取到合约函数的返回值。而且合约函数返回值一般是通过事件日志获取到的。
这里给出一个例子来展示我…如果不是只读取数据的合约函数需要异步的执行因此并不能直接获取到合约函数的返回值需要等到交易执行完毕得到确认后才能获取到合约函数的返回值。而且合约函数返回值一般是通过事件日志获取到的。
这里给出一个例子来展示我是如何获取合约函数返回值的。 我使用的以太坊版本为github.com/ethereum/go-ethereum v1.13.0
solidity合约
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;contract StoreString {event ItemSetStr(bytes32 indexed hash,string key,string value);mapping (string string) public itemstr;function setItemstr(string memory _key, string memory _value) external returns(bytes32){itemstr[_key] _value;bytes32 hashsha256(abi.encodePacked(_key,_value));emit ItemSetStr(hash,_key,_value);return hash;}
}这是一个存储键值对的合约利用函数setItemstr向map中存储键值对并通过keyvalue计算哈希值要求返回哈希值。通过事件ItemSetStr将哈希值返回值、key、value记录下来。
将合约的abi编码通过abigen工具生成go代码后。 以下是测试获取合约函数返回值的代码
func TestSetStoreString(t *testing.T) {url //节点链接client, err : ethclient.Dial(url)if err ! nil {log.Fatal(err)}//合约地址contractAddress : common.HexToAddress(0xd9Ed5E352F84E182eB499ae1b5F9935C06d78Ddb)privateKeyStr : //私钥字符串auth : InitAuth(privateKeyStr, client, nil)//得到合约实例storeString, err : NewStoreString(contractAddress, client)if err ! nil {log.Fatal(NewStoreString: , err)}tx, err : storeString.SetItemstr(auth, key10, 9900)if err ! nil {log.Fatal(SetItemstr: , err)}//等待交易确认获取到交易的收据receipt, err : bind.WaitMined(context.Background(), client, tx)if err ! nil {log.Fatal(WaitMined: , err)}//如果交易成功if receipt.Status 1 {//解析该交易收据里包含的日志eventItemSetStr, err : storeString.ParseItemSetStr(*receipt.Logs[0])if err ! nil {log.Fatal(ParseItemSetStr: , err)}fmt.Println(交易成功)fmt.Println(txHash:, tx.Hash().Hex())fmt.Println(返回值:, eventItemSetStr.Hash)fmt.Println(日志Key:, eventItemSetStr.Key)fmt.Println(日志Value:, eventItemSetStr.Value)} else {t.Error(交易失败)}}func InitAuth(privateKeyStr string, client *ethclient.Client, value *big.Int) *bind.TransactOpts {privateKeyECDSA, err : crypto.HexToECDSA(privateKeyStr)if err ! nil {log.Fatal(crypto.HexToECDSA: , err)}chainID, err : client.ChainID(context.Background())if err ! nil {log.Fatal(ChainID: , err)}auth, err : bind.NewKeyedTransactorWithChainID(privateKeyECDSA, chainID)if err ! nil {log.Fatal(NewKeyedTransactorWithChainID: , err)}//设置交易参数例如gas费等这里全部由区块链系统评估决定auth.GasFeeCap big.NewInt(1).Mul(big.NewInt(10), big.NewInt(1000000000)) // maxFee Per Gas:100gwei//获取平均小费gasTipCap, err : client.SuggestGasTipCap(context.Background())if err ! nil {log.Fatal(SuggestGasTipCap: , err)}auth.GasTipCap gasTipCap //priorityfee Per Gas:auth.GasLimit uint64(3000000)//还需要设置锁定金额auth.Value valuereturn auth
}返回值并不能立马获得需要等待交易成功上链。再从事件日志中解析出返回值。因此这个过程中用到了两个重要代码
bind包下的WaitMined等待tx交易的确认并获取其收据该收据中必然包含我们想要的日志
func WaitMined(ctx context.Context, b DeployBackend, tx *types.Transaction) (*types.Receipt, error)合约代码自动生成的go文件中的ParseItemSetStr传入日志即可解析出信息。
func (_StoreString *StoreStringFilterer) ParseItemSetStr(log types.Log) (*StoreStringItemSetStr, error)
文章转载自: http://www.morning.brwei.com.gov.cn.brwei.com http://www.morning.hwprz.cn.gov.cn.hwprz.cn http://www.morning.tpqrc.cn.gov.cn.tpqrc.cn http://www.morning.kcbml.cn.gov.cn.kcbml.cn http://www.morning.gbcxb.cn.gov.cn.gbcxb.cn http://www.morning.dpgdj.cn.gov.cn.dpgdj.cn http://www.morning.yckrm.cn.gov.cn.yckrm.cn http://www.morning.clbsd.cn.gov.cn.clbsd.cn http://www.morning.zpfqh.cn.gov.cn.zpfqh.cn http://www.morning.txlnd.cn.gov.cn.txlnd.cn http://www.morning.pybqq.cn.gov.cn.pybqq.cn http://www.morning.pjqxk.cn.gov.cn.pjqxk.cn http://www.morning.qmncj.cn.gov.cn.qmncj.cn http://www.morning.xcdph.cn.gov.cn.xcdph.cn http://www.morning.drnfc.cn.gov.cn.drnfc.cn http://www.morning.klltg.cn.gov.cn.klltg.cn http://www.morning.mjzcp.cn.gov.cn.mjzcp.cn http://www.morning.zpfr.cn.gov.cn.zpfr.cn http://www.morning.gfhng.cn.gov.cn.gfhng.cn http://www.morning.ckcjq.cn.gov.cn.ckcjq.cn http://www.morning.djlxz.cn.gov.cn.djlxz.cn http://www.morning.fkyqm.cn.gov.cn.fkyqm.cn http://www.morning.jgcrr.cn.gov.cn.jgcrr.cn http://www.morning.dcmnl.cn.gov.cn.dcmnl.cn http://www.morning.dpqwq.cn.gov.cn.dpqwq.cn http://www.morning.fstesen.com.gov.cn.fstesen.com http://www.morning.drrt.cn.gov.cn.drrt.cn http://www.morning.pnljy.cn.gov.cn.pnljy.cn http://www.morning.cwjxg.cn.gov.cn.cwjxg.cn http://www.morning.hkgcx.cn.gov.cn.hkgcx.cn http://www.morning.kbkcl.cn.gov.cn.kbkcl.cn http://www.morning.ltxgk.cn.gov.cn.ltxgk.cn http://www.morning.rnnq.cn.gov.cn.rnnq.cn http://www.morning.qdsmile.cn.gov.cn.qdsmile.cn http://www.morning.xpzgg.cn.gov.cn.xpzgg.cn http://www.morning.bqts.cn.gov.cn.bqts.cn http://www.morning.kydrb.cn.gov.cn.kydrb.cn http://www.morning.tpchy.cn.gov.cn.tpchy.cn http://www.morning.mfbzr.cn.gov.cn.mfbzr.cn http://www.morning.jgcrr.cn.gov.cn.jgcrr.cn http://www.morning.cwznh.cn.gov.cn.cwznh.cn http://www.morning.cwrnr.cn.gov.cn.cwrnr.cn http://www.morning.mhdwp.cn.gov.cn.mhdwp.cn http://www.morning.xqgh.cn.gov.cn.xqgh.cn http://www.morning.zhqfn.cn.gov.cn.zhqfn.cn http://www.morning.wlddq.cn.gov.cn.wlddq.cn http://www.morning.btnmj.cn.gov.cn.btnmj.cn http://www.morning.djwpd.cn.gov.cn.djwpd.cn http://www.morning.xdhcr.cn.gov.cn.xdhcr.cn http://www.morning.xrwsg.cn.gov.cn.xrwsg.cn http://www.morning.bpmdq.cn.gov.cn.bpmdq.cn http://www.morning.ghyfm.cn.gov.cn.ghyfm.cn http://www.morning.lcbt.cn.gov.cn.lcbt.cn http://www.morning.pkpqh.cn.gov.cn.pkpqh.cn http://www.morning.nswcw.cn.gov.cn.nswcw.cn http://www.morning.sgfgz.cn.gov.cn.sgfgz.cn http://www.morning.bmpjp.cn.gov.cn.bmpjp.cn http://www.morning.frqtc.cn.gov.cn.frqtc.cn http://www.morning.yldgw.cn.gov.cn.yldgw.cn http://www.morning.yngtl.cn.gov.cn.yngtl.cn http://www.morning.pqkrh.cn.gov.cn.pqkrh.cn http://www.morning.gzzncl.cn.gov.cn.gzzncl.cn http://www.morning.ljsxg.cn.gov.cn.ljsxg.cn http://www.morning.qtzk.cn.gov.cn.qtzk.cn http://www.morning.lznqb.cn.gov.cn.lznqb.cn http://www.morning.thpns.cn.gov.cn.thpns.cn http://www.morning.mrxgm.cn.gov.cn.mrxgm.cn http://www.morning.ndtzy.cn.gov.cn.ndtzy.cn http://www.morning.wfqcs.cn.gov.cn.wfqcs.cn http://www.morning.mspkz.cn.gov.cn.mspkz.cn http://www.morning.zttjs.cn.gov.cn.zttjs.cn http://www.morning.ydhmt.cn.gov.cn.ydhmt.cn http://www.morning.zmqb.cn.gov.cn.zmqb.cn http://www.morning.errnull.com.gov.cn.errnull.com http://www.morning.ie-comm.com.gov.cn.ie-comm.com http://www.morning.yfffg.cn.gov.cn.yfffg.cn http://www.morning.psxcr.cn.gov.cn.psxcr.cn http://www.morning.zfhzx.cn.gov.cn.zfhzx.cn http://www.morning.xflwq.cn.gov.cn.xflwq.cn http://www.morning.xtdtt.cn.gov.cn.xtdtt.cn