If you want to implement CAT-CAT swaps as well, precision will become a problem. Instead by multiplying RATE by a constant and then dividing the output amount, you can 'borrow' the mechanism of AMM liquidity distribution.
In short, every coin will have its amount, my_amount (the amount of asset to offer), and REMAINING_AMOUNT, the remaining amount of the asset to receive (if you can find a better naming thing, go for it!). When a user gives partial_amount <= REMAINING_AMOUNT from the coin, they'll receive my_amount * partial_amount / REMAINING_AMOUNT.
Note that doing it this way (as opposed to calculating what the user should give based on given_amount / my_amount) will slightly favor the partial coin until the very last trade.
Note: Best implemented after
SELF_HASH
If you want to implement CAT-CAT swaps as well, precision will become a problem. Instead by multiplying
RATE
by a constant and then dividing the output amount, you can 'borrow' the mechanism of AMM liquidity distribution.In short, every coin will have its amount,
my_amount
(the amount of asset to offer), andREMAINING_AMOUNT
, the remaining amount of the asset to receive (if you can find a better naming thing, go for it!). When a user givespartial_amount <= REMAINING_AMOUNT
from the coin, they'll receivemy_amount * partial_amount / REMAINING_AMOUNT
.Note that doing it this way (as opposed to calculating what the user should give based on
given_amount / my_amount
) will slightly favor the partial coin until the very last trade.