Skip to content
Snippets Groups Projects
Commit 83a50584 authored by Bernardo Cardoso's avatar Bernardo Cardoso
Browse files

Add transactions script

parent c84762dc
No related branches found
No related tags found
No related merge requests found
File moved
File moved
......@@ -13,7 +13,7 @@ async function wait(ms) {
}
/**
* Connect to xxNetwork Public Node thorugh a websocket
* Connect to an xx network node using websocket
* @return {Promise} chain api
*/
async function connect() {
......
File moved
File moved
const { ApiPromise, WsProvider } = require('@polkadot/api');
const { Keyring } = require('@polkadot/keyring');
require('dotenv').config();
const BOB = '6YXN1Q6Lx3u8tr2WVr5myb3zNa3pVG5FL3ku8uqckR5RoA21';
/**
* Connect to an xx network node using websocket
* @return {Promise} chain api
*/
async function connect() {
const provider = new WsProvider(process.env.WS_CHAIN);
const api = await ApiPromise.create({
provider,
});
await api.isReady;
return api;
}
async function sendAndWait(extrinsic, final) {
return new Promise((reject, resolve) => {
// Sign and send the extrinsic using our wallet
extrinsic.send(({ status }) => {
if (status.isInBlock) {
console.log(`Transfer included in block ${status.asInBlock}`);
if (!final) {
resolve();
}
}
if (status.isFinalized && final) {
console.log(`Transfer finalized in block ${status.asFinalized}`);
resolve();
}
});
})
}
async function main () {
// Connect to the node
const api = await connect();
// Create a extrinsic, transferring 1000 units to Bob (with 9 decimals)
const transfer = api.tx.balances.transfer(BOB, 1000_000000000);
// Construct the keying, using ss58 format 55, which is registered for xx network
const keyring = new Keyring({ type: 'sr25519', ss58Format: 55 });
// Add Alice to our keyring with a hard-derived path (empty phrase, so uses dev account)
const alice = keyring.addFromUri('//Alice');
// Sign the transfer extrinsic
await transfer.signAsync(alice);
// Send and wait for transfer to be included in a block or finalized
await sendAndWait(transfer, true);
}
main().catch(console.error).finally(() => process.exit());
{
"name": "transactions",
"version": "1.0.0",
"description": "Transaction construction, signing and broadcast",
"main": "index.js",
"repository": "github.com/xx-labs/scripts/transactions",
"author": "xx labs",
"license": "MIT",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"@polkadot/api": "^7.15.1",
"@polkadot/keyring": "^8.7.1",
"dotenv": "^16.0.0"
}
}
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment