Skip to content

Commit

Permalink
update modular
Browse files Browse the repository at this point in the history
  • Loading branch information
dangell7 committed Apr 16, 2024
1 parent 1302476 commit 2f0e5d5
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 17 deletions.
49 changes: 41 additions & 8 deletions contracts/funds/modular/master.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,29 +392,49 @@ int64_t hook(uint32_t r)
// settlement
case 'S':
{
ACCOUNT_TO_BUF(HOOKACC, hook_accid + 12);

uint8_t amt[48];
if (otxn_param(SBUF(amt), "AMT", 3) != 48)
NOPE("Master: Misconfigured. Missing AMT otxn parameter.");
int64_t sig_amt = *((int64_t *)amt);

uint8_t hash[32];
if (otxn_param(SBUF(hash), "AHS", 3) != 48)
if (otxn_param(SBUF(hash), "AHS", 3) != 32)
NOPE("Master: Misconfigured. Missing AHS otxn parameter.");

ACCOUNT_TO_BUF(HOOKACC, hook_accid + 12);
uint32_t sig_nce;
if (otxn_param(SVAR(sig_nce), "SEQ", 3) != 4)
NOPE("Master: Misconfigured. Missing SEQ otxn parameter.");

// check if the trustline exists
uint8_t keylet[34];
util_keylet(keylet, 34, KEYLET_LINE, hook_accid + 12, 20, amt + 28, 20, amt + 8, 20);
if (slot_set(SBUF(keylet), 10) != 10)
DONE("Master: Invalid trustline.");

// check trustline balance
slot_subfield(10, sfBalance, 11);
if (slot_size(11) != 48)
NOPE("Master: Could not fetch trustline balance.");

uint8_t low_limit[48];
if (slot_subfield(10, sfLowLimit, 13) != 13)
NOPE("Master: Could not slot subfield `sfLowLimit`");

if (slot(SVAR(low_limit), 13) != 48)
NOPE("Master: Could not slot `sfLowLimit`");

int64_t xfl_bal = slot_float(11);

// balance is negative and issuer is not low
if (float_sign(xfl_bal) && !BUFFER_EQUAL_20(amt + 28, low_limit + 28))
{
NOPE("Master: Insane balance on trustline.");
}

xfl_bal = float_sign(xfl_bal) ? float_negate(xfl_bal) : xfl_bal;

if (xfl_bal <= 0 || !float_compare(xfl_bal, 0, COMPARE_GREATER))
NOPE("Master: Insane balance on trustline.");

Expand All @@ -423,10 +443,6 @@ int64_t hook(uint32_t r)
state_set(SBUF(stl_account), SBUF(hash));
COPY_20(stl_account, DESTACC);

uint32_t sig_nce;
if (otxn_param(SVAR(sig_nce), "SEQ", 3) != 4)
NOPE("Master: Misconfigured. Missing SEQ otxn parameter.");

// check the nonce
uint32_t stl_seq;
state(SVAR(stl_seq), otxn_accid + 12, 20);
Expand Down Expand Up @@ -509,15 +525,32 @@ int64_t hook(uint32_t r)

// check if the trustline exists
uint8_t keylet[34];
util_keylet(keylet, 34, KEYLET_LINE, hook_accid + 12, 20, amt + 8, 20, amt + 28, 20);

util_keylet(keylet, 34, KEYLET_LINE, hook_accid + 12, 20, amt + 28, 20, amt + 8, 20);
if (slot_set(SBUF(keylet), 10) != 10)
DONE("User: Invalid trustline.");

// check trustline balance
slot_subfield(10, sfBalance, 11);
if (slot_size(11) != 48)
NOPE("Master: Could not fetch trustline balance.");

uint8_t low_limit[48];
if (slot_subfield(10, sfLowLimit, 13) != 13)
NOPE("Master: Could not slot subfield `sfLowLimit`");

if (slot(SVAR(low_limit), 13) != 48)
NOPE("Master: Could not slot `sfLowLimit`");

int64_t xfl_bal = slot_float(11);

// balance is negative and issuer is not low
if (float_sign(xfl_bal) && !BUFFER_EQUAL_20(amt + 28, low_limit + 28))
{
NOPE("Master: Insane balance on trustline.");
}

xfl_bal = float_sign(xfl_bal) ? float_negate(xfl_bal) : xfl_bal;

// check dtag balance
if (xfl_bal <= 0 || !float_compare(xfl_bal, 0, COMPARE_GREATER))
NOPE("Master: Insane balance on balance.");
Expand Down
63 changes: 54 additions & 9 deletions contracts/funds/modular/user.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,27 @@ int64_t hook(uint32_t r)
// check trustline balance
slot_subfield(10, sfBalance, 11);
if (slot_size(11) != 48)
NOPE("Funds: Could not fetch trustline balance.");
NOPE("User: Could not fetch trustline balance.");

uint8_t low_limit[48];
if (slot_subfield(10, sfLowLimit, 13) != 13)
NOPE("User: Could not slot subfield `sfLowLimit`");

if (slot(SVAR(low_limit), 13) != 48)
NOPE("User: Could not slot `sfLowLimit`");

int64_t xfl_bal = slot_float(11);

// balance is negative and issuer is not low
if (float_sign(xfl_bal) && !BUFFER_EQUAL_20(amt + 28, low_limit + 28))
{
NOPE("User: Insane balance on trustline.");
}

xfl_bal = float_sign(xfl_bal) ? float_negate(xfl_bal) : xfl_bal;

if (xfl_bal <= 0 || !float_compare(xfl_bal, 0, COMPARE_GREATER))
NOPE("Funds: Insane balance on trustline.");
NOPE("User: Insane balance on trustline.");

int64_t xfl_out;

Expand All @@ -424,10 +439,10 @@ int64_t hook(uint32_t r)
COPY_20(stl, DESTACC);

if (sig_amt <= 0)
NOPE("Funds: Must provide AMT param when performing settlement.");
NOPE("User: Must provide AMT param when performing settlement.");

if (float_compare(sig_amt, xfl_bal, COMPARE_GREATER))
NOPE("Funds: Balance not high enough for this settlement.");
NOPE("User: Balance not high enough for this settlement.");

// write payment amount
float_sto(OUTAMT, 49, amt + 8, 20, amt + 28, 20, sig_amt, sfAmount);
Expand All @@ -454,7 +469,7 @@ int64_t hook(uint32_t r)
int64_t emit_result = emit(SBUF(emithash), txn_out, TXNLEN);
if (emit_result < 0)
{
NOPE("Funds: Debit Emitted Failure.");
NOPE("User: Debit Emitted Failure.");
}

dbt_seq++;
Expand Down Expand Up @@ -517,12 +532,27 @@ int64_t hook(uint32_t r)
// check trustline balance
slot_subfield(10, sfBalance, 11);
if (slot_size(11) != 48)
NOPE("Funds: Could not fetch trustline balance.");
NOPE("User: Could not fetch trustline balance.");

uint8_t low_limit[48];
if (slot_subfield(10, sfLowLimit, 13) != 13)
NOPE("User: Could not slot subfield `sfLowLimit`");

if (slot(SVAR(low_limit), 13) != 48)
NOPE("User: Could not slot `sfLowLimit`");

int64_t xfl_bal = slot_float(11);

// balance is negative and issuer is not low
if (float_sign(xfl_bal) && !BUFFER_EQUAL_20(OUTISS, low_limit + 28))
{
NOPE("User: Insane balance on trustline.");
}

xfl_bal = float_sign(xfl_bal) ? float_negate(xfl_bal) : xfl_bal;

if (xfl_bal <= 0 || !float_compare(xfl_bal, 0, COMPARE_GREATER))
NOPE("Funds: Insane balance on trustline.");
NOPE("User: Insane balance on trustline.");

// check the nonce
uint32_t wth_seq;
Expand Down Expand Up @@ -597,12 +627,27 @@ int64_t hook(uint32_t r)
// check trustline balance
slot_subfield(10, sfBalance, 11);
if (slot_size(11) != 48)
NOPE("Funds: Could not fetch trustline balance.");
NOPE("User: Could not fetch trustline balance.");

uint8_t low_limit[48];
if (slot_subfield(10, sfLowLimit, 13) != 13)
NOPE("User: Could not slot subfield `sfLowLimit`");

if (slot(SVAR(low_limit), 13) != 48)
NOPE("User: Could not slot `sfLowLimit`");

int64_t xfl_bal = slot_float(11);

// balance is negative and issuer is not low
if (float_sign(xfl_bal) && !BUFFER_EQUAL_20(OUTISS, low_limit + 28))
{
NOPE("User: Insane balance on trustline.");
}

xfl_bal = float_sign(xfl_bal) ? float_negate(xfl_bal) : xfl_bal;

if (xfl_bal <= 0 || !float_compare(xfl_bal, 0, COMPARE_GREATER))
NOPE("Funds: Insane balance on trustline.");
NOPE("User: Insane balance on trustline.");

// check the nonce
uint32_t wth_seq;
Expand Down

0 comments on commit 2f0e5d5

Please sign in to comment.