Skip to content

Instantly share code, notes, and snippets.

@vgrichina
Last active September 13, 2019 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vgrichina/2f039ddffbc6eda844d1a75d6c17455e to your computer and use it in GitHub Desktop.
Save vgrichina/2f039ddffbc6eda844d1a75d6c17455e to your computer and use it in GitHub Desktop.
load testing nearcore payments
const nearlib = require('nearlib');
const { signTransaction, transfer } = nearlib.transactions;
const { base_decode } = nearlib.utils.serialize;
const sleep = (ms) => new Promise((resolved) => setTimeout(resolved, ms));
(async () => {
const [,, accountId, txCount] = process.argv;
const near = await nearlib.connect({...require('near-shell/get-config')(), deps: { keyStore: new nearlib.keyStores.UnencryptedFileSystemKeyStore('./neardev') } });
const account = await near.account(accountId);
const status = await account.connection.provider.status();
let nonce = account._accessKey.nonce;
let transactions = [];
for (let i = 0; i < parseInt(txCount); i++) {
nonce++;
const actions = [transfer(1)];
const [txHash, signedTx] = await signTransaction(
accountId, nonce, actions, base_decode(status.sync_info.latest_block_hash), account.connection.signer, account.accountId, account.connection.networkId
);
transactions.push(signedTx);
}
console.log('txs', await Promise.all(transactions.map(async (signedTx, i) => {
try {
await sleep(i);
return await account.connection.provider.sendTransaction(signedTx)
} catch (e) {
console.log('Error: ', e);
return null;
}
})));
})().then(console.log);
{
"dependencies": {
"near-shell": "^0.10.5",
"nearlib": "^0.13.2"
}
}
@vgrichina
Copy link
Author

Use like:

time node load-test.js <accountId> <number of transactions to create>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment