Bulk Transfer

Overview

Batch transaction

Bulk transfer leverages pallet-utility runtime module from substrate to send multiple transfer extrinsics to runtime.
Here is the example code provided by Polkadot.js cookbook:
1
// construct a list of transactions we want to batch
2
const txs = [
3
api.tx.balances.transfer(addrBob, 12345),
4
api.tx.balances.transfer(addrEve, 12345),
5
api.tx.staking.unbond(12345)
6
];
7
8
// construct the batch and send the transactions
9
api.tx.utility
10
.batch(txs)
11
.signAndSend(sender, ({ status }) => {
12
if (status.isInBlock) {
13
console.log(`included in ${status.asInBlock}`);
14
}
15
});
Copied!

Usage

To use bulk transfer in other systems using Parallel, Developers must installParallel.js, aJavascript frontend library to interact with Parallel parachain.

bulkTransfer

Parameter
Type
Description
api
APIPromise
Initialized Parallel API
sender
Keyring
Account to send funds from
recipients
Array<string>
Array of recipient addresses in string
amounts
Array<string>
Array of amount to transfer to

Example Code

1
import { ApiPromise, WsProvider } from '@parallel-finance/api';
2
import { Keyring } from "@polkadot/keyring";
3
import { bulkTransfer } from "@parallel-finance/bulk-transfer";
4
5
async function usage() {
6
// Initialize API
7
const provider = new WsProvider('wss://heiko-rpc.parallel.fi');
8
const api = await ApiPromise.create(
9
options({
10
types: {
11
TAssetBalance: 'Balance'
12
},
13
provider
14
})
15
);
16
// Initialize an account
17
const keyring = new Keyring();
18
const mnemonic = "test test test test test test test test test test test test"
19
const sender = keyring.addFromUri(mnemonic, { name: 'bulk transfer test' }, 'sr25519');
20
// Recipients
21
const recipients = [
22
'stAZnJwXAvRRo884Anfu2in9SBB6tssvcsjBAZnvnVn53krpP',
23
'st8p7os56kbysAKCxRjC1PeUyobEP8b94sQkBbmeWSc2GJzEt'
24
];
25
// Amounts
26
const amounts = ['1000', '1000'];
27
// Execute bulk transfer
28
await bulkTransfer(api, sender, recipients, amounts);
29
}
30
31
await usage();
Copied!

Glossary

substrate: A blockchain development framework developed and maintained by Parity Technologies.
pallet-utility: Utility pallet for substrate runtime, it includes generic helper functions for blockchain runtime processing
extrinsic: Extendable transaction. Coined by Polkadot, Unlike regular transaction in blockchain where it sends bytecode for smart contracts, extrinsic has specific data structure optimized for each blockchain.
APIPromise: The data type where it refers initialized asynchronous api for interacting with blockchain. It is used for making extrinsic
Keyring: The data type where it refers to initialized account to be used with initialized api.