Skip to content

Commit

Permalink
Merge pull request #81 from SundaeSwap-finance/pi/pool-manage-fix
Browse files Browse the repository at this point in the history
Pi/pool-manage-fix
  • Loading branch information
Quantumplation authored May 9, 2024
2 parents da66d15 + e675ea6 commit 1ea250d
Show file tree
Hide file tree
Showing 38 changed files with 3,785 additions and 2,458 deletions.
4 changes: 2 additions & 2 deletions aiken.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ source = "github"

[[requirements]]
name = "aiken-extra/tx_util"
version = "1.170.202312"
version = "39d4bceebeba6e02ffa72d2a2411487d97d2a738"
source = "github"

[[packages]]
Expand All @@ -30,7 +30,7 @@ source = "github"

[[packages]]
name = "aiken-extra/tx_util"
version = "1.170.202312"
version = "39d4bceebeba6e02ffa72d2a2411487d97d2a738"
requirements = []
source = "github"

Expand Down
2 changes: 1 addition & 1 deletion aiken.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ source = "github"

[[dependencies]]
name = "aiken-extra/tx_util"
version = "1.170.202312"
version = "39d4bceebeba6e02ffa72d2a2411487d97d2a738"
source = "github"
115 changes: 99 additions & 16 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,37 +1,120 @@
AIKEN=$1

aiken() {
/usr/bin/env aiken $*
${AIKEN} $*
}

set -e

aiken build
echo "Software versions:"
echo " Git commit = $(git rev-parse HEAD)"
echo " Aiken Version = $(aiken --version)"


echo
echo "File hashes:"
SHA256=$(cat validators/oracle.ak | sha256sum | cut -f 1 -d ' ')
echo " validators/oracle.ak = ${SHA256}"
SHA256=$(cat validators/order.ak | sha256sum | cut -f 1 -d ' ')
echo " validators/order.ak = ${SHA256}"
SHA256=$(cat validators/pool_stake.ak | sha256sum | cut -f 1 -d ' ')
echo " validators/pool_stake.ak = ${SHA256}"
SHA256=$(cat validators/pool.ak | sha256sum | cut -f 1 -d ' ')
echo " validators/pool.ak = ${SHA256}"
SHA256=$(cat validators/settings.ak | sha256sum | cut -f 1 -d ' ')
echo " validators/settings.ak = ${SHA256}"
SHA256=$(cat validators/stake.ak | sha256sum | cut -f 1 -d ' ')
echo " validators/stake.ak = ${SHA256}"
echo
SHA256=$(cat lib/shared.ak | sha256sum | cut -f 1 -d ' ')
echo " lib/shared.ak = ${SHA256}"
SHA256=$(cat lib/types/oracle.ak | sha256sum | cut -f 1 -d ' ')
echo " lib/types/oracle.ak = ${SHA256}"
SHA256=$(cat lib/types/order.ak | sha256sum | cut -f 1 -d ' ')
echo " lib/types/order.ak = ${SHA256}"
SHA256=$(cat lib/types/pool.ak | sha256sum | cut -f 1 -d ' ')
echo " lib/types/pool.ak = ${SHA256}"
SHA256=$(cat lib/types/settings.ak | sha256sum | cut -f 1 -d ' ')
echo " lib/types/settings.ak = ${SHA256}"
SHA256=$(cat lib/calculation/deposit.ak | sha256sum | cut -f 1 -d ' ')
echo " lib/calculation/deposit.ak = ${SHA256}"
SHA256=$(cat lib/calculation/donation.ak | sha256sum | cut -f 1 -d ' ')
echo " lib/calculation/donation.ak = ${SHA256}"
SHA256=$(cat lib/calculation/process.ak | sha256sum | cut -f 1 -d ' ')
echo " lib/calculation/process.ak = ${SHA256}"
SHA256=$(cat lib/calculation/record.ak | sha256sum | cut -f 1 -d ' ')
echo " lib/calculation/record.ak = ${SHA256}"
SHA256=$(cat lib/calculation/shared.ak | sha256sum | cut -f 1 -d ' ')
echo " lib/calculation/shared.ak = ${SHA256}"
SHA256=$(cat lib/calculation/strategy.ak | sha256sum | cut -f 1 -d ' ')
echo " lib/calculation/strategy.ak = ${SHA256}"
SHA256=$(cat lib/calculation/swap.ak | sha256sum | cut -f 1 -d ' ')
echo " lib/calculation/swap.ak = ${SHA256}"
SHA256=$(cat lib/calculation/withdrawal.ak | sha256sum | cut -f 1 -d ' ')
echo " lib/calculation/withdrawal.ak = ${SHA256}"

