centrifuge / go-substrate-rpc-client

Substrate RPC client for go aka GSRPC
Apache License 2.0
203 stars 178 forks source link

Error { cause: None, desc: "unexpected first byte decoding Option" }), desc: "Could not decode `Call::contribute.2`" }), desc: "Could not decode `Call::Crowdloan.0`" } by call Crowdloan.contribute #170

Closed ThreeAndTwo closed 10 months ago

ThreeAndTwo commented 3 years ago

I hope to participate in crowdloan through call gsrpc on v13, and then there are the following questions.

Verification Error: Runtime error: Execution failed: ApiError(FailedToConvertParameter { function: "validate_transaction", parameter: "tx", error: Error { cause: Some(Error { cause: Some(Error { cause: None, desc: "unexpected first byte decoding Option" }), desc: "Could not decodeCall::contribute.2" }), desc: "Could not decodeCall::Crowdloan.0" } }) the code as below


func Contribute(paraId, amountOut string) {
    var amount *big.Int
    var paraIndex *big.Int
    amount = utils.ParseAmount(amountOut, 12)
    paraIndex = utils.ParseAmount(paraId, 0)

    networkID := polkadotUtils.GetNetworkID()
    account := base.GetAccount()
    mnemonic := base.GetWords(account)
    keyringPair, err := polkadotUtils.GetPolkadotAccount(mnemonic, networkID)
    if err != nil {
        fmt.Println("get account err")
        return
    }

    chainInfo := polkadotUtils.GetChainInfo()
    api, err := gsrpc.NewSubstrateAPI(chainInfo.RpcAddr)

    if err != nil {
        fmt.Println(err)
        return
    }

    meta, err := api.RPC.State.GetMetadataLatest()
    if err != nil {
        fmt.Println(err)
        return
    }

    // TODO MultiSignature
    c, err := types.NewCall(meta, "Crowdloan.contribute", types.NewUCompact(paraIndex), types.NewUCompact(amount), types.NewSignature(keyringPair.PublicKey))
    if err != nil {
        fmt.Println(err)
        return
    }

    // Create the extrinsic
    ext := types.NewExtrinsic(c)
    genesisHash, err := api.RPC.Chain.GetBlockHash(0)
    if err != nil {
        fmt.Println(err)
        return
    }

    rv, err := api.RPC.State.GetRuntimeVersionLatest()
    if err != nil {
        fmt.Println(err)
        return
    }

    key, err := types.CreateStorageKey(meta, "System", "Account", keyringPair.PublicKey, nil)
    if err != nil {
        fmt.Println(err)
        return
    }

    var accountInfo types.AccountInfo
    ok, err := api.RPC.State.GetStorageLatest(key, &accountInfo)
    if err != nil || !ok {
        fmt.Println(err)
        return
    }

    nonce := uint32(accountInfo.Nonce)
    o := types.SignatureOptions{
        BlockHash:          genesisHash,
        Era:                types.ExtrinsicEra{IsMortalEra: false},
        GenesisHash:        genesisHash,
        Nonce:              types.NewUCompactFromUInt(uint64(nonce)),
        SpecVersion:        rv.SpecVersion,
        Tip:                types.NewUCompactFromUInt(0),
        TransactionVersion: rv.TransactionVersion,
    }

    err = ext.Sign(keyringPair, o)
    if err != nil {
        fmt.Println(err)
        return
    }

    // Send the extrinsic
    txHash, err := api.RPC.Author.SubmitExtrinsic(ext)
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Println("txHash: ", polkadotUtils.GetDnsForTx() + "extrinsic/" + txHash.Hex())
}

thanks for any help

vedhavyas commented 3 years ago

@ThreeAndTwo it looks like the the signature is Option instead of Signature. You would need to create a custom type for Option for it work.

here is the example for OptionBytes that you can use as base to create Option. You can also add that type to this lib through a PR if you would like. https://github.com/centrifuge/go-substrate-rpc-client/blob/master/types/option_bytes.go#L22

dprydehawking commented 3 years ago

@ThreeAndTwo it looks like the the signature is Option instead of Signature. You would need to create a custom type for Option for it work.

here is the example for OptionBytes that you can use as base to create Option. You can also add that type to this lib through a PR if you would like. https://github.com/centrifuge/go-substrate-rpc-client/blob/master/types/option_bytes.go#L22

However, option is private. I can't use it from outside. Any tips, please?

raojianli commented 2 years ago

@dprydehawking have you solved this issue? how to submit ext

cdamian commented 10 months ago

We also added a generic option some time ago - https://github.com/centrifuge/go-substrate-rpc-client/blob/master/types/generic.go#L5

Going to close this for now, please let us know if you still need help.