Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preview 11 review #3

Merged
merged 4 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 0 additions & 27 deletions contracts/src/c_pool/admin.rs

This file was deleted.

7 changes: 4 additions & 3 deletions contracts/src/c_pool/allowance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ pub fn write_allowance(
e.storage().temporary().set(&key.clone(), &allowance);

if amount > 0 {
let live_for = expiration_ledger
let new_expiration_ledger = expiration_ledger
.checked_sub(e.ledger().sequence())
.unwrap();

e.storage().temporary().bump(&key, live_for, live_for)
e.storage()
.temporary()
.bump(&key, new_expiration_ledger, new_expiration_ledger)
}
}

Expand Down
52 changes: 20 additions & 32 deletions contracts/src/c_pool/call_logic/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::{
c_pool::{
error::Error,
metadata::{
check_record_bound, read_controller, read_factory, read_finalize, read_record,
read_tokens, read_total_weight, write_record, write_tokens, write_total_weight,
read_controller, read_factory, read_finalize, read_record, read_tokens,
read_total_weight, write_record, write_tokens, write_total_weight,
},
storage_types::{DataKey, Record},
token_utility::{pull_underlying, push_underlying},
Expand All @@ -21,26 +21,16 @@ pub fn execute_bind(e: Env, token: Address, balance: i128, denorm: i128, admin:
assert_with_error!(&e, denorm >= 0, Error::ErrNegative);
assert_with_error!(&e, balance >= 0, Error::ErrNegative);
assert_with_error!(&e, !read_finalize(&e), Error::ErrFinalized);
assert_with_error!(
&e,
!check_record_bound(&e, token.clone()),
Error::ErrIsBound
);

assert_with_error!(
&e,
read_tokens(&e).len() < MAX_BOUND_TOKENS,
Error::ErrMaxTokens
);
let key = DataKey::AllTokenVec;
let key_rec = DataKey::AllRecordData;
let index = read_tokens(&e).len();
assert_with_error!(&e, index < MAX_BOUND_TOKENS, Error::ErrMaxTokens);

let mut tokens_arr = read_tokens(&e);
let mut record_map = e
.storage()
.persistent()
.get(&key_rec)
.unwrap_or(Map::<Address, Record>::new(&e)); // if no members on vector
let mut record_map = read_record(&e);
if record_map.contains_key(token.clone()) {
let record = record_map.get(token.clone()).unwrap_optimized();
assert_with_error!(&e, record.bound == false, Error::ErrIsBound);
}

let record = Record {
bound: true,
Expand All @@ -66,17 +56,15 @@ pub fn execute_rebind(e: Env, token: Address, balance: i128, denorm: i128, admin
read_tokens(&e).len() < MAX_BOUND_TOKENS,
Error::ErrMaxTokens
);
assert_with_error!(
&e,
check_record_bound(&e, token.clone()),
Error::ErrNotBound
);
assert_with_error!(&e, denorm >= MIN_WEIGHT, Error::ErrMinWeight);
assert_with_error!(&e, denorm <= MAX_WEIGHT, Error::ErrMaxWeight);
assert_with_error!(&e, balance >= MIN_BALANCE, Error::ErrMinBalance);

let mut record_map: Map<Address, Record> = read_record(&e);
let mut record = record_map.get(token.clone()).unwrap_optimized();
let mut record = record_map
.get(token.clone())
.unwrap_or_else(|| panic_with_error!(&e, Error::ErrNotBound));
assert_with_error!(&e, record.bound, Error::ErrNotBound);
let old_weight = record.denorm;
let mut total_weight = read_total_weight(&e);

Expand Down Expand Up @@ -135,20 +123,20 @@ pub fn execute_rebind(e: Env, token: Address, balance: i128, denorm: i128, admin
// Removes a specific token from the Liquidity Pool
pub fn execute_unbind(e: Env, token: Address, user: Address) {
assert_with_error!(&e, !read_finalize(&e), Error::ErrFinalized);
assert_with_error!(
&e,
check_record_bound(&e, token.clone()),
Error::ErrNotBound
);

let mut record_map: Map<Address, Record> = read_record(&e);
let mut record = record_map.get(token.clone()).unwrap_optimized();
let mut record = record_map
.get(token.clone())
.unwrap_or_else(|| panic_with_error!(&e, Error::ErrNotBound));
assert_with_error!(&e, record.bound, Error::ErrNotBound);

let token_balance = record.balance;
let token_exit_fee = c_mul(&e, token_balance, EXIT_FEE).unwrap_optimized();
let curr_weight = read_total_weight(&e);
write_total_weight(&e, c_sub(&e, curr_weight, record.denorm).unwrap_optimized());
let index = record.index;
let last = read_tokens(&e).len() - 1;
let mut tokens = read_tokens(&e);
let last = tokens.len() - 1;
let index_token = tokens.get(index).unwrap_optimized();
let last_token = tokens.get(last).unwrap_optimized();
tokens.set(index, last_token.clone());
Expand Down
103 changes: 21 additions & 82 deletions contracts/src/c_pool/call_logic/getter.rs
Original file line number Diff line number Diff line change
@@ -1,82 +1,45 @@
use soroban_sdk::{assert_with_error, unwrap::UnwrapOptimized, Address, Env, Vec};
use soroban_sdk::{
assert_with_error, panic_with_error, unwrap::UnwrapOptimized, Address, Env, Vec,
};

use crate::{
c_math::calc_spot_price,
c_num::c_div,
c_pool::{
error::Error,
metadata::{
check_record_bound, get_token_share, get_total_shares, read_controller, read_finalize,
read_public_swap, read_record, read_swap_fee, read_tokens, read_total_weight,
get_total_shares, read_controller, read_finalize, read_public_swap, read_record,
read_swap_fee, read_tokens, read_total_weight,
},
storage_types::Record,
},
};

pub fn execute_get_total_supply(e: Env) -> i128 {
get_total_shares(&e)
}

// Get the Controller Address
pub fn execute_get_controller(e: Env) -> Address {
read_controller(&e)
}

// Get the total dernormalized weight
pub fn execute_get_total_denormalized_weight(e: Env) -> i128 {
read_total_weight(&e)
}

// Get the number of tokens in the pool
pub fn execute_get_num_tokens(e: Env) -> u32 {
let token_vec = read_tokens(&e);
token_vec.len()
}

// Get the Current Tokens in the Pool
pub fn execute_get_current_tokens(e: Env) -> Vec<Address> {
read_tokens(&e)
}

// Get the finalized tokens in the pool
pub fn execute_get_final_tokens(e: Env) -> Vec<Address> {
assert_with_error!(&e, read_finalize(&e), Error::ErrNotFinalized);
read_tokens(&e)
}

// Get the balance of the Token
pub fn execute_get_balance(e: Env, token: Address) -> i128 {
let val = read_record(&e).get(token).unwrap_optimized();
assert_with_error!(&e, val.bound, Error::ErrNotBound);
val.balance
}

// Get the denormalized weight of the token
pub fn execute_get_denormalized_weight(e: Env, token: Address) -> i128 {
assert_with_error!(
&e,
check_record_bound(&e, token.clone()),
Error::ErrNotBound
);
let val = read_record(&e).get(token).unwrap_optimized();
let records = read_record(&e);
let val = records
.get(token.clone())
.unwrap_or_else(|| panic_with_error!(&e, Error::ErrNotBound));
assert_with_error!(&e, val.bound, Error::ErrNotBound);
val.denorm
}

// Get the normalized weight of the token
pub fn execute_get_normalized_weight(e: Env, token: Address) -> i128 {
assert_with_error!(
&e,
check_record_bound(&e, token.clone()),
Error::ErrNotBound
);
let val = read_record(&e).get(token).unwrap_optimized();
let records = read_record(&e);
let val = records
.get(token.clone())
.unwrap_or_else(|| panic_with_error!(&e, Error::ErrNotBound));
assert_with_error!(&e, val.bound, Error::ErrNotBound);
c_div(&e, val.denorm, read_total_weight(&e)).unwrap_optimized()
}

// Calculate the spot considering the swap fee
pub fn execute_get_spot_price(e: Env, token_in: Address, token_out: Address) -> i128 {
let in_record = read_record(&e).get(token_in).unwrap_optimized();
let out_record: Record = read_record(&e).get(token_out).unwrap_optimized();
let record = read_record(&e);
let in_record = record.get(token_in).unwrap_optimized();
let out_record = record.get(token_out).unwrap_optimized();
calc_spot_price(
&e,
in_record.balance,
Expand All @@ -87,15 +50,11 @@ pub fn execute_get_spot_price(e: Env, token_in: Address, token_out: Address) ->
)
}

// Get the Swap Fee of the Contract
pub fn execute_get_swap_fee(e: Env) -> i128 {
read_swap_fee(&e)
}

// Get the spot price without considering the swap fee
pub fn execute_get_spot_price_sans_fee(e: Env, token_in: Address, token_out: Address) -> i128 {
let in_record = read_record(&e).get(token_in).unwrap_optimized();
let out_record = read_record(&e).get(token_out).unwrap_optimized();
let record = read_record(&e);
let in_record = record.get(token_in).unwrap_optimized();
let out_record = record.get(token_out).unwrap_optimized();
calc_spot_price(
&e,
in_record.balance,
Expand All @@ -105,23 +64,3 @@ pub fn execute_get_spot_price_sans_fee(e: Env, token_in: Address, token_out: Add
0,
)
}

// Get LP Token Address
pub fn execute_share_id(e: Env) -> Address {
get_token_share(&e)
}

// Check if the Pool can be used for swapping by normal users
pub fn execute_is_public_swap(e: Env) -> bool {
read_public_swap(&e)
}

// Check if the Pool is finalized by the Controller
pub fn execute_is_finalized(e: Env) -> bool {
read_finalize(&e)
}

// Check if the token Address is bound to the pool
pub fn execute_is_bound(e: Env, t: Address) -> bool {
read_record(&e).get(t).unwrap_optimized().bound
}
18 changes: 2 additions & 16 deletions contracts/src/c_pool/call_logic/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ use soroban_token_sdk::metadata::TokenMetadata;
use crate::{
c_consts::MIN_FEE,
c_pool::{
admin::{has_administrator, write_administrator},
error::Error,
metadata::{
put_token_share, put_total_shares, write_controller, write_factory, write_finalize,
write_metadata, write_public_swap, write_swap_fee,
put_total_shares, write_controller, write_factory, write_finalize, write_metadata,
write_public_swap, write_swap_fee,
},
storage_types::DataKey,
},
Expand All @@ -35,9 +34,6 @@ pub fn execute_init(e: Env, factory: Address, controller: Address) {
// Symbol of the LP Token
let symbol = String::from_slice(&e, "CPAL");

// Current Contract is the LP Token as well
put_token_share(&e, val.clone());

// Set the Total Supply of the LP Token as 0
put_total_shares(&e, 0);

Expand All @@ -48,16 +44,6 @@ pub fn execute_init(e: Env, factory: Address, controller: Address) {
write_finalize(&e, false);
write_public_swap(&e, false);

// Initialize the LP Token

if has_administrator(&e) {
panic!("already initialized")
}
write_administrator(&e, val);
if 7u32 > u8::MAX.into() {
panic!("Decimal must fit in a u8");
}

write_metadata(
&e,
TokenMetadata {
Expand Down
Loading