-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement swapping of tokens via external DEXs (#4819)
- Loading branch information
Showing
28 changed files
with
565 additions
and
106 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
backend/canisters/user/api/src/queries/token_swap_status.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
pub use candid::CandidType; | ||
use icrc_ledger_types::icrc1::account::Account; | ||
use serde::{Deserialize, Serialize}; | ||
use types::TimestampMillis; | ||
|
||
#[derive(CandidType, Serialize, Deserialize, Debug)] | ||
pub struct Args { | ||
pub swap_id: u128, | ||
} | ||
|
||
#[derive(CandidType, Serialize, Deserialize, Debug)] | ||
pub enum Response { | ||
Success(SuccessResult), | ||
NotFound, | ||
} | ||
|
||
#[derive(CandidType, Serialize, Deserialize, Debug)] | ||
pub struct SuccessResult { | ||
pub status: TokenSwapStatus, | ||
} | ||
|
||
#[derive(CandidType, Serialize, Deserialize, Clone, Debug)] | ||
pub struct TokenSwapStatus { | ||
pub started: TimestampMillis, | ||
pub deposit_account: SwapSubtask<Account>, | ||
pub transfer: SwapSubtask<u64>, // Block Index | ||
pub notified_dex: SwapSubtask<()>, | ||
pub amount_swapped: SwapSubtask<u128>, | ||
pub withdrawn_from_dex: SwapSubtask<()>, | ||
} | ||
|
||
type SwapSubtask<T = ()> = Option<Result<T, String>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use candid::CandidType; | ||
use serde::{Deserialize, Serialize}; | ||
use types::{CanisterId, TokenInfo}; | ||
|
||
#[derive(CandidType, Serialize, Deserialize, Clone, Debug)] | ||
pub struct Args { | ||
pub swap_id: u128, | ||
pub input_token: TokenInfo, | ||
pub output_token: TokenInfo, | ||
pub input_amount: u128, | ||
pub exchange_args: ExchangeArgs, | ||
pub min_output_amount: u128, | ||
} | ||
|
||
#[derive(CandidType, Serialize, Deserialize, Clone, Debug)] | ||
pub enum ExchangeArgs { | ||
ICPSwap(ICPSwapArgs), | ||
} | ||
|
||
#[derive(CandidType, Serialize, Deserialize, Clone, Debug)] | ||
pub struct ICPSwapArgs { | ||
pub swap_canister_id: CanisterId, | ||
pub zero_for_one: bool, | ||
} | ||
|
||
#[derive(CandidType, Serialize, Deserialize, Debug)] | ||
pub enum Response { | ||
Success(SuccessResult), | ||
InternalError(String), | ||
} | ||
|
||
#[derive(CandidType, Serialize, Deserialize, Debug)] | ||
pub struct SuccessResult { | ||
pub amount_out: u128, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use icrc_ledger_types::icrc1::account::Account; | ||
use serde::{Deserialize, Serialize}; | ||
use std::collections::HashMap; | ||
use types::{TimestampMillis, Timestamped}; | ||
use user_canister::token_swap_status::TokenSwapStatus; | ||
|
||
#[derive(Serialize, Deserialize, Default)] | ||
pub struct TokenSwaps { | ||
swaps: HashMap<u128, TokenSwap>, | ||
} | ||
|
||
impl TokenSwaps { | ||
pub fn push_new(&mut self, args: user_canister::swap_tokens::Args, now: TimestampMillis) -> TokenSwap { | ||
let token_swap = TokenSwap::new(args, now); | ||
self.upsert(token_swap.clone()); | ||
token_swap | ||
} | ||
|
||
pub fn upsert(&mut self, swap: TokenSwap) { | ||
self.swaps.insert(swap.args.swap_id, swap); | ||
} | ||
|
||
pub fn get(&self, swap_id: u128) -> Option<&TokenSwap> { | ||
self.swaps.get(&swap_id) | ||
} | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone, Debug)] | ||
pub struct TokenSwap { | ||
pub args: user_canister::swap_tokens::Args, | ||
pub started: TimestampMillis, | ||
pub deposit_account: SwapSubtask<Account>, | ||
pub transfer: SwapSubtask<u64>, // Block Index | ||
pub notified_dex_at: SwapSubtask, | ||
pub amount_swapped: SwapSubtask<u128>, | ||
pub withdrawn_from_dex_at: SwapSubtask, | ||
pub success: Option<Timestamped<bool>>, | ||
} | ||
|
||
type SwapSubtask<T = ()> = Option<Timestamped<Result<T, String>>>; | ||
|
||
impl TokenSwap { | ||
pub fn new(args: user_canister::swap_tokens::Args, now: TimestampMillis) -> TokenSwap { | ||
TokenSwap { | ||
args, | ||
started: now, | ||
deposit_account: None, | ||
transfer: None, | ||
notified_dex_at: None, | ||
amount_swapped: None, | ||
withdrawn_from_dex_at: None, | ||
success: None, | ||
} | ||
} | ||
} | ||
|
||
impl From<TokenSwap> for TokenSwapStatus { | ||
fn from(value: TokenSwap) -> Self { | ||
TokenSwapStatus { | ||
started: value.started, | ||
deposit_account: value.deposit_account.map(|a| a.value), | ||
transfer: value.transfer.map(|t| t.value), | ||
notified_dex: value.notified_dex_at.map(|t| t.value.map(|_| ())), | ||
amount_swapped: value.amount_swapped.map(|t| t.value), | ||
withdrawn_from_dex: value.withdrawn_from_dex_at.map(|t| t.value.map(|_| ())), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
backend/canisters/user/impl/src/queries/token_swap_status.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
use crate::guards::caller_is_owner; | ||
use crate::{read_state, RuntimeState}; | ||
use ic_cdk_macros::query; | ||
use user_canister::token_swap_status::{Response::*, *}; | ||
|
||
#[query(guard = "caller_is_owner")] | ||
fn token_swap_status(args: Args) -> Response { | ||
read_state(|state| token_swap_status_impl(args, state)) | ||
} | ||
|
||
fn token_swap_status_impl(args: Args, state: &RuntimeState) -> Response { | ||
if let Some(token_swap) = state.data.token_swaps.get(args.swap_id).cloned() { | ||
Success(SuccessResult { | ||
status: token_swap.into(), | ||
}) | ||
} else { | ||
NotFound | ||
} | ||
} |
Oops, something went wrong.