1

I'm trying to write a script in Go to interact with a smart contract on Binance Smart Chain using an RPC and the github.com/ethereum/go-ethereum package. However, I keep getting "panic: hex string of odd length" when trying to call a method involving a bind.TransactOpts struct. Here's the relevant code:

secret_key := "<my_secret_key>"
sk, _ := crypto.HexToECDSA(secret_key)

wallet_addr := crypto.PubkeyToAddress(sk.PublicKey)

auth, _ := bind.NewKeyedTransactorWithChainID(sk, big.NewInt(56))

...

tx := &bind.TransactOpts{
        From:      wallet_addr,
        Nonce:     nil,
        Signer:    auth.Signer,
        Value:     min_bet,
        GasPrice:  nil,
        GasFeeCap: nil,
        GasTipCap: nil,
    }
    
attempt, err := instance.BetBull(tx, epoch)
if err != nil {
        panic(err)
    }
fmt.Println(attempt)

The problem seems to me to be with the Signer field of tx. When I run fmt.Println(auth.Signer) the output is 0x4341680, which has 9 characters. I'm new to coding and just doing this for fun, so it's likely that I'm missing something obvious, but I can't figure out how to fix this. Any help would be appreciated. Thanks.

what_4
  • 19
  • 2
  • It looks like you have a hexadecimal representation of a number instead of a sequence of bytes encoded to hex. The decoder is looking for the latter. Where did the data come from? What is decoding it? –  Feb 26 '22 at 01:46
  • @Zombo the auth.Signer is a github.com/ethereum/go-ethereum/accounts/abi/bind SignerFn. i'm not sure what's decoding it. i am assuming, perhaps incorrectly, that the hex value is the memory address for the signer function. i tried using a sequence of bytes encoded to a hex "0x04341680" but it won't compile because the field has to be of type bind.SignerFn. i'm not sure what's decoding it - the documentation is pretty complicated, but hopefully i'll figure it out. in any case, thank you. – what_4 Feb 26 '22 at 02:42
  • 1
    What line is panicing? Include the error message verbatim. – erik258 Feb 26 '22 at 04:08

0 Answers0