|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "text/tabwriter" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/trufnetwork/kwil-db/core/crypto" |
| 12 | + "github.com/trufnetwork/kwil-db/core/crypto/auth" |
| 13 | + "github.com/trufnetwork/sdk-go/core/tnclient" |
| 14 | + "github.com/trufnetwork/sdk-go/core/types" |
| 15 | +) |
| 16 | + |
| 17 | +func main() { |
| 18 | + ctx := context.Background() |
| 19 | + |
| 20 | + // 1. Initialize Client |
| 21 | + // Use environment variable for private key to avoid hardcoding |
| 22 | + privateKeyHex := os.Getenv("TN_PRIVATE_KEY") |
| 23 | + if privateKeyHex == "" { |
| 24 | + log.Fatal("TN_PRIVATE_KEY environment variable is required") |
| 25 | + } |
| 26 | + |
| 27 | + pk, err := crypto.Secp256k1PrivateKeyFromHex(privateKeyHex) |
| 28 | + if err != nil { |
| 29 | + log.Fatalf("Failed to parse private key: %v", err) |
| 30 | + } |
| 31 | + signer := &auth.EthPersonalSigner{Key: *pk} |
| 32 | + |
| 33 | + // Use testnet gateway or local node |
| 34 | + endpoint := os.Getenv("TN_GATEWAY_URL") |
| 35 | + if endpoint == "" { |
| 36 | + endpoint = "https://gateway.testnet.truf.network" |
| 37 | + } |
| 38 | + |
| 39 | + client, err := tnclient.NewClient(ctx, endpoint, tnclient.WithSigner(signer)) |
| 40 | + if err != nil { |
| 41 | + log.Fatalf("Failed to create client: %v", err) |
| 42 | + } |
| 43 | + |
| 44 | + fmt.Printf("🔄 Transaction History Demo\n") |
| 45 | + fmt.Printf("===========================\n") |
| 46 | + fmt.Printf("Endpoint: %s\n", endpoint) |
| 47 | + addr := client.Address() |
| 48 | + fmt.Printf("Wallet: %s\n\n", addr.Address()) |
| 49 | + |
| 50 | + // 2. Define History Query Parameters |
| 51 | + // We want history for the "hoodi_tt2" bridge |
| 52 | + bridgeID := "hoodi_tt2" |
| 53 | + walletAddress := "0xc11Ff6d3cC60823EcDCAB1089F1A4336053851EF" // Example address from issue |
| 54 | + limit := 10 |
| 55 | + offset := 0 |
| 56 | + |
| 57 | + fmt.Printf("📋 Fetching history for bridge '%s'...\n", bridgeID) |
| 58 | + fmt.Printf(" Wallet: %s\n", walletAddress) |
| 59 | + fmt.Printf(" Limit: %d\n", limit) |
| 60 | + fmt.Printf(" Offset: %d\n", offset) |
| 61 | + fmt.Println("-------------------------------------------------------") |
| 62 | + |
| 63 | + // 3. Call GetHistory |
| 64 | + history, err := client.GetHistory(ctx, types.GetHistoryInput{ |
| 65 | + BridgeIdentifier: bridgeID, |
| 66 | + Wallet: walletAddress, |
| 67 | + Limit: &limit, |
| 68 | + Offset: &offset, |
| 69 | + }) |
| 70 | + if err != nil { |
| 71 | + log.Fatalf("❌ Failed to fetch history: %v", err) |
| 72 | + } |
| 73 | + |
| 74 | + // 4. Display Results |
| 75 | + if len(history) == 0 { |
| 76 | + fmt.Println("No history records found.") |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + // Helper to format byte slices |
| 81 | + formatHexShort := func(b []byte) string { |
| 82 | + if len(b) == 0 { |
| 83 | + return "null" |
| 84 | + } |
| 85 | + if len(b) > 4 { |
| 86 | + return fmt.Sprintf("0x%x...", b[:4]) |
| 87 | + } |
| 88 | + return fmt.Sprintf("0x%x", b) |
| 89 | + } |
| 90 | + |
| 91 | + // Use tabwriter for aligned output |
| 92 | + w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) |
| 93 | + fmt.Fprintln(w, "TYPE\tAMOUNT\tFROM\tTO\tINTERNAL TX\tEXTERNAL TX\tSTATUS\tBLOCK\tEXT BLOCK\tTIMESTAMP") |
| 94 | + fmt.Fprintln(w, "----\t------\t----\t--\t-----------\t-----------\t------\t-----\t---------\t---------") |
| 95 | + |
| 96 | + for _, rec := range history { |
| 97 | + // Format timestamp |
| 98 | + tm := time.Unix(rec.BlockTimestamp, 0) |
| 99 | + timeStr := tm.Format(time.RFC3339) |
| 100 | + |
| 101 | + // Handle nullable external block height |
| 102 | + extBlock := "null" |
| 103 | + if rec.ExternalBlockHeight != nil { |
| 104 | + extBlock = fmt.Sprintf("%d", *rec.ExternalBlockHeight) |
| 105 | + } |
| 106 | + |
| 107 | + fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%d\t%s\t%s\n", |
| 108 | + rec.Type, |
| 109 | + rec.Amount, |
| 110 | + formatHexShort(rec.FromAddress), |
| 111 | + formatHexShort(rec.ToAddress), |
| 112 | + formatHexShort(rec.InternalTxHash), |
| 113 | + formatHexShort(rec.ExternalTxHash), |
| 114 | + rec.Status, |
| 115 | + rec.BlockHeight, |
| 116 | + extBlock, |
| 117 | + timeStr, |
| 118 | + ) |
| 119 | + } |
| 120 | + w.Flush() |
| 121 | + |
| 122 | + fmt.Printf("\n✅ Successfully retrieved %d records.\n", len(history)) |
| 123 | + fmt.Println("\nNote: 'completed' means credited (deposits) or ready to claim (withdrawals). 'claimed' means withdrawn on Ethereum.") |
| 124 | +} |
0 commit comments