Skip to content

Commit

Permalink
Use ceiling division
Browse files Browse the repository at this point in the history
This keeps the rounding logic the same as the heavily audited Sundae v1 logic
  • Loading branch information
Quantumplation committed Apr 11, 2024
1 parent 795542e commit 298f858
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
10 changes: 6 additions & 4 deletions lib/calculation/deposit.ak
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ pub fn do_deposit(
// If we have more b than a, then we can only take up to an quivalent amount of b, and return the rest
// otherwise if we have more a than b, we can return some amount of `a` to the user instead
if b_in_units_of_a > user_gives_a {
let give_b =
pool_state.quantity_b.3rd
* user_gives_a
/ pool_state.quantity_a.3rd
let num = pool_state.quantity_b.3rd * user_gives_a
let denom = pool_state.quantity_a.3rd
// Make sure to do ceiling division here, to round in favor fo the protocol
// That is, when in doubt, take up to one more token from the user than the
// LP tokens we issue would entail
let give_b = ((num - 1) / denom) + 1
(user_gives_a, give_b)
} else {
(b_in_units_of_a, user_gives_b)
Expand Down
4 changes: 2 additions & 2 deletions validators/tests/pool.ak
Original file line number Diff line number Diff line change
Expand Up @@ -507,15 +507,15 @@ fn scoop_swap_deposit(options: ScoopTestOptions) {
address: order2_out_addr,
value: value.from_lovelace(2_000_000)
|> value.add(constants.pool_script_hash, pool_lp_name(constants.pool_ident), 9_900_990)
|> value.add(constants.rberry_policy, constants.rberry_asset_name, 196_991),
|> value.add(constants.rberry_policy, constants.rberry_asset_name, 196_990),
datum: order2_out_datum,
reference_script: None,
}
let pool_output = Output {
address: pool_address,
value: option.or_else(options.edit_pool_output_value,
value.from_lovelace(1_000_000_000 + 20_000_000 + 5_000_000 + 2_000_000)
|> value.add(constants.rberry_policy, constants.rberry_asset_name, 1_000_000_000 - 9_896_088 + 10_000_000 - 196_991)
|> value.add(constants.rberry_policy, constants.rberry_asset_name, 1_000_000_000 - 9_896_088 + 10_000_000 - 196_990)
|> value.add(constants.pool_script_hash, pool_nft_name, 1)),
datum: InlineDatum(pool_out_datum),
reference_script: None,
Expand Down

0 comments on commit 298f858

Please sign in to comment.