Created
May 17, 2023 13:49
-
-
Save bLd75/8b90ba6c46c167d6959205f46b106a38 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Required imports | |
const { ApiPromise, WsProvider } = require('@polkadot/api'); | |
const { Keyring } = require('@polkadot/keyring'); | |
const { decodeAddress } = require('@polkadot/util-crypto'); | |
const fs = require('fs'); | |
const prompt = require('prompt'); | |
async function main () { | |
// Initialise the provider to connect to the node | |
const provider = new WsProvider('wss://rpc.astar.network'); | |
// Read private key from a file .secret | |
const secret = fs.readFileSync('.secret', 'utf8'); | |
// Read a recipients text file with addresses | |
const file = fs.readFileSync('recipients', 'utf8'); | |
// Create the API and wait until ready | |
const api = await ApiPromise.create({ provider }); | |
// Retrieve the chain & prefix | |
const [chain, decimals, prefix] = await Promise.all([ | |
api.rpc.system.chain(), | |
api.registry.chainDecimals[0], | |
api.consts.system.ss58Prefix | |
]); | |
// Construct the keyring from PK/seed | |
const keyring = new Keyring({ type: 'sr25519' }); | |
keyring.setSS58Format(prefix); | |
const pair = keyring.addFromUri(secret); | |
console.log(`Connected to chain ${chain} with address ${pair.address}`); | |
// Prepare transactions from file | |
const addresses = file.split(/\r?\n/); | |
let txs = []; | |
for (let i = 0; i < addresses.length; i++) { | |
let invalid = false; | |
try { | |
const decodedAddress = decodeAddress(addresses[i]); | |
} catch (e) { | |
invalid = true; | |
console.log(addresses[i],'is invalid dude'); | |
} | |
if (!invalid) { | |
// Sends 1 ASTR to each address | |
txs[i] = api.tx.balances.transfer(addresses[i], 1000000000000000000); | |
console.log('sending tokens to', addresses[i], ' hell yeah!'); | |
} | |
} | |
// construct the batch of 1000 transactions and send extrinsic | |
for (let i = 0; i < 1000; i++) { | |
const hash = await api.tx.utility.batch(txs).signAndSend(pair, { nonce: -1 }); | |
console.log('Transfer sent with hash', hash.toHex()); | |
} | |
} | |
main().catch(console.error).finally(() => process.exit()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment