Skip to content

Commit 805a16a

Browse files
committed
Let's not wait for finalized block
1 parent a46f4a5 commit 805a16a

File tree

1 file changed

+47
-12
lines changed

1 file changed

+47
-12
lines changed

examples/authorize_and_store_papi.js

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { getPolkadotSigner } from '@polkadot-api/signer';
55
import { Keyring } from '@polkadot/keyring';
66
import { cryptoWaitReady } from '@polkadot/util-crypto';
77
import { create } from 'ipfs-http-client';
8-
import { waitForNewBlock, cidFromBytes } from './common.js';
8+
import { cidFromBytes } from './common.js';
99
import { bulletin } from './.papi/descriptors/dist/index.mjs';
1010

1111
async function authorizeAccount(typedApi, sudoPair, who, transactions, bytes) {
@@ -20,10 +20,29 @@ async function authorizeAccount(typedApi, sudoPair, who, transactions, bytes) {
2020
const sudoTx = typedApi.tx.Sudo.sudo({
2121
call: authorizeTx.decodedCall
2222
});
23-
24-
const result = await sudoTx.signAndSubmit(sudoPair);
25-
console.log('Transaction authorizeAccount submitted:', result);
26-
return result;
23+
24+
// Wait for a new block.
25+
return new Promise((resolve, reject) => {
26+
const sub = sudoTx
27+
.signSubmitAndWatch(sudoPair)
28+
.subscribe({
29+
next: (ev) => {
30+
if (ev.type === "txBestBlocksState" && ev.found) {
31+
console.log("📦 Included in block:", ev.block.hash);
32+
sub.unsubscribe();
33+
resolve(ev);
34+
}
35+
},
36+
error: (err) => {
37+
console.log("Error:", err);
38+
sub.unsubscribe();
39+
reject(err);
40+
},
41+
complete: () => {
42+
console.log("Subscription complete");
43+
}
44+
});
45+
})
2746
}
2847

2948
async function store(typedApi, pair, data) {
@@ -38,11 +57,29 @@ async function store(typedApi, pair, data) {
3857
// Wrap in Binary object for typed API - pass as an object with 'data' property
3958
const binaryData = Binary.fromBytes(dataBytes);
4059
const tx = typedApi.tx.TransactionStorage.store({ data: binaryData });
41-
42-
const result = await tx.signAndSubmit(pair);
43-
console.log('Transaction store submitted:', result);
44-
45-
return cid;
60+
61+
// Wait for a new block.
62+
return new Promise((resolve, reject) => {
63+
const sub = tx
64+
.signSubmitAndWatch(pair)
65+
.subscribe({
66+
next: (ev) => {
67+
if (ev.type === "txBestBlocksState" && ev.found) {
68+
console.log("📦 Included in block:", ev.block.hash);
69+
sub.unsubscribe();
70+
resolve(cid);
71+
}
72+
},
73+
error: (err) => {
74+
console.log("Error:", err);
75+
sub.unsubscribe();
76+
reject(err);
77+
},
78+
complete: () => {
79+
console.log("Subscription complete");
80+
}
81+
});
82+
})
4683
}
4784

4885
// Connect to a local IPFS gateway (e.g. Kubo)
@@ -111,14 +148,12 @@ async function main() {
111148

112149
console.log('Doing authorization...');
113150
await authorizeAccount(typedApi, sudoSigner, who, transactions, bytes);
114-
await waitForNewBlock();
115151
console.log('Authorized!');
116152

117153
console.log('Storing data ...');
118154
const dataToStore = "Hello, Bulletin with PAPI - " + new Date().toString();
119155
let cid = await store(typedApi, whoSigner, dataToStore);
120156
console.log('Stored data with CID: ', cid);
121-
await waitForNewBlock();
122157

123158
console.log('Reading content... cid: ', cid);
124159
let content = await read_from_ipfs(cid);

0 commit comments

Comments
 (0)