Created
May 17, 2023 14:02
-
-
Save bLd75/0f748a5f46f0d5476e59e36ea52b9790 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 { cryptoWaitReady, decodeAddress, checkAddress, signatureVerify } = require('@polkadot/util-crypto'); | |
const { types } = require('@open-web3/orml-type-definitions'); | |
const fs = require('fs'); | |
async function main () { | |
//variables | |
// 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'); | |
// Initialise the provider to connect to the node | |
const provider = new WsProvider('wss://ws.gm.bldnodes.org'); | |
// Create the API and wait until ready | |
const api = await ApiPromise.create({ provider }); | |
api.registerTypes(types); | |
// 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}`); | |
const addresses = file.split(/\r?\n/); | |
let processed = 0; | |
// Send batches of 50 | |
while (processed<addresses.length) { | |
let batchSize = addresses.length - processed; | |
if (batchSize > 50) { | |
batchSize = 50; | |
} | |
let txs = []; | |
let j = 0; | |
for (let i = 0; i < batchSize; i++) { | |
let invalid = false; | |
try { | |
const decodedAddress = decodeAddress(addresses[processed]); | |
} catch (e) { | |
invalid = true; | |
j++; | |
processed++; | |
console.log(addresses[processed],'is invalid dude'); | |
} | |
if (!invalid) { | |
txs[j] = api.tx.currencies.transfer(addresses[processed],'GM', 1); | |
console.log('sending GM to', addresses[processed], ' hell yeah!'); | |
j++; | |
processed++; | |
// uncomment to send GM + GN | |
//txs[j] = api.tx.currencies.transfer(addresses[i],'GN', 1); j++; processed++; | |
} | |
} | |
// construct the batch and send the transactions | |
const hash = await api.tx.utility.batch(txs).signAndSend(pair, { nonce: -1 }); | |
console.log('Transfer sent with hash', hash.toHex()); | |
} | |
console.log('processed: ', processed, 'wooo maaaaan!!'); | |
} | |
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