@@ -14,21 +14,22 @@ use miden::standards::wallets::basic as wallet
1414# CONSTANTS
1515# =================================================================================================
1616
17- const NUM_STORAGE_ITEMS=6
17+ const NUM_STORAGE_ITEMS=7
1818const MAX_U32=0x0000000100000000
1919
2020# Attachment scheme used by both PSWAP output notes (payback P2ID + remainder PSWAP).
2121# Carries the word [amount, order_id, depth, 0]. Mirrors
2222# StandardNoteAttachment::PswapAttachment in the Rust standards crate.
2323const PSWAP_ATTACHMENT_SCHEME = 3
2424
25- # Note storage layout (6 felts, loaded at STORAGE_PTR by get_storage):
25+ # Note storage layout (7 felts, loaded at STORAGE_PTR by get_storage):
2626# - requested_faucet_suffix [0] : 1 felt
2727# - requested_faucet_prefix [1] : 1 felt
2828# - min_requested_amount [2] : 1 felt
2929# - payback_note_type [3] : 1 felt
30- # - creator_id_prefix [4] : 1 felt
31- # - creator_id_suffix [5] : 1 felt
30+ # - creator_id_suffix [4] : 1 felt
31+ # - creator_id_prefix [5] : 1 felt
32+ # - min_fill_amount [6] : 1 felt (0 = no minimum)
3233#
3334# Where:
3435# - requested_faucet_suffix: Suffix of the requested asset's faucet AccountId (Felt)
@@ -50,8 +51,9 @@ const REQUESTED_FAUCET_SUFFIX_ITEM = STORAGE_PTR
5051const REQUESTED_FAUCET_PREFIX_ITEM = STORAGE_PTR + 1
5152const MIN_REQUESTED_AMOUNT_ITEM = STORAGE_PTR + 2
5253const PAYBACK_NOTE_TYPE_ITEM = STORAGE_PTR + 3
53- const PSWAP_CREATOR_PREFIX_ITEM = STORAGE_PTR + 4
54- const PSWAP_CREATOR_SUFFIX_ITEM = STORAGE_PTR + 5
54+ const PSWAP_CREATOR_SUFFIX_ITEM = STORAGE_PTR + 4
55+ const PSWAP_CREATOR_PREFIX_ITEM = STORAGE_PTR + 5
56+ const MIN_FILL_AMOUNT_ITEM = STORAGE_PTR + 6
5557
5658# Local memory offsets
5759# =================================================================================================
@@ -99,6 +101,7 @@ const ERR_PSWAP_WRONG_NUMBER_OF_ASSETS="PSWAP script requires exactly one note a
99101const ERR_PSWAP_FILL_SUM_OVERFLOW="PSWAP account_fill + note_fill overflows u64"
100102const ERR_PSWAP_NOT_VALID_ASSET_AMOUNT="PSWAP computed amount exceeds max fungible asset amount"
101103const ERR_PSWAP_PAYOUT_OVERFLOW="PSWAP payout quotient does not fit in u64"
104+ const ERR_PSWAP_FILL_BELOW_MINIMUM="PSWAP fill amount is below minimum fill amount"
102105
103106# U64 VALIDATION
104107# =================================================================================================
550553#! Inputs: [account_fill_amount, note_fill_amount]
551554#! Outputs: []
552555#!
553- @locals(10 )
556+ @locals(11 )
554557# EXEC_AMT_OFFERED : amt_offered
555558# EXEC_AMT_MIN_REQUESTED : amt_min_requested
556559# EXEC_AMT_PAYOUT_TOTAL : amt_payout (total)
561564# EXEC_AMT_REQUESTED_ACCOUNT_FILL : amt_requested_account_fill
562565# EXEC_AMT_REQUESTED_NOTE_FILL : amt_requested_note_fill
563566# EXEC_AMT_FILL_REFERENCE : max(total_fill, min_requested_amount)
567+ # EXEC_MIN_FILL_AMOUNT : min_fill_amount (0 = no minimum)
564568proc execute_pswap
565569 loc_store.EXEC_AMT_REQUESTED_ACCOUNT_FILL
566570 loc_store.EXEC_AMT_REQUESTED_NOTE_FILL
@@ -608,6 +612,22 @@ proc execute_pswap
608612 swap mul.MAX_U32 add
609613 # => [fill_amount]
610614
615+ # Check min_fill_amount: if set (non-zero), fill must be >= min_fill_amount
616+ mem_load.MIN_FILL_AMOUNT_ITEM
617+ # => [min_fill_amount, fill_amount]
618+ dup.1 dup.1
619+ # => [fill_amount, min_fill_amount, min_fill_amount, fill_amount]
620+ eq.0
621+ # => [min_fill_amount_is_zero, min_fill_amount, fill_amount]
622+ if.true
623+ drop drop
624+ # => [fill_amount]
625+ else
626+ dup.1 dup.1 gte assert.err=ERR_PSWAP_FILL_BELOW_MINIMUM
627+ # => [fill_amount]
628+ end
629+ # => [fill_amount]
630+
611631 # fill_reference = max(fill, min_requested_amount).
612632 loc_load.EXEC_AMT_MIN_REQUESTED
613633 # => [min_requested, fill_amount]
0 commit comments