0

I want to send native enjin token on the enjin relay chain from my wallet to another wallet using a js script. I am using @polkadot/api package.

import { ApiPromise, WsProvider, Keyring } from '@polkadot/api';
import "dotenv/config";

async function main() {

  const wsProvider = new WsProvider('wss://rpc.relay.blockchain.enjin.io');
  const api = await ApiPromise.create({ provider: wsProvider });
  const keyring = new Keyring({ type: 'sr25519' })

  const PHRASE = process.env.PRIVATE_KEY_RELAYCHAIN;
  const newPair = keyring.addFromUri(PHRASE);

  const unsub = await api.tx.balances
    .transfer('enCxhF3S8ANdQ7dh75yar3ZrXKd9PMDLCHJmvNPSKgm38E5nh', 1)
    .signAndSend(newPair, (result) => {
      console.log(`Current status is ${result.status}`);

      if (result.status.isInBlock) {
        console.log(`Transaction included at blockHash ${result.status.asInBlock}`);
      } else if (result.status.isFinalized) {
        console.log(`Transaction finalized at blockHash ${result.status.asFinalized}`);
        unsub();
      }
    });
}
main().catch(console.error).finally(() => process.exit());

Here is the code snippet coppied mostly from https://polkadot.js.org/docs/api/start/api.tx.subs
Can someone explain what i am doing wrong and how to go about it.

1 Answer 1

0

What is exactly the issue? What's the error? My recommendation, try running a local testnet using this zombienet:

Then try sending a token from one account to another account. Once you get that to work with the Polkadot/Rococo Relay chain then move to trying it on the Enjin Relay chain.

Make sure you have sufficient balance.

Here are some sample snippets that I have used:

// Import the API, Keyring and some utility functions
import { ApiPromise, WsProvider } from "@polkadot/api"
const wsProvider = new WsProvider("ws://127.0.0.1:9944");
const api = await ApiPromise.create({ provider: wsProvider });
import { Keyring } from '@polkadot/keyring';

export const simpleTransfer = async () => {
    const BOB = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty';

    // Construct the keyring after the API (crypto has an async init)
    const keyring = new Keyring({ type: 'sr25519' });

    // Add Alice to our keyring with a hard-derivation path (empty phrase, so uses dev)
    const alice = keyring.addFromUri('//Alice');

    // Create a extrinsic, transferring 12345 units to Bob
    const transfer = api.tx.balances.transfer(BOB, 12345);

    // Sign and send the transaction using our account
    const hash = await transfer.signAndSend(alice);

    console.log('Transfer sent with hash', hash.toHex());
}
const wsProvider = new WsProvider("wss://rococo-statemint-rpc.polkadot.io");
const api = await ApiPromise.create({ provider: wsProvider });

const keyring = new Keyring({ type: 'sr25519' });
const mnemonic = "magnet foil stay blue brown core unveil require exotic electric quick judge"
const pair = keyring.addFromUri(mnemonic, { name: 'TEST002' }, 'sr25519');
console.log(keyring.pairs.length, 'pairs available');
console.log(pair.meta.name, 'has address', pair.address);
const receiver = 'GvFpQe1ACuSsMk1jn7RsRLzhYwYt92LLieDnUr1azbFsj9Z'

export const sendAll = async () => {
    const transferExtrinsic = api.tx.balances.transfer('5C5555yEXUcmEJ5kkcCMvdZjUo7NGJiQJMS7vZXEeoMhj3VQ', 123456)
}

Hope that is a start for you to start troubleshooting.

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.