-
-
Save chevdor/aed62310878042c81f8e71f9e13e7511 to your computer and use it in GitHub Desktop.
polkadot transfer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const createApi = require('@polkadot/api').default; | |
const WsProvider = require('@polkadot/api-provider/ws').default; | |
const extrinsics = require('@polkadot/extrinsics').default; | |
const encodeExtrinsic = require('@polkadot/extrinsics/codec/encode/extrinsic').default; | |
const encodeCall = require('@polkadot/extrinsics/codec/encode/call').default; | |
const encodeLength = require('@polkadot/extrinsics/codec/encode/length').default; | |
const Keyring = require('@polkadot/util-keyring').default; | |
const storage = require('@polkadot/storage').default; | |
const BN = require('bn.js') | |
const Encoder = new TextEncoder(); | |
const keyring = new Keyring(); | |
const provider = new WsProvider('ws://127.0.0.1:9944'); | |
const api = createApi(provider); | |
async function getAccountIndex(address) { | |
return await api.state.getStorage([storage.system.public.accountIndexOf, address]) | |
} | |
async function transfer(keyRingFrom, addressTo, amount) { | |
const nextAccountIndex = (await getAccountIndex(keyRingFrom.address())) | |
// create an encoded extrinsic from the data provided | |
const extrinsic = encodeExtrinsic( | |
extrinsics.staking.public.transfer, [addressTo, amount] | |
); | |
// encode the call for signing | |
const message = encodeCall(keyRingFrom.publicKey(), nextAccountIndex, extrinsic); | |
// get the signature | |
const signature = keyRingFrom.sign(message); | |
// encode the extrinsic for submission, adding the signature (postfix) and length (prefix) | |
const encoded = encodeLength( | |
new Uint8Array([0xff]), // When working with publicKeys, this prefix is 0xff for poc-2+ | |
message, | |
signature | |
); | |
await api.author.submitExtrinsic(encoded) | |
.catch(e => { | |
console.log(e); | |
}) | |
} | |
// Alice = 5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ | |
const Alice = keyring.addFromSeed(Encoder.encode("Alice ")); | |
const addressBob = '5Gw3s7q4QLkSWwknsiPtjujPv3XM4Trxi5d4PgKMMk3gfGTE' | |
// value we wish to transfer | |
const amount = new BN(999); // on dev chain, the fee is 1, that makes it round | |
transfer(Alice, addressBob, amount).then(() => { | |
console.log('DONE'); | |
process.exit(0) | |
}).catch((e) => { | |
console.log(e); | |
process.exit(1) | |
}) |
Simplified version -
const createApi = require('@polkadot/api').default;
const WsProvider = require('@polkadot/api-provider/ws').default;
const extrinsics = require('@polkadot/extrinsics').default;
const encodeExtrinsic = require('@polkadot/extrinsics/codec/encode').default;
const encodeLength = require('@polkadot/extrinsics/codec/encode/length').default;
const Keyring = require('@polkadot/util-keyring').default;
const storage = require('@polkadot/storage').default;
const BN = require('bn.js')
const Encoder = new TextEncoder();
const keyring = new Keyring();
const provider = new WsProvider('ws://127.0.0.1:9944');
const api = createApi(provider);
async function getAccountIndex(address) {
return await api.state.getStorage([storage.system.public.accountIndexOf, address])
}
async function transfer(keyRingFrom, addressTo, amount) {
const nextAccountIndex = (await getAccountIndex(keyRingFrom.address()))
// encode the call for signing
const message = encodeExtrinsic(keyRingFrom.publicKey(), nextAccountIndex)(
extrinsics.staking.public.transfer, [addressTo, amount]
)
// get the signature
const signature = keyRingFrom.sign(message)
// encode the extrinsic for submission, adding the signature (postfix) and length (prefix)
const encoded = encodeLength(
new Uint8Array([0xff]), // When working with publicKeys, this prefix is 0xff for poc-2+
message,
signature
);
await api.author.submitExtrinsic(encoded)
.catch(e => {
console.log(e);
})
}
// Alice = 5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ
const Alice = keyring.addFromSeed(Encoder.encode("Alice "));
const Bob = '5Gw3s7q4QLkSWwknsiPtjujPv3XM4Trxi5d4PgKMMk3gfGTE';
// value we wish to transfer
const amount = new BN(999); // on dev chain, the fee is 1, that makes it round
transfer(Alice, Bob, amount).then(() => {
console.log('DONE');
process.exit(0)
}).catch((e) => {
console.log(e);
process.exit(1)
})
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
buf2hex(buffer) - there is a polkadot/util/buffer/toHex function available. The rest looks good now, although we need to simplify the boilerplate. (A lot)