Skip to content

Commit

Permalink
fix: some interfaces + naming convention
Browse files Browse the repository at this point in the history
  • Loading branch information
parketh committed Apr 17, 2024
1 parent d3188c3 commit 2583b90
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 18 deletions.
27 changes: 20 additions & 7 deletions src/contracts/strategy.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub mod ReversionStrategy {
// Local imports.
use haiko_strategy_reversion::libraries::{trend_math, store_packing::StrategyStateStorePacking};
use haiko_strategy_reversion::types::{Trend, StrategyState};
use haiko_strategy_reversion::interfaces::ITrendStrategy::ITrendStrategy;
use haiko_strategy_reversion::interfaces::IReversionStrategy::IReversionStrategy;
use haiko_strategy_reversion::interfaces::IVaultToken::{
IVaultTokenDispatcher, IVaultTokenDispatcherTrait
};
Expand Down Expand Up @@ -229,6 +229,9 @@ pub mod ReversionStrategy {

// Get list of positions currently placed by strategy.
//
// # Arguments
// * `market_id` - market id
//
// # Returns
// * `positions` - list of positions
fn placed_positions(self: @ContractState, market_id: felt252) -> Span<PositionInfo> {
Expand Down Expand Up @@ -261,6 +264,10 @@ pub mod ReversionStrategy {
// are queued, the returned list will match the list returned by `placed_positions`. Note that
// the list of queued positions can differ depending on the incoming swap.
//
// # Arguments
// * `market_id` - market id
// * `swap_params` - (optional) incoming swap parameters
//
// # Returns
// * `positions` - list of positions
fn queued_positions(
Expand Down Expand Up @@ -312,7 +319,6 @@ pub mod ReversionStrategy {
Trend::Down => !is_buy && curr_limit < bid_upper,
}
};
// println!("update_bid: {}, update_ask: {}", update_bid, update_ask);

// Fetch amounts in existing position.
let contract: felt252 = get_contract_address().into();
Expand Down Expand Up @@ -405,7 +411,7 @@ pub mod ReversionStrategy {
}

#[abi(embed_v0)]
impl ReversionStrategy of ITrendStrategy<ContractState> {
impl ReversionStrategy of IReversionStrategy<ContractState> {
// Contract owner
fn owner(self: @ContractState) -> ContractAddress {
self.owner.read()
Expand All @@ -421,6 +427,11 @@ pub mod ReversionStrategy {
self.strategy_state.read(market_id).trend
}

// Queued trend
fn queued_trend(self: @ContractState, market_id: felt252) -> Trend {
self.strategy_state.read(market_id).queued_trend
}

// Strategy state
fn strategy_state(self: @ContractState, market_id: felt252) -> StrategyState {
self.strategy_state.read(market_id)
Expand Down Expand Up @@ -867,6 +878,9 @@ pub mod ReversionStrategy {

// Manually trigger contract to collect all outstanding positions and pause the contract.
// Only callable by owner.
//
// # Arguments
// * `market_id` - market id
fn collect_and_pause(ref self: ContractState, market_id: felt252) {
self.assert_owner();
self._collect_and_pause(market_id);
Expand All @@ -879,6 +893,9 @@ pub mod ReversionStrategy {
// * `receiver` - address to receive fees
// * `token` - token to collect fees for
// * `amount` - amount of fees requested
//
// # Returns
// * `amount` - amount collected
fn collect_withdraw_fees(
ref self: ContractState, receiver: ContractAddress, token: ContractAddress, amount: u256
) -> u256 {
Expand Down Expand Up @@ -1050,10 +1067,6 @@ pub mod ReversionStrategy {
let mut ask = *placed_positions.at(1);
let next_bid = *queued_positions.at(0);
let next_ask = *queued_positions.at(1);
// println!("[placed] bid_lower: {}, bid_upper: {}, bid_liquidity: {}", bid.lower_limit, bid.upper_limit, bid.liquidity);
// println!("[placed] ask_lower: {}, ask_upper: {}, ask_liquidity: {}", ask.lower_limit, ask.upper_limit, ask.liquidity);
// println!("[queued] bid_lower: {}, bid_upper: {}, bid_liquidity: {}", next_bid.lower_limit, next_bid.upper_limit, next_bid.liquidity);
// println!("[queued] ask_lower: {}, ask_upper: {}, ask_liquidity: {}", next_ask.lower_limit, next_ask.upper_limit, next_ask.liquidity);
let update_bid: bool = next_bid.lower_limit != bid.lower_limit
|| next_bid.upper_limit != bid.upper_limit;
let update_ask: bool = next_ask.lower_limit != ask.lower_limit
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pub mod ITrendStrategy;
pub mod IReversionStrategy;
pub mod IVaultToken;
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ use haiko_lib::types::core::{PositionInfo, SwapParams};


#[starknet::interface]
pub trait ITrendStrategy<TContractState> {
pub trait IReversionStrategy<TContractState> {
// Contract owner
fn owner(self: @TContractState) -> ContractAddress;

// Queued contract owner, used for ownership transfers
fn queued_owner(self: @TContractState) -> ContractAddress;

// Set trend for a given market
// Get trend for a given market
fn trend(self: @TContractState, market_id: felt252) -> Trend;

// Get queued trend for a given market
fn queued_trend(self: @TContractState, market_id: felt252) -> Trend;

// Strategy state
fn strategy_state(self: @TContractState, market_id: felt252) -> StrategyState;

Expand Down
6 changes: 3 additions & 3 deletions src/tests/helpers.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use starknet::ContractAddress;
use starknet::class_hash::ClassHash;

// Local imports.
use haiko_strategy_reversion::interfaces::ITrendStrategy::ITrendStrategyDispatcher;
use haiko_strategy_reversion::interfaces::IReversionStrategy::IReversionStrategyDispatcher;

// External imports.
use snforge_std::{declare, ContractClassTrait};

pub fn deploy_trend_strategy(
owner: ContractAddress, market_manager: ContractAddress, vault_token_class: ClassHash,
) -> ITrendStrategyDispatcher {
) -> IReversionStrategyDispatcher {
let contract = declare("ReversionStrategy");
let name: ByteArray = "Trend";
let symbol: ByteArray = "TRND";
Expand All @@ -22,5 +22,5 @@ pub fn deploy_trend_strategy(
market_manager.serialize(ref calldata);
vault_token_class.serialize(ref calldata);
let contract_address = contract.deploy(@calldata).unwrap();
ITrendStrategyDispatcher { contract_address }
IReversionStrategyDispatcher { contract_address }
}
10 changes: 5 additions & 5 deletions src/tests/test_strategy.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use starknet::ContractAddress;
use haiko_strategy_reversion::contracts::strategy::ReversionStrategy;
use haiko_strategy_reversion::types::{Trend, StrategyState};
use haiko_strategy_reversion::interfaces::{
ITrendStrategy::{ITrendStrategyDispatcher, ITrendStrategyDispatcherTrait},
IReversionStrategy::{IReversionStrategyDispatcher, IReversionStrategyDispatcherTrait},
IVaultToken::{IVaultTokenDispatcher, IVaultTokenDispatcherTrait}
};
use haiko_strategy_reversion::tests::helpers::deploy_trend_strategy;
Expand Down Expand Up @@ -43,7 +43,7 @@ fn _before(
ERC20ABIDispatcher,
ERC20ABIDispatcher,
felt252,
ITrendStrategyDispatcher,
IReversionStrategyDispatcher,
Option<ERC20ABIDispatcher>,
) {
// Deploy market manager.
Expand Down Expand Up @@ -113,7 +113,7 @@ fn before() -> (
ERC20ABIDispatcher,
ERC20ABIDispatcher,
felt252,
ITrendStrategyDispatcher,
IReversionStrategyDispatcher,
ERC20ABIDispatcher,
) {
let (market_manager, base_token, quote_token, market_id, strategy, token) = _before(true);
Expand All @@ -125,7 +125,7 @@ fn before_skip_initialise() -> (
ERC20ABIDispatcher,
ERC20ABIDispatcher,
felt252,
ITrendStrategyDispatcher,
IReversionStrategyDispatcher,
) {
let (market_manager, base_token, quote_token, market_id, strategy, _) = _before(false);
(market_manager, base_token, quote_token, market_id, strategy)
Expand Down Expand Up @@ -1309,7 +1309,7 @@ struct Snapshot {

fn _snapshot_state(
market_manager: IMarketManagerDispatcher,
strategy: ITrendStrategyDispatcher,
strategy: IReversionStrategyDispatcher,
market_id: felt252,
base_token: ERC20ABIDispatcher,
quote_token: ERC20ABIDispatcher,
Expand Down

0 comments on commit 2583b90

Please sign in to comment.