Skip to content

Commit

Permalink
Degen pool (#106)
Browse files Browse the repository at this point in the history
* margin_trading

* update gas

* skip_unwrap_near

* update makefile

* build release

* add swap_by_output function

* add check to internal_execute_actions_by_cache

* update swap by output logic

* add degen pool

* update swap by output

* update list_degen_configs to list_degen_oracle_configs

* update degen oracle config

* update stable_swap_ramp_amp

* add release

* fix test-token

* add pool limit

* add 1.9.3 wasm

* check tvl limit in hotzap

* add check in view

* add check to get_tvl

---------

Co-authored-by: Marco <[email protected]>
  • Loading branch information
MagicGordon and Marco authored Aug 17, 2024
1 parent d241d7a commit 84f07dd
Show file tree
Hide file tree
Showing 30 changed files with 3,812 additions and 192 deletions.
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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ test-pyth: mock-pyth
release:
$(call docker_build,_rust_setup.sh)
mkdir -p res
cp target/wasm32-unknown-unknown/release/ref_exchange.wasm res/ref_exchange_release.wasm
cp target/wasm32-unknown-unknown/release/ref_exchange_by_wasm_opt.wasm res/ref_exchange_release.wasm
cp target/wasm32-unknown-unknown/release/ref_farming.wasm res/ref_farming_release.wasm

clean:
Expand Down
4 changes: 3 additions & 1 deletion _rust_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ rustup toolchain install $VER
rustup default $VER
rustup target add wasm32-unknown-unknown
cargo build -p ref-exchange --target wasm32-unknown-unknown --release
cargo build -p ref_farming --target wasm32-unknown-unknown --release
cargo build -p ref_farming --target wasm32-unknown-unknown --release
cargo install wasm-opt --locked --version 0.116.0
wasm-opt -Oz -o target/wasm32-unknown-unknown/release/ref_exchange_by_wasm_opt.wasm target/wasm32-unknown-unknown/release/ref_exchange.wasm
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.2"
version = "1.9.4"
authors = ["Illia Polosukhin <[email protected]>"]
edition = "2018"
publish = false
Expand Down
14 changes: 14 additions & 0 deletions ref-exchange/release_notes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Release Notes

### Version 1.9.4
```
DBz69SAuDcvGWrEraoKjNEMiiDxE3PagejSfhYw3SfqH
```
1. add pool limit
2. add execute_actions_in_va

### Version 1.9.3
```
1PW1wtYsciZKsaRNqNMpY3P1W2wD42PjZVraL142VN4
```
1. add degen pool
2. add swap by output for simple pool

### Version 1.9.2
```
52Fmd38fqZbHoGQGRTmoXxr9xMu8zKQWaGggHSJYi23T
Expand Down
11 changes: 3 additions & 8 deletions ref-exchange/src/account_deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,14 +507,9 @@ impl Contract {
/// save token to owner account as lostfound, no need to care about storage
/// only global whitelisted token can be stored in lost-found
pub(crate) fn internal_lostfound(&mut self, token_id: &AccountId, amount: u128) {
if self.is_whitelisted_token(token_id) {
let mut lostfound = self.internal_unwrap_or_default_account(&self.owner_id);
lostfound.deposit(token_id, amount);
self.accounts.insert(&self.owner_id, &lostfound.into());
} else {
log!("Deposit {} {} failed due to non global whitelisted token", amount, token_id);
}

let mut lostfound = self.internal_unwrap_or_default_account(&self.owner_id);
lostfound.deposit(token_id, amount);
self.accounts.insert(&self.owner_id, &lostfound.into());
}


Expand Down
86 changes: 82 additions & 4 deletions ref-exchange/src/action.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::errors::ERR41_WRONG_ACTION_RESULT;
use crate::errors::{ERR41_WRONG_ACTION_RESULT, ERR77_INVALID_ACTION_TYPE};
use near_sdk::serde::{Deserialize, Serialize};
use near_sdk::{env, json_types::U128, AccountId, Balance};
use std::collections::HashSet;

/// Single swap action.
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Clone)]
#[serde(crate = "near_sdk::serde")]
pub struct SwapAction {
/// Pool which should be used for swapping.
Expand All @@ -21,12 +21,31 @@ pub struct SwapAction {
pub min_amount_out: U128,
}

/// Single swap by output action.
#[derive(Serialize, Deserialize, Clone)]
#[serde(crate = "near_sdk::serde")]
pub struct SwapByOutputAction {
/// Pool which should be used for swapping.
pub pool_id: u64,
/// Token to swap from.
pub token_in: AccountId,
/// The desired amount of the output token.
/// If amount_out is None, it will take amount_in from previous step.
/// Will fail if amount_out is None on the first step.
pub amount_out: Option<U128>,
/// Token to swap into.
pub token_out: AccountId,
/// The maximum amount of the input token that can be used for the swap.
pub max_amount_in: Option<U128>,
}

/// Single action. Allows to execute sequence of various actions initiated by an account.
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Clone)]
#[serde(crate = "near_sdk::serde")]
#[serde(untagged)]
pub enum Action {
Swap(SwapAction),
SwapByOutput(SwapByOutputAction),
}

impl Action {
Expand All @@ -36,6 +55,51 @@ impl Action {
Action::Swap(swap_action) => {
vec![swap_action.token_in.clone(), swap_action.token_out.clone()]
}
Action::SwapByOutput(swap_by_output_action) => {
vec![swap_by_output_action.token_in.clone(), swap_by_output_action.token_out.clone()]
}
}
}

pub fn get_pool_id(&self) -> u64 {
match self {
Action::Swap(swap_action) => {
swap_action.pool_id
}
Action::SwapByOutput(swap_by_output_action) => {
swap_by_output_action.pool_id
}
}
}

pub fn get_token_in(&self) -> &AccountId {
match self {
Action::Swap(swap_action) => {
&swap_action.token_in
}
Action::SwapByOutput(swap_by_output_action) => {
&swap_by_output_action.token_in
}
}
}

pub fn get_token_out(&self) -> &AccountId {
match self {
Action::Swap(swap_action) => {
&swap_action.token_out
}
Action::SwapByOutput(swap_by_output_action) => {
&swap_by_output_action.token_out
}
}
}

pub fn get_amount_out(&self) -> Option<U128> {
match self {
Action::Swap(_) => unimplemented!(),
Action::SwapByOutput(swap_by_output_action) => {
swap_by_output_action.amount_out
}
}
}
}
Expand All @@ -52,7 +116,7 @@ pub enum ActionResult {
}

impl ActionResult {
pub fn to_amount(self) -> Balance {
pub fn to_amount(&self) -> Balance {
match self {
// [AUDIT_02]
ActionResult::Amount(result) => result.0,
Expand All @@ -70,7 +134,21 @@ pub fn get_tokens_in_actions(actions: &[Action]) -> HashSet<AccountId> {
tokens.insert(swap_action.token_in.clone());
tokens.insert(swap_action.token_out.clone());
}
Action::SwapByOutput(swap_by_output_action) => {
tokens.insert(swap_by_output_action.token_in.clone());
tokens.insert(swap_by_output_action.token_out.clone());
}
}
}
tokens
}

pub fn assert_all_same_action_type(actions: &[Action]) {
if !actions.is_empty() {
let all_same_action_type = match &actions[0] {
Action::Swap(_) => actions.iter().all(|action| matches!(action, Action::Swap(_))),
Action::SwapByOutput(_) => actions.iter().all(|action| matches!(action, Action::SwapByOutput(_))),
};
assert!(all_same_action_type, "{}", ERR77_INVALID_ACTION_TYPE);
}
}
9 changes: 8 additions & 1 deletion ref-exchange/src/custom_keys.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
// Key for rated token info
pub const RATE_STORAGE_KEY: &str = "custom_rate_key";
pub const RATE_STORAGE_KEY: &str = "custom_rate_key";

// Key for degen token info
pub const DEGEN_STORAGE_KEY: &str = "custom_degen_key";
pub const DEGEN_ORACLE_CONFIG_STORAGE_KEY: &str = "custom_degen_oracle_config_key";

// Key for pool limit
pub const POOL_LIMIT: &str = "pl";
Loading

0 comments on commit 84f07dd

Please sign in to comment.