Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ICDex_V2 so that we can run both while making the switch #5313

Merged
merged 3 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backend/canisters/market_maker/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [unreleased]

### Added

- Add `ICDex_V2` so that we can run both while making the switch ([#5313](https://github.com/open-chat-labs/open-chat/pull/5313))

## [[2.0.1038](https://github.com/open-chat-labs/open-chat/releases/tag/v2.0.1038-market_maker)] - 2024-01-31

### Changed
Expand Down
1 change: 1 addition & 0 deletions backend/canisters/market_maker/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub use queries::*;
pub use updates::*;

pub const ICDEX_EXCHANGE_ID: ExchangeId = ExchangeId::new(1);
pub const ICDEX_EXCHANGE_V2_ID: ExchangeId = ExchangeId::new(2);

#[derive(CandidType, Serialize, Deserialize, Clone, Debug)]
pub struct ExchangeInfo {
Expand Down
5 changes: 0 additions & 5 deletions backend/canisters/market_maker/impl/src/exchanges/icdex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ use crate::exchanges::Exchange;
use async_trait::async_trait;
use ic_cdk::api::call::CallResult;
use icdex_client::ICDexClient;
use market_maker_canister::{ExchangeId, ICDEX_EXCHANGE_ID};
use types::{AggregatedOrders, CancelOrderRequest, MakeOrderRequest, Order, TokenInfo};

#[async_trait]
impl<M: Fn(MakeOrderRequest) + Send + Sync, C: Fn(CancelOrderRequest) + Send + Sync> Exchange for ICDexClient<M, C> {
fn exchange_id(&self) -> ExchangeId {
ICDEX_EXCHANGE_ID
}

fn quote_token(&self) -> &TokenInfo {
self.quote_token()
}
Expand Down
2 changes: 0 additions & 2 deletions backend/canisters/market_maker/impl/src/exchanges/mod.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use async_trait::async_trait;
use ic_cdk::api::call::CallResult;
use market_maker_canister::ExchangeId;
use types::{AggregatedOrders, CancelOrderRequest, MakeOrderRequest, MarketState, Order, TokenInfo};

pub mod icdex;

#[async_trait]
pub trait Exchange: Send + Sync {
fn exchange_id(&self) -> ExchangeId;
fn quote_token(&self) -> &TokenInfo;
fn base_token(&self) -> &TokenInfo;
async fn latest_price(&self) -> CallResult<u64>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn run() {
}
}

fn get_active_exchanges(state: &RuntimeState) -> Vec<(Box<dyn Exchange>, Config)> {
fn get_active_exchanges(state: &RuntimeState) -> Vec<(ExchangeId, Box<dyn Exchange>, Config)> {
let now = state.env.now();

state
Expand All @@ -41,21 +41,19 @@ fn get_active_exchanges(state: &RuntimeState) -> Vec<(Box<dyn Exchange>, Config)
.get(&id)
.map_or(true, |ts| now.saturating_sub(*ts) > 10 * MINUTE_IN_MS)
})
.filter_map(|(&id, c)| state.get_exchange_client(id).map(|e| (e, c.clone())))
.filter_map(|(&id, c)| state.get_exchange_client(id).map(|e| (id, e, c.clone())))
.collect()
}

async fn run_async(exchanges: Vec<(Box<dyn Exchange>, Config)>) {
futures::future::join_all(exchanges.into_iter().map(|(e, c)| async {
let exchange_id = e.exchange_id();
let _ = run_single(e, c).await;
mark_market_maker_complete(&exchange_id);
async fn run_async(exchanges: Vec<(ExchangeId, Box<dyn Exchange>, Config)>) {
futures::future::join_all(exchanges.into_iter().map(|(id, e, c)| async move {
let _ = run_single(id, e, c).await;
mark_market_maker_complete(&id);
}))
.await;
}

async fn run_single(exchange_client: Box<dyn Exchange>, config: Config) -> CallResult<()> {
let exchange_id = exchange_client.exchange_id();
async fn run_single(exchange_id: ExchangeId, exchange_client: Box<dyn Exchange>, config: Config) -> CallResult<()> {
trace!(%exchange_id, "Running market maker");

let my_previous_open_orders = mutate_state(|state| {
Expand Down
49 changes: 30 additions & 19 deletions backend/canisters/market_maker/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::exchanges::Exchange;
use crate::model::orders_log::OrdersLog;
use canister_state_macros::canister_state;
use icdex_client::ICDexClient;
use market_maker_canister::{ExchangeId, ICDEX_EXCHANGE_ID};
use market_maker_canister::{ExchangeId, ICDEX_EXCHANGE_ID, ICDEX_EXCHANGE_V2_ID};
use serde::{Deserialize, Serialize};
use std::cell::RefCell;
use std::collections::{BTreeMap, HashMap, VecDeque};
Expand Down Expand Up @@ -38,25 +38,14 @@ impl RuntimeState {

pub fn get_exchange_client(&self, exchange_id: ExchangeId) -> Option<Box<dyn Exchange>> {
match exchange_id {
ICDEX_EXCHANGE_ID => Some(Box::new(ICDexClient::new(
self.env.canister_id(),
ICDEX_EXCHANGE_ID => Some(self.create_icdex_client(
ICDEX_EXCHANGE_ID,
CanisterId::from_text("3we4s-lyaaa-aaaak-aegrq-cai").unwrap(),
TokenInfo {
token: Cryptocurrency::InternetComputer,
ledger: self.data.icp_ledger_canister_id,
decimals: 8,
fee: 10_000,
},
TokenInfo {
token: Cryptocurrency::CHAT,
ledger: self.data.chat_ledger_canister_id,
decimals: 8,
fee: 100_000,
},
10_000_000,
|order| on_order_made(ICDEX_EXCHANGE_ID, order),
|order| on_order_cancelled(ICDEX_EXCHANGE_ID, order),
))),
)),
ICDEX_EXCHANGE_V2_ID => Some(self.create_icdex_client(
ICDEX_EXCHANGE_V2_ID,
CanisterId::from_text("52ypw-riaaa-aaaar-qadjq-cai").unwrap(),
)),
_ => None,
}
}
Expand All @@ -79,6 +68,28 @@ impl RuntimeState {
},
}
}

fn create_icdex_client(&self, exchange_id: ExchangeId, dex_canister_id: CanisterId) -> Box<dyn Exchange> {
Box::new(ICDexClient::new(
self.env.canister_id(),
dex_canister_id,
TokenInfo {
token: Cryptocurrency::InternetComputer,
ledger: self.data.icp_ledger_canister_id,
decimals: 8,
fee: 10_000,
},
TokenInfo {
token: Cryptocurrency::CHAT,
ledger: self.data.chat_ledger_canister_id,
decimals: 8,
fee: 100_000,
},
10_000_000,
move |order| on_order_made(exchange_id, order),
move |order| on_order_cancelled(exchange_id, order),
))
}
}

#[derive(Serialize, Deserialize)]
Expand Down
Loading