From f39b89b497b3b7cf4c12f6ee2869c4d4c7827dab Mon Sep 17 00:00:00 2001 From: samlior Date: Thu, 8 Jan 2026 15:20:50 +0800 Subject: [PATCH] chore: add simple test --- pkg/shreder/txparser_test.go | 110 +++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/pkg/shreder/txparser_test.go b/pkg/shreder/txparser_test.go index d58b9d6..68b7b52 100644 --- a/pkg/shreder/txparser_test.go +++ b/pkg/shreder/txparser_test.go @@ -1,9 +1,13 @@ package shreder import ( + "context" "encoding/hex" + "os" "testing" + "github.com/gagliardetto/solana-go" + "github.com/gagliardetto/solana-go/rpc" "github.com/near/borsh-go" ) @@ -54,3 +58,109 @@ func TestDecodeAxiomArgs(t *testing.T) { }) } } + +func toUpdata(slot uint64, tx *solana.Transaction) *SubscribeUpdateTransaction { + signatures := make([][]byte, len(tx.Signatures)) + for i, sig := range tx.Signatures { + signatures[i] = sig[:] + } + + accountKeys := make([][]byte, len(tx.Message.AccountKeys)) + for i, key := range tx.Message.AccountKeys { + accountKeys[i] = key[:] + } + + instructions := make([]*CompiledInstruction, len(tx.Message.Instructions)) + for i, instr := range tx.Message.Instructions { + accounts := make([]byte, len(instr.Accounts)) + for j, acc := range instr.Accounts { + accounts[j] = byte(acc) + } + instructions[i] = &CompiledInstruction{ + ProgramIdIndex: uint32(instr.ProgramIDIndex), + Accounts: accounts, + Data: instr.Data[:], + } + } + + addressTableLookups := make([]*MessageAddressTableLookup, len(tx.Message.AddressTableLookups)) + for i, lookup := range tx.Message.AddressTableLookups { + writable := make([]byte, len(lookup.WritableIndexes)) + for j, idx := range lookup.WritableIndexes { + writable[j] = byte(idx) + } + readonly := make([]byte, len(lookup.ReadonlyIndexes)) + for j, idx := range lookup.ReadonlyIndexes { + readonly[j] = byte(idx) + } + addressTableLookups[i] = &MessageAddressTableLookup{ + AccountKey: lookup.AccountKey[:], + WritableIndexes: writable, + ReadonlyIndexes: readonly, + } + } + + return &SubscribeUpdateTransaction{ + Transaction: &Transaction{ + Signatures: signatures, + Message: &Message{ + Header: &MessageHeader{ + NumRequiredSignatures: uint32(tx.Message.Header.NumRequiredSignatures), + NumReadonlySignedAccounts: uint32(tx.Message.Header.NumReadonlySignedAccounts), + NumReadonlyUnsignedAccounts: uint32(tx.Message.Header.NumReadonlyUnsignedAccounts), + }, + AccountKeys: accountKeys, + RecentBlockhash: nil, // TODO + Instructions: instructions, + Versioned: false, // TODO + AddressTableLookups: addressTableLookups, + }, + }, + Slot: slot, + } +} + +func TestParseTerm(t *testing.T) { + rpcUrl := os.Getenv("SOL_RPC_URL") + if rpcUrl == "" { + t.Fatalf("SOL_RPC_URL is not set") + } + + client := rpc.New(rpcUrl) + version := uint64(0) + tx, err := client.GetTransaction( + context.Background(), + solana.MustSignatureFromBase58("5Gz1fa4Qhb35bkg9QCMXpxCX5uuNr7WcjcmrwajGZA7kXsvNS9pDnYe12ggWeSqf1nwZbVPob6DkX6fcwbE9ofBR"), + &rpc.GetTransactionOpts{ + Commitment: rpc.CommitmentFinalized, + MaxSupportedTransactionVersion: &version, + }, + ) + if err != nil { + t.Fatalf("failed to get transaction: %v", err) + } + + _tx, err := tx.Transaction.GetTransaction() + if err != nil { + t.Fatalf("failed to get transaction: %v", err) + } + + signals := ParseTransaction(toUpdata(tx.Slot, _tx), nil, false) + if len(signals) != 1 { + t.Fatalf("expected 1 signal, got %d", len(signals)) + } + + signal := signals[0] + if signal.Label != "terminal" { + t.Fatalf("expected terminal signal, got %s", signal.Label) + } + if signal.Event != "buy" { + t.Fatalf("expected buy event, got %s", signal.Event) + } + if signal.Maker != "BaLxyjXzATAnfm7cc5AFhWBpiwnsb71THcnofDLTWAPK" { + t.Fatalf("expected maker BaLxyjXzATAnfm7cc5AFhWBpiwnsb71THcnofDLTWAPK, got %s", signal.Maker) + } + if signal.Token0Address != "5Wgv54peXRKDHYHapAELzgNKEPEh9E5Bf3hUR3sTpump" { + t.Fatalf("expected token0 address 5Wgv54peXRKDHYHapAELzgNKEPEh9E5Bf3hUR3sTpump, got %s", signal.Token0Address) + } +}