paritytech / subxt

Interact with Substrate based nodes in Rust or WebAssembly
Other
391 stars 236 forks source link

How to transfer balance on frontier node template through subxt #1556

Closed Aideepakchaudhary closed 2 months ago

Aideepakchaudhary commented 2 months ago

I'm trying to transfer some balance on frontier-node-template through subxt. but getting this issue:

Rpc error: RPC error: ErrorObject { code: ServerError(1010), message: "Invalid Transaction", data: Some(RawValue("Inability to pay some fees (e.g. account balance too low)")) }

This is my main.rs:

use subxt::{
    PolkadotConfig,
    utils::{AccountId32, MultiAddress},
    OnlineClient,
};
// use subxt_signer::sr25519::dev::{self};
use subxt_signer::ecdsa::dev::{self};

// Generate an interface that we can use from the node's metadata.
#[subxt::subxt(runtime_metadata_path = "../../artifacts/frontier_metadata.scale")]
pub mod polkadot {}

// PolkadotConfig or SubstrateConfig will suffice for this example at the moment,
// but PolkadotConfig is a little more correct, having the right `Address` type.
// type StatemintConfig = PolkadotConfig;

#[tokio::main]
pub async fn main() {
    if let Err(err) = run().await {
        eprintln!("{err}");
    }
}

async fn run() -> Result<(), Box<dyn std::error::Error>> {
    // Create a new API client, configured to talk to Polkadot nodes.
    let api = OnlineClient::<PolkadotConfig>::new().await?;

    // Build a balance transfer extrinsic.
    let dest = dev::bob().public_key().into();
    let balance_transfer_tx = polkadot::tx().balances().transfer_allow_death(dest, 1);

    // Submit the balance transfer extrinsic from Alice, and wait for it to be successful
    // and in a finalized block. We get back the extrinsic events if all is well.
    let from = dev::alice();
    let events = api
        .tx()
        .sign_and_submit_then_watch_default(&balance_transfer_tx, &from)
        .await?
        .wait_for_finalized_success()
        .await?;

    // Find a Transfer event and print it.
    let transfer_event = events.find_first::<polkadot::balances::events::Transfer>()?;
    if let Some(event) = transfer_event {
        println!("Balance transfer success: {event:?}");
    }

    Ok(())
}

How to fix this?

niklasad1 commented 2 months ago

Hey hey,

You can't use the PolkadotConfig because frontier/ethereum is using a different AccountId.

Have a look at this example: https://gist.github.com/jsdw/13240f9341e433ea639b89d0d4235c8b#file-main-rs and replace your PolkadotConfig with EthConfig then it should work

You can use the AccountId20 from the ethereum signer in subxt IIRC.

Aideepakchaudhary commented 2 months ago

@niklasad1 thank you for the help. But If i change the PolkadotConfig with EthConfig in the current code, then it is showing this error: no EthConfig in the root

But I'm following the provided code: https://gist.github.com/jsdw/13240f9341e433ea639b89d0d4235c8b#file-main-rs. then I'm getting this issue:

balthasar pub:  043bc19e36ff1673910575b6727a974a9abd80c9a875d41ab3e2648dbfb9e4b518b483f1a67e492be9c1aa4d524d30888f79ec72ebcfc953c55c4b43ff82c9659d
balthasar addr: 3cd0a705a2dc65e5b1e1205896baa2be8a07c6e0
Error: Rpc error: RPC error: ErrorObject { code: ServerError(-32000), message: "Client error: Execution failed: Execution aborted due to trap: wasm trap: wasm `unreachable` instruction executed\nWASM backtrace:\nerror while executing at wasm backtrace:\n    0: 0x706c6b - <unknown>!rust_begin_unwind\n    1: 0x975c - <unknown>!core::panicking::panic_fmt::h578d45979beca5ff\n    2: 0x29e104 - <unknown>!AccountNonceApi_account_nonce", data: None }

Caused by:
    RPC error: ErrorObject { code: ServerError(-32000), message: "Client error: Execution failed: Execution aborted due to trap: wasm trap: wasm `unreachable` instruction executed\nWASM backtrace:\nerror while executing at wasm backtrace:\n    0: 0x706c6b - <unknown>!rust_begin_unwind\n    1: 0x975c - <unknown>!core::panicking::panic_fmt::h578d45979beca5ff\n    2: 0x29e104 - <unknown>!AccountNonceApi_account_nonce", data: None }
niklasad1 commented 2 months ago

Hey @Aideepakchaudhary again

I have created an example for this and I have tested it against https://github.com/polkadot-evm/frontier/commit/f7dc0ace3706ced905e1c754c5bd4ad8267b6c9b

It works for me and my hunch is that you are using an old frontier node without the proper runtime APIs available.

niklasad1 commented 2 months ago

We have added an example to our repo for ethereum/frontier and closing this.

Feel free to re-open if you still have issues with it.