Sebastian
Sebastian

Reputation: 17443

go-ethereum: failed to retrieve account 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.

Upvotes: 1

Views: 928

Answers (1)

user1676174
user1676174

Reputation:

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.

Upvotes: 1

Related Questions