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

Introduce new 'Exchange Bot' for swapping between currencies #4370

Merged
merged 51 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
bee08e2
Exchange bot WIP
hpeebles Sep 13, 2023
e550295
Add prod canister Id
hpeebles Sep 13, 2023
f1e92a3
More
hpeebles Sep 13, 2023
87e19d5
More
hpeebles Sep 13, 2023
9bf7b1b
Merge branch 'master' into exchange_bot
hpeebles Sep 14, 2023
ca0021d
More
hpeebles Sep 14, 2023
4e16b8e
More
hpeebles Sep 14, 2023
67c0625
Merge branch 'master' into exchange_bot
hpeebles Sep 14, 2023
4e7ad3d
clippy
hpeebles Sep 14, 2023
3d93272
Merge branch 'master' into exchange_bot
hpeebles Sep 14, 2023
5785900
Merge branch 'master' into exchange_bot
hpeebles Sep 14, 2023
46df9b7
More
hpeebles Sep 15, 2023
75acc94
Merge branch 'master' into exchange_bot
hpeebles Sep 15, 2023
44827db
Merge branch 'master' into exchange_bot
hpeebles Sep 15, 2023
8d92185
Parser
hpeebles Sep 15, 2023
1bf7852
More
hpeebles Sep 15, 2023
2d04546
More
hpeebles Sep 15, 2023
748dd82
Fix call to register bot
hpeebles Sep 15, 2023
b136440
More
hpeebles Sep 15, 2023
a1222b5
Make amount optional
hpeebles Sep 15, 2023
bd0363d
Add help text
hpeebles Sep 15, 2023
35f48ac
Merge branch 'master' into exchange_bot
hpeebles Sep 15, 2023
db4da8c
Merge branch 'master' into exchange_bot
hpeebles Sep 18, 2023
d26a0e2
Fix
hpeebles Sep 18, 2023
10e3141
Implement `withdraw` command
hpeebles Sep 18, 2023
eef9e9b
More
hpeebles Sep 18, 2023
81c82e0
More
hpeebles Sep 18, 2023
260f60b
More
hpeebles Sep 18, 2023
9fc8c58
More
hpeebles Sep 18, 2023
5dbfc3a
Add `balance` command
hpeebles Sep 18, 2023
d2a81b0
Merge branch 'master' into exchange_bot
hpeebles Sep 19, 2023
45d9e38
More
hpeebles Sep 18, 2023
16fd3dc
Add `withdraw` sub task
hpeebles Sep 19, 2023
579c565
Consolidate getting quotes
hpeebles Sep 19, 2023
16bd85c
Nearly done implementing `swap`
hpeebles Sep 19, 2023
fc2a4ad
Build swap messages
hpeebles Sep 19, 2023
c26e142
More
hpeebles Sep 19, 2023
83ba7f0
clippy
hpeebles Sep 19, 2023
da22770
Fix `format_crypto_amount`
hpeebles Sep 19, 2023
fbdfdd1
Retry failed notifications + withdrawals
hpeebles Sep 20, 2023
1919aaa
Fix token0 and token1 order
hpeebles Sep 20, 2023
7e34d2b
Merge branch 'master' into exchange_bot
hpeebles Sep 20, 2023
6f1af56
Withdraw from dex before transferring back to user
hpeebles Sep 20, 2023
dd391d0
Support withdrawing from default subaccount
hpeebles Sep 20, 2023
2edd838
Fix amount sent to dex which was off by 1 transfer fee
hpeebles Sep 20, 2023
67aec94
Simplify
hpeebles Sep 20, 2023
cb56ed2
Merge branch 'master' into exchange_bot
hpeebles Sep 20, 2023
1e4e8b7
Avoid unnecessary message edits
hpeebles Sep 20, 2023
1af7039
Add token symbol to quotes
hpeebles Sep 20, 2023
53d69c2
Merge branch 'master' into exchange_bot
hpeebles Sep 21, 2023
d4af59e
Switch to `regex-lite`
hpeebles Sep 21, 2023
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
68 changes: 67 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ members = [
"backend/canisters/community/impl",
"backend/canisters/cycles_dispenser/api",
"backend/canisters/cycles_dispenser/impl",
"backend/canisters/exchange_bot/api",
"backend/canisters/exchange_bot/impl",
"backend/canisters/group/api",
"backend/canisters/group/c2c_client",
"backend/canisters/group/client",
Expand Down Expand Up @@ -103,6 +105,7 @@ members = [
"backend/libraries/human_readable",
"backend/libraries/human_readable_derive",
"backend/libraries/icdex_client",
"backend/libraries/icpswap_client",
"backend/libraries/index_store",
"backend/libraries/instruction_counts_log",
"backend/libraries/ledger_utils",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ mod process_pending_actions {

async fn send_messages(recipient: UserId, messages: Vec<BotMessage>) {
let bot_name = read_state(|state| state.data.bot_name.clone());
let args = user_canister::c2c_handle_bot_messages::Args { bot_name, messages };
let args = user_canister::c2c_handle_bot_messages::Args {
bot_name,
bot_display_name: None,
messages,
};

let response = user_canister_c2c_client::c2c_handle_bot_messages(recipient.into(), &args).await;
if response.is_err() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ async fn process_action(action: Action) {
CanisterId::from(user_id),
&user_canister::c2c_handle_bot_messages::Args {
bot_name: read_state(|state| state.data.username.clone()),
bot_display_name: None,
messages: messages
.into_iter()
.map(|m| BotMessage {
Expand Down
1 change: 1 addition & 0 deletions backend/canister_installer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ candid = { workspace = true }
canister_agent_utils = { path = "../libraries/canister_agent_utils" }
clap = { workspace = true, features = ["derive"] }
cycles_dispenser_canister = { path = "../canisters/cycles_dispenser/api" }
exchange_bot_canister = { path = "../canisters/exchange_bot/api" }
futures = { workspace = true }
group_canister = { path = "../canisters/group/api" }
group_index_canister = { path = "../canisters/group_index/api" }
Expand Down
21 changes: 19 additions & 2 deletions backend/canister_installer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ async fn install_service_canisters_impl(
set_controllers(management_canister, &canister_ids.storage_index, controllers.clone()),
set_controllers(management_canister, &canister_ids.cycles_dispenser, controllers.clone()),
set_controllers(management_canister, &canister_ids.registry, controllers.clone()),
set_controllers(management_canister, &canister_ids.market_maker, controllers),
set_controllers(management_canister, &canister_ids.market_maker, controllers.clone()),
set_controllers(management_canister, &canister_ids.exchange_bot, controllers),
set_controllers(
management_canister,
&canister_ids.local_user_index,
Expand Down Expand Up @@ -170,6 +171,16 @@ async fn install_service_canisters_impl(
test_mode,
};

let exchange_bot_canister_wasm = get_canister_wasm(CanisterName::ExchangeBot, version);
let exchange_bot_init_args = exchange_bot_canister::init::Args {
governance_principals: vec![principal],
user_index_canister_id: canister_ids.user_index,
local_user_index_canister_id: canister_ids.local_user_index,
cycles_dispenser_canister_id: canister_ids.cycles_dispenser,
wasm_version: version,
test_mode,
};

futures::future::join5(
install_wasm(
management_canister,
Expand Down Expand Up @@ -204,7 +215,7 @@ async fn install_service_canisters_impl(
)
.await;

futures::future::join4(
futures::future::join5(
install_wasm(
management_canister,
&canister_ids.storage_index,
Expand All @@ -229,6 +240,12 @@ async fn install_service_canisters_impl(
&market_maker_canister_wasm.module,
market_maker_init_args,
),
install_wasm(
management_canister,
&canister_ids.exchange_bot,
&exchange_bot_canister_wasm.module,
exchange_bot_init_args,
),
)
.await;

Expand Down
4 changes: 4 additions & 0 deletions backend/canister_installer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ async fn main() {
cycles_dispenser: opts.cycles_dispenser,
registry: opts.registry,
market_maker: opts.market_maker,
exchange_bot: opts.exchange_bot,
nns_root: opts.nns_root,
nns_governance: opts.nns_governance,
nns_internet_identity: opts.nns_internet_identity,
Expand Down Expand Up @@ -80,6 +81,9 @@ struct Opts {
#[arg(long)]
market_maker: CanisterId,

#[arg(long)]
exchange_bot: CanisterId,

#[arg(long)]
nns_root: CanisterId,

Expand Down
1 change: 1 addition & 0 deletions backend/canister_upgrader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ candid = { workspace = true }
canister_agent_utils = { path = "../libraries/canister_agent_utils" }
clap = { workspace = true, features = ["derive"] }
cycles_dispenser_canister = { path = "../canisters/cycles_dispenser/api" }
exchange_bot_canister = { path = "../canisters/exchange_bot/api" }
group_canister = { path = "../canisters/group/api" }
group_index_canister = { path = "../canisters/group_index/api" }
group_index_canister_client = { path = "../canisters/group_index/client" }
Expand Down
1 change: 1 addition & 0 deletions backend/canister_upgrader/local-bin/exchange_bot.wasm.gz
19 changes: 19 additions & 0 deletions backend/canister_upgrader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,25 @@ pub async fn upgrade_market_maker_canister(
println!("Market maker canister upgraded");
}

pub async fn upgrade_exchange_bot_canister(
identity: Box<dyn Identity>,
url: String,
exchange_bot_canister_id: CanisterId,
version: BuildVersion,
) {
upgrade_top_level_canister(
identity,
url,
exchange_bot_canister_id,
version,
exchange_bot_canister::post_upgrade::Args { wasm_version: version },
CanisterName::ExchangeBot,
)
.await;

println!("Exchange bot canister upgraded");
}

pub async fn upgrade_local_group_index_canister(
identity: Box<dyn Identity>,
url: String,
Expand Down
4 changes: 4 additions & 0 deletions backend/canister_upgrader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ async fn main() {
CanisterName::CyclesDispenser => {
upgrade_cycles_dispenser_canister(identity, opts.url, opts.cycles_dispenser, opts.version).await
}
CanisterName::ExchangeBot => upgrade_exchange_bot_canister(identity, opts.url, opts.exchange_bot, opts.version).await,
CanisterName::Group => upgrade_group_canister(identity, opts.url, opts.group_index, opts.version).await,
CanisterName::LocalGroupIndex => {
upgrade_local_group_index_canister(identity, opts.url, opts.group_index, opts.version).await
Expand Down Expand Up @@ -80,6 +81,9 @@ struct Opts {
#[arg(long)]
market_maker: CanisterId,

#[arg(long)]
exchange_bot: CanisterId,

#[arg(long)]
canister_to_upgrade: CanisterName,

Expand Down
4 changes: 2 additions & 2 deletions backend/canisters/community/impl/src/jobs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod import_groups;

use crate::RuntimeState;

pub mod import_groups;

pub(crate) fn start(state: &RuntimeState) {
import_groups::start_job_if_required(state);
}
6 changes: 6 additions & 0 deletions backend/canisters/exchange_bot/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [unreleased]
15 changes: 15 additions & 0 deletions backend/canisters/exchange_bot/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "exchange_bot_canister"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bot_api = { path = "../../../bots/api" }
candid = { workspace = true }
candid_gen = { path = "../../../libraries/candid_gen" }
human_readable = { path = "../../../libraries/human_readable" }
serde = { workspace = true }
types = { path = "../../../libraries/types" }
user_index_canister = { path = "../../user_index/api" }
13 changes: 13 additions & 0 deletions backend/canisters/exchange_bot/api/can.did
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import "../../../libraries/types/can.did";

type InitArgs = record {
governance_principals : vec CanisterId;
user_index_canister_id : CanisterId;
local_user_index_canister_id : CanisterId;
cycles_dispenser_canister_id : CanisterId;
wasm_version : BuildVersion;
test_mode : bool;
};

service : {
};
22 changes: 22 additions & 0 deletions backend/canisters/exchange_bot/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use candid::CandidType;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};

mod lifecycle;
mod updates;

pub use lifecycle::*;
pub use updates::*;

#[derive(CandidType, Serialize, Deserialize, Clone, Copy, Debug, Eq, PartialEq)]
pub enum ExchangeId {
ICPSwap,
}

impl Display for ExchangeId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ExchangeId::ICPSwap => f.write_str("ICPSwap"),
}
}
}
13 changes: 13 additions & 0 deletions backend/canisters/exchange_bot/api/src/lifecycle/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use candid::{CandidType, Principal};
use serde::{Deserialize, Serialize};
use types::{BuildVersion, CanisterId};

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub governance_principals: Vec<Principal>,
pub user_index_canister_id: CanisterId,
pub local_user_index_canister_id: CanisterId,
pub cycles_dispenser_canister_id: CanisterId,
pub wasm_version: BuildVersion,
pub test_mode: bool,
}
2 changes: 2 additions & 0 deletions backend/canisters/exchange_bot/api/src/lifecycle/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod init;
pub mod post_upgrade;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use candid::CandidType;
use serde::{Deserialize, Serialize};
use types::BuildVersion;

#[derive(CandidType, Serialize, Deserialize, Debug)]
pub struct Args {
pub wasm_version: BuildVersion,
}
5 changes: 5 additions & 0 deletions backend/canisters/exchange_bot/api/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#[allow(deprecated)]
fn main() {
candid::export_service!();
std::print!("{}", __export_service());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub use bot_api::handle_direct_message::{Response::*, *};
2 changes: 2 additions & 0 deletions backend/canisters/exchange_bot/api/src/updates/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod handle_direct_message;
pub mod register_bot;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub use user_index_canister::c2c_register_bot::*;
Loading
Loading