Skip to content

Instantly share code, notes, and snippets.

@JesseAbram
Last active April 1, 2019 18:59
Show Gist options
  • Save JesseAbram/7f3652aac7e14b2a5c931a35233fcc5e to your computer and use it in GitHub Desktop.
Save JesseAbram/7f3652aac7e14b2a5c931a35233fcc5e to your computer and use it in GitHub Desktop.
const { ApiPromise } = require("@polkadot/api");
const WsProvider = require("@polkadot/rpc-provider").WsProvider;
const { Keyring } = require("@polkadot/keyring");
const keyring = new Keyring();
const address = "5CdPxYGJvhL8EmK5vK9snmws1mjHJTQEtUAx2ihtdBne5fAF";
const sender = "5DXnxgx4EAWc3d39gZmfUEw4g8NUMdiwFvWbwYsQuH72nRFj";
const pk = keyring.addFromMnemonic(
"mneumoinc goes here"
);
const provider = new WsProvider("wss://poc3-rpc.polkadot.io/");
const main = async () => {
const api = await ApiPromise.create(provider);
const balance = await api.query.balances.freeBalance(sender);
console.log("this is your balance", balance.toString());
const nonce = await api.query.system.accountNonce(sender);
console.log("nonce", nonce);
const hash = await api.tx.balances.transfer(address, 1).signAndSend(pk);
console.log(hash.toHex())
};
main()
@jacogr
Copy link

jacogr commented Feb 20, 2019

This worked exactly as-is for me (test mnemonic for my local account)

const { ApiPromise, WsProvider } = require('@polkadot/api');
const { Keyring } = require('@polkadot/keyring');

const BOB_ADDR = '5Gw3s7q4QLkSWwknsiPtjujPv3XM4Trxi5d4PgKMMk3gfGTE';

async function main () {
  // Create an instance of the keyring
  const keyring = new Keyring();

  // Add account to our keyring (with the known seed for the account)
  const pk = keyring.addFromMnemonic('pink palm cargo village alley style like finish always reject insane pumpkin', {});

  // Instantiate the API
  const api = await ApiPromise.create(new WsProvider('ws://127.0.0.1:9944/'));

  // Create a extrinsic, transferring 12345 units to Bob
  const hash = await api.tx.balances.transfer(BOB_ADDR, 123).signAndSend(pk);

  console.log('Transfer sent with hash', hash.toHex());
}

main().catch(console.error).finally(() => process.exit());

("@polkadot/api": "^0.45.9")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment