From ec0c792f8113259b71b858d3894b7a4c3c319d53 Mon Sep 17 00:00:00 2001 From: John Marcou Date: Thu, 2 May 2024 21:03:52 -0400 Subject: [PATCH] Enforce mainnet --- src/paymentGenerator.ts | 94 +++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 46 deletions(-) diff --git a/src/paymentGenerator.ts b/src/paymentGenerator.ts index c1f26a3..56bbcf2 100644 --- a/src/paymentGenerator.ts +++ b/src/paymentGenerator.ts @@ -1,55 +1,57 @@ import Client from 'mina-signer'; export async function processTransaction( - network: string, - deployerAccount: string, - receiver: string, - timeDelayMS: number, - amount: number, - fee: number + network: string, + deployerAccount: string, + receiver: string, + timeDelayMS: number, + amount: number, + fee: number ) { - const client = new Client({ network: 'testnet' }); - let sender_public = client.derivePublicKey(deployerAccount) - console.log("receiver: ", receiver); - let amountToSend = amount * 1000000000; - let feeToSend = fee * 1000000000; - const query = `query MyQuery { + const client = new Client({ network: 'mainnet' }); + let sender_public = client.derivePublicKey(deployerAccount); + console.log('receiver: ', receiver); + let amountToSend = amount * 1000000000; + let feeToSend = fee * 1000000000; + const query = `query MyQuery { account(publicKey: "${sender_public}") { inferredNonce } - }` - let response = await fetch(network, { - method: "POST", - headers: { - "Content-Type": "application/json" - }, - body: JSON.stringify({ - query: query - }), - }) - let inferred_nonce = parseInt((await response.json()).data.account.inferredNonce) - let signedPayment = client.signPayment( - { - to: receiver, - from: sender_public, - amount: amountToSend, - fee: feeToSend, - nonce: inferred_nonce - }, - deployerAccount - ); - const query_pay = `mutation MyMutation { + }`; + let response = await fetch(network, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + query: query, + }), + }); + let inferred_nonce = parseInt( + (await response.json()).data.account.inferredNonce + ); + let signedPayment = client.signPayment( + { + to: receiver, + from: sender_public, + amount: amountToSend, + fee: feeToSend, + nonce: inferred_nonce, + }, + deployerAccount + ); + const query_pay = `mutation MyMutation { sendPayment(input: {fee: "${feeToSend}", amount: "${amountToSend}", 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)); + 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)); }