Skip to content

Commit

Permalink
decoded data logger
Browse files Browse the repository at this point in the history
  • Loading branch information
mat1asm committed Mar 28, 2024
1 parent afff929 commit d9ca325
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
10 changes: 6 additions & 4 deletions backend/src/handlers/fund-transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]

Expand Down Expand Up @@ -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)}`)
}
13 changes: 10 additions & 3 deletions backend/src/utils/fund-transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 6 additions & 12 deletions backend/test/handlers/fund-transactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down Expand Up @@ -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()
})
})

Expand Down

0 comments on commit d9ca325

Please sign in to comment.