Skip to content

Commit

Permalink
Fix typo in upgradable module and make pairs upgradable
Browse files Browse the repository at this point in the history
  • Loading branch information
iddriss committed Nov 30, 2023
1 parent 57b5506 commit 3d49660
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 35 deletions.
7 changes: 3 additions & 4 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
account = "mainnet"
accounts-file = "./accounts/mainnet_deployer.json"
url = "https://starknet-mainnet.infura.io/v3/${INFURA_PROJECT_ID}"
14 changes: 5 additions & 9 deletions src/dex/v1/factory/factory.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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 });
}
Expand Down Expand Up @@ -401,11 +397,11 @@ mod StarkDFactory {

/// @notice upgradable at moment, a future implementation will drop this
#[external(v0)]
impl UpgradableImpl of IUpgradeable<ContractState> {
impl UpgradableImpl of IUpgradable<ContractState> {
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);
}
}

Expand Down
35 changes: 31 additions & 4 deletions src/dex/v1/pair/Pair.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<ContractState> {
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 {
Expand Down Expand Up @@ -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');
}
}
}

10 changes: 7 additions & 3 deletions src/dex/v1/pair/interface.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ struct Snapshot {
reserve0: u256,
reserve1: u256,
is_stable: bool,
fee_tier: u8,
}

#[derive(Copy, Drop, Serde, starknet::Store)]
Expand Down Expand Up @@ -140,7 +141,9 @@ trait IStarkDPairABI<TContractState> {
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]
Expand Down Expand Up @@ -187,7 +190,9 @@ trait IStarkDPairCamelABI<TContractState> {
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);
}


Expand All @@ -207,5 +212,4 @@ trait IFeesVault<TContractState> {
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);
}
5 changes: 0 additions & 5 deletions src/dex/v1/pair/pairFeesVault.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
8 changes: 4 additions & 4 deletions src/dex/v1/router/router.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -255,11 +255,11 @@ mod StarkDRouter {
}

#[external(v0)]
impl UpgradableImpl of IUpgradeable<ContractState> {
impl UpgradableImpl of IUpgradable<ContractState> {
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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/tests/pair/test_stable_pair.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/tests/pair/test_volatile_pair.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/tests/router/test_router.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/utils.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod callFallback;

use callFallback::call_contract_with_selector_fallback;

mod upgradeable;
mod upgradable;

use PartialOrdContractAddress::ContractAddressPartialOrd;
use array_ext::ArrayTraitExt;
Expand Down
4 changes: 2 additions & 2 deletions src/utils/upgradeable.cairo → src/utils/upgradable.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
use starknet::ClassHash;

#[starknet::interface]
trait IUpgradeable<TState> {
trait IUpgradable<TState> {
fn upgrade(ref self: TState, new_class_hash: ClassHash);
}

#[starknet::contract]
mod Upgradeable {
mod Upgradable {
use starknet::ClassHash;
use core::zeroable::Zeroable;

Expand Down

0 comments on commit 3d49660

Please sign in to comment.