Skip to content

Commit

Permalink
Merge pull request #7 from MinaFoundation/PM-1135-txns-generator-run-…
Browse files Browse the repository at this point in the history
…indefinitely

PM-1135: paymentGenerator run indefinitely
  • Loading branch information
simisimis authored Feb 1, 2024
2 parents 2a5c727 + 91fa2e3 commit 5b82cae
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 54 deletions.
71 changes: 43 additions & 28 deletions src/paymentGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,28 @@ export async function paymentGenerator(
receivers: string[],
noTransactions: number,
timeDelayMS: number
) {
if (noTransactions === -1) {
while (true) {
const receiver = receivers[Math.floor(Math.random() * receivers.length)];
await processTransaction(network, deployerAccount, receiver, timeDelayMS);
}
} else {
for (let i = 0; i < noTransactions; i++) {
const receiver = receivers[Math.floor(Math.random() * receivers.length)];
await processTransaction(network, deployerAccount, receiver, timeDelayMS);
}
}
}
async function processTransaction(
network: string,
deployerAccount: string,
receiver: string,
timeDelayMS: number
) {
const client = new Client({ network: 'testnet' });
let sender_public = client.derivePublicKey(deployerAccount)
console.log("receiver: ", receiver);
const query = `query MyQuery {
account(publicKey: "${sender_public}") {
inferredNonce
Expand All @@ -24,32 +43,28 @@ export async function paymentGenerator(
}),
})
let inferred_nonce = parseInt((await response.json()).data.account.inferredNonce)
for (let i = 0; i < noTransactions; i++) {
const receiver = receivers[Math.floor(Math.random() * receivers.length)]
console.log("receiver: ", receiver)
let signedPayment = client.signPayment(
{
to: receiver,
from: sender_public,
amount: 1500000,
fee: 2000000000,
nonce: inferred_nonce + i
},
deployerAccount
);
const query_pay = `mutation MyMutation {
sendPayment(input: {fee: 2000000000, amount: "1500000", to: "${receiver}", from: "${sender_public}", nonce: "${inferred_nonce + i}"}, signature: {field: "${signedPayment.signature.field}", scalar: "${signedPayment.signature.scalar}"})}`
await fetch(network, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
query: query_pay
}),
})
.then(r => r.json())
.then(data => console.log("data returned:", data))
await new Promise(r => setTimeout(r, timeDelayMS));
}
let signedPayment = client.signPayment(
{
to: receiver,
from: sender_public,
amount: 1500000,
fee: 2000000000,
nonce: inferred_nonce
},
deployerAccount
);
const query_pay = `mutation MyMutation {
sendPayment(input: {fee: 2000000000, amount: "1500000", to: "${receiver}", from: "${sender_public}", nonce: "${inferred_nonce}"}, signature: {field: "${signedPayment.signature.field}", scalar: "${signedPayment.signature.scalar}"})}`;
await fetch(network, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
query: query_pay
}),
})
.then(r => r.json())
.then(data => console.log("data returned:", data));
await new Promise(r => setTimeout(r, timeDelayMS));
}
68 changes: 42 additions & 26 deletions src/zkAppGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,35 @@ import {
export async function zkAppGenerator(
network: string,
deployerAccount: string,
walletList: string[],
receivers: string[],
noTransactions: number,
timeDelayMS: number
) {
if (noTransactions === -1) {
while (true) {
const receiver = receivers[Math.floor(Math.random() * receivers.length)];
await processZKTransaction(network, deployerAccount, receiver, timeDelayMS);
}
} else {
for (let i = 0; i < noTransactions; i++) {
const receiver = receivers[Math.floor(Math.random() * receivers.length)];
await processZKTransaction(network, deployerAccount, receiver, timeDelayMS);
}
}
}

async function processZKTransaction(
network: string,
deployerAccount: string,
receiver: string,
timeDelayMS: number
) {
const devNet = Mina.Network(
network
);
Mina.setActiveInstance(devNet);

const deployerPrivKey = PrivateKey.fromBase58(deployerAccount); //
const deployerPrivKey = PrivateKey.fromBase58(deployerAccount);
const deployerPubKey = deployerPrivKey.toPublicKey();
console.log('Sender public key:', deployerPubKey.toBase58().toString());
const query = `query MyQuery {
Expand All @@ -37,34 +56,31 @@ export async function zkAppGenerator(
})
let inferred_nonce = parseInt((await response.json()).data.account.inferredNonce)

for (let i = 0; i < noTransactions; i++) {
let receiver = walletList[Math.floor(Math.random() * walletList.length)]
const toUserPublicKey = PublicKey.fromBase58(receiver);
console.log('Receiver public key:', toUserPublicKey.toBase58().toString());
const toUserPublicKey = PublicKey.fromBase58(receiver);
console.log('Receiver public key:', toUserPublicKey.toBase58().toString());

const MINA = 1e9;
const amount = 1 * MINA;
const MINA = 1e9;
const amount = 1 * MINA;

let memo = 'Test ZKApp to Receiver';
const transactionFee = 2 * MINA;
let memo = 'Test ZKApp to Receiver';
const transactionFee = 2 * MINA;

console.log('amount:', amount);
const tx = await Mina.transaction({ sender: deployerPubKey, fee: transactionFee, memo: memo, nonce: inferred_nonce + i }, () => {
let accountUpdate;
accountUpdate = AccountUpdate.createSigned(deployerPubKey);
accountUpdate.send({ to: toUserPublicKey, amount: UInt64.from(amount) });
});
await tx.prove();
console.log('tx.toPretty() : ' + JSON.stringify(tx.toPretty()));
console.log('amount:', amount);
const tx = await Mina.transaction({ sender: deployerPubKey, fee: transactionFee, memo: memo, nonce: inferred_nonce }, () => {
let accountUpdate;
accountUpdate = AccountUpdate.createSigned(deployerPubKey);
accountUpdate.send({ to: toUserPublicKey, amount: UInt64.from(amount) });
});
await tx.prove();
console.log('tx.toPretty() : ' + JSON.stringify(tx.toPretty()));

const res = await tx.sign([deployerPrivKey]).send();
const res = await tx.sign([deployerPrivKey]).send();

console.log('res : ' + JSON.stringify(res));
const hash = await res.hash();
console.log('res : ' + JSON.stringify(res));
const hash = await res.hash();

if (hash == null) {
console.log('error sending transaction (see above)');
}
await new Promise(r => setTimeout(r, timeDelayMS));
if (hash == null) {
console.log('error sending transaction (see above)');
}
}
await new Promise(r => setTimeout(r, timeDelayMS));
}

0 comments on commit 5b82cae

Please sign in to comment.