Skip to content

audition recommendations #113

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

Merged
merged 7 commits into from
Mar 17, 2025
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ref-exchange/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ref-exchange"
version = "1.9.9"
version = "1.9.10"
authors = ["Illia Polosukhin <illia.polosukhin@gmail.com>"]
edition = "2018"
publish = false
Expand Down
7 changes: 7 additions & 0 deletions ref-exchange/release_notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Release Notes

### Version 1.9.10
```
H2sfzjphuQSTzrQByyPXoAnQnkYFejWCNsT9uwq51XnS
```
1. amendments according to audition recommendations.
2. add skip_degen_price_sync param.

### Version 1.9.9
```
2VqDt4y5CYDi1FCKu5LLVCTXNzh3kEvXpjfSe1XbDJcG
Expand Down
33 changes: 24 additions & 9 deletions ref-exchange/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ impl Contract {
amp_factor: u64,
) -> u64 {
assert!(self.is_owner_or_guardians(), "{}", ERR100_NOT_ALLOWED);
assert!(tokens.len() == decimals.len(), "The number of tokens is inconsistent with the number of decimals.");
check_token_duplicates(&tokens);
self.internal_add_pool(Pool::StableSwapPool(StableSwapPool::new(
self.pools.len() as u32,
Expand All @@ -208,6 +209,7 @@ impl Contract {
amp_factor: u64,
) -> u64 {
assert!(self.is_owner_or_guardians(), "{}", ERR100_NOT_ALLOWED);
assert!(tokens.len() == decimals.len(), "The number of tokens is inconsistent with the number of decimals.");
check_token_duplicates(&tokens);
self.internal_add_pool(Pool::RatedSwapPool(RatedSwapPool::new(
self.pools.len() as u32,
Expand All @@ -227,6 +229,7 @@ impl Contract {
amp_factor: u64,
) -> u64 {
assert!(self.is_owner_or_guardians(), "{}", ERR100_NOT_ALLOWED);
assert!(tokens.len() == decimals.len(), "The number of tokens is inconsistent with the number of decimals.");
check_token_duplicates(&tokens);
self.internal_add_pool(Pool::DegenSwapPool(DegenSwapPool::new(
self.pools.len() as u32,
Expand All @@ -243,6 +246,7 @@ impl Contract {
use_tokens: HashMap<AccountId, U128>,
actions: Vec<Action>,
referral_id: Option<ValidAccountId>,
skip_degen_price_sync: Option<bool>,
) -> HashMap<AccountId, U128> {
self.assert_contract_running();
assert_ne!(actions.len(), 0, "{}", ERR72_AT_LEAST_ONE_SWAP);
Expand Down Expand Up @@ -276,6 +280,7 @@ impl Contract {
&referral_info,
&actions,
ActionResult::None,
skip_degen_price_sync.unwrap_or(false)
);
let mut result = HashMap::new();
for (token, amount) in virtual_account.tokens.to_vec() {
Expand All @@ -300,6 +305,7 @@ impl Contract {
&mut self,
actions: Vec<Action>,
referral_id: Option<ValidAccountId>,
skip_degen_price_sync: Option<bool>,
) -> ActionResult {
self.assert_contract_running();
assert_ne!(actions.len(), 0, "{}", ERR72_AT_LEAST_ONE_SWAP);
Expand All @@ -325,7 +331,7 @@ impl Contract {
.map(|fee| (referral_id.unwrap().into(), fee));

let result =
self.internal_execute_actions(&mut account, &referral_info, &actions, ActionResult::None);
self.internal_execute_actions(&mut account, &referral_info, &actions, ActionResult::None, skip_degen_price_sync.unwrap_or(false));
self.internal_save_account(&sender_id, account);
result
}
Expand All @@ -334,14 +340,15 @@ impl Contract {
/// If referrer provided, pays referral_fee to it.
/// If no attached deposit, outgoing tokens used in swaps must be whitelisted.
#[payable]
pub fn swap(&mut self, actions: Vec<SwapAction>, referral_id: Option<ValidAccountId>) -> U128 {
pub fn swap(&mut self, actions: Vec<SwapAction>, referral_id: Option<ValidAccountId>, skip_degen_price_sync: Option<bool>) -> U128 {
U128(
self.execute_actions(
actions
.into_iter()
.map(|swap_action| Action::Swap(swap_action))
.collect(),
referral_id,
skip_degen_price_sync
)
.to_amount(),
)
Expand All @@ -351,14 +358,15 @@ impl Contract {
/// If referrer provided, pays referral_fee to it.
/// If no attached deposit, outgoing tokens used in swaps must be whitelisted.
#[payable]
pub fn swap_by_output(&mut self, actions: Vec<SwapByOutputAction>, referral_id: Option<ValidAccountId>) -> U128 {
pub fn swap_by_output(&mut self, actions: Vec<SwapByOutputAction>, referral_id: Option<ValidAccountId>, skip_degen_price_sync: Option<bool>) -> U128 {
U128(
self.execute_actions(
actions
.into_iter()
.map(|swap_by_output_action| Action::SwapByOutput(swap_by_output_action))
.collect(),
referral_id,
skip_degen_price_sync,
)
.to_amount(),
)
Expand Down Expand Up @@ -704,6 +712,7 @@ impl Contract {
referral_info: &Option<(AccountId, u32)>,
actions: &[Action],
prev_result: ActionResult,
skip_degen_price_sync: bool,
) -> ActionResult {
assert_all_same_action_type(actions);
// fronzen token feature
Expand Down Expand Up @@ -737,8 +746,10 @@ impl Contract {
self.finalize_prev_swap_chain(account, prev_action, &result);
}
}
let degen_token_ids = self.get_degen_tokens_in_actions(actions).into_iter().collect::<Vec<_>>();
internal_batch_update_degen_token_price(degen_token_ids);
if !skip_degen_price_sync {
let degen_token_ids = self.get_degen_tokens_in_actions(actions).into_iter().collect::<Vec<_>>();
internal_batch_update_degen_token_price(degen_token_ids);
}
result
}

Expand Down Expand Up @@ -921,7 +932,7 @@ impl Contract {
&swap_action.token_in,
amount_in,
&swap_action.token_out,
0,
swap_action.min_amount_out.0,
referral_info,
);
token_cache.add(&swap_action.token_out, amount_out);
Expand Down Expand Up @@ -994,7 +1005,7 @@ impl Contract {
exchange_id: env::current_account_id(),
referral_info: referral_info.clone(),
},
false
true
);
pool_cache.insert(pool_id, pool);
amount_in
Expand Down Expand Up @@ -1104,6 +1115,7 @@ mod tests {
min_amount_out: U128(1),
}],
None,
None,
)
.0
}
Expand Down Expand Up @@ -1359,6 +1371,7 @@ mod tests {
min_amount_out: U128(0),
}],
None,
None,
);
}

Expand Down Expand Up @@ -1472,7 +1485,7 @@ mod tests {
testing_env!(context
.predecessor_account_id(acc.clone())
.build());
contract.execute_actions(actions, None);
contract.execute_actions(actions, None, None);
}

#[test]
Expand Down Expand Up @@ -1532,6 +1545,7 @@ mod tests {
min_amount_out: U128(1_000_000),
}],
None,
None,
);
}

Expand All @@ -1551,7 +1565,7 @@ mod tests {
testing_env!(context.attached_deposit(to_yocto("1")).build());
contract.storage_deposit(None, None);
testing_env!(context.attached_deposit(1).build());
contract.swap(vec![], None);
contract.swap(vec![], None, None);
}

/// Check that can not swap non whitelisted tokens when attaching 0 deposit (access key).
Expand Down Expand Up @@ -1617,6 +1631,7 @@ mod tests {
},
],
None,
None,
);
// Roundtrip returns almost everything except 0.25% fee.
assert_eq!(contract.get_deposit(acc, accounts(1)).0, 1_000_000 - 6);
Expand Down
14 changes: 11 additions & 3 deletions ref-exchange/src/token_receiver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ enum TokenReceiverMessage {
/// to send token_out back to predecessor with this msg.
client_echo: Option<String>,
skip_unwrap_near: Option<bool>,
swap_out_recipient: Option<ValidAccountId>
swap_out_recipient: Option<ValidAccountId>,
skip_degen_price_sync: Option<bool>,
},
HotZap {
referral_id: Option<ValidAccountId>,
hot_zap_actions: Vec<Action>,
add_liquidity_infos: Vec<AddLiquidityInfo>
add_liquidity_infos: Vec<AddLiquidityInfo>,
skip_degen_price_sync: Option<bool>,
},
}

Expand All @@ -48,6 +50,7 @@ impl Contract {
amount_in: Balance,
referral_id: Option<AccountId>,
actions: &[Action],
skip_degen_price_sync: bool,
) -> Vec<(AccountId, Balance)> {

// let @ be the virtual account
Expand All @@ -66,6 +69,7 @@ impl Contract {
Action::Swap(_) => ActionResult::Amount(U128(amount_in)),
Action::SwapByOutput(_) => ActionResult::None,
},
skip_degen_price_sync
);

let mut result = vec![];
Expand Down Expand Up @@ -110,6 +114,7 @@ impl FungibleTokenReceiver for Contract {
client_echo,
skip_unwrap_near,
swap_out_recipient,
skip_degen_price_sync,
} => {
assert!(!(swap_out_recipient.is_some() && client_echo.is_some()), "client_echo and swap_out_recipient cannot have value at the same time");
assert_ne!(actions.len(), 0, "{}", ERR72_AT_LEAST_ONE_SWAP);
Expand All @@ -122,6 +127,7 @@ impl FungibleTokenReceiver for Contract {
amount.0,
referral_id,
&actions,
skip_degen_price_sync.unwrap_or(false),
);
if client_echo.is_some() && sender_id.to_string() == self.burrowland_id {
assert!(out_amounts.len() == 1, "Invalid actions, only one out token is allowed");
Expand All @@ -139,7 +145,8 @@ impl FungibleTokenReceiver for Contract {
TokenReceiverMessage::HotZap {
referral_id,
hot_zap_actions,
add_liquidity_infos
add_liquidity_infos,
skip_degen_price_sync,
} => {
assert!(hot_zap_actions.len() > 0 && add_liquidity_infos.len() > 0);
let sender_id: AccountId = sender_id.into();
Expand All @@ -160,6 +167,7 @@ impl FungibleTokenReceiver for Contract {
amount.0,
referral_id,
&hot_zap_actions,
skip_degen_price_sync.unwrap_or(false)
);

let mut token_cache = TokenCache::new();
Expand Down
2 changes: 2 additions & 0 deletions ref-exchange/tests/fuzzy/pool_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub fn swap_action(pool :&ContractAccount<Exchange>, operator: &Operator, token_
token_out: token_out,
min_amount_out: U128(1)
}],
None,
None
),
deposit = 1
Expand Down Expand Up @@ -225,6 +226,7 @@ pub fn do_stable_pool_swap(token_contracts: &Vec<ContractAccount<TestToken>>, rn
token_out: token_out.clone(),
min_amount_out: U128(1)
}],
None,
None
),
deposit = 1
Expand Down
5 changes: 4 additions & 1 deletion ref-exchange/tests/test_admin_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ fn modify_admin_fee() {
token_out: eth(),
min_amount_out: U128(1)
}],
None,
None
),
deposit = 1
Expand Down Expand Up @@ -110,6 +111,7 @@ fn modify_admin_fee() {
token_out: eth(),
min_amount_out: U128(1)
}],
None,
None
),
deposit = 1
Expand Down Expand Up @@ -221,7 +223,8 @@ fn referral_fee() {
token_out: eth(),
min_amount_out: U128(1)
}],
Some(referral1.valid_account_id())
Some(referral1.valid_account_id()),
None
),
deposit = 1
);
Expand Down
2 changes: 2 additions & 0 deletions ref-exchange/tests/test_degen_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ fn sim_degen() {
token_out: eth(),
min_amount_out: U128(1)
}],
None,
None
),
gas = 300000000000000
Expand Down Expand Up @@ -318,6 +319,7 @@ fn sim_degen1() {
token_out: eth(),
min_amount_out: U128(1)
}],
None,
None
),
gas = 300000000000000
Expand Down
5 changes: 5 additions & 0 deletions ref-exchange/tests/test_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ fn sim_stable_e63 () {
token_out: usdt(),
min_amount_out: U128(1)
}],
None,
None
),
deposit = 1
Expand Down Expand Up @@ -424,6 +425,7 @@ fn sim_stable_e68 () {
token_out: usdt(),
min_amount_out: U128(2 * ONE_USDT)
}],
None,
None
),
deposit = 1
Expand Down Expand Up @@ -489,6 +491,7 @@ fn sim_stable_e69 () {
token_out: dai(),
min_amount_out: U128(1)
}],
None,
None
),
deposit = 1
Expand All @@ -503,6 +506,7 @@ fn sim_stable_e69 () {
token_out: dai(),
min_amount_out: U128(1)
}],
None,
None
),
deposit = 1
Expand Down Expand Up @@ -546,6 +550,7 @@ fn sim_stable_e71 () {
token_out: dai(),
min_amount_out: U128(1)
}],
None,
None
),
deposit = 1
Expand Down
3 changes: 3 additions & 0 deletions ref-exchange/tests/test_frozenlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ fn frozenlist_scenario_02() {
token_out: eth(),
min_amount_out: U128(1)
})],
None,
None
),
deposit = 1
Expand All @@ -183,6 +184,7 @@ fn frozenlist_scenario_02() {
token_out: eth(),
min_amount_out: U128(1)
}],
None,
None
),
deposit = 1
Expand Down Expand Up @@ -232,6 +234,7 @@ fn frozenlist_scenario_02() {
token_out: dai(),
min_amount_out: U128(1)
}],
None,
None
),
deposit = 1
Expand Down
Loading