2

After transaction is send, it waits till block is included.

The following code though transaction gets successful, it doesn't return the value:

export async function transfer_balance(wsprovider, mnemonic, credit_user, value) {
    const api = await get_api(wsprovider);
    const keyring = new Keyring({ type: 'sr25519' });
    const pair = keyring.createFromUri(mnemonic);

    api.tx.balances.transfer(credit_user, value)
        .signAndSend(pair, ({ status, dispatchError }) => {
            if (dispatchError) {
                if (dispatchError.isModule) {
                    const decoded = api.registry.findMetaError(dispatchError.asModule);
                    const { docs, name, section } = decoded;
                    return `Error ${section}.${name}: ${docs.join(' ')}`;
                } else {
                    return `Error ${dispatchError.toString()}`;
                }
            } else if (status.isFinalized) {
                return `Finalized ${status.asFinalized.toString()}`;
            }
        })
}

How to get the dispatchError and asFinalized as return variable?

I am using the code in wasm.

use wasm_bindgen::prelude::*;
use js_sys::Promise;

#[wasm_bindgen(module = "/src/package.js")]
extern "C" {
    #[wasm_bindgen]
    pub fn transfer_balance(wsprovider: String, mnemonic: String, credit_user: String, value: i32) -> String;

}

I also tried promises, still it doesn't work.

export function transfer_balance(wsprovider, mnemonic, credit_user, value) {
    return new Promise((resolve, reject) => {
        const api = get_api(wsprovider);
        const keyring = new Keyring({ type: 'sr25519' });
        const pair = keyring.createFromUri(mnemonic);

        api.tx.balances.transfer(credit_user, value)
            .signAndSend(pair, ({ status, dispatchError }) => {
                if (dispatchError) {
                    if (dispatchError.isModule) {
                        const decoded = api.registry.findMetaError(dispatchError.asModule);
                        const { docs, name, section } = decoded;
                        reject(`Error ${section}.${name}: ${docs.join(' ')}`);
                    } else {
                        reject(`Error ${dispatchError.toString()}`);
                    }
                } else if (status.isFinalized) {
                    resolve(`Finalized ${status.asFinalized.toString()}`);
                }
            })
            .catch(reject);
    });
}

1 Answer 1

1

It worked with following code:

import { ApiPromise, WsProvider } from '@polkadot/api';
import { Keyring } from '@polkadot/keyring';
import { waitReady } from "@polkadot/wasm-crypto";
export async function transfer_balance(wsprovider, mnemonic, credit_user, value) {
    await waitReady();
    return new Promise((resolve, reject) => {
        const apiPromise = get_api(wsprovider);
        const keyring = new Keyring({ type: 'sr25519' });
        const pair = keyring.createFromUri(mnemonic);
         apiPromise.then((api) => {
            api.tx.balances.transfer(credit_user, value)
                .signAndSend(pair, ({ status, dispatchError }) => {
                    if (dispatchError) {
                        if (dispatchError.isModule) {
                            const decoded = api.registry.findMetaError(dispatchError.asModule);
                            const { docs, name, section } = decoded;
                            reject(`Error ${section}.${name}: ${docs.join(' ')}`);
                        } else {
                            reject(`Error ${dispatchError.toString()}`);
                        }
                    } else if (status.isFinalized) {
                        resolve(`Finalized ${status.asFinalized.toString()}`);
                    }
                })
                .catch(reject);
        }).catch(reject);
    });
}

Your Answer

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

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