aiken build &> /dev/null

PROTOCOL_BOOT_UTXO="d8799fd8799f5820ebcee8dcdbd7312f5e04a0033472465003617abe9935a6e56f007961897cfabbff01ff"
aiken blueprint apply -v settings.spend $PROTOCOL_BOOT_UTXO > tmp
PROTOCOL_BOOT_TX="fad11baadca1e52bf34599746fb0152d9d10b31c2591b79deab34536a7998ea0"
PROTOCOL_BOOT_IX="01"
PROTOCOL_BOOT_UTXO="d8799fd8799f5820${PROTOCOL_BOOT_TX}ff${PROTOCOL_BOOT_IX}ff"

aiken blueprint apply -v settings.spend $PROTOCOL_BOOT_UTXO 2> /dev/null > tmp
mv tmp plutus.json

aiken blueprint apply -v settings.mint $PROTOCOL_BOOT_UTXO > tmp
aiken blueprint apply -v settings.mint $PROTOCOL_BOOT_UTXO 2> /dev/null > tmp
mv tmp plutus.json

SETTINGS_SCRIPT_HASH="581c$(aiken blueprint policy -v settings.mint)"
aiken blueprint apply -v pool.manage $SETTINGS_SCRIPT_HASH > tmp
SETTINGS_SCRIPT_HASH="$(aiken blueprint policy -v settings.mint 2> /dev/null)"
aiken blueprint apply -v pool.manage "581c${SETTINGS_SCRIPT_HASH}" 2> /dev/null > tmp
mv tmp plutus.json

MANAGE_STAKE_SCRIPT_HASH="581c$(aiken blueprint policy -v pool.manage)"
aiken blueprint apply -v pool.spend $MANAGE_STAKE_SCRIPT_HASH > tmp
MANAGE_STAKE_SCRIPT_HASH="$(aiken blueprint policy -v pool.manage 2> /dev/null)"
aiken blueprint apply -v pool.spend "581c${MANAGE_STAKE_SCRIPT_HASH}" 2> /dev/null > tmp
mv tmp plutus.json
aiken blueprint apply -v pool.spend $SETTINGS_SCRIPT_HASH > tmp
aiken blueprint apply -v pool.spend "581c${SETTINGS_SCRIPT_HASH}" 2> /dev/null > tmp
mv tmp plutus.json

aiken blueprint apply -v pool.mint $MANAGE_STAKE_SCRIPT_HASH > tmp
aiken blueprint apply -v pool.mint "581c${MANAGE_STAKE_SCRIPT_HASH}" 2> /dev/null > tmp
mv tmp plutus.json
aiken blueprint apply -v pool.mint "581c${SETTINGS_SCRIPT_HASH}" 2> /dev/null > tmp
mv tmp plutus.json
aiken blueprint apply -v pool.mint $SETTINGS_SCRIPT_HASH > tmp

POOL_SCRIPT_HASH="$(aiken blueprint policy -v pool.mint 2> /dev/null)"
aiken blueprint apply -v stake.stake "581c${POOL_SCRIPT_HASH}" 2> /dev/null > tmp
mv tmp plutus.json

POOL_SCRIPT_HASH="581c$(aiken blueprint policy -v pool.mint)"
aiken blueprint apply -v stake.stake $POOL_SCRIPT_HASH > tmp
aiken blueprint apply -v pool_stake.stake "581c${SETTINGS_SCRIPT_HASH}" 2> /dev/null > tmp
mv tmp plutus.json
aiken blueprint apply -v pool_stake.stake "00" 2> /dev/null > tmp
mv tmp plutus.json

STAKE_SCRIPT_HASH="581c$(aiken blueprint policy -v stake.stake)"
aiken blueprint apply -v order.spend $STAKE_SCRIPT_HASH > tmp
aiken blueprint apply -v oracle.spend "581c${POOL_SCRIPT_HASH}" 2> /dev/null > tmp
mv tmp plutus.json
aiken blueprint apply -v oracle.mint "581c${POOL_SCRIPT_HASH}" 2> /dev/null > tmp
mv tmp plutus.json

