Filters are essential for controlling the flow of transactions in your hooks. They allow you to specify conditions under which certain transactions should be allowed or rejected. Each hook execution is associated with a transaction, and you can access the transaction details through the otxn_json
function. Below are examples of how to implement various filters based on transaction types.
FILTERSincoming
Only Allow Incoming
import { arrayEqual, sfAccount } from 'jshooks-api'
...
const hook_accid = hook_account()
const otxn_accid = otxn_field(sfAccount)
if (!arrayEqual(hook_accid as number[], otxn_accid as number[])) {
return rollback('myhook.ts: Outgoing Tx.', 0)
}
// ... Do Stuff
FILTERSoutgoing
Only Allow Outgoing
import { arrayEqual, sfAccount } from 'jshooks-api'
...
const hook_accid = hook_account()
const otxn_accid = otxn_field(sfAccount)
if (arrayEqual(hook_accid as number[], otxn_accid as number[])) {
return rollback('myhook.ts: Incoming Tx.', 0)
}
// ... Do Stuff
FILTERSxah
Only Allow XAH
import { Payment } from '@transia/xahau-models'
...
const txn = otxn_json() as Payment
if (typeof txn.Amount === "object") {
return rollback('myhook.ts: Ignoring non XAH Transaction.', 0)
}
// ... Do Stuff
FILTERStoken
Only Allow IOU Tokens
import { Payment } from '@transia/xahau-models'
...
const txn = otxn_json() as Payment
if (typeof txn.Amount === "string") {
return rollback('myhook.ts: Ignoring XAH Transaction.', 0)
}
// ... Do Stuff