Phu Quoc Dog
Search…
How to integration wallet
How to get account/Address balance to check there is enough coins to withdraw?
Let's dive right in, connect to a general chain and retrieve some information on the current state. Of interest may be retrieving the nonce of a particular account as well as the current balance, this can be achieved via
1
// Some code
2
// Import the API, Keyring and some utility functions
3
const { ApiPromise,WsProvider } = require('@polkadot/api');
4
const { Keyring } = require('@polkadot/keyring');
5
6
const ADDR = '5CNChyk2fnVgVSZDLAVVFb4QBTMGm6WfuQvseBG6hj8xWzKP';
7
8
9
async function getBalance () {
10
11
const provider = new WsProvider('wss://rpc.phuquoc.dog');
12
const api = await ApiPromise.create({provider});
13
// Retrieve the last timestamp
14
const now = await api.query.timestamp.now();
15
const { nonce, data: balance } = await api.query.system.account(ADDR);
16
17
console.log(`${now}: balance of ${balance.free} and a nonce of ${nonce}`);
18
console.log('balance for human: ' + balance.free.toHuman());
19
console.log(balance);
20
21
}
22
getBalance().catch(console.error).finally(() => process.exit());
23
Copied!

How to transfer out?

Transaction endpoints are exposed, as determined by the metadata, on the api.tx endpoint. These allow you to submit transactions for inclusion in blocks, be it transfers, setting information or anything else your chain supports.
1
// Import the API, Keyring and some utility functions
2
const { ApiPromise,WsProvider } = require('@polkadot/api');
3
const { Keyring } = require('@polkadot/keyring');
4
5
6
const BN = require('bn.js');
7
8
const ADDR = '5FUZZjdRkb7Z8YC7iTfPyNjtoc5zXvRw4kXqtpeVEituaRom';
9
10
11
async function transferBalance (amount) {
12
13
// Some mnemonic phrase
14
const PHRASE = 'barrel outer about develop dignity nice slab lottery sort album knock salt';
15
16
const provider = new WsProvider('wss://rpc.phuquoc.dog');
17
const api = await ApiPromise.create({provider});
18
19
20
// Constuct the keyring after the API (crypto has an async init)
21
const keyring = new Keyring({ type: 'sr25519' });
22
23
// Add //Alice to our keyring with a hard-deived path (empty phrase, so uses dev)
24
const alice = keyring.addFromUri(PHRASE);
25
26
const decims = new BN(api.registry.chainDecimals);
27
const factor = new BN(10).pow(decims);
28
const amountUnit = factor.imuln(amount);
29
30
//console.log(amountUnit)
31
32
// Create a extrinsic, transferring 12345 units to Bob
33
const transfer = api.tx.balances.transfer(ADDR, amountUnit);
34
35
// Sign and send the transaction using our account
36
const hash = await transfer.signAndSend(alice);
37
38
console.log('Transfer sent with hash', hash.toHex());
39
40
}
41
42
transferBalance(10).catch(console.error).finally(() => console.log('------Finish Demo getBalance ----'));
43
44
Copied!
In the example above we just send 10PQD to address 5FUZZjdRkb7Z8YC7iTfPyNjtoc5zXvRw4kXqtpeVEituaRom, to send multiple at the same time you need to change
1
// Some code
2
const hash = await transfer.signAndSend(alice);
Copied!
Tobe
1
// Some code
2
const hash = await transfer.signAndSend(alice,{ nonce: -1 });
Copied!
If not, you will be get the error like this Hash:: 1014: Priority is too low: (3752826182276 vs 3752826182276): The transaction has too low priority to replace another transaction already in the pool.See more https://polkadot.js.org/docs/api/cookbook/tx/#how-do-i-take-the-pending-tx-pool-into-account-in-my-nonce

How to iterator the trancsation list by transaction-ids

You can send a GET request with trancsation id
1
// Some code
2
GET
3
https://hasura.phuquoc.dog/api/rest/transactions?id=trancsationid
Copied!
To get information transaction id 0xc66213c30e88c88e099f297ccd90ead6367b43723fcdb2f863541b6d6f5f8c52
1
// Some code
2
https://hasura.phuquoc.dog/api/rest/transactions?id=0xc66213c30e88c88e099f297ccd90ead6367b43723fcdb2f863541b6d6f5f8c52
3
{
4
"extrinsic": [
5
{
6
"args": "[{\"id\":\"5DM8un9Cn9CjWD9oEAbKcZvU8xGf3HFJC8DJUTQ2jGsgn4hR\"},500000000000000]",
7
"block_number": 1852,
8
"doc": "[ Same as the [`transfer`] call, but with a check that the transfer will not kill the, origin account., , 99% of the time you want [`transfer`] instead., , [`transfer`]: struct.Pallet.html#method.transfer, # <weight>, - Cheaper than transfer because account cannot be killed., - Base Weight: 51.4 µs, - DB Weight: 1 Read and 1 Write to dest (sender is in overlay already), #</weight>]",
9
"extrinsic_index": 1,
10
"fee_details": "{\"inclusionFee\":{\"baseFee\":10000000,\"lenFee\":148000000,\"adjustedWeightFee\":13202109}}",
11
"fee_info": "{\"weight\":167334000,\"class\":\"Normal\",\"partialFee\":171202109}",
12
"hash": "0xc66213c30e88c88e099f297ccd90ead6367b43723fcdb2f863541b6d6f5f8c52",
13
"is_signed": true,
14
"method": "transferKeepAlive",
15
"section": "balances",
16
"signer": "5DAFV7VmRcoxkXQq1xjdFeJrA8tSDyqCZxUfbuqv6DY4BinA",
17
"success": true,
18
"timestamp": 1637296116
19
}
20
]
21
}
Copied!
Get all history transaction an account
1
// Some code
2
https://hasura.phuquoc.dog/api/rest/account_transactions?id=xxx
Copied!
For example get transaction send and recived account 5HGnAjebsXJrscjrk7TzCSc4p21faUaMEdTY9Xrp4CLwYKtQ
1
// Some code
2
https://hasura.phuquoc.dog/api/rest/account_transactions?id=5HGnAjebsXJrscjrk7TzCSc4p21faUaMEdTY9Xrp4CLwYKtQ
Copied!
Copy link
Edit on GitHub
Contents