STAKE_SCRIPT_HASH="$(aiken blueprint policy -v stake.stake 2> /dev/null)"
aiken blueprint apply -v order.spend "581c${STAKE_SCRIPT_HASH}" 2> /dev/null > tmp
mv tmp plutus.json

ORACLE_SCRIPT_HASH="$(aiken blueprint policy -v oracle.mint 2> /dev/null)"
POOL_STAKE_SCRIPT_HASH="$(aiken blueprint policy -v pool_stake.stake 2> /dev/null)"
ORDER_SCRIPT_HASH="$(aiken blueprint hash -v order.spend 2> /dev/null)"

echo
echo "Parameters:"
echo -e " PROTOCOL_BOOT_UTXO = \e[32m ${PROTOCOL_BOOT_TX}#${PROTOCOL_BOOT_IX} \e[0m"

echo
echo "Script Hashes:"
echo -e " Settings Script Hash / Policy = \e[32m ${SETTINGS_SCRIPT_HASH} \e[0m"
echo -e " Pool Script Hash / Policy = \e[32m ${POOL_SCRIPT_HASH} \e[0m"
echo -e " Pool Stake Script Hash = \e[32m ${POOL_STAKE_SCRIPT_HASH} \e[0m"
echo -e " Manage Stake Script Hash = \e[32m ${MANAGE_STAKE_SCRIPT_HASH} \e[0m"
echo -e " Treasury Stake Script Hash = \e[32m ${STAKE_SCRIPT_HASH} \e[0m"
echo -e " Order Script Hash = \e[32m ${ORDER_SCRIPT_HASH} \e[0m"
echo -e " Oracle Script Hash = \e[32m ${ORACLE_SCRIPT_HASH} \e[0m"

echo
echo
144 changes: 76 additions & 68 deletions lib/calculation/deposit.ak
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use aiken/math
use aiken/transaction.{NoDatum, Output, InlineDatum}
use aiken/transaction.{InlineDatum, NoDatum, Output}
use aiken/transaction/credential.{Address, VerificationKeyCredential}
use aiken/transaction/value.{ada_policy_id, ada_asset_name, PolicyId, AssetName}
use aiken/transaction/value.{AssetName, PolicyId, ada_asset_name, ada_policy_id}
use calculation/shared.{PoolState} as calc_shared
use sundae/multisig
use shared.{SingletonValue}
use types/order.{Destination, Fixed, Self, OrderDatum}
use sundae/multisig
use types/order.{Destination, Fixed, OrderDatum, Self}

