Skip to content

Instantly share code, notes, and snippets.

@WietseWind
Created April 14, 2022 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WietseWind/ec1bcbd42c159d45a206a4e29f5135c7 to your computer and use it in GitHub Desktop.
Save WietseWind/ec1bcbd42c159d45a206a4e29f5135c7 to your computer and use it in GitHub Desktop.
Hooks AssemblyScript V2
#!/bin/bash
rm ./module.wasm
rm ./module.wat
node assemblyscript/bin/asc \
-O3 \
--noAssert \
--runtime minimal \
--textFile ./module.wat \
--outFile ./module.wasm \
--lib ./src/lib \
./src/module.ts
node install sXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
require('./xrpld-hooks/hook-api-examples/utils-tests.js')
.TestRig('wss://hooks-testnet-v2.xrpl-labs.com').then(t =>
{
let wasm = t.findWasm()
if (wasm.length != 1) {
console.log('There should be exactly one .wasm file in the current working directory.')
process.exit(1)
}
const secret = process.argv[2]
const account = t.xrpljs.Wallet.fromSeed(secret)
t.api.submit({
Account: account.classicAddress,
TransactionType: 'SetHook',
Hooks: [
{
Hook: {
CreateCode: t.wasm(wasm[0]),
HookApiVersion: 0,
HookNamespace: 'CAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFE',
HookOn: '0000000000000000',
Flags: t.hsfOVERRIDE
}
}
],
Fee: '100000'
}, {
wallet: account
}).then(x => {
t.assertTxnSuccess(x)
console.log(x)
process.exit(0)
}).catch(e => console.log('e2', e))
})
.catch(e => console.log('e1', e))
@external("env", "_g")
export declare function GUARD(id: i32, maxiter: i32): i32
@external("env", "accept")
export declare function ACCEPT(read_ptr: string, read_len: i32, err: i64): i64
@external("env", "trace")
export declare function TRACE(mread_ptr: string, mread_len: i32, dread_ptr: string, dread_len: i32, as_hex: i32): i64
////////////////////
export function LOG (text: string): void {
TRACE(text, text.length * 2, text, text.length * 2, 0)
}
export function OK (text: string = 'OK'): void {
ACCEPT(text, text.length * 2, 0)
}
export function cbak(reserved: i32): i64 {
return 0
}
export function hook(reserved: i32): i64 {
for (let i = 0; GUARD(10, 10), i < 3; i++) {
LOG('-- LOOP :D --')
}
OK('-- OK :) --')
GUARD(1, 1)
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment