英文:
go-ethereum: failed to retrieve account nonce
问题
我为我的智能合约创建了go绑定,但在执行交易时遇到了问题。只有在显式指定txOpts.Nonce
时才能正常工作(请参见已注释的行)。当我将该行注释掉时,会出现以下错误:
Failed to execute transaction: failed to retrieve account nonce: json: cannot unmarshal hex number with leading zero digits into Go value of type hexutil.Uint64`
以下是相关代码:
txOpts := bind.NewKeyedTransactor(key)
//txOpts.Nonce = big.NewInt(<nonce>)
tx, err := token.MyContract(txOpts, big.NewInt(1))
if err != nil {
log.Fatalf("Failed to execute transaction: %v", err)
}
文档中说明,当txOpts.Nonce
为nil
时,它将从txOpts.From
中检索待处理的nonce。
英文:
I create go bindings for my smart contract but have an issue when executing a transaction. It only works when I explicitly specify the txOpts.Nonce
(see commented line). When I leave the line commented I get this error:
Failed to execute transaction: failed to retrieve account nonce: json: cannot unmarshal hex number with leading zero digits into Go value of type hexutil.Uint64`
Here is the relevant code:
txOpts := bind.NewKeyedTransactor(key)
//txOpts.Nonce = big.NewInt(<nonce>)
tx, err := token.MyContract(txOpts, big.NewInt(1))
if err != nil {
log.Fatalf("Failed to execute transaction: %v", err)
}
The documentation tells it would retrieve the pending nonce from txOpts.From
when txOpts.Nonce
is nil
.
答案1
得分: 1
对于其他人对此感到困惑的人,我在使用Truffle开发框架进行测试时遇到了这个错误。对我来说,这个问题是因为Truffle JSON-RPC服务器返回的十六进制值带有前导零,例如"0x04",这违反了这里的规范:
https://github.com/ethereum/wiki/wiki/JSON-RPC#hex-value-encoding
在编码QUANTITIES(整数,数字)时:以十六进制表示,前缀为"0x",这是最紧凑的表示形式(略有例外:零应表示为"0x0")。
错误示例:0x0400(不允许有前导零)
话虽如此,对于Truffle来说,这里已经有一个拉取请求:https://github.com/trufflesuite/ganache-core/pull/32
如果您使用的是另一个JSON-RPC服务器,您需要自行验证它是否真正遵循了这个规范。
英文:
For anyone else wondering about this, I encountered this error while testing with the Truffle develop framework. For me, this problem was because the Truffle JSON-RPC server was returning hex values with leading zeros like "0x04", which violates the spec here:
https://github.com/ethereum/wiki/wiki/JSON-RPC#hex-value-encoding
> When encoding QUANTITIES (integers, numbers): encode as hex, prefix with "0x", the most compact representation (slight exception: zero should be represented as "0x0").
>
> WRONG: 0x0400 (no leading zeroes allowed)
That said, for Truffle, there's already a pull request here: https://github.com/trufflesuite/ganache-core/pull/32
If you're using another JSON-RPC server, you'd have to verify for yourself that it's actually following this spec.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论