/// Calculate the result of depositing some amount of tokens into the pool
///
Expand Down Expand Up @@ -73,8 +73,7 @@ pub fn do_deposit(
// The ratio of a to b might have shifted since the user created their order amount
// So some small amount of a or b might be returned to the user
// So, calculate how much "b" do we have, in units of asset A, so we can check which is greater
let b_in_units_of_a =
user_gives_b * pool_quantity_a / pool_quantity_b
let b_in_units_of_a = user_gives_b * pool_quantity_a / pool_quantity_b

// Amount that user actually deposits in the pool, after giving back change.
let (deposited_a, deposited_b) =
Expand All @@ -84,7 +83,7 @@ pub fn do_deposit(
// 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 = ((pool_quantity_b * user_gives_a - 1) / pool_quantity_a) + 1
let give_b = ( pool_quantity_b * user_gives_a - 1 ) / pool_quantity_a + 1
(user_gives_a, give_b)
} else {
(b_in_units_of_a, user_gives_b)
Expand All @@ -98,8 +97,7 @@ pub fn do_deposit(
// issued_lp_tokens / (pool_state.quantity_lp.3rd + issued_lp_tokens) = deposited_a / (pool_state.quantity_a.3rd + deposited_a)
//
// Solving for `issued_lp_tokens` gives:
let issued_lp_tokens =
deposited_a * pool_quantity_lp / pool_quantity_a
let issued_lp_tokens = deposited_a * pool_quantity_lp / pool_quantity_a

// Make sure we don't ever allow this to round to zero, which would just eat some of the users assets
expect issued_lp_tokens > 0
Expand All @@ -112,31 +110,27 @@ pub fn do_deposit(
|> value.add(asset_a.1st, asset_a.2nd, -deposited_a)
|> value.add(asset_b.1st, asset_b.2nd, -deposited_b)
|> value.add(ada_policy_id, ada_asset_name, -actual_protocol_fee)
|> value.add(
pool_policy_lp,
pool_asset_name_lp,
issued_lp_tokens,
)
|> value.add(pool_policy_lp, pool_asset_name_lp, issued_lp_tokens)

// Make sure we're paying the result to the correct destination (both the address and the datum),
// with the correct amount; In the special case where Datum is "Self" (for example for a repeating strategy)
// use the input datum for validation
expect output.value == out_value
expect when destination is {
Fixed { address, datum } -> {
and {
output.address == address,
output.datum == datum
expect
when destination is {
Fixed { address, datum } -> and {
output.address == address,
output.datum == datum,
}
Self -> {
let Output { address: input_address, datum: input_datum, .. } =
input_utxo
and {
output.address == input_address,
output.datum == input_datum,
}
}
}
Self -> {
let Output { address: input_address, datum: input_datum, .. } = input_utxo
and {
output.address == input_address,
output.datum == input_datum
}
}
}

// And construct the final pool state
continuation(
Expand All @@ -155,53 +149,67 @@ test deposit_test() {
None,
)
let ada = (#"", #"")
let rberry = (#"01010101010101010101010101010101010101010101010101010101", "RBERRY")
let rberry =
(#"01010101010101010101010101010101010101010101010101010101", "RBERRY")
let lp = (#"99999999999999999999999999999999999999999999999999999999", "LP")
let pool_state = PoolState {
quantity_a: (#"", #"", 1_000_000_000),
quantity_b: (rberry.1st, rberry.2nd, 1_000_000_000),
quantity_lp: (lp.1st, lp.2nd, 1_000_000_000),
}
let pool_state =
PoolState {
quantity_a: (#"", #"", 1_000_000_000),
quantity_b: (rberry.1st, rberry.2nd, 1_000_000_000),
quantity_lp: (lp.1st, lp.2nd, 1_000_000_000),
}
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(
#"6af53ff4f054348ad825c692dd9db8f1760a8e0eacf9af9f99306513",
),
max_protocol_fee: 2_500_000,
destination: Fixed {
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(
#"6af53ff4f054348ad825c692dd9db8f1760a8e0eacf9af9f99306513",
),
max_protocol_fee: 2_500_000,
destination: Fixed { address: addr, datum: NoDatum },
details: order.Deposit { assets },
extension: Void,
}
let output =
Output {
address: addr,
value: value.from_lovelace(2_000_000)
|> value.add(lp.1st, lp.2nd, 10_000_000),
datum: NoDatum,
},
details: order.Deposit {
assets: assets,
},
extension: Void,
}
let output = Output {
address: addr,
value: value.from_lovelace(2_000_000)
|> value.add(lp.1st, lp.2nd, 10_000_000),
datum: NoDatum,
reference_script: None,
}
let input = Output {
address: addr,
value: input_value,
datum: InlineDatum(order),
reference_script: None,
}
let new_a, new_b, new_lp <- do_deposit(
pool_state.quantity_a.1st, pool_state.quantity_a.2nd, pool_state.quantity_a.3rd,
pool_state.quantity_b.1st, pool_state.quantity_b.2nd, pool_state.quantity_b.3rd,
pool_state.quantity_lp.1st, pool_state.quantity_lp.2nd, pool_state.quantity_lp.3rd,
input, assets,
order.destination, 2_500_000,
output
)
reference_script: None,
}
let input =
Output {
address: addr,
value: input_value,
datum: InlineDatum(order),
reference_script: None,
}
let
new_a,
new_b,
new_lp,
<-
do_deposit(
pool_state.quantity_a.1st,
pool_state.quantity_a.2nd,
pool_state.quantity_a.3rd,
pool_state.quantity_b.1st,
pool_state.quantity_b.2nd,
pool_state.quantity_b.3rd,
pool_state.quantity_lp.1st,
pool_state.quantity_lp.2nd,
pool_state.quantity_lp.3rd,
input,
assets,
order.destination,
2_500_000,
output,
)
expect new_a == 1_010_000_000
expect new_b == 1_010_000_000
expect new_lp == 1_000_000_000 + 10_000_000
Expand Down
Loading

0 comments on commit 1ea250d

Please sign in to comment.