-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.ts
42 lines (33 loc) · 1.56 KB
/
script.ts
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
import { ApiPromise, WsProvider, Keyring } from '@polkadot/api'
import { options } from '@acala-network/api'
const PHRASE = 'time copper course profit all pledge priority illegal please couch nest glance'
const Receiver = '5ED8PaU5m2gQoaASMzrF7JrcJMfj4yZMBPaUY84VjozyTo7Y'
const url = 'wss://mandala.polkawallet.io'
async function main() {
//Create first connection
const firstApi = await connectionCreator(url)
//Create second connection
const secondApi = await connectionCreator(url)
const keyring = new Keyring({ type: 'sr25519' })
const newPair = keyring.addFromUri(PHRASE)
let count = 0
//subscribe to the balance from first connection
firstApi.query.tokens.accounts(newPair.address, { 'token': 'DOT' }, async ({ free }) => {
console.log(`Balance from subscripttion is ${free}`)
//make a transfer only one times
if (++count === 1) {
await firstApi.tx.currencies.transfer(Receiver, { 'token': 'DOT' }, 100000000).signAndSend(newPair)
}
//if subscribe to the balance with second connection immediately after have got updated event it lead to the problem.
if (count === 2) {
secondApi.query.tokens.accounts(newPair.address, { 'token': 'DOT' }, async ({ free }) => {
console.log(`Balance from NEW subscripttion is ${free}`)
})
}
});
}
async function connectionCreator(url: string): Promise<ApiPromise> {
const provider = new WsProvider(url)
return ApiPromise.create(options({ provider }))
}
main().catch(console.error).finally()