forked from Dreyhh/Vested
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processCsvRow.js
75 lines (63 loc) · 3.31 KB
/
processCsvRow.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//Description: This file contains the function that processes a single row of the CSV file and sends the transaction to the blockchain.
// It is used by the MakeAllTransactions.js file to process each row of the CSV file.
import dotenv from 'dotenv';
import { Keyring } from '@polkadot/api';
import fs from 'fs';
import BN from 'bn.js';
let successCount = 0;
let failCount = 0;
// Define an async function to process a single row of the CSV file using the Polkadot JS API
const processCsvRow = async (row, currentRowIndex, api, rowsLen) => {
dotenv.config();
const senderPublicKey = process.env.SENDER_ADDRESS;
const senderPhrase = process.env.SENDER_PHRASE;
const [arg1, arg2, arg3, arg4] = row;
// Initialize the keyring with the sender's account
const keyring = new Keyring({ type: 'sr25519' });
const senderPair = keyring.addFromUri(senderPhrase);
//Parse the row data
const recipientAddress = arg1;
const locked = new BN(arg2);
const perBlock = new BN(arg3);
const startingBlock = new BN (arg4);
const vestingInfo = await api.createType('VestingInfo', { locked: locked, perBlock: perBlock, startingBlock: startingBlock});
// Create a new transfer extrinsic using the vestedTransfer method from the pallet_vesting module
const tx = await api.tx.vesting.vestedTransfer(recipientAddress, vestingInfo);
const unsub = await tx.signAndSend(senderPair, { nonce: -1 }, async ({ events = [], status }) => {
if (status.isFinalized) {
const finalizedStatus = await status.asFinalized;
console.log('\x1b[33m%s\x1b[0m', `###Sending VestTransfer to user ${recipientAddress} (row # ${currentRowIndex})###`);
console.log(`Transaction has been sent`);
console.log(`Transaction included at blockHash ${finalizedStatus}`);
``
let result = '';
events.slice(-1).forEach(({ event: { method, section } }) => {
result += `\t${section}.${method}:\t${status}\n`;
});
if (result.includes("Success")) {
console.log('\x1b[32m%s\x1b[0m', 'Transaction succeeded');
fs.appendFileSync('output.txt', `Row: ${currentRowIndex},recipientAddress: ${recipientAddress},Transaction succeeded at: ${finalizedStatus}\n`);
successCount++;
} else {
console.log('\x1b[31m%s\x1b[0m', 'Transaction failed!');
fs.appendFileSync('output.txt', `Row: ${currentRowIndex},recipientAddress: ${recipientAddress},Transaction Failed at: ${finalizedStatus}\n`);
failCount++;
}
if (successCount + failCount >= rowsLen){
const boxWidth = 40;
console.log('+' + '-'.repeat(boxWidth - 2) + '+');
console.log('\x1b[33m%s\x1b[0m',`Total transactions sent: ${successCount + failCount}`);
console.log('\x1b[32m%s\x1b[0m',`Successful transactions: ${successCount}`);
console.log('\x1b[31m%s\x1b[0m',`Failed transactions: ${failCount}`);
console.log('\x1b[33m%s\x1b[0m',`Records saved to output.txt`);
console.log('+' + '-'.repeat(boxWidth - 2) + '+');
process.exit();
}
unsub();
}
//events.forEach(({ event: { method, section } }) => {
//console.log(`\t${section}.${method}:\t${status}`);
//});
});
};
export default processCsvRow;