forked from AstarNetwork/Astar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtx-utils.mjs
32 lines (27 loc) · 892 Bytes
/
tx-utils.mjs
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
import { ApiPromise, WsProvider } from '@polkadot/api';
import { Keyring } from '@polkadot/api';
export async function sendTransaction(transaction, sender) {
return new Promise((resolve, reject) => {
let unsubscribe;
let timeout;
const SPAWNING_TIME = 500000;
transaction.signAndSend(sender, async (result) => {
console.log(`Current status is ${result?.status}`);
if (result.isFinalized) {
if (unsubscribe) {
unsubscribe();
}
clearTimeout(timeout);
resolve(true);
}
}).then(unsub => {
unsubscribe = unsub;
}).catch(error => {
console.error(error);
reject(error);
});
timeout = setTimeout(() => {
reject(new Error('Transaction timeout'));
}, SPAWNING_TIME);
});
}