diff --git a/Cargo.lock b/Cargo.lock index e4e92587..fb72ddb1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -18,9 +18,9 @@ dependencies = [ "amm-helpers", "ink", "ink_metadata", - "openbrush", "parity-scale-codec", - "primitive-types 0.12.1", + "primitive-types", + "psp22", "scale-info", "sp-arithmetic", ] @@ -32,7 +32,7 @@ dependencies = [ "ink", "ink_metadata", "parity-scale-codec", - "primitive-types 0.12.1", + "primitive-types", "scale-info", ] @@ -43,7 +43,7 @@ dependencies = [ "amm-helpers", "ink", "parity-scale-codec", - "primitive-types 0.12.1", + "primitive-types", "psp22-traits", "scale-info", "sp-arithmetic", @@ -354,15 +354,6 @@ dependencies = [ "scale-info", ] -[[package]] -name = "fixed-hash" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cfcf0ed7fe52a17a03854ec54a9f76d6d84508d1c0e66bc1793301c73fc8493c" -dependencies = [ - "static_assertions", -] - [[package]] name = "fixed-hash" version = "0.8.0" @@ -910,9 +901,9 @@ dependencies = [ "amm", "amm-helpers", "ink", - "openbrush", "parity-scale-codec", - "primitive-types 0.11.1", + "primitive-types", + "psp22", "scale-info", "sp-arithmetic", ] @@ -977,24 +968,13 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" -[[package]] -name = "primitive-types" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e28720988bff275df1f51b171e1b2a18c30d194c4d2b61defdacecd625a5d94a" -dependencies = [ - "fixed-hash 0.7.0", - "impl-codec", - "uint", -] - [[package]] name = "primitive-types" version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f3486ccba82358b11a77516035647c34ba167dfa53312630de83b12bd4f3d66" dependencies = [ - "fixed-hash 0.8.0", + "fixed-hash", "impl-codec", "impl-num-traits", "scale-info", @@ -1020,6 +1000,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "psp22" +version = "0.1.0" +source = "git+https://github.com/Cardinal-Cryptography/PSP22.git#c6744743e645f8120ceef84e2dbadf8f194fdb4f" +dependencies = [ + "ink", + "parity-scale-codec", + "scale-info", +] + [[package]] name = "psp22-traits" version = "1.0.0" @@ -1034,8 +1024,8 @@ name = "psp22_token" version = "0.1.0" dependencies = [ "ink", - "openbrush", "parity-scale-codec", + "psp22", "scale-info", ] @@ -1113,9 +1103,8 @@ version = "0.1.0" dependencies = [ "amm", "ink", - "openbrush", "parity-scale-codec", - "primitive-types 0.12.1", + "primitive-types", "scale-info", ] @@ -1209,9 +1198,9 @@ dependencies = [ [[package]] name = "scale-info" -version = "2.8.0" +version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad560913365790f17cbf12479491169f01b9d46d29cfc7422bf8c64bdc61b731" +checksum = "35c0a159d0c45c12b20c5a844feb1fe4bea86e28f17b92a5f0c42193634d3782" dependencies = [ "bitvec", "cfg-if", @@ -1223,9 +1212,9 @@ dependencies = [ [[package]] name = "scale-info-derive" -version = "2.8.0" +version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19df9bd9ace6cc2fe19387c96ce677e823e07d017ceed253e7bb3d1d1bd9c73b" +checksum = "912e55f6d20e0e80d63733872b40e1227c0bce1e1ab81ba67d696339bfd7fd29" dependencies = [ "proc-macro-crate", "proc-macro2", @@ -1647,8 +1636,8 @@ version = "0.1.0" dependencies = [ "amm", "ink", - "openbrush", "parity-scale-codec", + "psp22", "scale-info", ] diff --git a/amm/contracts/factory/Cargo.toml b/amm/contracts/factory/Cargo.toml index 7743f7f8..ce1266e0 100644 --- a/amm/contracts/factory/Cargo.toml +++ b/amm/contracts/factory/Cargo.toml @@ -10,7 +10,7 @@ ink = { version = "=4.3.0", default-features = false } scale = { package = "parity-scale-codec", version = "3", default-features = false, features = [ "derive", ] } -scale-info = { version = "2.3", default-features = false, features = [ +scale-info = { version = "2.9", default-features = false, features = [ "derive", ], optional = true } diff --git a/amm/contracts/pair/Cargo.toml b/amm/contracts/pair/Cargo.toml index 9ed7bbb2..c9ed9dac 100644 --- a/amm/contracts/pair/Cargo.toml +++ b/amm/contracts/pair/Cargo.toml @@ -10,17 +10,18 @@ ink = { version = "=4.3.0", default-features = false } scale = { package = "parity-scale-codec", version = "3", default-features = false, features = [ "derive", ] } -scale-info = { version = "2.3", default-features = false, features = [ +scale-info = { version = "2.9", default-features = false, features = [ "derive", ], optional = true } -openbrush = { version = "3.2.0", default-features = false, features = [ - "psp22", - "reentrancy_guard", -] } +psp22 = { git = "https://github.com/Cardinal-Cryptography/PSP22.git", default-features = false, features = [ + "mintable", + "burnable", +]} + amm = { path = "../../logics", default-features = false } amm-helpers = { path = "../../../helpers", default-features = false } -primitive-types = { version = "0.11.1", default-features = false, features = [ +primitive-types = { version = "0.12.1", default-features = false, features = [ "codec", ] } sp-arithmetic = { version = "18.0.0", default-features = false } @@ -36,7 +37,7 @@ std = [ "ink/std", "scale/std", "scale-info/std", - "openbrush/std", + "psp22/std", "amm/std", "amm-helpers/std", ] diff --git a/amm/logics/impls/pair/data.rs b/amm/contracts/pair/data.rs similarity index 82% rename from amm/logics/impls/pair/data.rs rename to amm/contracts/pair/data.rs index 8d635097..2029b7b4 100644 --- a/amm/logics/impls/pair/data.rs +++ b/amm/contracts/pair/data.rs @@ -1,15 +1,13 @@ -use crate::helpers::ZERO_ADDRESS; -use amm_helpers::types::WrappedU256; -use openbrush::traits::{ - AccountId, +use amm::{ + helpers::ZERO_ADDRESS, Balance, Timestamp, }; +use amm_helpers::types::WrappedU256; +use ink::primitives::AccountId; -pub const STORAGE_KEY: u32 = openbrush::storage_unique_key!(Data); - +#[ink::storage_item] #[derive(Debug)] -#[openbrush::upgradeable_storage(STORAGE_KEY)] pub struct Data { pub factory: AccountId, pub token_0: AccountId, diff --git a/amm/contracts/pair/lib.rs b/amm/contracts/pair/lib.rs index 596e991f..5a772708 100644 --- a/amm/contracts/pair/lib.rs +++ b/amm/contracts/pair/lib.rs @@ -1,41 +1,43 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -#![feature(min_specialization)] -#[openbrush::contract] +mod data; + +#[ink::contract] pub mod pair { + use crate::data::Data; use amm::{ ensure, helpers::{ - transfer_helper::safe_transfer, + helper::update_cumulative, + transfer_helper::{ + balance_of, + safe_transfer, + }, + MINIMUM_LIQUIDITY, ZERO_ADDRESS, }, - impls::pair::{ + traits::{ + factory::Factory, pair::{ - Internal, - MINIMUM_LIQUIDITY, + Pair, + PairError, }, - *, }, - traits::pair::*, }; use amm_helpers::{ math::casted_mul, types::WrappedU256, }; use ink::{ - codegen::{ - EmitEvent, - Env, - }, + contract_ref, prelude::vec::Vec, }; - use openbrush::{ - contracts::{ - psp22::*, - reentrancy_guard::*, - }, - modifiers, - traits::Storage, + use primitive_types::U256; + use psp22::{ + PSP22Data, + PSP22Error, + PSP22Event, + PSP22, }; use sp_arithmetic::traits::IntegerSquareRoot; @@ -90,18 +92,123 @@ pub mod pair { owner: AccountId, #[ink(topic)] spender: AccountId, - value: Balance, + amount: Balance, } #[ink(storage)] - #[derive(Default, Storage)] + #[derive(Default)] pub struct PairContract { - #[storage_field] - psp22: psp22::Data, - #[storage_field] - guard: reentrancy_guard::Data, - #[storage_field] - pair: data::Data, + psp22: PSP22Data, + pair: Data, + } + + impl PairContract { + #[ink(constructor)] + pub fn new(token_a: AccountId, token_b: AccountId) -> Self { + let mut instance = Self::default(); + let caller = instance.env().caller(); + instance.pair.token_0 = token_a; + instance.pair.token_1 = token_b; + instance.pair.factory = caller; + instance + } + + fn mint_fee(&mut self, reserve_0: Balance, reserve_1: Balance) -> Result { + let factory_ref: contract_ref!(Factory) = self.pair.factory.into(); + let fee_to = factory_ref.fee_to(); + let fee_on = fee_to != ZERO_ADDRESS.into(); + let k_last: U256 = self.pair.k_last.into(); + if fee_on { + // Section 2.4 Protocol fee in the whitepaper. + if !k_last.is_zero() { + let root_k: Balance = casted_mul(reserve_0, reserve_1) + .integer_sqrt() + .try_into() + .map_err(|_| PairError::CastOverflow1)?; + let root_k_last = k_last + .integer_sqrt() + .try_into() + .map_err(|_| PairError::CastOverflow2)?; + if root_k > root_k_last { + let total_supply = self.psp22.total_supply(); + let numerator = total_supply + .checked_mul( + root_k + .checked_sub(root_k_last) + .ok_or(PairError::SubUnderFlow14)?, + ) + .ok_or(PairError::MulOverFlow13)?; + let denominator = root_k + .checked_mul(5) + .ok_or(PairError::MulOverFlow13)? + .checked_add(root_k_last) + .ok_or(PairError::AddOverflow1)?; + let liquidity = numerator + .checked_div(denominator) + .ok_or(PairError::DivByZero5)?; + if liquidity > 0 { + let events = self.psp22.mint(fee_to, liquidity)?; + self.emit_events(events) + } + } + } + } else if !k_last.is_zero() { + self.pair.k_last = 0.into(); + } + Ok(fee_on) + } + + fn update( + &mut self, + balance_0: Balance, + balance_1: Balance, + reserve_0: Balance, + reserve_1: Balance, + ) -> Result<(), PairError> { + let now = Self::env().block_timestamp(); + let last_timestamp = self.pair.block_timestamp_last; + if now != last_timestamp { + let (price_0_cumulative_last, price_1_cumulative_last) = update_cumulative( + self.pair.price_0_cumulative_last, + self.pair.price_1_cumulative_last, + now.saturating_sub(last_timestamp).into(), + reserve_0, + reserve_1, + ); + self.pair.price_0_cumulative_last = price_0_cumulative_last; + self.pair.price_1_cumulative_last = price_1_cumulative_last; + } + self.pair.reserve_0 = balance_0; + self.pair.reserve_1 = balance_1; + self.pair.block_timestamp_last = now; + + self.env().emit_event(Sync { + reserve_0: balance_0, + reserve_1: balance_1, + }); + Ok(()) + } + + fn emit_events(&self, events: Vec) { + for event in events { + match event { + PSP22Event::Transfer { from, to, value } => { + self.env().emit_event(Transfer { from, to, value }) + } + PSP22Event::Approval { + owner, + spender, + amount, + } => { + self.env().emit_event(Approval { + owner, + spender, + amount, + }) + } + } + } + } } impl Pair for PairContract { @@ -123,13 +230,12 @@ pub mod pair { self.pair.price_1_cumulative_last } - #[modifiers(non_reentrant)] #[ink(message)] fn mint(&mut self, to: AccountId) -> Result { let reserves = self.get_reserves(); let contract = self.env().account_id(); - let balance_0 = PSP22Ref::balance_of(&self.pair.token_0, contract); - let balance_1 = PSP22Ref::balance_of(&self.pair.token_1, contract); + let balance_0 = balance_of(self.pair.token_0, contract); + let balance_1 = balance_of(self.pair.token_1, contract); let amount_0_transferred = balance_0 .checked_sub(reserves.0) .ok_or(PairError::SubUnderFlow1)?; @@ -137,8 +243,8 @@ pub mod pair { .checked_sub(reserves.1) .ok_or(PairError::SubUnderFlow2)?; - let fee_on = self._mint_fee(reserves.0, reserves.1)?; - let total_supply = self.psp22.supply; + let fee_on = self.mint_fee(reserves.0, reserves.1)?; + let total_supply = self.psp22.total_supply(); let liquidity; if total_supply == 0 { @@ -149,7 +255,8 @@ pub mod pair { .integer_sqrt() .checked_sub(MINIMUM_LIQUIDITY) .ok_or(PairError::SubUnderFlow3)?; - self._mint_to(ZERO_ADDRESS.into(), MINIMUM_LIQUIDITY)?; + let events = self.psp22.mint(ZERO_ADDRESS.into(), MINIMUM_LIQUIDITY)?; + self.emit_events(events) } else { let liquidity_1 = amount_0_transferred .checked_mul(total_supply) @@ -161,41 +268,45 @@ pub mod pair { .ok_or(PairError::MulOverFlow3)? .checked_div(reserves.1) .ok_or(PairError::DivByZero2)?; - liquidity = min(liquidity_1, liquidity_2); + liquidity = if liquidity_1 < liquidity_2 { + liquidity_1 + } else { + liquidity_2 + }; } ensure!(liquidity > 0, PairError::InsufficientLiquidityMinted); - self._mint_to(to, liquidity)?; + let events = self.psp22.mint(to, liquidity)?; + self.emit_events(events); - self._update(balance_0, balance_1, reserves.0, reserves.1)?; + self.update(balance_0, balance_1, reserves.0, reserves.1)?; if fee_on { self.pair.k_last = casted_mul(reserves.0, reserves.1).into(); } - self._emit_mint_event( - self.env().caller(), - amount_0_transferred, - amount_1_transferred, - ); + self.env().emit_event(Mint { + sender: self.env().caller(), + amount_0: amount_0_transferred, + amount_1: amount_1_transferred, + }); Ok(liquidity) } - #[modifiers(non_reentrant)] #[ink(message)] fn burn(&mut self, to: AccountId) -> Result<(Balance, Balance), PairError> { let reserves = self.get_reserves(); let contract = self.env().account_id(); let token_0 = self.pair.token_0; let token_1 = self.pair.token_1; - let balance_0_before = PSP22Ref::balance_of(&token_0, contract); - let balance_1_before = PSP22Ref::balance_of(&token_1, contract); - let liquidity = self._balance_of(&contract); + let balance_0_before = balance_of(token_0, contract); + let balance_1_before = balance_of(token_1, contract); + let liquidity = self.balance_of(contract); - let fee_on = self._mint_fee(reserves.0, reserves.1)?; - let total_supply = self.psp22.supply; + let fee_on = self.mint_fee(reserves.0, reserves.1)?; + let total_supply = self.psp22.total_supply(); let amount_0 = liquidity .checked_mul(balance_0_before) .ok_or(PairError::MulOverFlow5)? @@ -212,26 +323,31 @@ pub mod pair { PairError::InsufficientLiquidityBurned ); - self._burn_from(contract, liquidity)?; + let events = self.psp22.burn(contract, liquidity)?; + self.emit_events(events); safe_transfer(token_0, to, amount_0)?; safe_transfer(token_1, to, amount_1)?; - let balance_0_after = PSP22Ref::balance_of(&token_0, contract); - let balance_1_after = PSP22Ref::balance_of(&token_1, contract); + let balance_0_after = balance_of(token_0, contract); + let balance_1_after = balance_of(token_1, contract); - self._update(balance_0_after, balance_1_after, reserves.0, reserves.1)?; + self.update(balance_0_after, balance_1_after, reserves.0, reserves.1)?; if fee_on { self.pair.k_last = casted_mul(reserves.0, reserves.1).into(); } - self._emit_burn_event(self.env().caller(), amount_0, amount_1, to); + self.env().emit_event(Burn { + sender: self.env().caller(), + amount_0, + amount_1, + to, + }); Ok((amount_0, amount_1)) } - #[modifiers(non_reentrant)] #[ink(message)] fn swap( &mut self, @@ -260,8 +376,8 @@ pub mod pair { safe_transfer(token_1, to, amount_1_out)?; } let contract = self.env().account_id(); - let balance_0 = PSP22Ref::balance_of(&token_0, contract); - let balance_1 = PSP22Ref::balance_of(&token_1, contract); + let balance_0 = balance_of(token_0, contract); + let balance_1 = balance_of(token_1, contract); let amount_0_in = if balance_0 > reserves @@ -323,20 +439,19 @@ pub mod pair { PairError::K ); - self._update(balance_0, balance_1, reserves.0, reserves.1)?; + self.update(balance_0, balance_1, reserves.0, reserves.1)?; - self._emit_swap_event( - self.env().caller(), + self.env().emit_event(Swap { + sender: self.env().caller(), amount_0_in, amount_1_in, amount_0_out, amount_1_out, to, - ); + }); Ok(()) } - #[modifiers(non_reentrant)] #[ink(message)] fn skim(&mut self, to: AccountId) -> Result<(), PairError> { let contract = self.env().account_id(); @@ -344,8 +459,8 @@ pub mod pair { let reserve_1 = self.pair.reserve_1; let token_0 = self.pair.token_0; let token_1 = self.pair.token_1; - let balance_0 = PSP22Ref::balance_of(&token_0, contract); - let balance_1 = PSP22Ref::balance_of(&token_1, contract); + let balance_0 = balance_of(token_0, contract); + let balance_1 = balance_of(token_1, contract); safe_transfer( token_0, to, @@ -363,7 +478,6 @@ pub mod pair { Ok(()) } - #[modifiers(non_reentrant)] #[ink(message)] fn sync(&mut self) -> Result<(), PairError> { let contract = self.env().account_id(); @@ -371,9 +485,9 @@ pub mod pair { let reserve_1 = self.pair.reserve_1; let token_0 = self.pair.token_0; let token_1 = self.pair.token_1; - let balance_0 = PSP22Ref::balance_of(&token_0, contract); - let balance_1 = PSP22Ref::balance_of(&token_1, contract); - self._update(balance_0, balance_1, reserve_0, reserve_1) + let balance_0 = balance_of(token_0, contract); + let balance_1 = balance_of(token_1, contract); + self.update(balance_0, balance_1, reserve_0, reserve_1) } #[ink(message)] @@ -387,175 +501,93 @@ pub mod pair { } } - fn min(x: u128, y: u128) -> u128 { - if x < y { - return x - } - y - } - impl PSP22 for PairContract { #[ink(message)] - fn transfer_from( - &mut self, - from: AccountId, - to: AccountId, - value: Balance, - data: Vec, - ) -> Result<(), PSP22Error> { - let caller = self.env().caller(); - let allowance = self._allowance(&from, &caller); - - // In uniswapv2 max allowance never decrease - if allowance != u128::MAX { - ensure!(allowance >= value, PSP22Error::InsufficientAllowance); - self._approve_from_to(from, caller, allowance - value)?; - } - self._transfer_from_to(from, to, value, data)?; - Ok(()) + fn total_supply(&self) -> u128 { + self.psp22.total_supply() } - } - impl psp22::Internal for PairContract { - // in uniswapv2 no check for zero account - fn _mint_to(&mut self, account: AccountId, amount: Balance) -> Result<(), PSP22Error> { - let mut new_balance = self._balance_of(&account); - new_balance += amount; - self.psp22.balances.insert(&account, &new_balance); - self.psp22.supply += amount; - self._emit_transfer_event(None, Some(account), amount); - Ok(()) + #[ink(message)] + fn balance_of(&self, owner: AccountId) -> u128 { + self.psp22.balance_of(owner) } - fn _burn_from(&mut self, account: AccountId, amount: Balance) -> Result<(), PSP22Error> { - let mut from_balance = self._balance_of(&account); - - ensure!(from_balance >= amount, PSP22Error::InsufficientBalance); - - from_balance -= amount; - self.psp22.balances.insert(&account, &from_balance); - self.psp22.supply -= amount; - self._emit_transfer_event(Some(account), None, amount); - Ok(()) + #[ink(message)] + fn allowance(&self, owner: AccountId, spender: AccountId) -> u128 { + self.psp22.allowance(owner, spender) } - fn _approve_from_to( + #[ink(message)] + fn transfer( &mut self, - owner: AccountId, - spender: AccountId, - amount: Balance, + to: AccountId, + value: u128, + _data: Vec, ) -> Result<(), PSP22Error> { - self.psp22.allowances.insert(&(&owner, &spender), &amount); - self._emit_approval_event(owner, spender, amount); + let events = self.psp22.transfer(self.env().caller(), to, value)?; + self.emit_events(events); Ok(()) } - fn _transfer_from_to( + #[ink(message)] + fn transfer_from( &mut self, from: AccountId, to: AccountId, - amount: Balance, + value: u128, _data: Vec, ) -> Result<(), PSP22Error> { - let from_balance = self._balance_of(&from); - - ensure!(from_balance >= amount, PSP22Error::InsufficientBalance); - - self.psp22.balances.insert(&from, &(from_balance - amount)); - let to_balance = self._balance_of(&to); - self.psp22.balances.insert(&to, &(to_balance + amount)); - - self._emit_transfer_event(Some(from), Some(to), amount); + let caller = self.env().caller(); + let infinite_allowance = self.psp22.allowance(from, caller) == u128::MAX; + let mut events = self + .psp22 + .transfer_from(self.env().caller(), from, to, value)?; + if infinite_allowance { + self.psp22.approve(from, caller, u128::MAX)?; + events.remove(0); + } + self.emit_events(events); Ok(()) } - fn _emit_approval_event(&self, owner: AccountId, spender: AccountId, amount: Balance) { - self.env().emit_event(Approval { - owner, - spender, - value: amount, - }); - } - - fn _emit_transfer_event( - &self, - from: Option, - to: Option, - amount: Balance, - ) { - self.env().emit_event(Transfer { - from, - to, - value: amount, - }); - } - } - - impl pair::Internal for PairContract { - fn _emit_mint_event(&self, sender: AccountId, amount_0: Balance, amount_1: Balance) { - self.env().emit_event(Mint { - sender, - amount_0, - amount_1, - }) - } - - fn _emit_burn_event( - &self, - sender: AccountId, - amount_0: Balance, - amount_1: Balance, - to: AccountId, - ) { - self.env().emit_event(Burn { - sender, - amount_0, - amount_1, - to, - }) + #[ink(message)] + fn approve(&mut self, spender: AccountId, value: u128) -> Result<(), PSP22Error> { + let events = self.psp22.approve(self.env().caller(), spender, value)?; + self.emit_events(events); + Ok(()) } - fn _emit_swap_event( - &self, - sender: AccountId, - amount_0_in: Balance, - amount_1_in: Balance, - amount_0_out: Balance, - amount_1_out: Balance, - to: AccountId, - ) { - self.env().emit_event(Swap { - sender, - amount_0_in, - amount_1_in, - amount_0_out, - amount_1_out, - to, - }) + #[ink(message)] + fn increase_allowance( + &mut self, + spender: AccountId, + delta_value: u128, + ) -> Result<(), PSP22Error> { + let events = + self.psp22 + .increase_allowance(self.env().caller(), spender, delta_value)?; + self.emit_events(events); + Ok(()) } - fn _emit_sync_event(&self, reserve_0: Balance, reserve_1: Balance) { - self.env().emit_event(Sync { - reserve_0, - reserve_1, - }) + #[ink(message)] + fn decrease_allowance( + &mut self, + spender: AccountId, + delta_value: u128, + ) -> Result<(), PSP22Error> { + let events = + self.psp22 + .decrease_allowance(self.env().caller(), spender, delta_value)?; + self.emit_events(events); + Ok(()) } } - impl PairContract { - #[ink(constructor)] - pub fn new(token_a: AccountId, token_b: AccountId) -> Self { - let mut instance = Self::default(); - let caller = instance.env().caller(); - instance.pair.token_0 = token_a; - instance.pair.token_1 = token_b; - instance.pair.factory = caller; - instance - } - } #[cfg(test)] mod tests { use super::*; + use sp_arithmetic::FixedU128; #[ink::test] fn initialize_works() { @@ -565,5 +597,27 @@ pub mod pair { assert_eq!(pair.get_token_0(), token_0); assert_eq!(pair.get_token_1(), token_1); } + + #[ink::test] + fn update_cumulative_from_zero_time_elapsed() { + let (cumulative0, cumulative1) = + update_cumulative(0.into(), 0.into(), 0.into(), 10, 10); + assert_eq!(cumulative0, 0.into()); + assert_eq!(cumulative1, 0.into()); + } + + #[ink::test] + fn update_cumulative_from_one_time_elapsed() { + let (cumulative0, cumulative1) = + update_cumulative(0.into(), 0.into(), 1.into(), 10, 10); + assert_eq!( + FixedU128::from_inner(U256::from(cumulative0).as_u128()), + 1.into() + ); + assert_eq!( + FixedU128::from_inner(U256::from(cumulative1).as_u128()), + 1.into() + ); + } } } diff --git a/amm/contracts/psp22/Cargo.toml b/amm/contracts/psp22/Cargo.toml index 4abd6366..78697f1e 100644 --- a/amm/contracts/psp22/Cargo.toml +++ b/amm/contracts/psp22/Cargo.toml @@ -10,13 +10,12 @@ ink = { version = "=4.3.0", default-features = false } scale = { package = "parity-scale-codec", version = "3", default-features = false, features = [ "derive", ] } -scale-info = { version = "2.3", default-features = false, features = [ + +scale-info = { version = "2.9", default-features = false, features = [ "derive", ], optional = true } -openbrush = { version = "3.2.0", default-features = false, features = [ - "psp22", -] } +psp22 = { git = "https://github.com/Cardinal-Cryptography/PSP22.git", default-features = false } [lib] name = "psp22_token" @@ -24,5 +23,10 @@ path = "lib.rs" [features] default = ["std"] -std = ["ink/std", "scale/std", "scale-info/std", "openbrush/std"] +std = [ + "ink/std", + "scale/std", + "scale-info/std", + "psp22/std", +] ink-as-dependency = [] diff --git a/amm/contracts/psp22/lib.rs b/amm/contracts/psp22/lib.rs index d7ee0dd6..39b511b4 100644 --- a/amm/contracts/psp22/lib.rs +++ b/amm/contracts/psp22/lib.rs @@ -1,134 +1,125 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -#![feature(min_specialization)] -#[openbrush::contract] +#[ink::contract] pub mod token { - use ink::{ - codegen::{ - EmitEvent, - Env, - }, - prelude::vec::Vec, + use ink::prelude::{ + string::String, + vec::Vec, }; - use openbrush::{ - contracts::psp22::extensions::metadata::*, - traits::{ - Storage, - String, - }, + use psp22::{ + PSP22Data, + PSP22Error, + PSP22Event, + PSP22Metadata, + PSP22, }; #[ink(event)] - pub struct Transfer { + pub struct Approval { #[ink(topic)] - from: Option, + owner: AccountId, #[ink(topic)] - to: Option, - value: Balance, + spender: AccountId, + amount: u128, } #[ink(event)] - pub struct Approval { + pub struct Transfer { #[ink(topic)] - owner: AccountId, + from: Option, #[ink(topic)] - spender: AccountId, - value: Balance, + to: Option, + value: u128, } #[ink(storage)] - #[derive(Default, Storage)] pub struct MyPSP22 { - #[storage_field] - psp22: psp22::Data, - #[storage_field] - metadata: metadata::Data, - } - - impl psp22::Internal for MyPSP22 { - fn _emit_transfer_event( - &self, - from: Option, - to: Option, - amount: Balance, - ) { - self.env().emit_event(Transfer { - from, - to, - value: amount, - }); - } - - fn _emit_approval_event(&self, owner: AccountId, spender: AccountId, amount: Balance) { - self.env().emit_event(Approval { - owner, - spender, - value: amount, - }); - } + data: PSP22Data, + name: Option, + symbol: Option, + decimals: u8, } impl MyPSP22 { #[ink(constructor)] pub fn new( - total_supply: Balance, + total_supply: u128, name: Option, symbol: Option, decimals: u8, ) -> Self { - let mut instance = Self::default(); - instance.metadata.name = name; - instance.metadata.symbol = symbol; - instance.metadata.decimals = decimals; - instance - ._mint_to(instance.env().caller(), total_supply) - .expect("Should mint"); - instance + Self { + data: PSP22Data::new(total_supply, Self::env().caller()), + name, + symbol, + decimals, + } + } + + fn emit_events(&self, events: Vec) { + for event in events { + match event { + PSP22Event::Transfer { from, to, value } => { + self.env().emit_event(Transfer { from, to, value }) + } + PSP22Event::Approval { + owner, + spender, + amount, + } => { + self.env().emit_event(Approval { + owner, + spender, + amount, + }) + } + } + } } } impl PSP22Metadata for MyPSP22 { #[ink(message)] fn token_name(&self) -> Option { - self.metadata.name.clone() + self.name.clone() } #[ink(message)] fn token_symbol(&self) -> Option { - self.metadata.symbol.clone() + self.symbol.clone() } #[ink(message)] fn token_decimals(&self) -> u8 { - self.metadata.decimals + self.decimals } } impl PSP22 for MyPSP22 { #[ink(message)] - fn total_supply(&self) -> Balance { - self._total_supply() + fn total_supply(&self) -> u128 { + self.data.total_supply() } #[ink(message)] - fn balance_of(&self, owner: AccountId) -> Balance { - self._balance_of(&owner) + fn balance_of(&self, owner: AccountId) -> u128 { + self.data.balance_of(owner) } #[ink(message)] - fn allowance(&self, owner: AccountId, spender: AccountId) -> Balance { - self._allowance(&owner, &spender) + fn allowance(&self, owner: AccountId, spender: AccountId) -> u128 { + self.data.allowance(owner, spender) } #[ink(message)] fn transfer( &mut self, to: AccountId, - value: Balance, - data: Vec, + value: u128, + _data: Vec, ) -> Result<(), PSP22Error> { - let from = self.env().caller(); - self._transfer_from_to(from, to, value, data)?; + let events = self.data.transfer(self.env().caller(), to, value)?; + self.emit_events(events); Ok(()) } @@ -137,25 +128,20 @@ pub mod token { &mut self, from: AccountId, to: AccountId, - value: Balance, - data: Vec, + value: u128, + _data: Vec, ) -> Result<(), PSP22Error> { - let caller = self.env().caller(); - let allowance = self._allowance(&from, &caller); - - if allowance < value { - return Err(PSP22Error::InsufficientAllowance) - } - - self._approve_from_to(from, caller, allowance - value)?; - self._transfer_from_to(from, to, value, data)?; + let events = self + .data + .transfer_from(self.env().caller(), from, to, value)?; + self.emit_events(events); Ok(()) } #[ink(message)] - fn approve(&mut self, spender: AccountId, value: Balance) -> Result<(), PSP22Error> { - let owner = self.env().caller(); - self._approve_from_to(owner, spender, value)?; + fn approve(&mut self, spender: AccountId, value: u128) -> Result<(), PSP22Error> { + let events = self.data.approve(self.env().caller(), spender, value)?; + self.emit_events(events); Ok(()) } @@ -163,30 +149,26 @@ pub mod token { fn increase_allowance( &mut self, spender: AccountId, - delta_value: Balance, + delta_value: u128, ) -> Result<(), PSP22Error> { - let owner = self.env().caller(); - self._approve_from_to( - owner, - spender, - self._allowance(&owner, &spender) + delta_value, - ) + let events = self + .data + .increase_allowance(self.env().caller(), spender, delta_value)?; + self.emit_events(events); + Ok(()) } #[ink(message)] fn decrease_allowance( &mut self, spender: AccountId, - delta_value: Balance, + delta_value: u128, ) -> Result<(), PSP22Error> { - let owner = self.env().caller(); - let allowance = self._allowance(&owner, &spender); - - if allowance < delta_value { - return Err(PSP22Error::InsufficientAllowance) - } - - self._approve_from_to(owner, spender, allowance - delta_value) + let events = self + .data + .decrease_allowance(self.env().caller(), spender, delta_value)?; + self.emit_events(events); + Ok(()) } } @@ -205,9 +187,9 @@ pub mod token { Some(symbol.clone()), decimals, ); - assert_eq!(token.metadata.name.unwrap(), name); - assert_eq!(token.metadata.symbol.unwrap(), symbol); - assert_eq!(token.metadata.decimals, decimals); + assert_eq!(token.name.unwrap(), name); + assert_eq!(token.symbol.unwrap(), symbol); + assert_eq!(token.decimals, decimals); } } } diff --git a/amm/contracts/router/Cargo.toml b/amm/contracts/router/Cargo.toml index 5014da6b..dec3b6d2 100755 --- a/amm/contracts/router/Cargo.toml +++ b/amm/contracts/router/Cargo.toml @@ -13,13 +13,10 @@ ink = { version = "=4.3.0", default-features = false } scale = { package = "parity-scale-codec", version = "3", default-features = false, features = [ "derive", ] } -scale-info = { version = "2.3", default-features = false, features = [ +scale-info = { version = "2.9", default-features = false, features = [ "derive", ], optional = true } -openbrush = { version = "3.2.0", default-features = false, features = [ - "psp22", -] } amm = { path = "../../logics", default-features = false } [lib] @@ -33,7 +30,6 @@ std = [ "scale/std", "scale-info", "scale-info/std", - "openbrush/std", "amm/std", ] ink-as-dependency = [] diff --git a/amm/contracts/router/lib.rs b/amm/contracts/router/lib.rs index fa319419..a158ccdb 100755 --- a/amm/contracts/router/lib.rs +++ b/amm/contracts/router/lib.rs @@ -1,7 +1,6 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -#![feature(min_specialization)] -#[openbrush::contract] +#[ink::contract] pub mod router { use amm::{ ensure, @@ -9,57 +8,141 @@ pub mod router { helper::*, transfer_helper::*, }, - impls::router::{ + traits::{ + factory::Factory, + pair::Pair, router::{ - ensure_deadline, - Internal, + Router, + RouterError, }, - *, - }, - traits::{ - pair::PairRef, - router::*, }, }; use ink::{ - codegen::Env, + codegen::TraitCallBuilder, + contract_ref, env::CallFlags, prelude::vec::Vec, }; - use openbrush::{ - modifiers, - traits::Storage, - }; #[ink(storage)] - #[derive(Default, Storage)] pub struct RouterContract { - #[storage_field] - router: data::Data, + factory: AccountId, + wnative: AccountId, } impl RouterContract { #[ink(constructor)] pub fn new(factory: AccountId, wnative: AccountId) -> Self { - let mut instance = Self::default(); - instance.router.factory = factory; - instance.router.wnative = wnative; - instance + Self { factory, wnative } + } + + fn _add_liquidity( + &self, + token_a: AccountId, + token_b: AccountId, + amount_a_desired: Balance, + amount_b_desired: Balance, + amount_a_min: Balance, + amount_b_min: Balance, + ) -> Result<(Balance, Balance), RouterError> { + if pair_for_on_chain(&self.factory, token_a, token_b).is_none() { + let mut factory: contract_ref!(Factory) = self.factory.into(); + factory.create_pair(token_a, token_b)?; + }; + + let (reserve_a, reserve_b) = get_reserves(&self.factory, token_a, token_b)?; + if reserve_a == 0 && reserve_b == 0 { + return Ok((amount_a_desired, amount_b_desired)) + } + + let amount_b_optimal = quote(amount_a_desired, reserve_a, reserve_b)?; + if amount_b_optimal <= amount_b_desired { + ensure!( + amount_b_optimal >= amount_b_min, + RouterError::InsufficientBAmount + ); + Ok((amount_a_desired, amount_b_optimal)) + } else { + let amount_a_optimal = quote(amount_b_desired, reserve_b, reserve_a)?; + // amount_a_optimal <= amount_a_desired holds as amount_b_optimal > amount_b_desired + ensure!( + amount_a_optimal >= amount_a_min, + RouterError::InsufficientAAmount + ); + Ok((amount_a_optimal, amount_b_desired)) + } + } + + fn _swap( + &self, + amounts: &[Balance], + path: &Vec, + _to: AccountId, + ) -> Result<(), RouterError> { + for i in 0..path.len() - 1 { + let (input, output) = (path[i], path[i + 1]); + let (token_0, _) = sort_tokens(input, output)?; + let amount_out = amounts[i + 1]; + let (amount_0_out, amount_1_out) = if input == token_0 { + (0, amount_out) + } else { + (amount_out, 0) + }; + // If last pair in the path, transfer tokens to the `_to` recipient. + // Otherwise, transfer to the next Pair contract instance. + let to = if i < path.len() - 2 { + pair_for_on_chain(&self.factory, output, path[i + 2]) + .ok_or(RouterError::PairNotFound)? + } else { + _to + }; + let pair = pair_for_on_chain(&self.factory, input, output) + .ok_or(RouterError::PairNotFound)?; + let mut pair: contract_ref!(Pair) = pair.into(); + + match pair + .call_mut() + .swap(amount_0_out, amount_1_out, to) + .call_flags(CallFlags::default().set_allow_reentry(true)) + .try_invoke() + { + Ok(res) => { + match res { + Ok(v) => { + match v { + Ok(v) => Ok(v), + Err(err) => Err(RouterError::PairError(err)), + } + } + Err(err) => Err(RouterError::LangError(err)), + } + } + Err(_) => Err(RouterError::TransferError), + }?; + } + Ok(()) + } + + fn check_timestamp(&self, deadline: Timestamp) -> Result<(), RouterError> { + ensure!( + deadline >= self.env().block_timestamp(), + RouterError::Expired + ); + Ok(()) } } impl Router for RouterContract { #[ink(message)] fn factory(&self) -> AccountId { - self.router.factory + self.factory } #[ink(message)] fn wnative(&self) -> AccountId { - self.router.wnative + self.wnative } - #[modifiers(ensure_deadline(deadline))] #[ink(message)] fn add_liquidity( &mut self, @@ -72,6 +155,7 @@ pub mod router { to: AccountId, deadline: u64, ) -> Result<(Balance, Balance, Balance), RouterError> { + self.check_timestamp(deadline)?; let (amount_a, amount_b) = self._add_liquidity( token_a, token_b, @@ -81,19 +165,19 @@ pub mod router { amount_b_min, )?; - let pair_contract = pair_for_on_chain(&self.router.factory, token_a, token_b) + let pair_contract = pair_for_on_chain(&self.factory, token_a, token_b) .ok_or(RouterError::PairNotFound)?; let caller = self.env().caller(); safe_transfer_from(token_a, caller, pair_contract, amount_a)?; safe_transfer_from(token_b, caller, pair_contract, amount_b)?; - let liquidity = PairRef::mint(&pair_contract, to)?; + let mut pair: contract_ref!(Pair) = pair_contract.into(); + let liquidity = pair.mint(to)?; Ok((amount_a, amount_b, liquidity)) } - #[modifiers(ensure_deadline(deadline))] #[ink(message)] fn add_liquidity_native( &mut self, @@ -104,7 +188,8 @@ pub mod router { to: AccountId, deadline: u64, ) -> Result<(Balance, Balance, Balance), RouterError> { - let wnative = self.router.wnative; + self.check_timestamp(deadline)?; + let wnative = self.wnative; let received_value = self.env().transferred_value(); let (amount_a, amount_native) = self._add_liquidity( @@ -116,7 +201,7 @@ pub mod router { amount_native_min, )?; - let pair_contract = pair_for_on_chain(&self.router.factory, token, wnative) + let pair_contract = pair_for_on_chain(&self.factory, token, wnative) .ok_or(RouterError::PairNotFound)?; let caller = self.env().caller(); @@ -124,7 +209,8 @@ pub mod router { wrap(&wnative, amount_native)?; safe_transfer(wnative, pair_contract, amount_native)?; - let liquidity = PairRef::mint(&pair_contract, to)?; + let mut pair: contract_ref!(Pair) = pair_contract.into(); + let liquidity = pair.mint(to)?; if received_value > amount_native { safe_transfer_native(caller, received_value - amount_native)? @@ -133,7 +219,6 @@ pub mod router { Ok((amount_a, amount_native, liquidity)) } - #[modifiers(ensure_deadline(deadline))] #[ink(message)] fn remove_liquidity( &mut self, @@ -145,12 +230,17 @@ pub mod router { to: AccountId, deadline: u64, ) -> Result<(Balance, Balance), RouterError> { - let pair_contract = pair_for_on_chain(&self.router.factory, token_a, token_b) + self.check_timestamp(deadline)?; + let pair_contract = pair_for_on_chain(&self.factory, token_a, token_b) .ok_or(RouterError::PairNotFound)?; safe_transfer_from(pair_contract, self.env().caller(), pair_contract, liquidity)?; - let (amount_0, amount_1) = match PairRef::burn_builder(&pair_contract, to) + let mut pair: contract_ref!(Pair) = pair_contract.into(); + + let (amount_0, amount_1) = match pair + .call_mut() + .burn(to) .call_flags(CallFlags::default().set_allow_reentry(true)) .try_invoke() { @@ -180,7 +270,6 @@ pub mod router { Ok((amount_a, amount_b)) } - #[modifiers(ensure_deadline(deadline))] #[ink(message)] fn remove_liquidity_native( &mut self, @@ -191,7 +280,8 @@ pub mod router { to: AccountId, deadline: u64, ) -> Result<(Balance, Balance), RouterError> { - let wnative = self.router.wnative; + self.check_timestamp(deadline)?; + let wnative = self.wnative; let (amount_token, amount_native) = self.remove_liquidity( token, wnative, @@ -207,7 +297,6 @@ pub mod router { Ok((amount_token, amount_native)) } - #[modifiers(ensure_deadline(deadline))] #[ink(message)] fn swap_exact_tokens_for_tokens( &mut self, @@ -217,7 +306,8 @@ pub mod router { to: AccountId, deadline: u64, ) -> Result, RouterError> { - let factory = self.router.factory; + self.check_timestamp(deadline)?; + let factory = self.factory; let amounts = get_amounts_out(&factory, amount_in, &path)?; ensure!( @@ -234,7 +324,6 @@ pub mod router { Ok(amounts) } - #[modifiers(ensure_deadline(deadline))] #[ink(message)] fn swap_tokens_for_exact_tokens( &mut self, @@ -244,7 +333,8 @@ pub mod router { to: AccountId, deadline: u64, ) -> Result, RouterError> { - let factory = self.router.factory; + self.check_timestamp(deadline)?; + let factory = self.factory; let amounts = get_amounts_in(&factory, amount_out, &path)?; ensure!( amounts[0] <= amount_in_max, @@ -260,7 +350,6 @@ pub mod router { Ok(amounts) } - #[modifiers(ensure_deadline(deadline))] #[ink(message)] fn swap_exact_native_for_tokens( &mut self, @@ -269,10 +358,11 @@ pub mod router { to: AccountId, deadline: u64, ) -> Result, RouterError> { - let factory = self.router.factory; + self.check_timestamp(deadline)?; + let factory = self.factory; let received_value = self.env().transferred_value(); - let wnative = self.router.wnative; + let wnative = self.wnative; ensure!(path[0] == wnative, RouterError::InvalidPath); let amounts = get_amounts_out(&factory, received_value, &path)?; ensure!( @@ -289,7 +379,6 @@ pub mod router { Ok(amounts) } - #[modifiers(ensure_deadline(deadline))] #[ink(message)] fn swap_tokens_for_exact_native( &mut self, @@ -299,9 +388,10 @@ pub mod router { to: AccountId, deadline: u64, ) -> Result, RouterError> { - let factory = self.router.factory; + self.check_timestamp(deadline)?; + let factory = self.factory; - let wnative = self.router.wnative; + let wnative = self.wnative; ensure!(path[path.len() - 1] == wnative, RouterError::InvalidPath); let amounts = get_amounts_in(&factory, amount_out, &path)?; ensure!( @@ -321,7 +411,6 @@ pub mod router { Ok(amounts) } - #[modifiers(ensure_deadline(deadline))] #[ink(message)] fn swap_exact_tokens_for_native( &mut self, @@ -331,9 +420,10 @@ pub mod router { to: AccountId, deadline: u64, ) -> Result, RouterError> { - let factory = self.router.factory; + self.check_timestamp(deadline)?; + let factory = self.factory; - let wnative = self.router.wnative; + let wnative = self.wnative; ensure!(path[path.len() - 1] == wnative, RouterError::InvalidPath); let amounts = get_amounts_out(&factory, amount_in, &path)?; let native_out = amounts[amounts.len() - 1]; @@ -353,7 +443,6 @@ pub mod router { Ok(amounts) } - #[modifiers(ensure_deadline(deadline))] #[ink(message)] fn swap_native_for_exact_tokens( &mut self, @@ -362,8 +451,9 @@ pub mod router { to: AccountId, deadline: u64, ) -> Result, RouterError> { - let factory = self.router.factory; - let wnative = self.router.wnative; + self.check_timestamp(deadline)?; + let factory = self.factory; + let wnative = self.wnative; let received_native = self.env().transferred_value(); ensure!(path[0] == wnative, RouterError::InvalidPath); @@ -422,7 +512,7 @@ pub mod router { amount_in: Balance, path: Vec, ) -> Result, RouterError> { - Ok(get_amounts_out(&self.router.factory, amount_in, &path)?) + Ok(get_amounts_out(&self.factory, amount_in, &path)?) } #[ink(message)] @@ -431,7 +521,7 @@ pub mod router { amount_out: Balance, path: Vec, ) -> Result, RouterError> { - Ok(get_amounts_in(&self.router.factory, amount_out, &path)?) + Ok(get_amounts_in(&self.factory, amount_out, &path)?) } } diff --git a/amm/contracts/wnative/Cargo.toml b/amm/contracts/wnative/Cargo.toml index 660e09e2..538f17fe 100644 --- a/amm/contracts/wnative/Cargo.toml +++ b/amm/contracts/wnative/Cargo.toml @@ -10,15 +10,17 @@ ink = { version = "=4.3.0", default-features = false } scale = { package = "parity-scale-codec", version = "3", default-features = false, features = [ "derive", ] } -scale-info = { version = "2.3", default-features = false, features = [ +scale-info = { version = "2.9", default-features = false, features = [ "derive", ], optional = true } -openbrush = { version = "3.2.0", default-features = false, features = [ - "psp22", -] } amm = { path = "../../logics", default-features = false } +psp22 = { git = "https://github.com/Cardinal-Cryptography/PSP22.git", default-features = false, features = [ + "mintable", + "burnable", +] } + [lib] name = "wnative_contract" path = "lib.rs" @@ -30,7 +32,7 @@ std = [ "scale/std", "scale-info", "scale-info/std", - "openbrush/std", "amm/std", + "psp22/std", ] ink-as-dependency = [] diff --git a/amm/contracts/wnative/lib.rs b/amm/contracts/wnative/lib.rs index fa6f5dce..6cd1ed23 100644 --- a/amm/contracts/wnative/lib.rs +++ b/amm/contracts/wnative/lib.rs @@ -1,114 +1,144 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -#![feature(min_specialization)] -#[openbrush::contract] +#[ink::contract] pub mod wnative { - use amm::traits::wnative::{ - wnative_external, - Wnative, + use amm::traits::wnative::Wnative; + use ink::prelude::{ + string::String, + vec::Vec, }; - use ink::{ - codegen::{ - EmitEvent, - Env, - }, - prelude::vec::Vec, - }; - use openbrush::{ - contracts::psp22::extensions::metadata::*, - traits::{ - Storage, - String, - }, + use psp22::{ + PSP22Data, + PSP22Error, + PSP22Event, + PSP22Metadata, + PSP22, }; - #[ink(event)] - pub struct Transfer { - #[ink(topic)] - from: Option, - #[ink(topic)] - to: Option, - value: Balance, + #[ink(storage)] + #[derive(Default)] + pub struct WnativeContract { + data: PSP22Data, } - #[ink(event)] - pub struct Approval { - #[ink(topic)] - owner: AccountId, - #[ink(topic)] - spender: AccountId, - value: Balance, - } + impl WnativeContract { + #[ink(constructor)] + pub fn new() -> Self { + Self::default() + } - #[ink(storage)] - #[derive(Default, Storage)] - pub struct WnativeContract { - #[storage_field] - psp22: psp22::Data, - #[storage_field] - metadata: metadata::Data, + fn emit_events(&self, events: Vec) { + for event in events { + match event { + PSP22Event::Transfer { from, to, value } => { + self.env().emit_event(Transfer { from, to, value }) + } + PSP22Event::Approval { + owner, + spender, + amount, + } => { + self.env().emit_event(Approval { + owner, + spender, + amount, + }) + } + } + } + } + + /// For e2e testing purposes only. Do not use in production! + #[cfg(feature = "e2e-tests")] + #[ink(message)] + pub fn terminate(&mut self) -> Result<(), PSP22Error> { + let caller = self.env().caller(); + self.env().terminate_contract(caller) + } } impl Wnative for WnativeContract { #[ink(message)] fn deposit(&mut self) -> Result<(), PSP22Error> { - let transfer_value = self.env().transferred_value(); - let caller = self.env().caller(); - self._mint_to(caller, transfer_value) + let events = self + .data + .mint(self.env().caller(), self.env().transferred_value())?; + self.emit_events(events); + Ok(()) } #[ink(message)] - fn withdraw(&mut self, amount: Balance) -> Result<(), PSP22Error> { + fn withdraw(&mut self, value: Balance) -> Result<(), PSP22Error> { let caller = self.env().caller(); - self._burn_from(caller, amount)?; + let events = self.data.burn(caller, value)?; self.env() - .transfer(caller, amount) - .map_err(|_| PSP22Error::Custom(String::from("WNATIVE: transfer failed"))) + .transfer(caller, value) + .map_err(|_| PSP22Error::Custom(String::from("Wrapper AZERO: withdraw failed")))?; + self.emit_events(events); + Ok(()) } } impl PSP22Metadata for WnativeContract { #[ink(message)] fn token_name(&self) -> Option { - self.metadata.name.clone() + Some(String::from("Wrapped AZERO")) } #[ink(message)] fn token_symbol(&self) -> Option { - self.metadata.symbol.clone() + Some(String::from("WAZERO")) } #[ink(message)] fn token_decimals(&self) -> u8 { - self.metadata.decimals + 12 } } + #[ink(event)] + pub struct Approval { + #[ink(topic)] + owner: AccountId, + #[ink(topic)] + spender: AccountId, + amount: u128, + } + + #[ink(event)] + pub struct Transfer { + #[ink(topic)] + from: Option, + #[ink(topic)] + to: Option, + value: u128, + } + impl PSP22 for WnativeContract { #[ink(message)] - fn total_supply(&self) -> Balance { - self._total_supply() + fn total_supply(&self) -> u128 { + self.data.total_supply() } #[ink(message)] - fn balance_of(&self, owner: AccountId) -> Balance { - self._balance_of(&owner) + fn balance_of(&self, owner: AccountId) -> u128 { + self.data.balance_of(owner) } #[ink(message)] - fn allowance(&self, owner: AccountId, spender: AccountId) -> Balance { - self._allowance(&owner, &spender) + fn allowance(&self, owner: AccountId, spender: AccountId) -> u128 { + self.data.allowance(owner, spender) } #[ink(message)] fn transfer( &mut self, to: AccountId, - value: Balance, - data: Vec, + value: u128, + _data: ink::prelude::vec::Vec, ) -> Result<(), PSP22Error> { - let from = self.env().caller(); - self._transfer_from_to(from, to, value, data)?; + let events = self.data.transfer(self.env().caller(), to, value)?; + self.emit_events(events); Ok(()) } @@ -117,25 +147,20 @@ pub mod wnative { &mut self, from: AccountId, to: AccountId, - value: Balance, - data: Vec, + value: u128, + _data: ink::prelude::vec::Vec, ) -> Result<(), PSP22Error> { - let caller = self.env().caller(); - let allowance = self._allowance(&from, &caller); - - if allowance < value { - return Err(PSP22Error::InsufficientAllowance) - } - - self._approve_from_to(from, caller, allowance - value)?; - self._transfer_from_to(from, to, value, data)?; + let events = self + .data + .transfer_from(self.env().caller(), from, to, value)?; + self.emit_events(events); Ok(()) } #[ink(message)] - fn approve(&mut self, spender: AccountId, value: Balance) -> Result<(), PSP22Error> { - let owner = self.env().caller(); - self._approve_from_to(owner, spender, value)?; + fn approve(&mut self, spender: AccountId, value: u128) -> Result<(), PSP22Error> { + let events = self.data.approve(self.env().caller(), spender, value)?; + self.emit_events(events); Ok(()) } @@ -143,71 +168,26 @@ pub mod wnative { fn increase_allowance( &mut self, spender: AccountId, - delta_value: Balance, + delta_value: u128, ) -> Result<(), PSP22Error> { - let owner = self.env().caller(); - self._approve_from_to( - owner, - spender, - self._allowance(&owner, &spender) + delta_value, - ) + let events = self + .data + .increase_allowance(self.env().caller(), spender, delta_value)?; + self.emit_events(events); + Ok(()) } #[ink(message)] fn decrease_allowance( &mut self, spender: AccountId, - delta_value: Balance, + delta_value: u128, ) -> Result<(), PSP22Error> { - let owner = self.env().caller(); - let allowance = self._allowance(&owner, &spender); - - if allowance < delta_value { - return Err(PSP22Error::InsufficientAllowance) - } - - self._approve_from_to(owner, spender, allowance - delta_value) - } - } - - impl psp22::Internal for WnativeContract { - fn _emit_transfer_event( - &self, - from: Option, - to: Option, - amount: Balance, - ) { - self.env().emit_event(Transfer { - from, - to, - value: amount, - }); - } - - fn _emit_approval_event(&self, owner: AccountId, spender: AccountId, amount: Balance) { - self.env().emit_event(Approval { - owner, - spender, - value: amount, - }); - } - } - - impl WnativeContract { - #[ink(constructor)] - pub fn new() -> Self { - let mut instance = Self::default(); - instance.metadata.name = Some(String::from("Wrapped Native")); - instance.metadata.symbol = Some(String::from("WNATIVE")); - instance.metadata.decimals = 12; - instance - } - /// For e2e testing purposes only. Do not use in production! - #[cfg(feature = "e2e-tests")] - #[ink(message)] - pub fn terminate(&mut self) -> Result<(), PSP22Error> { - let caller = self.env().caller(); - self.env().terminate_contract(caller) + let events = self + .data + .decrease_allowance(self.env().caller(), spender, delta_value)?; + self.emit_events(events); + Ok(()) } } @@ -219,12 +199,12 @@ pub mod wnative { fn register_works() { let wnative_contract = WnativeContract::new(); assert_eq!( - wnative_contract.metadata.name, - Some(String::from("Wrapped Native")) + wnative_contract.token_name(), + Some(String::from("Wrapped AZERO")) ); assert_eq!( - wnative_contract.metadata.symbol, - Some(String::from("WNATIVE")) + wnative_contract.token_symbol(), + Some(String::from("WAZERO")) ); } @@ -235,7 +215,7 @@ pub mod wnative { assert_eq!(deposit(&mut wnative_contract, 1000), Ok(())); let balance = wnative_contract.balance_of(accounts.alice); assert_eq!(balance, 1000, "balance not correct!"); - let native_balance: Balance = wnative_contract.env().balance(); + let native_balance: Balance = get_balance(contract_id()); assert_eq!(native_balance, 1000, "native balance not correct!"); } @@ -243,10 +223,9 @@ pub mod wnative { fn test_withdraw() { let accounts = default_accounts(); let mut wnative_contract = create_contract(1000); - assert_eq!(get_balance(wnative_contract.env().account_id()), 1000); - assert_eq!( - wnative_contract._mint_to(accounts.alice, 1000), - Ok(()), + assert_eq!(get_balance(contract_id()), 1000); + assert!( + wnative_contract.data.mint(accounts.alice, 1000).is_ok(), "mint failed" ); let wnative_balance = wnative_contract.balance_of(accounts.alice); @@ -271,6 +250,15 @@ pub mod wnative { ink::env::test::set_caller::(caller); } + fn contract_id() -> AccountId { + ink::env::test::callee::() + } + + fn get_balance(account_id: AccountId) -> Balance { + ink::env::test::get_account_balance::(account_id) + .expect("Cannot get account balance") + } + fn set_balance(account_id: AccountId, balance: Balance) { ink::env::test::set_account_balance::(account_id, balance) } @@ -285,18 +273,9 @@ pub mod wnative { WnativeContract::new() } - fn contract_id() -> AccountId { - ink::env::test::callee::() - } - - fn get_balance(account_id: AccountId) -> Balance { - ink::env::test::get_account_balance::(account_id) - .expect("Cannot get account balance") - } - fn deposit(contract: &mut WnativeContract, amount: Balance) -> Result<(), PSP22Error> { let sender = ink::env::caller::(); - let contract_id = contract.env().account_id(); + let contract_id = contract_id(); let sender_balance = get_balance(sender); let contract_balance = get_balance(contract_id); // ↓ doesn't work, is upstream issue: https://github.com/paritytech/ink/issues/1117 diff --git a/amm/logics/Cargo.lock b/amm/logics/Cargo.lock index c746e8e3..051b2dc6 100644 --- a/amm/logics/Cargo.lock +++ b/amm/logics/Cargo.lock @@ -19,9 +19,9 @@ dependencies = [ "curve25519-dalek", "ink", "ink_metadata", - "openbrush", "parity-scale-codec", "primitive-types", + "psp22", "scale-info", "sha2", "sp-arithmetic", @@ -80,24 +80,13 @@ dependencies = [ "wyz", ] -[[package]] -name = "blake2" -version = "0.9.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a4e37d16930f5459780f5621038b6382b9bb37c19016f39fb6b5808d831f174" -dependencies = [ - "crypto-mac", - "digest 0.9.0", - "opaque-debug", -] - [[package]] name = "blake2" version = "0.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "46502ad458c9a52b69d4d4d32775c788b7a1b85e8bc9d482d92250fc0e3f8efe" dependencies = [ - "digest 0.10.6", + "digest", ] [[package]] @@ -121,38 +110,6 @@ version = "1.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" -[[package]] -name = "camino" -version = "1.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c530edf18f37068ac2d977409ed5cd50d53d73bc653c7647b48eb78976ac9ae2" -dependencies = [ - "serde", -] - -[[package]] -name = "cargo-platform" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cbdb825da8a5df079a43676dbe042702f1707b1109f713a01420fbb4cc71fa27" -dependencies = [ - "serde", -] - -[[package]] -name = "cargo_metadata" -version = "0.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "081e3f0755c1f380c2d010481b6fa2e02973586d5f2b24eebb7a2a1d98b143d8" -dependencies = [ - "camino", - "cargo-platform", - "semver", - "semver-parser", - "serde", - "serde_json", -] - [[package]] name = "cc" version = "1.0.79" @@ -165,26 +122,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "const_format" -version = "0.2.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7309d9b4d3d2c0641e018d449232f2e28f1b22933c137f157d3dbc14228b8c0e" -dependencies = [ - "const_format_proc_macros", -] - -[[package]] -name = "const_format_proc_macros" -version = "0.2.29" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d897f47bf7270cf70d370f8f98c1abb6d2d4cf60a6845d30e05bfb90c6568650" -dependencies = [ - "proc-macro2", - "quote", - "unicode-xid", -] - [[package]] name = "cpufeatures" version = "0.2.6" @@ -210,16 +147,6 @@ dependencies = [ "typenum", ] -[[package]] -name = "crypto-mac" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b584a330336237c1eecd3e94266efb216c56ed91225d634cb2991c5f3fd1aeab" -dependencies = [ - "generic-array", - "subtle", -] - [[package]] name = "curve25519-dalek" version = "4.0.0-rc.2" @@ -227,7 +154,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "03d928d978dbec61a1167414f5ec534f24bea0d7a0d24dd9b6233d3d8223e585" dependencies = [ "cfg-if", - "digest 0.10.6", + "digest", "fiat-crypto", "packed_simd_2", "platforms", @@ -281,15 +208,6 @@ dependencies = [ "syn 1.0.109", ] -[[package]] -name = "digest" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" -dependencies = [ - "generic-array", -] - [[package]] name = "digest" version = "0.10.6" @@ -349,9 +267,9 @@ checksum = "e825f6987101665dea6ec934c09ec6d721de7bc1bf92248e1d5810c8cd636b77" [[package]] name = "fixed-hash" -version = "0.7.0" +version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cfcf0ed7fe52a17a03854ec54a9f76d6d84508d1c0e66bc1793301c73fc8493c" +checksum = "835c052cb0c08c1acf6ffd71c022172e18723949c8282f2b9f27efbc51e64534" dependencies = [ "byteorder", "rand", @@ -365,16 +283,6 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" -[[package]] -name = "fs2" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213" -dependencies = [ - "libc", - "winapi", -] - [[package]] name = "funty" version = "2.0.0" @@ -408,15 +316,6 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" -[[package]] -name = "heck" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" -dependencies = [ - "unicode-segmentation", -] - [[package]] name = "heck" version = "0.4.1" @@ -517,11 +416,11 @@ version = "4.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22d79057b2565df31a10af6510a44b161093f110c5f9c22ad02c20af9cea4c29" dependencies = [ - "blake2 0.10.6", + "blake2", "derive_more", "either", "env_logger", - "heck 0.4.1", + "heck", "impl-serde", "ink_ir", "ink_primitives", @@ -532,7 +431,7 @@ dependencies = [ "quote", "serde", "serde_json", - "syn 2.0.11", + "syn 2.0.38", ] [[package]] @@ -541,7 +440,7 @@ version = "4.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "722ec3a5eb557124b001c60ff8f961079f6d566af643edea579f152b15822fe5" dependencies = [ - "blake2 0.10.6", + "blake2", "derive_more", "ink_primitives", "parity-scale-codec", @@ -557,7 +456,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "584e73bc0982f6f1a067bb63ebc75262f6dc54ed2a17060efa73eaba84dc9308" dependencies = [ "arrayref", - "blake2 0.10.6", + "blake2", "cfg-if", "derive_more", "ink_allocator", @@ -584,12 +483,12 @@ version = "4.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b529c941518e8f450395fab9fe8ebba0a7acbb18778fc7e0a87f6248286ec72" dependencies = [ - "blake2 0.10.6", + "blake2", "either", "itertools", "proc-macro2", "quote", - "syn 2.0.11", + "syn 2.0.38", ] [[package]] @@ -604,8 +503,8 @@ dependencies = [ "parity-scale-codec", "proc-macro2", "quote", - "syn 2.0.11", - "synstructure 0.13.0", + "syn 2.0.38", + "synstructure", ] [[package]] @@ -775,122 +674,12 @@ dependencies = [ "autocfg", ] -[[package]] -name = "obce" -version = "0.1.0" -source = "git+https://github.com/727-Ventures/obce?branch=polkadot-v0.9.37#d452f6eda1bc1ecb36e7e332d61529ad440d5a89" -dependencies = [ - "ink", - "ink_engine", - "obce-macro", - "parity-scale-codec", - "scale-info", -] - -[[package]] -name = "obce-codegen" -version = "0.1.0" -source = "git+https://github.com/727-Ventures/obce?branch=polkadot-v0.9.37#d452f6eda1bc1ecb36e7e332d61529ad440d5a89" -dependencies = [ - "blake2 0.10.6", - "itertools", - "proc-macro2", - "quote", - "syn 1.0.109", - "tuple", -] - -[[package]] -name = "obce-macro" -version = "0.1.0" -source = "git+https://github.com/727-Ventures/obce?branch=polkadot-v0.9.37#d452f6eda1bc1ecb36e7e332d61529ad440d5a89" -dependencies = [ - "obce-codegen", - "proc-macro2", - "syn 1.0.109", - "synstructure 0.12.6", -] - [[package]] name = "once_cell" version = "1.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7e5500299e16ebb147ae15a00a942af264cf3688f47923b8fc2cd5858f23ad3" -[[package]] -name = "opaque-debug" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" - -[[package]] -name = "openbrush" -version = "3.1.1" -source = "git+https://github.com/727-Ventures/openbrush-contracts?tag=3.1.1#13e82f3f02e8ea65a95ea02c381106ea738e1fb8" -dependencies = [ - "ink", - "openbrush_contracts", - "openbrush_lang", - "parity-scale-codec", - "scale-info", -] - -[[package]] -name = "openbrush_contracts" -version = "3.1.1" -source = "git+https://github.com/727-Ventures/openbrush-contracts?tag=3.1.1#13e82f3f02e8ea65a95ea02c381106ea738e1fb8" -dependencies = [ - "ink", - "openbrush_lang", - "pallet-assets-chain-extension", - "parity-scale-codec", - "scale-info", -] - -[[package]] -name = "openbrush_lang" -version = "3.1.1" -source = "git+https://github.com/727-Ventures/openbrush-contracts?tag=3.1.1#13e82f3f02e8ea65a95ea02c381106ea738e1fb8" -dependencies = [ - "const_format", - "ink", - "openbrush_lang_macro", - "parity-scale-codec", - "scale-info", - "xxhash-rust", -] - -[[package]] -name = "openbrush_lang_codegen" -version = "3.1.1" -source = "git+https://github.com/727-Ventures/openbrush-contracts?tag=3.1.1#13e82f3f02e8ea65a95ea02c381106ea738e1fb8" -dependencies = [ - "blake2 0.9.2", - "cargo_metadata", - "fs2", - "heck 0.3.3", - "ink_ir", - "ink_primitives", - "proc-macro2", - "quote", - "serde", - "serde_json", - "syn 1.0.109", - "synstructure 0.12.6", - "unwrap", -] - -[[package]] -name = "openbrush_lang_macro" -version = "3.1.1" -source = "git+https://github.com/727-Ventures/openbrush-contracts?tag=3.1.1#13e82f3f02e8ea65a95ea02c381106ea738e1fb8" -dependencies = [ - "openbrush_lang_codegen", - "proc-macro2", - "syn 1.0.109", - "synstructure 0.12.6", -] - [[package]] name = "packed_simd_2" version = "0.3.8" @@ -901,22 +690,11 @@ dependencies = [ "libm", ] -[[package]] -name = "pallet-assets-chain-extension" -version = "0.1.1" -source = "git+https://github.com/727-ventures/pallet-assets-chain-extension?branch=polkadot-v0.9.37#f8ea374186df2a3fc139c8d585719e58d83df582" -dependencies = [ - "ink", - "obce", - "parity-scale-codec", - "scale-info", -] - [[package]] name = "parity-scale-codec" -version = "3.4.0" +version = "3.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "637935964ff85a605d114591d4d2c13c5d1ba2806dae97cea6bf180238a749ac" +checksum = "0dec8a8073036902368c2cdc0387e85ff9a37054d7e7c98e592145e0c92cd4fb" dependencies = [ "arrayvec", "bitvec", @@ -928,9 +706,9 @@ dependencies = [ [[package]] name = "parity-scale-codec-derive" -version = "3.1.4" +version = "3.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "86b26a931f824dd4eca30b3e43bb4f31cd5f0d3a403c5f5ff27106b805bfde7b" +checksum = "312270ee71e1cd70289dacf597cab7b207aa107d2f28191c2ae45b2ece18a260" dependencies = [ "proc-macro-crate", "proc-macro2", @@ -944,16 +722,6 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f746c4065a8fa3fe23974dd82f15431cc8d40779821001404d10d2e79ca7d79" -[[package]] -name = "pest" -version = "2.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cbd939b234e95d72bc393d51788aec68aeeb5d51e748ca08ff3aad58cb722f7" -dependencies = [ - "thiserror", - "ucd-trie", -] - [[package]] name = "platforms" version = "3.0.2" @@ -968,9 +736,9 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "primitive-types" -version = "0.11.1" +version = "0.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e28720988bff275df1f51b171e1b2a18c30d194c4d2b61defdacecd625a5d94a" +checksum = "0b34d9fd68ae0b74a41b21c03c2f62847aa0ffea044eee893b4c140b37e244e2" dependencies = [ "fixed-hash", "impl-codec", @@ -990,18 +758,28 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.54" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e472a104799c74b514a57226160104aa483546de37e839ec50e3c2e41dd87534" +checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" dependencies = [ "unicode-ident", ] +[[package]] +name = "psp22" +version = "0.1.0" +source = "git+https://github.com/Cardinal-Cryptography/PSP22.git#c6744743e645f8120ceef84e2dbadf8f194fdb4f" +dependencies = [ + "ink", + "parity-scale-codec", + "scale-info", +] + [[package]] name = "quote" -version = "1.0.26" +version = "1.0.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" dependencies = [ "proc-macro2", ] @@ -1156,9 +934,9 @@ dependencies = [ [[package]] name = "scale-info" -version = "2.7.0" +version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b569c32c806ec3abdf3b5869fb8bf1e0d275a7c1c9b0b05603d9464632649edf" +checksum = "35c0a159d0c45c12b20c5a844feb1fe4bea86e28f17b92a5f0c42193634d3782" dependencies = [ "bitvec", "cfg-if", @@ -1170,9 +948,9 @@ dependencies = [ [[package]] name = "scale-info-derive" -version = "2.6.0" +version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53012eae69e5aa5c14671942a5dd47de59d4cdcff8532a6dd0e081faf1119482" +checksum = "912e55f6d20e0e80d63733872b40e1227c0bce1e1ab81ba67d696339bfd7fd29" dependencies = [ "proc-macro-crate", "proc-macro2", @@ -1198,43 +976,24 @@ dependencies = [ "cc", ] -[[package]] -name = "semver" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f301af10236f6df4160f7c3f04eec6dbc70ace82d23326abad5edee88801c6b6" -dependencies = [ - "semver-parser", - "serde", -] - -[[package]] -name = "semver-parser" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00b0bef5b7f9e0df16536d3961cfb6e84331c065b4066afb39768d0e319411f7" -dependencies = [ - "pest", -] - [[package]] name = "serde" -version = "1.0.159" +version = "1.0.188" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3c04e8343c3daeec41f58990b9d77068df31209f2af111e059e9fe9646693065" +checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.159" +version = "1.0.188" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c614d17805b093df4b147b51339e7e44bf05ef59fba1e45d83500bcfb4d8585" +checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" dependencies = [ "proc-macro2", "quote", - "syn 2.0.11", + "syn 2.0.38", ] [[package]] @@ -1256,7 +1015,7 @@ checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0" dependencies = [ "cfg-if", "cpufeatures", - "digest 0.10.6", + "digest", ] [[package]] @@ -1265,7 +1024,7 @@ version = "0.10.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "75872d278a8f37ef87fa0ddbda7802605cb18344497949862c0d4dcb291eba60" dependencies = [ - "digest 0.10.6", + "digest", "keccak", ] @@ -1277,8 +1036,9 @@ checksum = "62bb4feee49fdd9f707ef802e22365a35de4b7b299de4763d44bfea899442ff9" [[package]] name = "sp-arithmetic" -version = "6.0.0" -source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.37#f38bd6671d460293c93062cc1e4fe9e9e490cb29" +version = "18.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d3d3ff6d6d717d7563659e9e47e958d33ebd2d0b3d8b1a9961cf9832944375e" dependencies = [ "integer-sqrt", "num-traits", @@ -1291,8 +1051,9 @@ dependencies = [ [[package]] name = "sp-std" -version = "5.0.0" -source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.37#f38bd6671d460293c93062cc1e4fe9e9e490cb29" +version = "10.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ed09ef1760e8be9b64b7f739f1cf9a94528130be475d8e4f2d1be1e690c9f9c" [[package]] name = "static_assertions" @@ -1325,27 +1086,15 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.11" +version = "2.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21e3787bb71465627110e7d87ed4faaa36c1f61042ee67badb9e2ef173accc40" +checksum = "e96b79aaa137db8f61e26363a0c9b47d8b4ec75da28b7d1d614c2303e232408b" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] -[[package]] -name = "synstructure" -version = "0.12.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", - "unicode-xid", -] - [[package]] name = "synstructure" version = "0.13.0" @@ -1354,7 +1103,7 @@ checksum = "285ba80e733fac80aa4270fbcdf83772a79b80aa35c97075320abfee4a915b06" dependencies = [ "proc-macro2", "quote", - "syn 2.0.11", + "syn 2.0.38", "unicode-xid", ] @@ -1373,26 +1122,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "thiserror" -version = "1.0.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "978c9a314bd8dc99be594bc3c175faaa9794be04a5a5e153caba6915336cebac" -dependencies = [ - "thiserror-impl", -] - -[[package]] -name = "thiserror-impl" -version = "1.0.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.11", -] - [[package]] name = "toml_datetime" version = "0.6.1" @@ -1410,28 +1139,12 @@ dependencies = [ "winnow", ] -[[package]] -name = "tuple" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39a40ba241047e1174c927dc5f61c141a166b938d61a2ff61838441368cc7d0e" -dependencies = [ - "num-traits", - "serde", -] - [[package]] name = "typenum" version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" -[[package]] -name = "ucd-trie" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e79c4d996edb816c91e4308506774452e55e95c3c9de07b6729e17e15a5ef81" - [[package]] name = "uint" version = "0.9.5" @@ -1450,24 +1163,12 @@ version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" -[[package]] -name = "unicode-segmentation" -version = "1.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" - [[package]] name = "unicode-xid" version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" -[[package]] -name = "unwrap" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e33648dd74328e622c7be51f3b40a303c63f93e6fa5f08778b6203a4c25c20f" - [[package]] name = "version_check" version = "0.9.4" diff --git a/amm/logics/Cargo.toml b/amm/logics/Cargo.toml index bfdbd699..c22b6352 100644 --- a/amm/logics/Cargo.toml +++ b/amm/logics/Cargo.toml @@ -11,15 +11,12 @@ ink_metadata = { version = "=4.3.0", features = ["derive"], optional = true } scale = { package = "parity-scale-codec", version = "3", default-features = false, features = [ "derive", ] } -scale-info = { version = "2.3", default-features = false, features = [ +scale-info = { version = "2.9", default-features = false, features = [ "derive", ], optional = true } -openbrush = { version = "3.2.0", default-features = false, features = [ - "psp22", - "ownable", - "reentrancy_guard", -] } +psp22 = { git = "https://github.com/Cardinal-Cryptography/PSP22.git", default-features = false } + primitive-types = { version = "0.12.1", default-features = false, features = [ "codec", ] } @@ -43,9 +40,9 @@ std = [ "ink_metadata/std", "scale/std", "scale-info/std", - "openbrush/std", "primitive-types/std", "primitive-types/scale-info", + "psp22/std", "sp-arithmetic/std", "amm-helpers/std", ] diff --git a/amm/logics/helpers/helper.rs b/amm/logics/helpers/helper.rs index 1442f1f4..a1246a3d 100644 --- a/amm/logics/helpers/helper.rs +++ b/amm/logics/helpers/helper.rs @@ -1,16 +1,24 @@ -use crate::traits::{ - factory::{ - Factory, - FactoryRef, +use crate::{ + traits::{ + factory::Factory, + pair::Pair, }, - pair::PairRef, -}; -use amm_helpers::math::casted_mul; -use ink::prelude::vec::Vec; -use openbrush::traits::{ - AccountId, - AccountIdExt, Balance, + Env, +}; +use amm_helpers::{ + math::casted_mul, + types::WrappedU256, +}; +use ink::{ + contract_ref, + prelude::vec::Vec, + primitives::AccountId, +}; +use primitive_types::U256; +use sp_arithmetic::{ + FixedPointNumber, + FixedU128, }; /// Evaluate `$x:expr` and if not true return `Err($y:expr)`. @@ -37,8 +45,6 @@ pub fn sort_tokens( (token_b, token_a) }; - ensure!(!token_0.is_zero(), HelperError::ZeroAddress); - Ok((token_0, token_1)) } @@ -50,8 +56,8 @@ pub fn pair_for_on_chain( token_a: AccountId, token_b: AccountId, ) -> Option { - let factory_ref: FactoryRef = (*factory).into(); - factory_ref.get_pair(token_a, token_b) + let factory: contract_ref!(Factory, Env) = (*factory).into(); + factory.get_pair(token_a, token_b) } /// Returns balances of token reserves for particular `Factory` instance. @@ -63,7 +69,8 @@ pub fn get_reserves( let (token_0, _) = sort_tokens(token_a, token_b)?; let pair_contract = pair_for_on_chain(factory, token_a, token_b).ok_or(HelperError::PairNotFound)?; - let (reserve_0, reserve_1, _) = PairRef::get_reserves(&pair_contract); + let pair_contract: contract_ref!(Pair, Env) = pair_contract.into(); + let (reserve_0, reserve_1, _) = pair_contract.get_reserves(); if token_a == token_0 { Ok((reserve_0, reserve_1)) } else { @@ -210,6 +217,33 @@ pub fn get_amounts_in( Ok(amounts) } +#[inline] +pub fn update_cumulative( + price_0_cumulative_last: WrappedU256, + price_1_cumulative_last: WrappedU256, + time_elapsed: U256, + reserve_0: Balance, + reserve_1: Balance, +) -> (WrappedU256, WrappedU256) { + let price_cumulative_last_0: WrappedU256 = U256::from( + FixedU128::checked_from_rational(reserve_1, reserve_0) + .unwrap_or_default() + .into_inner(), + ) + .saturating_mul(time_elapsed) + .saturating_add(price_0_cumulative_last.into()) + .into(); + let price_cumulative_last_1: WrappedU256 = U256::from( + FixedU128::checked_from_rational(reserve_0, reserve_1) + .unwrap_or_default() + .into_inner(), + ) + .saturating_mul(time_elapsed) + .saturating_add(price_1_cumulative_last.into()) + .into(); + (price_cumulative_last_0, price_cumulative_last_1) +} + #[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] pub enum HelperError { diff --git a/amm/logics/helpers/mod.rs b/amm/logics/helpers/mod.rs index c9289723..210f20b9 100644 --- a/amm/logics/helpers/mod.rs +++ b/amm/logics/helpers/mod.rs @@ -13,6 +13,14 @@ pub const ZERO_ADDRESS: [u8; 32] = [ #[allow(unused)] const ZERO_ADDRESS_MSG: &str = "This is Aleph Zero DEX's zero address."; +/// Minimum liquidity threshold that is subtracted +/// from the minted liquidity and sent to the `ZERO_ADDRESS`. +/// Prevents price manipulation and saturation. +/// See UniswapV2 whitepaper for more details. +/// NOTE: This value is taken from UniswapV2 whitepaper and is correct +/// only for liquidity tokens with precision = 18. +pub const MINIMUM_LIQUIDITY: u128 = 1000; + #[cfg(test)] mod zero_address { use curve25519_dalek::ristretto::RistrettoPoint; @@ -22,8 +30,8 @@ mod zero_address { #[test] fn test_zero_address() { - let P = RistrettoPoint::hash_from_bytes::(ZERO_ADDRESS_MSG.as_bytes()); - let zero_address_address = P.compress(); + let p = RistrettoPoint::hash_from_bytes::(ZERO_ADDRESS_MSG.as_bytes()); + let zero_address_address = p.compress(); assert_eq!(super::ZERO_ADDRESS, zero_address_address.to_bytes()); } } diff --git a/amm/logics/helpers/transfer_helper.rs b/amm/logics/helpers/transfer_helper.rs index 9983dab8..f2b889aa 100644 --- a/amm/logics/helpers/transfer_helper.rs +++ b/amm/logics/helpers/transfer_helper.rs @@ -1,30 +1,38 @@ -use crate::traits::wnative::WnativeRef; -use ink::{ - env::DefaultEnvironment, - prelude::vec::Vec, +use crate::{ + traits::wnative::Wnative, + Balance, + Env, }; -use openbrush::{ - contracts::psp22::{ - PSP22Error, - PSP22Ref, - }, - traits::{ - AccountId, - Balance, - String, +use ink::{ + codegen::TraitCallBuilder, + contract_ref, + prelude::{ + string::String, + vec::Vec, }, + primitives::AccountId, +}; +use psp22::{ + PSP22Error, + PSP22, }; +#[inline] +pub fn balance_of(token: AccountId, who: AccountId) -> Balance { + let token: contract_ref!(PSP22, Env) = token.into(); + token.balance_of(who) +} + /// Transfers `value` amount of `token` to an account controlled by `to` address. #[inline] pub fn safe_transfer(token: AccountId, to: AccountId, value: Balance) -> Result<(), PSP22Error> { - PSP22Ref::transfer(&token, to, value, Vec::new()) + let mut token: contract_ref!(PSP22, Env) = token.into(); + token.transfer(to, value, Vec::new()) } /// Transfers `value` amount of native tokens to an `to` address. pub fn safe_transfer_native(to: AccountId, value: Balance) -> Result<(), TransferHelperError> { - ink::env::transfer::(to, value) - .map_err(|_| TransferHelperError::TransferFailed) + ink::env::transfer::(to, value).map_err(|_| TransferHelperError::TransferFailed) } /// Transfers `value` amount of `token` tokens `from` account `to` address. @@ -35,13 +43,18 @@ pub fn safe_transfer_from( to: AccountId, value: Balance, ) -> Result<(), PSP22Error> { - PSP22Ref::transfer_from(&token, from, to, value, Vec::new()) + let mut token: contract_ref!(PSP22, Env) = token.into(); + token.transfer_from(from, to, value, Vec::new()) } /// Wraps `value` amount of native tokens with a contract under `wnative` address. #[inline] pub fn wrap(wnative: &AccountId, value: Balance) -> Result<(), PSP22Error> { - match WnativeRef::deposit_builder(wnative) + let mut wnative: contract_ref!(Wnative, Env) = (*wnative).into(); + + match wnative + .call_mut() + .deposit() .transferred_value(value) .try_invoke() { @@ -58,7 +71,8 @@ pub fn wrap(wnative: &AccountId, value: Balance) -> Result<(), PSP22Error> { /// Unwraps `value` amount of wrapped native tokens. #[inline] pub fn unwrap(wnative: &AccountId, value: Balance) -> Result<(), PSP22Error> { - WnativeRef::withdraw(wnative, value) + let mut wnative: contract_ref!(Wnative, Env) = (*wnative).into(); + wnative.withdraw(value) } #[derive(Debug, PartialEq, Eq, scale::Encode, scale::Decode)] diff --git a/amm/logics/impls/mod.rs b/amm/logics/impls/mod.rs deleted file mode 100644 index 20d2f674..00000000 --- a/amm/logics/impls/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod pair; -pub mod router; diff --git a/amm/logics/impls/pair/mod.rs b/amm/logics/impls/pair/mod.rs deleted file mode 100644 index 142068b6..00000000 --- a/amm/logics/impls/pair/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod data; -pub mod pair; diff --git a/amm/logics/impls/pair/pair.rs b/amm/logics/impls/pair/pair.rs deleted file mode 100644 index 91f1e504..00000000 --- a/amm/logics/impls/pair/pair.rs +++ /dev/null @@ -1,230 +0,0 @@ -use crate::{ - ensure, - traits::factory::{ - Factory, - FactoryRef, - }, -}; -pub use crate::{ - impls::pair::*, - traits::pair::*, -}; -use amm_helpers::{ - math::casted_mul, - types::WrappedU256, -}; -use openbrush::{ - contracts::psp22::*, - traits::{ - AccountId, - AccountIdExt, - Balance, - Storage, - }, -}; -use primitive_types::U256; -use sp_arithmetic::{ - FixedPointNumber, - FixedU128, -}; - -/// Minimum liquidity threshold that is subtracted -/// from the minted liquidity and sent to the `ZERO_ADDRESS`. -/// Prevents price manipulation and saturation. -/// See UniswapV2 whitepaper for more details. -/// NOTE: This value is taken from UniswapV2 whitepaper and is correct -/// only for liquidity tokens with precision = 18. -pub const MINIMUM_LIQUIDITY: u128 = 1000; - -pub trait Internal { - /// If turned on, controlled via `fee_to` parameter, mints protocol fee - /// and transfer to `fee_to` address. Mints liquidity equivalent to 1/6th of the growth in sqrt(k). - /// SHOULD be called before any new tokens are minted or burnt so that no fees are lost. - fn _mint_fee(&mut self, reserve_0: Balance, reserve_1: Balance) -> Result; - - fn _update( - &mut self, - balance_0: Balance, - balance_1: Balance, - reserve_0: Balance, - reserve_1: Balance, - ) -> Result<(), PairError>; - - fn _emit_mint_event(&self, _sender: AccountId, _amount_0: Balance, _amount_1: Balance); - fn _emit_burn_event( - &self, - _sender: AccountId, - _amount_0: Balance, - _amount_1: Balance, - _to: AccountId, - ); - fn _emit_swap_event( - &self, - _sender: AccountId, - _amount_0_in: Balance, - _amount_1_in: Balance, - _amount_0_out: Balance, - _amount_1_out: Balance, - _to: AccountId, - ); - fn _emit_sync_event(&self, reserve_0: Balance, reserve_1: Balance); -} - -#[inline] -pub fn update_cumulative( - price_0_cumulative_last: WrappedU256, - price_1_cumulative_last: WrappedU256, - time_elapsed: U256, - reserve_0: Balance, - reserve_1: Balance, -) -> (WrappedU256, WrappedU256) { - let price_cumulative_last_0: WrappedU256 = U256::from( - FixedU128::checked_from_rational(reserve_1, reserve_0) - .unwrap_or_default() - .into_inner(), - ) - .saturating_mul(time_elapsed) - .saturating_add(price_0_cumulative_last.into()) - .into(); - let price_cumulative_last_1: WrappedU256 = U256::from( - FixedU128::checked_from_rational(reserve_0, reserve_1) - .unwrap_or_default() - .into_inner(), - ) - .saturating_mul(time_elapsed) - .saturating_add(price_1_cumulative_last.into()) - .into(); - (price_cumulative_last_0, price_cumulative_last_1) -} - -impl + Storage> Internal for T { - default fn _mint_fee( - &mut self, - reserve_0: Balance, - reserve_1: Balance, - ) -> Result { - let factory_ref: FactoryRef = self.data::().factory.into(); - let fee_to = factory_ref.fee_to(); - let fee_on = !fee_to.is_zero(); - let k_last: U256 = self.data::().k_last.into(); - if fee_on { - // Section 2.4 Protocol fee in the whitepaper. - if !k_last.is_zero() { - let root_k: Balance = casted_mul(reserve_0, reserve_1) - .integer_sqrt() - .try_into() - .map_err(|_| PairError::CastOverflow1)?; - let root_k_last = k_last - .integer_sqrt() - .try_into() - .map_err(|_| PairError::CastOverflow2)?; - if root_k > root_k_last { - let total_supply = self.data::().supply; - let numerator = total_supply - .checked_mul( - root_k - .checked_sub(root_k_last) - .ok_or(PairError::SubUnderFlow14)?, - ) - .ok_or(PairError::MulOverFlow13)?; - let denominator = root_k - .checked_mul(5) - .ok_or(PairError::MulOverFlow13)? - .checked_add(root_k_last) - .ok_or(PairError::AddOverflow1)?; - let liquidity = numerator - .checked_div(denominator) - .ok_or(PairError::DivByZero5)?; - if liquidity > 0 { - self._mint_to(fee_to, liquidity)?; - } - } - } - } else if !k_last.is_zero() { - self.data::().k_last = 0.into(); - } - Ok(fee_on) - } - - default fn _update( - &mut self, - balance_0: Balance, - balance_1: Balance, - reserve_0: Balance, - reserve_1: Balance, - ) -> Result<(), PairError> { - ensure!( - balance_0 <= u128::MAX && balance_1 <= u128::MAX, - PairError::Overflow - ); - let now = Self::env().block_timestamp(); - let last_timestamp = self.data::().block_timestamp_last; - if now != last_timestamp { - let (price_0_cumulative_last, price_1_cumulative_last) = update_cumulative( - self.data::().price_0_cumulative_last, - self.data::().price_1_cumulative_last, - now.saturating_sub(last_timestamp).into(), - reserve_0, - reserve_1, - ); - self.data::().price_0_cumulative_last = price_0_cumulative_last; - self.data::().price_1_cumulative_last = price_1_cumulative_last; - } - self.data::().reserve_0 = balance_0; - self.data::().reserve_1 = balance_1; - self.data::().block_timestamp_last = now; - - self._emit_sync_event(balance_0, balance_1); - Ok(()) - } - - default fn _emit_mint_event(&self, _sender: AccountId, _amount_0: Balance, _amount_1: Balance) { - } - default fn _emit_burn_event( - &self, - _sender: AccountId, - _amount_0: Balance, - _amount_1: Balance, - _to: AccountId, - ) { - } - default fn _emit_swap_event( - &self, - _sender: AccountId, - _amount_0_in: Balance, - _amount_1_in: Balance, - _amount_0_out: Balance, - _amount_1_out: Balance, - _to: AccountId, - ) { - } - default fn _emit_sync_event(&self, _reserve_0: Balance, _reserve_1: Balance) {} -} - -#[cfg(test)] -mod tests { - use primitive_types::U256; - use sp_arithmetic::FixedU128; - - use super::update_cumulative; - - #[ink::test] - fn update_cumulative_from_zero_time_elapsed() { - let (cumulative0, cumulative1) = update_cumulative(0.into(), 0.into(), 0.into(), 10, 10); - assert_eq!(cumulative0, 0.into()); - assert_eq!(cumulative1, 0.into()); - } - - #[ink::test] - fn update_cumulative_from_one_time_elapsed() { - let (cumulative0, cumulative1) = update_cumulative(0.into(), 0.into(), 1.into(), 10, 10); - assert_eq!( - FixedU128::from_inner(U256::from(cumulative0).as_u128()), - 1.into() - ); - assert_eq!( - FixedU128::from_inner(U256::from(cumulative1).as_u128()), - 1.into() - ); - } -} diff --git a/amm/logics/impls/router/data.rs b/amm/logics/impls/router/data.rs deleted file mode 100644 index 91692846..00000000 --- a/amm/logics/impls/router/data.rs +++ /dev/null @@ -1,20 +0,0 @@ -use crate::helpers::ZERO_ADDRESS; -use openbrush::traits::AccountId; - -pub const STORAGE_KEY: u32 = openbrush::storage_unique_key!(Data); - -#[derive(Debug)] -#[openbrush::upgradeable_storage(STORAGE_KEY)] -pub struct Data { - pub factory: AccountId, - pub wnative: AccountId, -} - -impl Default for Data { - fn default() -> Self { - Self { - factory: ZERO_ADDRESS.into(), - wnative: ZERO_ADDRESS.into(), - } - } -} diff --git a/amm/logics/impls/router/mod.rs b/amm/logics/impls/router/mod.rs deleted file mode 100644 index 1c790956..00000000 --- a/amm/logics/impls/router/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod data; -pub mod router; diff --git a/amm/logics/impls/router/router.rs b/amm/logics/impls/router/router.rs deleted file mode 100644 index 7eee9cf9..00000000 --- a/amm/logics/impls/router/router.rs +++ /dev/null @@ -1,152 +0,0 @@ -use crate::{ - ensure, - helpers::helper::{ - get_reserves, - pair_for_on_chain, - quote, - sort_tokens, - }, - traits::{ - factory::{ - Factory, - FactoryRef, - }, - pair::PairRef, - }, -}; -use ink::{ - env::CallFlags, - prelude::vec::Vec, -}; -use openbrush::{ - modifier_definition, - traits::{ - AccountId, - Balance, - Storage, - }, -}; - -pub use crate::{ - impls::router::*, - traits::router::*, -}; - -pub trait Internal { - fn _add_liquidity( - &self, - token_a: AccountId, - token_b: AccountId, - amount_a_desired: Balance, - amount_b_desired: Balance, - amount_a_min: Balance, - amount_b_min: Balance, - ) -> Result<(Balance, Balance), RouterError>; - - fn _swap( - &self, - amounts: &Vec, - path: &Vec, - to: AccountId, - ) -> Result<(), RouterError>; -} - -impl> Internal for T { - fn _add_liquidity( - &self, - token_a: AccountId, - token_b: AccountId, - amount_a_desired: Balance, - amount_b_desired: Balance, - amount_a_min: Balance, - amount_b_min: Balance, - ) -> Result<(Balance, Balance), RouterError> { - let factory = self.data().factory; - if pair_for_on_chain(&factory, token_a, token_b).is_none() { - let mut factory_ref: FactoryRef = factory.into(); - factory_ref.create_pair(token_a, token_b)?; - }; - - let (reserve_a, reserve_b) = get_reserves(&factory, token_a, token_b)?; - if reserve_a == 0 && reserve_b == 0 { - return Ok((amount_a_desired, amount_b_desired)) - } - - let amount_b_optimal = quote(amount_a_desired, reserve_a, reserve_b)?; - if amount_b_optimal <= amount_b_desired { - ensure!( - amount_b_optimal >= amount_b_min, - RouterError::InsufficientBAmount - ); - Ok((amount_a_desired, amount_b_optimal)) - } else { - let amount_a_optimal = quote(amount_b_desired, reserve_b, reserve_a)?; - // amount_a_optimal <= amount_a_desired holds as amount_b_optimal > amount_b_desired - ensure!( - amount_a_optimal >= amount_a_min, - RouterError::InsufficientAAmount - ); - Ok((amount_a_optimal, amount_b_desired)) - } - } - - fn _swap( - &self, - amounts: &Vec, - path: &Vec, - _to: AccountId, - ) -> Result<(), RouterError> { - let factory = self.data().factory; - for i in 0..path.len() - 1 { - let (input, output) = (path[i], path[i + 1]); - let (token_0, _) = sort_tokens(input, output)?; - let amount_out = amounts[i + 1]; - let (amount_0_out, amount_1_out) = if input == token_0 { - (0, amount_out) - } else { - (amount_out, 0) - }; - // If last pair in the path, transfer tokens to the `_to` recipient. - // Otherwise, transfer to the next Pair contract instance. - let to = if i < path.len() - 2 { - pair_for_on_chain(&factory, output, path[i + 2]).ok_or(RouterError::PairNotFound)? - } else { - _to - }; - match PairRef::swap_builder( - &pair_for_on_chain(&factory, input, output).ok_or(RouterError::PairNotFound)?, - amount_0_out, - amount_1_out, - to, - ) - .call_flags(CallFlags::default().set_allow_reentry(true)) - .try_invoke() - { - Ok(res) => { - match res { - Ok(v) => { - match v { - Ok(v) => Ok(v), - Err(err) => Err(RouterError::PairError(err)), - } - } - Err(err) => Err(RouterError::LangError(err)), - } - } - Err(_) => Err(RouterError::TransferError), - }?; - } - Ok(()) - } -} - -#[modifier_definition] -pub fn ensure_deadline(instance: &mut T, body: F, deadline: u64) -> Result -where - T: Storage, - F: FnOnce(&mut T) -> Result, - E: From, -{ - ensure!(deadline >= T::env().block_timestamp(), RouterError::Expired); - body(instance) -} diff --git a/amm/logics/lib.rs b/amm/logics/lib.rs index b1bf4e93..61433693 100644 --- a/amm/logics/lib.rs +++ b/amm/logics/lib.rs @@ -1,6 +1,8 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -#![feature(min_specialization)] pub mod helpers; -pub mod impls; pub mod traits; + +pub use ink::env::DefaultEnvironment as Env; +pub type Balance = ::Balance; +pub type Timestamp = ::Timestamp; diff --git a/amm/logics/traits/factory.rs b/amm/logics/traits/factory.rs index 43eb12d8..5256ef96 100644 --- a/amm/logics/traits/factory.rs +++ b/amm/logics/traits/factory.rs @@ -1,15 +1,9 @@ use crate::traits::pair::PairError; -use ink::{ - contract_ref, - env::DefaultEnvironment, - primitives::{ - AccountId, - Hash, - }, +use ink::primitives::{ + AccountId, + Hash, }; -pub type FactoryRef = contract_ref!(Factory, DefaultEnvironment); - /// Factory trait for tracking all pairs within the UniswapV2 DEX. /// Creates new, unique instances of `Pair` smart contract per token pairs. /// Contains the logic to turn on the protocol charge. diff --git a/amm/logics/traits/pair.rs b/amm/logics/traits/pair.rs index 83d5f9b1..41b47a9d 100644 --- a/amm/logics/traits/pair.rs +++ b/amm/logics/traits/pair.rs @@ -1,25 +1,15 @@ -use ink::LangError; -use openbrush::{ - contracts::{ - reentrancy_guard::*, - traits::{ - ownable::*, - psp22::PSP22Error, - }, - }, - traits::{ - AccountId, - Balance, - Timestamp, - }, +use crate::{ + Balance, + Timestamp, }; - use amm_helpers::types::WrappedU256; +use ink::{ + primitives::AccountId, + LangError, +}; +use psp22::PSP22Error; -#[openbrush::wrapper] -pub type PairRef = dyn Pair; - -#[openbrush::trait_definition] +#[ink::trait_definition] pub trait Pair { /// Returns amounts of tokens this pair holds at `Timestamp`. /// @@ -94,8 +84,6 @@ pub trait Pair { #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] pub enum PairError { PSP22Error(PSP22Error), - OwnableError(OwnableError), - ReentrancyGuardError(ReentrancyGuardError), LangError(LangError), TransferError, K, @@ -146,24 +134,12 @@ pub enum PairError { CastOverflow2, } -impl From for PairError { - fn from(error: OwnableError) -> Self { - PairError::OwnableError(error) - } -} - impl From for PairError { fn from(error: PSP22Error) -> Self { PairError::PSP22Error(error) } } -impl From for PairError { - fn from(error: ReentrancyGuardError) -> Self { - PairError::ReentrancyGuardError(error) - } -} - impl From for PairError { fn from(error: LangError) -> Self { PairError::LangError(error) diff --git a/amm/logics/traits/router.rs b/amm/logics/traits/router.rs index 33dccd68..81237210 100644 --- a/amm/logics/traits/router.rs +++ b/amm/logics/traits/router.rs @@ -1,27 +1,22 @@ -use super::{ - factory::FactoryError, - pair::PairError, -}; -use crate::helpers::{ - helper::HelperError, - transfer_helper::TransferHelperError, +use crate::{ + helpers::{ + helper::HelperError, + transfer_helper::TransferHelperError, + }, + traits::{ + factory::FactoryError, + pair::PairError, + }, + Balance, }; use ink::{ prelude::vec::Vec, + primitives::AccountId, LangError, }; -use openbrush::{ - contracts::psp22::PSP22Error, - traits::{ - AccountId, - Balance, - }, -}; +use psp22::PSP22Error; -#[openbrush::wrapper] -pub type RouterRef = dyn Router; - -#[openbrush::trait_definition] +#[ink::trait_definition] pub trait Router { /// Returns address of the `Factory` contract for this `Router` instance. #[ink(message)] @@ -36,7 +31,7 @@ pub trait Router { /// Will add at least `*_min` amount of tokens and up to `*_desired` /// while still maintaining the constant `k` product of the pair. /// - /// If succesful, liquidity tokens will be minted for `to` account. + /// If successful, liquidity tokens will be minted for `to` account. #[ink(message)] fn add_liquidity( &mut self, diff --git a/amm/logics/traits/wnative.rs b/amm/logics/traits/wnative.rs index d8c55325..c07682c5 100644 --- a/amm/logics/traits/wnative.rs +++ b/amm/logics/traits/wnative.rs @@ -1,12 +1,6 @@ -use openbrush::{ - contracts::psp22::PSP22Error, - traits::Balance, -}; +use psp22::PSP22Error; -#[openbrush::wrapper] -pub type WnativeRef = dyn Wnative; - -#[openbrush::trait_definition] +#[ink::trait_definition] pub trait Wnative { /// Deposit NATIVE to wrap it #[ink(message, payable)] @@ -14,5 +8,5 @@ pub trait Wnative { /// Unwrap NATIVE #[ink(message)] - fn withdraw(&mut self, amount: Balance) -> Result<(), PSP22Error>; + fn withdraw(&mut self, value: u128) -> Result<(), PSP22Error>; } diff --git a/e2e-tests/Cargo.lock b/e2e-tests/Cargo.lock index c46bcfd4..a711d060 100644 --- a/e2e-tests/Cargo.lock +++ b/e2e-tests/Cargo.lock @@ -72,13 +72,25 @@ dependencies = [ name = "amm" version = "0.2.0" dependencies = [ + "amm-helpers", "ink", "ink_metadata", - "openbrush", "parity-scale-codec", - "primitive-types 0.11.1", + "primitive-types", + "psp22", + "scale-info", + "sp-arithmetic 18.0.0", +] + +[[package]] +name = "amm-helpers" +version = "0.2.0" +dependencies = [ + "ink", + "ink_metadata", + "parity-scale-codec", + "primitive-types", "scale-info", - "sp-arithmetic 6.0.0 (git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.37)", ] [[package]] @@ -181,7 +193,7 @@ checksum = "b9ccdd8f2a161be9bd5c023df56f1b2a0bd1d83872ae53b71a84a12c9bf6e842" dependencies = [ "proc-macro2", "quote", - "syn 2.0.22", + "syn 2.0.32", ] [[package]] @@ -250,17 +262,6 @@ dependencies = [ "wyz", ] -[[package]] -name = "blake2" -version = "0.9.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0a4e37d16930f5459780f5621038b6382b9bb37c19016f39fb6b5808d831f174" -dependencies = [ - "crypto-mac 0.8.0", - "digest 0.9.0", - "opaque-debug 0.3.0", -] - [[package]] name = "blake2" version = "0.10.6" @@ -348,38 +349,6 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" -[[package]] -name = "camino" -version = "1.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c530edf18f37068ac2d977409ed5cd50d53d73bc653c7647b48eb78976ac9ae2" -dependencies = [ - "serde", -] - -[[package]] -name = "cargo-platform" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cbdb825da8a5df079a43676dbe042702f1707b1109f713a01420fbb4cc71fa27" -dependencies = [ - "serde", -] - -[[package]] -name = "cargo_metadata" -version = "0.13.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "081e3f0755c1f380c2d010481b6fa2e02973586d5f2b24eebb7a2a1d98b143d8" -dependencies = [ - "camino", - "cargo-platform", - "semver 0.11.0", - "semver-parser", - "serde", - "serde_json", -] - [[package]] name = "cc" version = "1.0.79" @@ -404,26 +373,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "const_format" -version = "0.2.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c990efc7a285731f9a4378d81aff2f0e85a2c8781a05ef0f8baa8dac54d0ff48" -dependencies = [ - "const_format_proc_macros", -] - -[[package]] -name = "const_format_proc_macros" -version = "0.2.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e026b6ce194a874cb9cf32cd5772d1ef9767cc8fcb5765948d74f37a9d8b2bf6" -dependencies = [ - "proc-macro2", - "quote", - "unicode-xid", -] - [[package]] name = "contract-metadata" version = "2.2.1" @@ -432,7 +381,7 @@ checksum = "e6aa9a99669a8f4eba55782175659dbb20459698c5a65a9f3efe7b9330dd667b" dependencies = [ "anyhow", "impl-serde", - "semver 1.0.17", + "semver", "serde", "serde_json", "url", @@ -446,7 +395,7 @@ checksum = "e4e3806efabf4c29f2aeff708e076cac60609ee6c5897299f163355b1cec242c" dependencies = [ "anyhow", "base58", - "blake2 0.10.6", + "blake2", "contract-metadata", "escape8259", "hex", @@ -457,7 +406,7 @@ dependencies = [ "nom", "nom-supreme", "parity-scale-codec", - "primitive-types 0.12.1", + "primitive-types", "scale-info", "serde", "serde_json", @@ -775,18 +724,6 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" -[[package]] -name = "fixed-hash" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cfcf0ed7fe52a17a03854ec54a9f76d6d84508d1c0e66bc1793301c73fc8493c" -dependencies = [ - "byteorder", - "rand 0.8.5", - "rustc-hex", - "static_assertions", -] - [[package]] name = "fixed-hash" version = "0.8.0" @@ -826,16 +763,6 @@ dependencies = [ "serde", ] -[[package]] -name = "fs2" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9564fc758e15025b46aa6643b1b77d047d1a56a1aea6e01002ac0c7026876213" -dependencies = [ - "libc", - "winapi", -] - [[package]] name = "funty" version = "2.0.0" @@ -899,7 +826,7 @@ checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" dependencies = [ "proc-macro2", "quote", - "syn 2.0.22", + "syn 2.0.32", ] [[package]] @@ -1038,15 +965,6 @@ version = "0.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a" -[[package]] -name = "heck" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" -dependencies = [ - "unicode-segmentation", -] - [[package]] name = "heck" version = "0.4.1" @@ -1325,11 +1243,11 @@ version = "4.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22d79057b2565df31a10af6510a44b161093f110c5f9c22ad02c20af9cea4c29" dependencies = [ - "blake2 0.10.6", + "blake2", "derive_more", "either", "env_logger", - "heck 0.4.1", + "heck", "impl-serde", "ink_ir", "ink_primitives", @@ -1340,7 +1258,7 @@ dependencies = [ "quote", "serde", "serde_json", - "syn 2.0.22", + "syn 2.0.32", ] [[package]] @@ -1349,7 +1267,7 @@ version = "4.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "722ec3a5eb557124b001c60ff8f961079f6d566af643edea579f152b15822fe5" dependencies = [ - "blake2 0.10.6", + "blake2", "derive_more", "ink_primitives", "parity-scale-codec", @@ -1365,7 +1283,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "584e73bc0982f6f1a067bb63ebc75262f6dc54ed2a17060efa73eaba84dc9308" dependencies = [ "arrayref", - "blake2 0.10.6", + "blake2", "cfg-if", "derive_more", "ink_allocator", @@ -1392,12 +1310,12 @@ version = "4.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b529c941518e8f450395fab9fe8ebba0a7acbb18778fc7e0a87f6248286ec72" dependencies = [ - "blake2 0.10.6", + "blake2", "either", "itertools", "proc-macro2", "quote", - "syn 2.0.22", + "syn 2.0.32", ] [[package]] @@ -1412,7 +1330,7 @@ dependencies = [ "parity-scale-codec", "proc-macro2", "quote", - "syn 2.0.22", + "syn 2.0.32", "synstructure 0.13.0", ] @@ -1903,42 +1821,6 @@ dependencies = [ "libc", ] -[[package]] -name = "obce" -version = "0.1.0" -source = "git+https://github.com/727-Ventures/obce?branch=polkadot-v0.9.37#d452f6eda1bc1ecb36e7e332d61529ad440d5a89" -dependencies = [ - "ink", - "ink_engine", - "obce-macro", - "parity-scale-codec", - "scale-info", -] - -[[package]] -name = "obce-codegen" -version = "0.1.0" -source = "git+https://github.com/727-Ventures/obce?branch=polkadot-v0.9.37#d452f6eda1bc1ecb36e7e332d61529ad440d5a89" -dependencies = [ - "blake2 0.10.6", - "itertools", - "proc-macro2", - "quote", - "syn 1.0.109", - "tuple", -] - -[[package]] -name = "obce-macro" -version = "0.1.0" -source = "git+https://github.com/727-Ventures/obce?branch=polkadot-v0.9.37#d452f6eda1bc1ecb36e7e332d61529ad440d5a89" -dependencies = [ - "obce-codegen", - "proc-macro2", - "syn 1.0.109", - "synstructure 0.12.6", -] - [[package]] name = "object" version = "0.30.4" @@ -1966,91 +1848,12 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" -[[package]] -name = "openbrush" -version = "3.1.1" -source = "git+https://github.com/727-Ventures/openbrush-contracts?tag=3.1.1#13e82f3f02e8ea65a95ea02c381106ea738e1fb8" -dependencies = [ - "ink", - "openbrush_contracts", - "openbrush_lang", - "parity-scale-codec", - "scale-info", -] - -[[package]] -name = "openbrush_contracts" -version = "3.1.1" -source = "git+https://github.com/727-Ventures/openbrush-contracts?tag=3.1.1#13e82f3f02e8ea65a95ea02c381106ea738e1fb8" -dependencies = [ - "ink", - "openbrush_lang", - "pallet-assets-chain-extension", - "parity-scale-codec", - "scale-info", -] - -[[package]] -name = "openbrush_lang" -version = "3.1.1" -source = "git+https://github.com/727-Ventures/openbrush-contracts?tag=3.1.1#13e82f3f02e8ea65a95ea02c381106ea738e1fb8" -dependencies = [ - "const_format", - "ink", - "openbrush_lang_macro", - "parity-scale-codec", - "scale-info", - "xxhash-rust", -] - -[[package]] -name = "openbrush_lang_codegen" -version = "3.1.1" -source = "git+https://github.com/727-Ventures/openbrush-contracts?tag=3.1.1#13e82f3f02e8ea65a95ea02c381106ea738e1fb8" -dependencies = [ - "blake2 0.9.2", - "cargo_metadata", - "fs2", - "heck 0.3.3", - "ink_ir", - "ink_primitives", - "proc-macro2", - "quote", - "serde", - "serde_json", - "syn 1.0.109", - "synstructure 0.12.6", - "unwrap", -] - -[[package]] -name = "openbrush_lang_macro" -version = "3.1.1" -source = "git+https://github.com/727-Ventures/openbrush-contracts?tag=3.1.1#13e82f3f02e8ea65a95ea02c381106ea738e1fb8" -dependencies = [ - "openbrush_lang_codegen", - "proc-macro2", - "syn 1.0.109", - "synstructure 0.12.6", -] - [[package]] name = "openssl-probe" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" -[[package]] -name = "pallet-assets-chain-extension" -version = "0.1.1" -source = "git+https://github.com/727-ventures/pallet-assets-chain-extension?branch=polkadot-v0.9.37#f8ea374186df2a3fc139c8d585719e58d83df582" -dependencies = [ - "ink", - "obce", - "parity-scale-codec", - "scale-info", -] - [[package]] name = "pallet-contracts-primitives" version = "7.0.0" @@ -2060,7 +1863,7 @@ dependencies = [ "bitflags", "parity-scale-codec", "sp-runtime", - "sp-std 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sp-std 5.0.0", "sp-weights", ] @@ -2102,7 +1905,7 @@ dependencies = [ "impl-trait-for-tuples", "parity-util-mem-derive", "parking_lot", - "primitive-types 0.12.1", + "primitive-types", "winapi", ] @@ -2176,16 +1979,6 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94" -[[package]] -name = "pest" -version = "2.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f73935e4d55e2abf7f130186537b19e7a4abc886a0252380b59248af473a3fc9" -dependencies = [ - "thiserror", - "ucd-trie", -] - [[package]] name = "pin-project" version = "1.1.0" @@ -2203,7 +1996,7 @@ checksum = "39407670928234ebc5e6e580247dd567ad73a3578460c5990f9503df207e8f07" dependencies = [ "proc-macro2", "quote", - "syn 2.0.22", + "syn 2.0.32", ] [[package]] @@ -2224,25 +2017,13 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" -[[package]] -name = "primitive-types" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e28720988bff275df1f51b171e1b2a18c30d194c4d2b61defdacecd625a5d94a" -dependencies = [ - "fixed-hash 0.7.0", - "impl-codec", - "scale-info", - "uint", -] - [[package]] name = "primitive-types" version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f3486ccba82358b11a77516035647c34ba167dfa53312630de83b12bd4f3d66" dependencies = [ - "fixed-hash 0.8.0", + "fixed-hash", "impl-codec", "impl-serde", "scale-info", @@ -2292,6 +2073,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "psp22" +version = "0.1.0" +source = "git+https://github.com/Cardinal-Cryptography/PSP22.git#c6744743e645f8120ceef84e2dbadf8f194fdb4f" +dependencies = [ + "ink", + "parity-scale-codec", + "scale-info", +] + [[package]] name = "quote" version = "1.0.28" @@ -2414,7 +2205,7 @@ checksum = "8d2275aab483050ab2a7364c1a46604865ee7d6906684e08db0f090acf74f9e7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.22", + "syn 2.0.32", ] [[package]] @@ -2494,7 +2285,7 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" dependencies = [ - "semver 1.0.17", + "semver", ] [[package]] @@ -2644,9 +2435,9 @@ dependencies = [ [[package]] name = "scale-info" -version = "2.8.0" +version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad560913365790f17cbf12479491169f01b9d46d29cfc7422bf8c64bdc61b731" +checksum = "35c0a159d0c45c12b20c5a844feb1fe4bea86e28f17b92a5f0c42193634d3782" dependencies = [ "bitvec", "cfg-if", @@ -2658,9 +2449,9 @@ dependencies = [ [[package]] name = "scale-info-derive" -version = "2.8.0" +version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19df9bd9ace6cc2fe19387c96ce677e823e07d017ceed253e7bb3d1d1bd9c73b" +checksum = "912e55f6d20e0e80d63733872b40e1227c0bce1e1ab81ba67d696339bfd7fd29" dependencies = [ "proc-macro-crate", "proc-macro2", @@ -2796,16 +2587,6 @@ dependencies = [ "libc", ] -[[package]] -name = "semver" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f301af10236f6df4160f7c3f04eec6dbc70ace82d23326abad5edee88801c6b6" -dependencies = [ - "semver-parser", - "serde", -] - [[package]] name = "semver" version = "1.0.17" @@ -2815,33 +2596,24 @@ dependencies = [ "serde", ] -[[package]] -name = "semver-parser" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00b0bef5b7f9e0df16536d3961cfb6e84331c065b4066afb39768d0e319411f7" -dependencies = [ - "pest", -] - [[package]] name = "serde" -version = "1.0.164" +version = "1.0.188" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e8c8cf938e98f769bc164923b06dce91cea1751522f46f8466461af04c9027d" +checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.164" +version = "1.0.188" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9735b638ccc51c28bf6914d90a2e9725b377144fc612c49a611fddd1b631d68" +checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" dependencies = [ "proc-macro2", "quote", - "syn 2.0.22", + "syn 2.0.32", ] [[package]] @@ -2974,7 +2746,7 @@ dependencies = [ "serde", "sp-core", "sp-io", - "sp-std 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sp-std 5.0.0", ] [[package]] @@ -2989,21 +2761,22 @@ dependencies = [ "scale-info", "serde", "sp-debug-derive", - "sp-std 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sp-std 5.0.0", "static_assertions", ] [[package]] name = "sp-arithmetic" -version = "6.0.0" -source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.37#6fa7fe1326ecaab9921c2c3888530ad679cfbb87" +version = "18.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d3d3ff6d6d717d7563659e9e47e958d33ebd2d0b3d8b1a9961cf9832944375e" dependencies = [ "integer-sqrt", "num-traits", "parity-scale-codec", "scale-info", "serde", - "sp-std 5.0.0 (git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.37)", + "sp-std 10.0.0", "static_assertions", ] @@ -3016,7 +2789,7 @@ dependencies = [ "array-bytes", "base58", "bitflags", - "blake2 0.10.6", + "blake2", "byteorder", "dyn-clonable", "ed25519-zebra", @@ -3031,7 +2804,7 @@ dependencies = [ "num-traits", "parity-scale-codec", "parking_lot", - "primitive-types 0.12.1", + "primitive-types", "rand 0.7.3", "regex", "scale-info", @@ -3043,7 +2816,7 @@ dependencies = [ "sp-debug-derive", "sp-externalities", "sp-runtime-interface", - "sp-std 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sp-std 5.0.0", "sp-storage", "ss58-registry", "substrate-bip39", @@ -3059,12 +2832,12 @@ version = "5.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49b9d1daa6aebfc144729b630885e91df92ff00560490ec065a56cb538e8895a" dependencies = [ - "blake2 0.10.6", + "blake2", "byteorder", "digest 0.10.7", "sha2 0.10.7", "sha3", - "sp-std 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sp-std 5.0.0", "twox-hash", ] @@ -3087,7 +2860,7 @@ checksum = "ef739442230f49d88ece41259e5d886d6b8bc0f4197ef7f1585c39c762ce7ef2" dependencies = [ "environmental", "parity-scale-codec", - "sp-std 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sp-std 5.0.0", "sp-storage", ] @@ -3110,7 +2883,7 @@ dependencies = [ "sp-keystore", "sp-runtime-interface", "sp-state-machine", - "sp-std 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sp-std 5.0.0", "sp-tracing", "sp-trie", "sp-wasm-interface", @@ -3163,10 +2936,10 @@ dependencies = [ "scale-info", "serde", "sp-application-crypto", - "sp-arithmetic 6.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sp-arithmetic 6.0.0", "sp-core", "sp-io", - "sp-std 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sp-std 5.0.0", "sp-weights", ] @@ -3179,10 +2952,10 @@ dependencies = [ "bytes", "impl-trait-for-tuples", "parity-scale-codec", - "primitive-types 0.12.1", + "primitive-types", "sp-externalities", "sp-runtime-interface-proc-macro", - "sp-std 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sp-std 5.0.0", "sp-storage", "sp-tracing", "sp-wasm-interface", @@ -3218,7 +2991,7 @@ dependencies = [ "sp-core", "sp-externalities", "sp-panic-handler", - "sp-std 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sp-std 5.0.0", "sp-trie", "thiserror", "tracing", @@ -3233,8 +3006,9 @@ checksum = "cf3fd4c1d304be101e6ebbafd3d4be9a37b320c970ef4e8df188b16873981c93" [[package]] name = "sp-std" -version = "5.0.0" -source = "git+https://github.com/paritytech/substrate?branch=polkadot-v0.9.37#6fa7fe1326ecaab9921c2c3888530ad679cfbb87" +version = "10.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ed09ef1760e8be9b64b7f739f1cf9a94528130be475d8e4f2d1be1e690c9f9c" [[package]] name = "sp-storage" @@ -3247,7 +3021,7 @@ dependencies = [ "ref-cast", "serde", "sp-debug-derive", - "sp-std 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sp-std 5.0.0", ] [[package]] @@ -3257,7 +3031,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e761df87dc940d87720939de8f976d1fc0657e523886ae0d7bf3f7e2e2f0abb6" dependencies = [ "parity-scale-codec", - "sp-std 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sp-std 5.0.0", "tracing", "tracing-core", "tracing-subscriber", @@ -3280,7 +3054,7 @@ dependencies = [ "parking_lot", "scale-info", "sp-core", - "sp-std 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sp-std 5.0.0", "thiserror", "tracing", "trie-db", @@ -3296,7 +3070,7 @@ dependencies = [ "impl-trait-for-tuples", "log", "parity-scale-codec", - "sp-std 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sp-std 5.0.0", "wasmi", ] @@ -3311,10 +3085,10 @@ dependencies = [ "scale-info", "serde", "smallvec", - "sp-arithmetic 6.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sp-arithmetic 6.0.0", "sp-core", "sp-debug-derive", - "sp-std 5.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sp-std 5.0.0", ] [[package]] @@ -3405,7 +3179,7 @@ checksum = "7722c31febf55eb300c73d977da5d65cfd6fb443419b1185b9abcdd9925fd7be" dependencies = [ "darling", "frame-metadata", - "heck 0.4.1", + "heck", "hex", "jsonrpsee", "parity-scale-codec", @@ -3455,9 +3229,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.22" +version = "2.0.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2efbeae7acf4eabd6bcdcbd11c92f45231ddda7539edc7806bd1a04a03b24616" +checksum = "239814284fd6f1a4ffe4ca893952cdd93c224b6a1571c9a9eadd670295c0c9e2" dependencies = [ "proc-macro2", "quote", @@ -3484,7 +3258,7 @@ checksum = "285ba80e733fac80aa4270fbcdf83772a79b80aa35c97075320abfee4a915b06" dependencies = [ "proc-macro2", "quote", - "syn 2.0.22", + "syn 2.0.32", "unicode-xid", ] @@ -3520,7 +3294,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.22", + "syn 2.0.32", ] [[package]] @@ -3593,7 +3367,7 @@ checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.22", + "syn 2.0.32", ] [[package]] @@ -3665,7 +3439,7 @@ checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab" dependencies = [ "proc-macro2", "quote", - "syn 2.0.22", + "syn 2.0.32", ] [[package]] @@ -3749,16 +3523,6 @@ version = "0.2.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3528ecfd12c466c6f163363caf2d02a71161dd5e1cc6ae7b34207ea2d42d81ed" -[[package]] -name = "tuple" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39a40ba241047e1174c927dc5f61c141a166b938d61a2ff61838441368cc7d0e" -dependencies = [ - "num-traits", - "serde", -] - [[package]] name = "twox-hash" version = "1.6.3" @@ -3777,12 +3541,6 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "497961ef93d974e23eb6f433eb5fe1b7930b659f06d12dec6fc44a8f554c0bba" -[[package]] -name = "ucd-trie" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e79c4d996edb816c91e4308506774452e55e95c3c9de07b6729e17e15a5ef81" - [[package]] name = "uint" version = "0.9.5" @@ -3816,12 +3574,6 @@ dependencies = [ "tinyvec", ] -[[package]] -name = "unicode-segmentation" -version = "1.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" - [[package]] name = "unicode-xid" version = "0.2.4" @@ -3834,12 +3586,6 @@ version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" -[[package]] -name = "unwrap" -version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e33648dd74328e622c7be51f3b40a303c63f93e6fa5f08778b6203a4c25c20f" - [[package]] name = "url" version = "2.4.0" @@ -3906,7 +3652,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.22", + "syn 2.0.32", "wasm-bindgen-shared", ] @@ -3928,7 +3674,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.22", + "syn 2.0.32", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -4217,5 +3963,5 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.22", + "syn 2.0.32", ] diff --git a/e2e-tests/src/test/router.rs b/e2e-tests/src/test/router.rs index c4153a19..fafbab37 100644 --- a/e2e-tests/src/test/router.rs +++ b/e2e-tests/src/test/router.rs @@ -15,7 +15,7 @@ use aleph_client::{ Balance, SignedConnection, }; -use amm::impls::pair::pair::MINIMUM_LIQUIDITY; +use amm::helpers::MINIMUM_LIQUIDITY; use ink_wrapper_types::{ util::ToAccountId, Connection,