1

I want to automate sending astar(NOT EVM) with Python(SubstrateInterface). I received an error when executing the following code.

    # Imports
    from substrateinterface import SubstrateInterface, Keypair, KeypairType
    from substrateinterface.exceptions import SubstrateRequestException
    
    # Construct the API provider
    substrate = SubstrateInterface(
        url="wss://rpc.astar.network",
        ss58_format=42,
        type_registry_preset='substrate-node-template',
    )
    
    keypair = Keypair.create_from_mnemonic(
        "***",
        ss58_format = 42,
    )
    print(keypair.ss58_address)
    
    # Form a transaction call
    call = substrate.compose_call(
        call_module='Balances',
        call_function="transfer",
        call_params={
            "dest": "***",
            'value': 1 * 10**16,
            "gas_limit": 21000,
         }
    )
    
    # Form a signed extrinsic
    extrinsic = substrate.create_signed_extrinsic(
        call = call,
        keypair = keypair,
        nonce = substrate.get_account_nonce(keypair.ss58_address),
    )
    print(extrinsic)
    
    # Submit the extrinsic
    try:
        receipt = substrate.submit_extrinsic(
            extrinsic,
            wait_for_inclusion = True,
        )
        print("Extrinsic '{}' sent and included in block '{}'".format(receipt.extrinsic_hash, receipt.block_hash))
    except SubstrateRequestException as e:
        print("Failed to send: {}".format(e))

