-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy path0008-transaction_gets_finalized.js
More file actions
69 lines (57 loc) · 2.32 KB
/
Copy path0008-transaction_gets_finalized.js
File metadata and controls
69 lines (57 loc) · 2.32 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//based on: https://polkadot.js.org/docs/api/examples/promise/transfer-events
const assert = require("assert");
async function run(nodeName, networkInfo, args) {
const {wsUri, userDefinedTypes} = networkInfo.nodesByName[nodeName];
// Create the API and wait until ready
var api = null;
var keyring = null;
if (zombie == null) {
const testKeyring = require('@polkadot/keyring/testing');
const { WsProvider, ApiPromise } = require('@polkadot/api');
const provider = new WsProvider(wsUri);
api = await ApiPromise.create({provider});
// Construct the keyring after the API (crypto has an async init)
keyring = testKeyring.createTestKeyring({ type: "sr25519" });
} else {
keyring = new zombie.Keyring({ type: "sr25519" });
api = await zombie.connect(wsUri, userDefinedTypes);
}
// Add Alice to our keyring with a hard-derivation path (empty phrase, so uses dev)
const alice = keyring.addFromUri('//Alice');
// Create an extrinsic:
const extrinsic = api.tx.system.remark("xxx");
let extrinsic_success_event = false;
try {
await new Promise( async (resolve, reject) => {
const unsubscribe = await extrinsic
.signAndSend(alice, { nonce: -1 }, ({ events = [], status }) => {
console.log('Extrinsic status:', status.type);
if (status.isInBlock) {
console.log('Included at block hash', status.asInBlock.toHex());
console.log('Events:');
events.forEach(({ event: { data, method, section }, phase }) => {
console.log('\t', phase.toString(), `: ${section}.${method}`, data.toString());
if (section=="system" && method =="ExtrinsicSuccess") {
extrinsic_success_event = true;
}
});
} else if (status.isFinalized) {
console.log('Finalized block hash', status.asFinalized.toHex());
unsubscribe();
if (extrinsic_success_event) {
resolve();
} else {
reject("ExtrinsicSuccess has not been seen");
}
} else if (status.isError) {
unsubscribe();
reject("Extrinsic status.isError");
}
});
});
} catch (error) {
assert.fail("Transfer promise failed, error: " + error);
}
assert.ok("test passed");
}
module.exports = { run }