-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
export SubscriptionFilter type, add comments to WebSocket functions a…
…nd allow to pass custom RPC URL on PushChain.initialize (#75) * export SubscriptionFilter type and add comments to WebSocket functions * add websocket example and allow RPCURL at PushChain.initialize for custom RPC URLs
- Loading branch information
Showing
9 changed files
with
1,445 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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,41 @@ | ||
import { PushChain } from '@pushchain/devnet'; | ||
|
||
const pushChain = await PushChain.initialize(); | ||
|
||
// Connect to the WebSocket server | ||
await pushChain.ws.connect(); | ||
console.log('WebSocket connected.'); | ||
|
||
// Define a custom filter to only subscribe to blocks that include transactions with the category 'CUSTOM:SAMPLE_TX' | ||
const customFilters = [{ type: 'CATEGORY', value: ['CUSTOM:SAMPLE_TX'] }]; | ||
|
||
// Subscribe to block updates using the custom filter | ||
await pushChain.ws.subscribe(async (block) => { | ||
console.log('New block received:', block.blockHash); | ||
|
||
// Iterate over each transaction in the block | ||
for (const tx of block.transactions) { | ||
// Check if the transaction category matches our filter | ||
if (tx.category === 'CUSTOM:SAMPLE_TX') { | ||
console.log( | ||
`Found transaction with hash ${tx.hash} and category ${tx.category}` | ||
); | ||
|
||
try { | ||
// Fetch the full transaction details using the transaction hash | ||
const txDetails = await pushChain.tx.get(tx.hash); | ||
|
||
// Assume the fetched result contains a list of blocks and each block contains an array of transactions | ||
if (txDetails.blocks && txDetails.blocks.length > 0) { | ||
const fetchedTx = txDetails.blocks[0].transactions[0]; | ||
// Log the transaction data from the fetched transaction details | ||
console.log('Transaction Data:', fetchedTx.data); | ||
} else { | ||
console.log(`No details found for transaction hash ${tx.hash}`); | ||
} | ||
} catch (error) { | ||
console.error('Error fetching transaction details:', error); | ||
} | ||
} | ||
} | ||
}, customFilters); |
Oops, something went wrong.