-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestTrans.js
53 lines (37 loc) · 1.58 KB
/
testTrans.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Import
const { ApiPromise, WsProvider, Keyring } = require('@polkadot/api');
const ADDR = '5DTestUPts3kjeXSTMyerHihn1uwMfLj8vU8sqF7qYrFabHE';
async function main() {
// Construct
const wsProvider = new WsProvider('ws://127.0.0.1:9944');
//const wsProvider = new WsProvider('ws://127.0.0.1:8546');
const api = await ApiPromise.create({ provider: wsProvider });
const keyring = new Keyring({ type: 'sr25519' });
const alice = keyring.addFromUri('//Alice', {name: 'Alice default' });
console.log(`${alice.meta.name}: has address ${alice.address} with public key [${alice.publicKey}]`);
console.log(`api query: ${JSON.stringify(api.query.system.accountNonce)}`);
const n = await api.query.system.accountNonce(alice.address);
console.log(`n ${n}`);
const unsub = await api.rpc.chain.subscribeNewHeads((lastHeader) => {
console.log(`#${lastHeader.number} was authored by `);
});
const {nonce, data, balance } = await api.query.system.account(alice.address);
console.log(`balance of ${balance.free} and a nonce of ${nonce}`);
const txHash = await api.tx.balances
.transfer(ADDR, 12345)
.signAndSend(alice);
console.log(`submitted with hash ${txHash}`);
console.log('all done');
}
async function getBlockHash(number, api) {
console.log(`getBlockHash number: ${number}`);
const hash = await api.rpc.chain.getBlockHash(number);
console.log(`block hash for ${number}: ${hash}`);
getblock(hash, api);
}
async function getblock(hash, api) {
console.log(`getBlock hash: ${hash}`);
const block = await api.rpc.chain.getBlock(hash);
console.log(`block ${block}`);
}
main();