Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

script to test polkadot.js/api functions #1336

Merged
merged 3 commits into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions tests/polkadotjs_test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "apitest",
"version": "1.0.0",
"description": "test polkadot js api stuff",
"main": "test.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@polkadot/api": "^2.10.1"
}
}
76 changes: 76 additions & 0 deletions tests/polkadotjs_test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Import
const { ApiPromise, WsProvider } = require('@polkadot/api');
const {Keyring } = require('@polkadot/keyring');

async function main() {
// Construct

const wsProvider = new WsProvider('ws://127.0.0.1:8546');
const api = await ApiPromise.create({ provider: wsProvider });

// chain defaults
const genesisHash = await api.genesisHash;
console.log(`genesis hash: ${genesisHash}`);

const runtimeMetadata = await api.runtimeMetadata;
// currently not sending runtimeMetadata to console because it's very large, uncomment if you want to see
// console.log(`runtime metadata: ${runtimeMetadata}`);

const runtimeVersion = await api.runtimeVersion;
console.log(`runtime version: ${runtimeVersion}`);

const libraryInfo = await api.libraryInfo;
console.log(`library info: ${libraryInfo}`);

//Basic queries
const now = await api.query.timestamp.now();
console.log(`timestamp now: ${now}`);

// Retrieve the account balance & nonce via the system module
const ADDR_Alice = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
const { nonce, data: balance } = await api.query.system.account(ADDR_Alice);
console.log(`Alice: balance of ${balance.free} and a nonce of ${nonce}`)

// RPC queries
const chain = await api.rpc.system.chain();
console.log(`system chain: ${chain}`);

const sysProperties = await api.rpc.system.properties();
console.log(`system properties: ${sysProperties}`);

const chainType = await api.rpc.system.chainType();
console.log(`system chainType: ${chainType}`);

const header = await api.rpc.chain.getHeader();
console.log(`header ${header}`);

// Subscribe to the new headers
// TODO: Issue: chain.subscribeNewHeads is returning values twice for each result new head.
let count = 0;
const unsubHeads = await api.rpc.chain.subscribeNewHeads((lastHeader) => {
console.log(`${chain}: last block #${lastHeader.number} has hash ${lastHeader.hash}`);
if (++count === 5) {
unsubHeads();
}
});

const blockHash = await api.rpc.chain.getBlockHash();
console.log(`current blockhash ${blockHash}`);

const block = await api.rpc.chain.getBlock(blockHash);
console.log(`current block: ${block}`);

// Simple transaction
// TODO Issue: This currently fails with error: RPC-CORE: submitExtrinsic(extrinsic: Extrinsic): Hash:: -32000: validator: (nil *modules.Extrinsic): null
const keyring = new Keyring({type: 'sr25519' });
const aliceKey = keyring.addFromUri('//Alice');
const ADDR_Bob = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty';

const transfer = await api.tx.balances.transfer(ADDR_Bob, 12345)
.signAndSend(aliceKey);

console.log(`hxHash ${transfer}`);

}

main().catch(console.error);