From d9ca32599d3b1c231369f6187968fe5181a67dea Mon Sep 17 00:00:00 2001 From: matias martinez Date: Thu, 28 Mar 2024 16:52:46 -0300 Subject: [PATCH] decoded data logger --- backend/src/handlers/fund-transactions.ts | 10 ++++++---- backend/src/utils/fund-transactions.ts | 13 ++++++++++--- .../test/handlers/fund-transactions.test.ts | 18 ++++++------------ 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/backend/src/handlers/fund-transactions.ts b/backend/src/handlers/fund-transactions.ts index fcfe5ecd..58d7d8d0 100644 --- a/backend/src/handlers/fund-transactions.ts +++ b/backend/src/handlers/fund-transactions.ts @@ -3,12 +3,14 @@ import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda' import { getFundingKey } from '../utils/secrets' import { checkTransactions, - deserializeTransactions + deserializeTransactions, + extractCallData } from '../utils/fund-transactions' import { Keypair, VersionedTransaction } from '@solana/web3.js' import bs58 from 'bs58' import { HandlerError } from '../utils/errors' import { asJsonResponse } from '../utils/response' +import { Instruction } from '@coral-xyz/anchor' export type FundTransactionRequest = Uint8Array[] @@ -80,9 +82,9 @@ function getSignature(tx: VersionedTransaction): string { } function logSignatures(signedTransactions: VersionedTransaction[]) { - const sigs: string[] = [] + const sigs: { sig: string; instruction?: Instruction | null }[] = [] signedTransactions.forEach((tx) => { - sigs.push(getSignature(tx)) + sigs.push({ sig: getSignature(tx), instruction: extractCallData(tx) }) }) - console.log(`Signed transactions: ${sigs}`) + console.log(`Signed transactions: ${JSON.stringify(sigs)}`) } diff --git a/backend/src/utils/fund-transactions.ts b/backend/src/utils/fund-transactions.ts index 634202bb..11122df6 100644 --- a/backend/src/utils/fund-transactions.ts +++ b/backend/src/utils/fund-transactions.ts @@ -7,7 +7,7 @@ import { TransactionInstruction, VersionedTransaction } from '@solana/web3.js' -import IDL from '../token_dispenser.json' +import IDL from '../token-dispenser.json' import * as anchor from '@coral-xyz/anchor' import config from '../config' @@ -17,6 +17,7 @@ const SET_COMPUTE_UNIT_PRICE_DISCRIMINANT = 3 const MAX_COMPUTE_UNIT_PRICE = BigInt(1_000_000) +// eslint-disable-next-line @typescript-eslint/no-explicit-any const coder = new anchor.BorshCoder(IDL as any) export function deserializeTransactions( @@ -215,13 +216,19 @@ export async function checkTransactions( export function extractCallData( versionedTx: VersionedTransaction, - programId: string + programId?: string ) { + const tokenDispenserPid = programId || config.tokenDispenserProgramId() + if (!tokenDispenserPid) { + console.error('Token dispenser program ID not set') + throw new Error('Token dispenser program ID not set') + } + try { const instruction = versionedTx.message.compiledInstructions.find( (ix) => versionedTx.message.staticAccountKeys[ix.programIdIndex].toBase58() === - programId + tokenDispenserPid ) if (!instruction) { diff --git a/backend/test/handlers/fund-transactions.test.ts b/backend/test/handlers/fund-transactions.test.ts index d19cc54c..72ae704a 100644 --- a/backend/test/handlers/fund-transactions.test.ts +++ b/backend/test/handlers/fund-transactions.test.ts @@ -21,7 +21,7 @@ import IDL from '../../src/token-dispenser.json' import { fundTransactions } from '../../src/handlers/fund-transactions' const RANDOM_BLOCKHASH = 'HXq5QPm883r7834LWwDpcmEM8G8uQ9Hqm1xakCHGxprV' -const PROGRAM_ID = 'Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS' //new Keypair().publicKey +const PROGRAM_ID = new PublicKey('Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS') const FUNDER_KEY = new Keypair() const server = setupServer() let input: VersionedTransaction[] @@ -222,27 +222,21 @@ describe('fundTransactions integration test', () => { expect(response.statusCode).toBe(403) }) - test.only('should extract claim info', async () => { - const x = VersionedTransaction.deserialize(serialedSignedOnceEvmClaimTx) - - const found = x.message.compiledInstructions.find( - (i) => - x.message.staticAccountKeys[i.programIdIndex].toBase58() === PROGRAM_ID + test('should extract claim info', async () => { + const versionedTx = VersionedTransaction.deserialize( + serialedSignedOnceEvmClaimTx ) - const callData = extractCallData(x, PROGRAM_ID) - console.log(callData) + const callData = extractCallData(versionedTx, PROGRAM_ID.toBase58()) expect(callData).not.toBeNull() expect(callData.name).toBe('claim') expect(callData.data.claimCertificate.amount.toNumber()).toBe(3000000) - expect(callData?.data.claimCertificate.proofOfInclusion).toBeDefined() + expect(callData.data.claimCertificate.proofOfInclusion).toBeDefined() expect( Buffer.from( callData.data.claimCertificate.proofOfIdentity.evm.pubkey ).toString('hex') ).toBe('b80eb09f118ca9df95b2df575f68e41ac7b9e2f8') - - expect(found).toBeDefined() }) })