-
Notifications
You must be signed in to change notification settings - Fork 1
feat(sdk-js): view bridge transaction history #151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,7 @@ out | |
| *.env | ||
|
|
||
| CLAUDE.md | ||
| GEMINI.md | ||
|
|
||
| # Orderbook example temporary files | ||
| examples/orderbook/.query_id | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import { NodeTNClient } from "../../src/index.node"; | ||
| import { Wallet } from "ethers"; | ||
| import dotenv from "dotenv"; | ||
|
|
||
| dotenv.config(); | ||
|
|
||
| async function main() { | ||
| const privateKey = process.env.TN_PRIVATE_KEY || "0000000000000000000000000000000000000000000000000000000000000001"; | ||
| const endpoint = process.env.TN_GATEWAY_URL || "https://gateway.testnet.truf.network"; | ||
| const wallet = new Wallet(privateKey); | ||
|
|
||
| const client = new NodeTNClient({ | ||
| endpoint: endpoint, | ||
| signerInfo: { | ||
| address: wallet.address, | ||
| signer: wallet, | ||
| }, | ||
| chainId: "testnet-v1", | ||
| timeout: 30000, | ||
| }); | ||
|
|
||
| console.log("🔄 Transaction History Demo (JS)"); | ||
| console.log("==============================="); | ||
| console.log(`Endpoint: ${endpoint}`); | ||
| console.log(`Wallet: ${wallet.address}\n`); | ||
|
|
||
| const bridgeId = "hoodi_tt2"; | ||
| const targetWallet = "0xc11Ff6d3cC60823EcDCAB1089F1A4336053851EF"; | ||
| const limit = 10; | ||
| const offset = 0; | ||
|
|
||
| console.log(`📋 Fetching history for bridge '${bridgeId}'...`); | ||
| console.log(` Wallet: ${targetWallet}`); | ||
| console.log(` Limit: ${limit}`); | ||
| console.log(` Offset: ${offset}`); | ||
| console.log("-".repeat(60)); | ||
|
|
||
| try { | ||
| const history = await client.getHistory(bridgeId, targetWallet, limit, offset); | ||
|
|
||
| if (history.length === 0) { | ||
| console.log("No history records found."); | ||
| return; | ||
| } | ||
|
|
||
| // Helper to format/shorten | ||
| const formatShort = (val: string | null): string => { | ||
| if (!val) return "null"; | ||
|
|
||
| let s = val; | ||
| // Check if it's Base64 (Kwil default) or already Hex | ||
| if (!s.startsWith("0x")) { | ||
| try { | ||
| const buffer = Buffer.from(s, 'base64'); | ||
| // Check if it looks like a hash/address (20 or 32 bytes) | ||
| if (buffer.length === 20 || buffer.length === 32) { | ||
| s = "0x" + buffer.toString('hex'); | ||
| } | ||
| } catch (e) { | ||
| // Not base64, keep as is | ||
| } | ||
| } | ||
|
|
||
| if (s.length > 12) { | ||
| return s.substring(0, 10) + "..."; | ||
| } | ||
| return s; | ||
| }; | ||
|
|
||
| // Print Header | ||
| console.log( | ||
| "TYPE".padEnd(12) + | ||
| "AMOUNT".padEnd(22) + | ||
| "FROM".padEnd(14) + | ||
| "TO".padEnd(14) + | ||
| "INT TX".padEnd(14) + | ||
| "EXT TX".padEnd(14) + | ||
| "STATUS".padEnd(12) + | ||
| "BLOCK".padEnd(8) + | ||
| "TIMESTAMP" | ||
| ); | ||
| console.log( | ||
| "-".repeat(12) + " " + | ||
| "-".repeat(22) + " " + | ||
| "-".repeat(14) + " " + | ||
| "-".repeat(14) + " " + | ||
| "-".repeat(14) + " " + | ||
| "-".repeat(14) + " " + | ||
| "-".repeat(12) + " " + | ||
| "-".repeat(8) + " " + | ||
| "-".repeat(20) | ||
| ); | ||
|
|
||
| for (const rec of history) { | ||
| const date = new Date(rec.block_timestamp * 1000).toISOString(); | ||
|
|
||
| console.log( | ||
| rec.type.padEnd(12) + | ||
| rec.amount.toString().padEnd(22) + | ||
| formatShort(rec.from_address).padEnd(14) + | ||
| formatShort(rec.to_address).padEnd(14) + | ||
| formatShort(rec.internal_tx_hash).padEnd(14) + | ||
| formatShort(rec.external_tx_hash).padEnd(14) + | ||
| rec.status.padEnd(12) + | ||
| rec.block_height.toString().padEnd(8) + | ||
| date | ||
| ); | ||
| } | ||
|
|
||
| console.log(`\n✅ Successfully retrieved ${history.length} records.`); | ||
| console.log("\nNote: 'completed' means credited (deposits) or ready to claim (withdrawals). 'claimed' means withdrawn on Ethereum."); | ||
|
|
||
| } catch (error) { | ||
| console.error("❌ Failed to fetch history:", error); | ||
| } | ||
| } | ||
|
|
||
| main(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { Action } from "./action"; | ||
| import { BridgeHistory } from "../types/bridge"; | ||
| import { KwilSigner, NodeKwil } from "@trufnetwork/kwil-js"; | ||
| import { Either } from "monads-io"; | ||
| import { vi, describe, it, expect } from "vitest"; | ||
|
|
||
| describe("Action", () => { | ||
| // Mock dependencies | ||
| const mockKwil = { | ||
| call: vi.fn(), | ||
| } as unknown as NodeKwil; | ||
|
|
||
| const mockSigner = { | ||
| signatureType: "secp256k1_ep", | ||
| } as unknown as KwilSigner; | ||
|
|
||
| it("should call get_history with correct parameters", async () => { | ||
| const action = new Action(mockKwil, mockSigner); | ||
|
|
||
| // Spy on the 'call' method of the action instance | ||
| const callSpy = vi.spyOn(action as any, "call"); | ||
|
|
||
| const mockHistory: BridgeHistory[] = [{ | ||
| type: "deposit", | ||
| amount: "100", | ||
| from_address: null, | ||
| to_address: "0x123", | ||
| internal_tx_hash: null, | ||
| external_tx_hash: "0xabc", | ||
| status: "completed", | ||
| block_height: 10, | ||
| block_timestamp: 1000, | ||
| external_block_height: 5 | ||
| }]; | ||
|
|
||
| callSpy.mockResolvedValue(Either.right(mockHistory)); | ||
|
|
||
| const result = await action.getHistory("sepolia_bridge", "0x123", 10, 5); | ||
|
|
||
| expect(callSpy).toHaveBeenCalledWith( | ||
| "sepolia_bridge_get_history", | ||
| { | ||
| $wallet_address: "0x123", | ||
| $limit: 10, | ||
| $offset: 5 | ||
| } | ||
| ); | ||
|
|
||
| expect(result).toEqual(mockHistory); | ||
| }); | ||
|
|
||
| it("should handle null results from get_history", async () => { | ||
| const action = new Action(mockKwil, mockSigner); | ||
| const callSpy = vi.spyOn(action as any, "call"); | ||
|
|
||
| callSpy.mockResolvedValue(Either.right(null)); | ||
|
|
||
| const result = await action.getHistory("bridge", "0x123"); | ||
|
|
||
| expect(result).toEqual([]); | ||
| }); | ||
|
|
||
| it("should use default parameters for getHistory", async () => { | ||
| const action = new Action(mockKwil, mockSigner); | ||
| const callSpy = vi.spyOn(action as any, "call"); | ||
|
|
||
| callSpy.mockResolvedValue(Either.right([])); | ||
|
|
||
| await action.getHistory("bridge", "0x123"); | ||
|
|
||
| expect(callSpy).toHaveBeenCalledWith( | ||
| "bridge_get_history", | ||
| { | ||
| $wallet_address: "0x123", | ||
| $limit: 20, | ||
| $offset: 0 | ||
| } | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.