Failed to send: { 'code': 1002, 'message': 'Verification Error: Runtime error: Execution failed: Execution aborted due to trap: wasm trap: wasm unreachable instruction executed\nWASM backtrace:\n\n
0: 0x44fa8a - !rust_begin_unwind\n 1: 0x3922e4 - !core::panicking::panic_fmt::h32de9c76c9d5eb0c\n 2: 0x1e31a5 - !TaggedTransactionQueue_validate_transaction\n', 'data': 'RuntimeApi("Execution failed: Execution aborted due to trap: wasm trap: wasm unreachable instruction executed\nWASM backtrace:\n\n 0: 0x44fa8a - !rust_begin_unwind\n 1: 0x3922e4 - !core::panicking::panic_fmt::h32de9c76c9d5eb0c\n 2: 0x1e31a5 - !TaggedTransactionQueue_validate_transaction\n")' }

What could be the issue?

1
  • I think it's due to the issue of Astar's implementation. It's better to create the issue to let them know.
    – Jay Pan
    Sep 20 at 9:29

2 Answers 2

0

This source code works.

command

% python3 test.py
/Users/shin.takahashi/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:34: NotOpenSSLWarning: urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020
  warnings.warn(
5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY
{'account_id': '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d', 'signature': {'Sr25519': '0x183aada62201ad0a84b1c08212f64dfb266cc678d41d5f60f68cafaeb052ae3e52c57e8c590b1049e56047bfbf5d594784e27604f7b76b77fe8ec196844a2488'}, 'call_function': 'transfer', 'call_module': 'Balances', 'call_args': {'dest': 'ZfQqz2FPet1fD9yqokjiPy2Uzk3EakwQdKyBVFdfTrgVe8E', 'value': 10000000000000000}, 'nonce': 3, 'era': '00', 'tip': 0, 'asset_id': {'tip': 0, 'asset_id': None}, 'signature_version': 1, 'address': '0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d', 'call': {'call_function': 'transfer', 'call_module': 'Balances', 'call_args': {'dest': 'ZfQqz2FPet1fD9yqokjiPy2Uzk3EakwQdKyBVFdfTrgVe8E', 'value': 10000000000000000}}}
Extrinsic '0x00a6386e01e3915fa5c89f913163ef6eac360521bcb3f35568b44d55fb485215' sent and included in block '0x9ab42000c6fdcfe2512ef3fd21f227e277fe51df33684f5592d5a43cb742373e'

source code

% cat test.py


# Imports
from substrateinterface import SubstrateInterface, Keypair, KeypairType
from substrateinterface.exceptions import SubstrateRequestException

# Construct the API provider
substrate = SubstrateInterface(
    url="ws://127.0.0.1:9944",
#    ss58_format=42,
#    type_registry_preset='substrate-node-template',
)

#keypair = Keypair.create_from_mnemonic(
#    "*******",
#    ss58_format = 42,
#)
keypair = Keypair.create_from_uri('//Alice')

print(keypair.ss58_address)

# Form a transaction call
call = substrate.compose_call(
    call_module='Balances',
    call_function="transfer",
    call_params={
        "dest": "ZfQqz2FPet1fD9yqokjiPy2Uzk3EakwQdKyBVFdfTrgVe8E",
        'value': 1 * 10**16,
#        "gas_limit": 21000000000,
     }
)

# Form a signed extrinsic
extrinsic = substrate.create_signed_extrinsic(
    call = call,
    keypair = keypair,
#    nonce = substrate.get_account_nonce(keypair.ss58_address),
)
print(extrinsic)

# Submit the extrinsic
try:
    receipt = substrate.submit_extrinsic(
        extrinsic,
        wait_for_inclusion = True,
    )
    print("Extrinsic '{}' sent and included in block '{}'".format(receipt.extrinsic_hash, receipt.block_hash))
except SubstrateRequestException as e:
    print("Failed to send: {}".format(e))
1
  • 1
    Your code worked well. Furthermore, even when I changed the url to mainnet and rewrote it to generate the key pair from mnemonic, it still worked. Thanks!
    – shinji
    Sep 20 at 14:10
0

I have the same issue when using Go api

https://github.com/centrifuge/go-substrate-rpc-client

I am currently using on the latest version 4.1.0

My node version: polkadot-v0.9.43

Issue:

Verification Error: Runtime error: Execution failed: Execution aborted due to trap: wasm trap: wasm unreachable instruction executed WASM backtrace: error while executing at wasm backtrace: 0: 0x579efd - !rust_begin_unwind 1: 0xe402 - !core::panicking::panic_fmt::he4489d678d6570d5 2: 0x13b4c1 - !TaggedTransactionQueue_validate_transaction

My example code:

package main

import (
    "fmt"
    "github.com/centrifuge/go-substrate-rpc-client/v4/signature"
    "github.com/centrifuge/go-substrate-rpc-client/v4/types"
    //"github.com/centrifuge/go-substrate-rpc-client/v4/rpc/author"
    gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"


)

func main(){
    //Alice account 
    nemo := "0xe5be9a5092b81bca64be81d212e7f2f9eba183bb7a90954f7b76361f6edb5c0a";

    api, err:= gsrpc.NewSubstrateAPI("wss://shibuya-rpc.dwellir.com")
    if err != nil {
        fmt.Println("Can not connect blockchain")
    }

    fmt.Println("Connected")
    keypair, err := signature.KeyringPairFromSecret(nemo, 5)
    fmt.Println("keypair", keypair.Address, keypair.PublicKey, keypair.URI)
    if err != nil {
        fmt.Println("keypair", err)
    }
    meta, err := api.RPC.State.GetMetadataLatest()
    if err != nil {
        fmt.Println("MEta", err)
    }
    // fmt.Println("Meta:",meta)
    c, err := types.NewCall(meta, "Balances.transfer","5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y", types.NewUCompactFromUInt(10000))

    if err != nil {
        fmt.Println("New call", err)
    }

    ext := types.NewExtrinsic(c)

    era := types.ExtrinsicEra{IsImmortalEra: false}

    genesisHash ,err := api.RPC.Chain.GetBlockHash(0)

    if err != nil {
        fmt.Println("Genesis hash", err)
    }
    rv, err := api.RPC.State.GetRuntimeVersionLatest()
    if err != nil {
        fmt.Println("Runtime version", err)
    }

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

    var accountInfo types.AccountInfo
    ok, err := api.RPC.State.GetStorageLatest(key, &accountInfo)
    fmt.Println("OK", ok)
    nonce := uint32(accountInfo.Nonce)
    o := types.SignatureOptions{
        // BlockHash:   blockHash,
        BlockHash:          genesisHash, // BlockHash needs to == GenesisHash if era is immortal. // TODO: add an error?
        Era:                era,
        GenesisHash:        genesisHash,
        Nonce:              types.NewUCompactFromUInt(uint64(nonce)),
        SpecVersion:        rv.SpecVersion,
        Tip:                types.NewUCompactFromUInt(0),
        TransactionVersion: rv.TransactionVersion,
    }
    err = ext.Sign(keypair, o)
    if err != nil {
        fmt.Println("Sign", err)
    }

    txnHash, err := api.RPC.Author.SubmitAndWatchExtrinsic(ext)
    if err != nil {
        fmt.Println("submit", err)
        
    }   

    fmt.Println("after submission txnHash", txnHash)


}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.