-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
187 additions
and
49 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
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,61 @@ | ||
#include "hookapi.h" | ||
|
||
#define OTXN_AMT_TO_XFL(buf) float_set(-6, (AMOUNT_TO_DROPS(buf))) | ||
|
||
int64_t hook(uint32_t reserved) | ||
{ | ||
// ROLLBACK: Because not setting this would be a misconfiguration | ||
uint64_t limit_amt; | ||
if(hook_param(SVAR(limit_amt), "A", 1) != 8) | ||
rollback(SBUF("Treasury: Misconfigured. Amount 'A' not set as Hook parameter"), 1); | ||
|
||
// ROLLBACK: Because not setting this would be a misconfiguration | ||
int32_t limit_ledger; | ||
if(hook_param(SVAR(limit_ledger), "L", 1) != 4) | ||
rollback(SBUF("Treasury: Misconfigured. Ledger limit 'L' not set as Hook parameter"), 2); | ||
|
||
uint8_t otxn_account[20]; | ||
otxn_field(SBUF(otxn_account), sfAccount); | ||
|
||
uint8_t account[20]; | ||
hook_account(SBUF(otxn_account)); | ||
if(!BUFFER_EQUAL_20(otxn_account, account)) | ||
accept(SBUF("Treasury: Incoming Transaction."), 7); | ||
|
||
int64_t type = otxn_type(); | ||
if (type == ttCLAIM_REWARD) | ||
accept(SBUF("Treasury: ClaimReward Successful."), 4); | ||
|
||
// MUST ROLLBACK | ||
uint8_t amount_buf[8]; | ||
if(otxn_field(amount_buf, 8, sfAmount) != 8) | ||
rollback(SBUF("Treasury: Non XAH currency payments are forbidden."), 6); | ||
|
||
int32_t last_release = 0; | ||
state(SVAR(last_release), "LAST", 4); | ||
|
||
int32_t current_ledger = ledger_seq(); | ||
|
||
// MUST ROLLBACK | ||
if ((last_release + limit_ledger) > current_ledger) | ||
rollback(SBUF("Treasury: You need to wait longer to withdraw."), 8); | ||
|
||
if (last_release == 0 && type == ttSET_HOOK) | ||
accept(SBUF("Treasury: Hook Set Successfully."), 13); | ||
|
||
// MUST ROLLBACK | ||
if (type != ttPAYMENT) | ||
rollback(SBUF("Treasury: Only ClaimReward and Payment txns are allowed."), 5); | ||
|
||
// MUST ROLLBACK | ||
int64_t amount_xfl = OTXN_AMT_TO_XFL(amount_buf); | ||
if(float_compare(amount_xfl, limit_amt, COMPARE_GREATER) == 1) | ||
rollback(SBUF("Treasury: Outgoing transaction exceeds the limit set by you."), 9); | ||
|
||
if (state_set(SVAR(current_ledger), "LAST", 4) != 4) | ||
rollback(SBUF("Treasury: Could not update state entry, bailing."), 11); | ||
|
||
accept(SBUF("Treasury: Released successfully."), 12); | ||
_g(1,1); | ||
return 0; | ||
} |
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,104 @@ | ||
// xrpl | ||
import { | ||
Client, | ||
Wallet, | ||
AccountSetAsfFlags, | ||
SetHookFlags, | ||
TransactionMetadata, | ||
ClaimReward, | ||
} from '@transia/xrpl' | ||
// xrpl-helpers | ||
import { accountSet } from '@transia/hooks-toolkit/dist/npm/src/libs/xrpl-helpers' | ||
// src | ||
import { | ||
Xrpld, | ||
SetHookParams, | ||
setHooksV3, | ||
createHookPayload, | ||
iHookParamEntry, | ||
iHookParamName, | ||
iHookParamValue, | ||
ExecutionUtility, | ||
} from '@transia/hooks-toolkit' | ||
import { xrpAddressToHex } from '@transia/hooks-toolkit/dist/npm/src/libs/binary-models' | ||
|
||
const serverUrl = 'wss://xahau-test.net' | ||
const hookSeed = 'snnf9az3KyNUfVRtER43grtixDrRy' | ||
const destSeed = 'snDnCTwy5k2UdFq7fYnWxiXeEbdfJ' | ||
|
||
describe('treasury', () => { | ||
let client: Client | ||
|
||
beforeAll(async () => { | ||
client = new Client(serverUrl) | ||
await client.connect() | ||
client.networkID = await client.getNetworkID() | ||
const hookWallet = Wallet.fromSeed(hookSeed) | ||
const destWallet = Wallet.fromSeed(destSeed) | ||
|
||
accountSet(client, hookWallet, AccountSetAsfFlags.asfTshCollect) | ||
const hookParam1 = new iHookParamEntry( | ||
new iHookParamName('DST'), | ||
new iHookParamValue(xrpAddressToHex(destWallet.classicAddress), true) | ||
) | ||
const acct1hook1 = createHookPayload({ | ||
version: 0, | ||
createFile: 'genesis_mint', | ||
namespace: 'treasury', | ||
flags: SetHookFlags.hsfCollect + SetHookFlags.hsfOverride, | ||
hookOnArray: ['GenesisMint'], | ||
hookParams: [hookParam1.toXrpl()], | ||
}) | ||
await setHooksV3({ | ||
client: client, | ||
seed: hookWallet.seed, | ||
hooks: [{ Hook: acct1hook1 }], | ||
} as SetHookParams) | ||
}) | ||
|
||
afterAll(async () => { | ||
client.disconnect() | ||
}) | ||
|
||
it('treasury - success', async () => { | ||
client.networkID = await client.getNetworkID() | ||
const hookWallet = Wallet.fromSeed(hookSeed) | ||
|
||
// Opt In ClaimReward | ||
const optInTx: ClaimReward = { | ||
TransactionType: 'ClaimReward', | ||
Account: hookWallet.classicAddress, | ||
Issuer: 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', | ||
} | ||
await Xrpld.submit(client, { | ||
wallet: hookWallet, | ||
tx: optInTx, | ||
debugStream: true, | ||
}) | ||
|
||
// Wait 100 Seconds | ||
await new Promise((resolve) => setTimeout(resolve, 100000)) // await | ||
|
||
// ClaimReward | ||
const claimTx: ClaimReward = { | ||
TransactionType: 'ClaimReward', | ||
Account: hookWallet.classicAddress, | ||
Issuer: 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh', | ||
} | ||
const result = await Xrpld.submit(client, { | ||
wallet: hookWallet, | ||
tx: claimTx, | ||
debugStream: true, | ||
}) | ||
|
||
const hookExecutions = await ExecutionUtility.getHookExecutionsFromMeta( | ||
client, | ||
result.meta as TransactionMetadata | ||
) | ||
console.log(hookExecutions) | ||
|
||
expect(hookExecutions.executions[0].HookReturnString).toMatch( | ||
'auction_start.c: Tx emitted success' | ||
) | ||
}) | ||
}) |