Skip to content

Commit

Permalink
Merge pull request #29 from SundaeSwap-finance/pi/SSW-304
Browse files Browse the repository at this point in the history
SSW-304: redundant datum parameter
  • Loading branch information
Quantumplation authored Jan 16, 2024
2 parents 5d78f9e + 38f2eb7 commit c143cd3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 32 deletions.
16 changes: 9 additions & 7 deletions lib/calculation/deposit.ak
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ use aiken/transaction/credential.{Address, VerificationKeyCredential}
use aiken/transaction/value.{Value, ada_policy_id, ada_asset_name}
use calculation/shared.{PoolState} as calc_shared
use sundae/multisig
use types/order.{Destination, Deposit, OrderDatum}
use shared.{SingletonValue}
use types/order.{Destination, OrderDatum}

pub fn do_deposit(
pool_state: PoolState,
input_value: Value,
order: OrderDatum,
assets: (SingletonValue, SingletonValue),
destination: Destination,
actual_protocol_fee: Int,
output: Output,
) -> PoolState {
expect order.Deposit { assets } = order.details
let (asset_a, asset_b) = assets

// Policy ID and token name of the assets must match the pool.
Expand Down Expand Up @@ -74,8 +75,8 @@ pub fn do_deposit(
issued_lp_tokens,
)

expect output.address == order.destination.address
expect output.datum == order.destination.datum
expect output.address == destination.address
expect output.datum == destination.datum
expect output.value == out_value
PoolState {
quantity_a: (
Expand Down Expand Up @@ -119,6 +120,7 @@ test deposit_test() {
let input_value =
value.from_lovelace(14_500_000)
|> value.add(rberry.1st, rberry.2nd, 10_000_000)
let assets = ((ada.1st, ada.2nd, 10_000_000), (rberry.1st, rberry.2nd, 10_000_000))
let order = OrderDatum {
pool_ident: None,
owner: multisig.Signature(
Expand All @@ -130,7 +132,7 @@ test deposit_test() {
datum: NoDatum,
},
details: order.Deposit {
assets: ((ada.1st, ada.2nd, 10_000_000), (rberry.1st, rberry.2nd, 10_000_000)),
assets: assets,
},
extension: Void,
}
Expand All @@ -141,7 +143,7 @@ test deposit_test() {
datum: NoDatum,
reference_script: None,
}
let final_pool_state = do_deposit(pool_state, input_value, order, 2_500_000, output)
let final_pool_state = do_deposit(pool_state, input_value, assets, order.destination, 2_500_000, output)
expect final_pool_state.quantity_a.3rd == 1_010_000_000
expect final_pool_state.quantity_b.3rd == 1_010_000_000
expect final_pool_state.protocol_fees == 4_500_000
Expand Down
21 changes: 11 additions & 10 deletions lib/calculation/donation.ak
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ use aiken/transaction.{NoDatum, Output}
use aiken/transaction/credential.{Address, VerificationKeyCredential}
use aiken/transaction/value.{Value}
use calculation/shared.{PoolState} as calc_shared
use shared
use shared.{SingletonValue}
use sundae/multisig
use types/order.{Destination, OrderDatum}

pub fn do_donation(
pool_state: PoolState,
input_value: Value,
order: OrderDatum,
assets: (SingletonValue, SingletonValue),
destination: Destination,
actual_protocol_fee: Int,
output: Output,
) -> (PoolState, Bool) {
expect order.Donation { assets } = order.details
expect assets.1st.1st == pool_state.quantity_a.1st
expect assets.1st.2nd == pool_state.quantity_a.2nd
expect assets.2nd.1st == pool_state.quantity_b.1st
Expand All @@ -27,8 +27,8 @@ pub fn do_donation(
let has_remainder = remainder != value.zero()
let Void =
if has_remainder {
expect output.address == order.destination.address
expect output.datum == order.destination.datum
expect output.address == destination.address
expect output.datum == destination.datum
expect output.value == remainder
Void
} else {
Expand Down Expand Up @@ -77,6 +77,10 @@ test donation() {
let input_value =
value.from_lovelace(1_000_000)
|> value.add(rberry.1st, rberry.2nd, 1_000_000)
let assets = (
(ada.1st, ada.2nd, 1_000_000),
(rberry.1st, rberry.2nd, 1_000_000),
)
let order =
OrderDatum {
pool_ident: None,
Expand All @@ -86,10 +90,7 @@ test donation() {
max_protocol_fee: 2_500_000,
destination: Destination { address: addr, datum: NoDatum },
details: order.Donation {
assets: (
(ada.1st, ada.2nd, 1_000_000),
(rberry.1st, rberry.2nd, 1_000_000),
),
assets: assets,
},
extension: Void,
}
Expand All @@ -102,7 +103,7 @@ test donation() {
reference_script: None,
}
let (final_pool_state, has_remainder) =
do_donation(pool_state, input_value, order, 2_500_000, output)
do_donation(pool_state, input_value, assets, order.destination, 2_500_000, output)
expect !has_remainder
expect final_pool_state.quantity_a.3rd == 1_001_000_000
expect final_pool_state.quantity_b.3rd == 1_001_000_000
Expand Down
15 changes: 6 additions & 9 deletions lib/calculation/process.ak
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ pub fn process_order(
value: Value,
details: Order,
max_protocol_fee: Int,
datum: OrderDatum,
destination: Destination,
amortized_base_fee: Int,
simple_fee: Int,
Expand All @@ -120,7 +119,6 @@ pub fn process_order(
value,
details,
max_protocol_fee,
datum,
destination,
amortized_base_fee,
// We pass strategy_fee here, instead of simple_fee,
Expand All @@ -147,31 +145,31 @@ pub fn process_order(
)
(next, rest_outputs, fee)
}
order.Deposit(..) -> {
order.Deposit(assets) -> {
expect [output, ..rest_outputs] = outputs
// Make sure the scooper can only take up to the max fee the user has agreed to
let fee = amortized_base_fee + simple_fee
expect max_protocol_fee >= fee
let next = deposit.do_deposit(initial, value, datum, fee, output)
let next = deposit.do_deposit(initial, value, assets, destination, fee, output)
(next, rest_outputs, fee)
}
order.Withdrawal(..) -> {
order.Withdrawal(amount) -> {
expect [output, ..rest_outputs] = outputs
// Make sure the scooper can only take up to the max fee the user has agreed to
let fee = amortized_base_fee + simple_fee
expect max_protocol_fee >= fee
let next =
withdrawal.do_withdrawal(initial, value, datum, fee, output)
withdrawal.do_withdrawal(initial, value, amount, destination, fee, output)
(next, rest_outputs, fee)
}
// order.Zap(..) -> do_zap(initial, input, datum)
order.Donation(..) -> {
order.Donation(assets) -> {
expect [output, ..rest_outputs] = outputs
// Make sure the scooper can only take up to the max fee the user has agreed to
let fee = amortized_base_fee + simple_fee
expect max_protocol_fee >= fee
let (next, used_output) =
donation.do_donation(initial, value, datum, fee, output)
donation.do_donation(initial, value, assets, destination, fee, output)
if used_output {
(next, rest_outputs, fee)
} else {
Expand Down Expand Up @@ -237,7 +235,6 @@ pub fn process_orders(
order.value,
details,
max_protocol_fee,
datum, // TODO: can we get rid of this?
destination,
amortized_base_fee,
simple_fee,
Expand Down
14 changes: 8 additions & 6 deletions lib/calculation/withdrawal.ak
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ use aiken/transaction/credential.{Address, VerificationKeyCredential}
use aiken/transaction/value.{Value, ada_policy_id, ada_asset_name}
use calculation/shared.{PoolState} as calc_shared
use sundae/multisig
use shared.{SingletonValue}
use types/order.{Destination, OrderDatum}

pub fn do_withdrawal(
pool_state: PoolState,
input_value: Value,
order: OrderDatum,
amount: SingletonValue,
destination: Destination,
actual_protocol_fee: Int,
output: Output,
) -> PoolState {
expect order.Withdrawal { amount } = order.details
let (lp_policy, lp_asset_name, amount) = amount

expect lp_policy == pool_state.quantity_lp.1st
Expand All @@ -41,8 +42,8 @@ pub fn do_withdrawal(
)

// Check that the payout is satisfied by the next output
expect output.address == order.destination.address
expect output.datum == order.destination.datum
expect output.address == destination.address
expect output.datum == destination.datum
expect output.value == remainder
PoolState {
quantity_a: (
Expand Down Expand Up @@ -125,6 +126,7 @@ fn withdrawal_test(options: WithdrawalTestOptions) {
let input_value =
value.from_lovelace(4_500_000)
|> value.add(lp.1st, lp.2nd, 10_000_000)
let amount = (lp.1st, lp.2nd, 10_000_000)
let order = OrderDatum {
pool_ident: None,
owner: multisig.Signature(
Expand All @@ -136,7 +138,7 @@ fn withdrawal_test(options: WithdrawalTestOptions) {
datum: NoDatum,
},
details: order.Withdrawal {
amount: (lp.1st, lp.2nd, 10_000_000),
amount
},
extension: Void,
}
Expand All @@ -150,7 +152,7 @@ fn withdrawal_test(options: WithdrawalTestOptions) {
datum: NoDatum,
reference_script: None,
}
let final_pool_state = do_withdrawal(pool_state, input_value, order, 2_500_000, output)
let final_pool_state = do_withdrawal(pool_state, input_value, amount, order.destination, 2_500_000, output)
expect final_pool_state.quantity_a.3rd == 1_000_000_000
expect final_pool_state.quantity_b.3rd == 1_000_000_000
expect final_pool_state.protocol_fees == 4_500_000
Expand Down

0 comments on commit c143cd3

Please sign in to comment.