Skip to content

Instantly share code, notes, and snippets.

@drbh
Created February 10, 2022 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drbh/9c588858434985a1e9509e27a98e6c35 to your computer and use it in GitHub Desktop.
Save drbh/9c588858434985a1e9509e27a98e6c35 to your computer and use it in GitHub Desktop.
import { ApiPromise, Keyring, WsProvider } from "@polkadot/api";
async function main() {
const wsProvider = new WsProvider("wss://rpc.parallel.fi");
const api = await ApiPromise.create({ provider: wsProvider });
// no blockHash is specified, so we retrieve the latest
const signedBlock = await api.rpc.chain.getBlock();
// the information for each of the contained extrinsics
signedBlock.block.extrinsics.forEach((ex, index) => {
// the extrinsics are decoded by the API, human-like view
console.log(`index: ${index}`);
console.log(ex.toHuman());
});
}
// run
main()
.catch(console.error)
.finally(() => process.exit());
import { ApiPromise, Keyring, WsProvider } from "@polkadot/api";
async function main() {
const wsProvider = new WsProvider("wss://rpc.parallel.fi");
const api = await ApiPromise.create({ provider: wsProvider });
const ADDR = 'p8FhJ2kCvAtetismfbfgLBeGwBknFuqT77L9nJXdLTvct35HD';
// Retrieve the last timestamp
const now = await api.query.timestamp.now();
// Retrieve the account balance & nonce via the system module
const result: any = await api.query.system.account(ADDR);
const nonce = result.nonce;
const free = result.data.free / 1e12;
console.log(`${now}: balance of ${free} and a nonce of ${nonce}`);
}
// run
main()
.catch(console.error)
.finally(() => process.exit());
import { ApiPromise, Keyring, WsProvider } from "@polkadot/api";
async function main() {
const wsProvider = new WsProvider("wss://rpc.parallel.fi");
const api = await ApiPromise.create({ provider: wsProvider });
// Retrieve the latest header
const lastHeader = await api.rpc.chain.getHeader();
// fetch last block using the last block's hash
const block: any = await api.rpc.chain.getBlock(lastHeader.hash);
console.log(block)
}
// run
main()
.catch(console.error)
.finally(() => process.exit());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment