From 3d49660370ff505b2ac9e7dfedf7e43d4928a47e Mon Sep 17 00:00:00 2001 From: Iddriss Raaj Date: Sun, 26 Nov 2023 14:08:08 +0000 Subject: [PATCH] Fix typo in upgradable module and make pairs upgradable --- Scarb.toml | 7 ++-- src/dex/v1/factory/factory.cairo | 14 +++----- src/dex/v1/pair/Pair.cairo | 35 ++++++++++++++++--- src/dex/v1/pair/interface.cairo | 10 ++++-- src/dex/v1/pair/pairFeesVault.cairo | 5 --- src/dex/v1/router/router.cairo | 8 ++--- src/tests/pair/test_stable_pair.cairo | 2 +- src/tests/pair/test_volatile_pair.cairo | 2 +- src/tests/router/test_router.cairo | 2 +- src/utils.cairo | 2 +- .../{upgradeable.cairo => upgradable.cairo} | 4 +-- 11 files changed, 56 insertions(+), 35 deletions(-) rename src/utils/{upgradeable.cairo => upgradable.cairo} (94%) diff --git a/Scarb.toml b/Scarb.toml index 2ced998..504ff18 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -24,7 +24,6 @@ keystore = "./accounts/testnet_deployer_key.json" url = "https://starknet-goerli.infura.io/v3/${INFURA_PROJECT_ID}" [tool.sncast.mainnet] -name="mainnet" -account = "./accounts/mainnet_deployer.json" -keystore = "./accounts/mainnet_deployer_key.json" -url = "https://starknet-mainnet.infura.io/v3/${INFURA_PROJECT_ID}" \ No newline at end of file +account = "mainnet" +accounts-file = "./accounts/mainnet_deployer.json" +url = "https://starknet-mainnet.infura.io/v3/${INFURA_PROJECT_ID}" diff --git a/src/dex/v1/factory/factory.cairo b/src/dex/v1/factory/factory.cairo index e35a1e0..10177b9 100644 --- a/src/dex/v1/factory/factory.cairo +++ b/src/dex/v1/factory/factory.cairo @@ -41,7 +41,7 @@ mod StarkDFactory { use starknet::syscalls::deploy_syscall; use starknet::replace_class_syscall; use starkDefi::utils::{ContractAddressPartialOrd}; - use starkDefi::utils::upgradeable::{Upgradeable, IUpgradeable}; + use starkDefi::utils::upgradable::{Upgradable, IUpgradable}; #[event] @@ -317,11 +317,7 @@ mod StarkDFactory { assert(fee <= MAX_FEE, 'fee too high'); let mut pair_info = self.valid_pairs.read(pair); assert(pair_info.is_valid, 'invalid pair'); - if pair_info.is_stable { - pair_info.custom_fee = fee; - } else { - pair_info.custom_fee = fee; - } + pair_info.custom_fee = fee; self.valid_pairs.write(pair, pair_info); self.emit(SetPairFee { pair, stable: pair_info.is_stable, fee }); } @@ -401,11 +397,11 @@ mod StarkDFactory { /// @notice upgradable at moment, a future implementation will drop this #[external(v0)] - impl UpgradableImpl of IUpgradeable { + impl UpgradableImpl of IUpgradable { fn upgrade(ref self: ContractState, new_class_hash: ClassHash) { Modifiers::assert_only_handler(@self); - let mut state = Upgradeable::unsafe_new_contract_state(); - Upgradeable::InternalImpl::_upgrade(ref state, new_class_hash); + let mut state = Upgradable::unsafe_new_contract_state(); + Upgradable::InternalImpl::_upgrade(ref state, new_class_hash); } } diff --git a/src/dex/v1/pair/Pair.cairo b/src/dex/v1/pair/Pair.cairo index be46f15..4cfba60 100644 --- a/src/dex/v1/pair/Pair.cairo +++ b/src/dex/v1/pair/Pair.cairo @@ -60,6 +60,7 @@ mod StarkDPair { use starkDefi::utils::call_contract_with_selector_fallback; use starkDefi::utils::selectors; use starkDefi::utils::callFallback::UnwrapAndCast; + use starkDefi::utils::upgradable::{Upgradable, IUpgradable}; use integer::u128_try_from_felt252; @@ -245,7 +246,8 @@ mod StarkDPair { decimal1: config.decimal1, reserve0: data.reserve0, reserve1: data.reserve1, - is_stable: config.stable + is_stable: config.stable, + fee_tier: config.fee_tier, } } @@ -324,6 +326,7 @@ mod StarkDPair { fn mint(ref self: ContractState, to: ContractAddress) -> u256 { Modifiers::_lock(ref self); Modifiers::_assert_not_paused(@self); + InternalFunctions::_update_user_fee(ref self, to); let config = self.config.read(); let (reserve0, reserve1, _) = self.get_reserves(); @@ -607,17 +610,32 @@ mod StarkDPair { } #[external(v0)] - fn fee_state(self: @ContractState, user: ContractAddress) -> (u256, GlobalFeesAccum) { + fn fee_state( + self: @ContractState, user: ContractAddress + ) -> (u256, RelativeFeesAccum, GlobalFeesAccum) { let global_fees = self.global_fees.read(); + let user_fees = self.users_fee.read(user); let balance = self.balance_of(user); - (balance, global_fees) + (balance, user_fees, global_fees) } #[external(v0)] - fn feeState(self: @ContractState, user: ContractAddress) -> (u256, GlobalFeesAccum) { + fn feeState( + self: @ContractState, user: ContractAddress + ) -> (u256, RelativeFeesAccum, GlobalFeesAccum) { fee_state(self, user) } + /// @notice upgradable at moment, a future implementation will drop this + #[external(v0)] + impl UpgradableImpl of IUpgradable { + fn upgrade(ref self: ContractState, new_class_hash: ClassHash) { + Modifiers::assert_only_handler(@self); + let mut state = Upgradable::unsafe_new_contract_state(); + Upgradable::InternalImpl::_upgrade(ref state, new_class_hash); + } + } + #[generate_trait] impl InternalFunctions of InternalFunctionsTrait { @@ -1002,6 +1020,15 @@ mod StarkDPair { let factoryDipatcher = IStarkDFactoryABIDispatcher { contract_address: config.factory }; factoryDipatcher.assert_not_paused(); } + + /// @dev reverts if not handler + fn assert_only_handler(self: @ContractState) { + let caller = get_caller_address(); + let factory = IStarkDFactoryABIDispatcher { + contract_address: self.config.read().factory + }; + assert(caller == factory.fee_handler(), 'not allowed'); + } } } diff --git a/src/dex/v1/pair/interface.cairo b/src/dex/v1/pair/interface.cairo index 1172f2c..2daabd5 100644 --- a/src/dex/v1/pair/interface.cairo +++ b/src/dex/v1/pair/interface.cairo @@ -9,6 +9,7 @@ struct Snapshot { reserve0: u256, reserve1: u256, is_stable: bool, + fee_tier: u8, } #[derive(Copy, Drop, Serde, starknet::Store)] @@ -140,7 +141,9 @@ trait IStarkDPairABI { fn sync(ref self: TContractState); fn claim_fees(ref self: TContractState); fn get_amount_out(ref self: TContractState, tokenIn: ContractAddress, amountIn: u256) -> u256; - fn fee_state(self: @TContractState, user: ContractAddress) -> (u256, GlobalFeesAccum); + fn fee_state( + self: @TContractState, user: ContractAddress + ) -> (u256, RelativeFeesAccum, GlobalFeesAccum); } #[starknet::interface] @@ -187,7 +190,9 @@ trait IStarkDPairCamelABI { fn sync(ref self: TContractState); fn claimFees(ref self: TContractState); fn getAmountOut(ref self: TContractState, tokenIn: ContractAddress, amountIn: u256) -> u256; - fn feeState(self: @TContractState, user: ContractAddress) -> (u256, GlobalFeesAccum); + fn feeState( + self: @TContractState, user: ContractAddress + ) -> (u256, RelativeFeesAccum, GlobalFeesAccum); } @@ -207,5 +212,4 @@ trait IFeesVault { fn claim_lp_fees(ref self: TContractState, user: ContractAddress, amount0: u256, amount1: u256); fn update_protocol_fees(ref self: TContractState, amount0: u256, amount1: u256); fn claim_protocol_fees(ref self: TContractState); - fn get_protocol_fees(self: @TContractState) -> (u256, u256); } diff --git a/src/dex/v1/pair/pairFeesVault.cairo b/src/dex/v1/pair/pairFeesVault.cairo index 2b8d481..9cb88cf 100644 --- a/src/dex/v1/pair/pairFeesVault.cairo +++ b/src/dex/v1/pair/pairFeesVault.cairo @@ -64,11 +64,6 @@ mod FeesVault { FeesVaultImpl::claim_protocol_fees(ref self); } - fn get_protocol_fees(self: @ContractState) -> (u256, u256) { - let protocol = self.protocol.read(); - (protocol.token0, protocol.token1) - } - fn update_protocol_fees(ref self: ContractState, amount0: u256, amount1: u256) { assert(get_caller_address() == self.pair.read(), 'not authorized'); diff --git a/src/dex/v1/router/router.cairo b/src/dex/v1/router/router.cairo index 4e0b678..75351ba 100644 --- a/src/dex/v1/router/router.cairo +++ b/src/dex/v1/router/router.cairo @@ -23,7 +23,7 @@ mod StarkDRouter { use starknet::{ ContractAddress, ClassHash, get_caller_address, get_block_timestamp, contract_address_const }; - use starkDefi::utils::upgradeable::{Upgradeable, IUpgradeable}; + use starkDefi::utils::upgradable::{Upgradable, IUpgradable}; #[storage] @@ -255,11 +255,11 @@ mod StarkDRouter { } #[external(v0)] - impl UpgradableImpl of IUpgradeable { + impl UpgradableImpl of IUpgradable { fn upgrade(ref self: ContractState, new_class_hash: ClassHash) { Modifiers::assert_only_handler(@self); - let mut state = Upgradeable::unsafe_new_contract_state(); - Upgradeable::InternalImpl::_upgrade(ref state, new_class_hash); + let mut state = Upgradable::unsafe_new_contract_state(); + Upgradable::InternalImpl::_upgrade(ref state, new_class_hash); } } diff --git a/src/tests/pair/test_stable_pair.cairo b/src/tests/pair/test_stable_pair.cairo index 571631c..bf36173 100644 --- a/src/tests/pair/test_stable_pair.cairo +++ b/src/tests/pair/test_stable_pair.cairo @@ -158,7 +158,7 @@ fn test_sPair_mint_more_lp() { } #[test] -#[available_gas(20000000)] +#[available_gas(200000000)] fn test_sPair_swap_token0_for_token1() { let stable = true; let feeTier = 0; diff --git a/src/tests/pair/test_volatile_pair.cairo b/src/tests/pair/test_volatile_pair.cairo index 3336aab..186bec1 100644 --- a/src/tests/pair/test_volatile_pair.cairo +++ b/src/tests/pair/test_volatile_pair.cairo @@ -123,7 +123,7 @@ fn test_vPair_mint_more_lp() { } #[test] -#[available_gas(20000000)] +#[available_gas(200000000)] fn test_vPair_swap_token0_for_token1() { let stable = false; let feeTier = 0; diff --git a/src/tests/router/test_router.cairo b/src/tests/router/test_router.cairo index dd2c7fc..f7b200a 100644 --- a/src/tests/router/test_router.cairo +++ b/src/tests/router/test_router.cairo @@ -454,7 +454,7 @@ fn test_router_add_new_liquidity() { } #[test] -#[available_gas(20000000)] +#[available_gas(200000000)] fn test_router_add_more_liquidity() { let (router, account, factoryDispatcher, token0, token1) = add_initial_liquidity(false, 0); diff --git a/src/utils.cairo b/src/utils.cairo index 17c970c..fcc60a7 100644 --- a/src/utils.cairo +++ b/src/utils.cairo @@ -6,7 +6,7 @@ mod callFallback; use callFallback::call_contract_with_selector_fallback; -mod upgradeable; +mod upgradable; use PartialOrdContractAddress::ContractAddressPartialOrd; use array_ext::ArrayTraitExt; diff --git a/src/utils/upgradeable.cairo b/src/utils/upgradable.cairo similarity index 94% rename from src/utils/upgradeable.cairo rename to src/utils/upgradable.cairo index 50496fd..1bad897 100644 --- a/src/utils/upgradeable.cairo +++ b/src/utils/upgradable.cairo @@ -2,12 +2,12 @@ use starknet::ClassHash; #[starknet::interface] -trait IUpgradeable { +trait IUpgradable { fn upgrade(ref self: TState, new_class_hash: ClassHash); } #[starknet::contract] -mod Upgradeable { +mod Upgradable { use starknet::ClassHash; use core::zeroable::Zeroable;