Discussions

Ask a Question
Back to All

How to send additional params with a transaction and read in a hook ....

I have the following POC i am working on is it possible with hooks as i am unsure how i send and receive params ...

A transaction comes in from an account with an amount to the hook - it will also have the following params.

paymentAccount = xrpl account address
fundAccount = xrpl account address
contribution = % of transaction amount that will be for the fund

The hook will do the following ...

  1. Take the amount and calculate 1.5% and pay that to an account address that hook holds that is called the slpDAO
  2. It will take the contribution param and calculate % contribution from the amount and pay that to the fundAccount param
  3. It will add the 1.5% to the fund contribution amount and minus that from the amount and pay what is remaining to the paymentAccount

Here is an example ...

I make a transaction for 100 xrp
The contribution is 2%

1.5 xrp is payed to the slpDao
2 xrp is payed to the fundAccount
96.5 xrp is payed to the paymentAccount account

SUDO code ...

# define SLP_DAO_ADDR "rSLPDAOAddress"

# define PAYMENT_ACCOUNT_ADDR "rPaymentAccount"

# define FUND_ACCOUNT_ADDR "rFundAccount"

int64_t cbak(uint32_t reserved) {  
    return 0;  
}

int64_t hook(uint32_t reserved) {  
    // Addresses converted to account IDs  
    uint8_t slp_dao_accid[20], payment_accid[20], fund_accid[20];  
    util_accid(SBUF(slp_dao_accid), SBUF(SLP_DAO_ADDR));  
    // For payment_accid and fund_accid, you would need a way to parse these from the transaction, which is not explicitly covered here


uint8_t amount_buffer[48];
int64_t otxn_drops = AMOUNT_TO_DROPS(amount_buffer);

// Calculate distributions
int64_t slp_dao_amount = otxn_drops * 0.015; // 1.5% to slpDAO
int64_t fund_amount = otxn_drops * 0.02; // Example: 2% contribution to fundAccount, assuming this value is parsed from the transaction
int64_t payment_amount = otxn_drops - slp_dao_amount - fund_amount; // Remaining amount to paymentAccount

// Emit transactions for each distribution
emit_payment(slp_dao_accid, slp_dao_amount);
emit_payment(fund_accid, fund_amount);
emit_payment(payment_accid, payment_amount);

return 0;
  }

// Simplified function to emit a payment, pseudo-code as emitting requires detailed transaction preparation  
void emit_payment(uint8_t\* dest_accid, int64_t amount) {  
    unsigned char tx[PREPARE_PAYMENT_SIMPLE_SIZE];  
    PREPARE_PAYMENT_SIMPLE(SBUF(tx), amount, dest_accid, 0, 0);  
    uint8_t emithash[32];  
    emit(SBUF(emithash), SBUF(tx));  
}