Skip to content

Instantly share code, notes, and snippets.

@garethtdavies
Last active October 20, 2022 15:08
Show Gist options
  • Save garethtdavies/552aa3c65f9d4bf4e5f9a891f6c25fbb to your computer and use it in GitHub Desktop.
Save garethtdavies/552aa3c65f9d4bf4e5f9a891f6c25fbb to your computer and use it in GitHub Desktop.
Batch sending
import {
PrivateKey,
Mina,
isReady,
shutdown,
PublicKey,
UInt64,
AccountUpdate,
UInt32,
} from 'snarkyjs';
import * as fs from 'fs';
await isReady;
// Use MinaExplorer GraphQL endpoint
let Berkeley = Mina.BerkeleyQANet('https://proxy.berkeley.minaexplorer.com/graphql');
Mina.setActiveInstance(Berkeley);
let nonce = 456;
// Fee payer key this is B62qij84rwTzzTiompizumg46WFAGgQ7bss7ephB47xxag8oDaDsGhX
// These are throwaway testnet keys, intentionally left in so easy to replicate - don't use these outside of this script
// This uses the same account to pay fees and also to send funds
let feePayerKey = PrivateKey.fromBase58(
'EKEEvWvRxGzLmb4RY2EFtMMxr2tAbKUnmMUBh6K5iv73VjnassXH'
);
let feePayerAddress = feePayerKey.toPublicKey();
// These are the precalculated values that we have determined via a payout script to send
let zkAppSendAddresses = [
{
publicKey: 'B62qmQsEHcsPUs5xdtHKjEmWqqhUPRSF2GNmdguqnNvpEZpKftPC69e',
total: 100_000,
},
];
// Transaction fee to use
let transactionFee = 100_000_000;
// This will increment the nonce for the zkApp tx
for (let i = nonce; i < nonce + 5; i = i + 1) {
console.log('Making a transfer');
let tx = await Mina.transaction(
{
feePayerKey,
fee: transactionFee,
memo: 'Constrain nonce',
nonce: i,
},
() => {
let payer = AccountUpdate.create(feePayerAddress);
for (let [key, value] of Object.entries(zkAppSendAddresses)) {
payer.send({
to: PublicKey.fromBase58(value['publicKey']),
amount: UInt64.from(value['total']),
});
}
// This doesn't work, maybe another way to do this...
//payer.account.nonce.assertEquals(UInt32.from(i+1));
payer.sign(feePayerKey);
}
);
// Need to sign the transaction before broadcast
tx.sign();
// Write this to a file so we can broadcast seperately
fs.writeFileSync('payments/' + i + '.json', tx.toGraphqlQuery());
// Sending the transaction to debug
await tx.send().wait();
}
shutdown();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment