Set Hook
The setHooksV3
function in the SDK is used to set hooks on the XRPL. It takes in a SetHookParams
object as a parameter, which includes the client, seed, and hooks to be set.
Setting Hooks with setHooksV3
To set a hook on the XRPL using the setHooksV3
function, you need to provide the following parameters:
client
: The XRPL client object.seed
: The seed of the account that will set the hook.hooks
: An array of hook objects to be set.
Each hook object in the hooks
array should have the following properties:
Hook
: The hook payload object.
Here is an example of setting a hook using the setHooksV3
function:
import {
SetHookFlags
} from '@transia/xrpl'
import {
setHooksV3,
createHookPayload,
SetHookParams
} from '@transia/hooks-toolkit'
const hook = createHookPayload({
version: 0, // HookApiVersion
createFile: 'hook_on_tt', // filename in /build
namespace: 'hook_on_tt', // namespace (ascii)
flags: SetHookFlags.hsfOverride, // SetHookFlag
hookOnArray: ['Invoke'] // HookOn Transactions
})
await setHooksV3({
client: testContext.client,
seed: testContext.hook1.seed,
hooks: [{ Hook: hook }],
} as SetHookParams)
In the example above, we create a hook payload using the createHookPayload
function and set the hook_on
field to trigger on the Invoke
transaction type. We then pass the hook payload as an object in the hooks
array to the setHooksV3
function.
Note that the setHooksV3
function is an asynchronous function and returns a Promise. You can use await
to wait for the function to complete.
Deleting Hooks with clearAllHooksV3
To delete all hooks on the XRPL using the clearAllHooksV3
function, you need to provide the following parameters:
client
: The XRPL client object.seed
: The seed of the account that will remove the hook.
Here is an example of deleting all hooks using the clearAllHooksV3
function:
import {
clearAllHooksV3,
} from '@transia/hooks-toolkit'
await clearAllHooksV3({
client: testContext.client,
seed: testContext.hook1.seed,
} as SetHookParams)
Deleting a single hook with setHooksV3
To delete a single hook and state on the XRPL using the setHooksV3
function, you need to provide the following parameters:
client
: The XRPL client object.seed
: The seed of the account that will delete the hook.hooks
: An array of hook objects to be deleted.
Here is an example of deleting a single hook for the hook in position 2 using the setHooksV3
function:
import {
SetHookFlags
} from '@transia/xrpl'
import {
SetHookParams,
createHookPayload,
setHooksV3,
} from '@transia/hooks-toolkit'
const clearHook = createHookPayload({
namespace: 'mynamespace', // namespace (ascii)
flags: SetHookFlags.hsfOverride | SetHookFlags.hsfNSDelete, // SetHookFlag
})
await setHooksV3({
client: testContext.client,
seed: testContext.hook1.seed,
hooks: [{Hook: {}}, { Hook: clearHook }],
} as SetHookParams)
Credit: Omar Khan: https://github.com/khancode