-
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.
Integrate with Sonic for token swaps (#5908)
- Loading branch information
Showing
22 changed files
with
364 additions
and
62 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod icpswap; | ||
pub mod sonic; | ||
pub mod swap_client; |
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,24 @@ | ||
use super::swap_client::SwapClient; | ||
use async_trait::async_trait; | ||
use ic_cdk::api::call::CallResult; | ||
use icrc_ledger_types::icrc1::account::Account; | ||
use sonic_client::SonicClient; | ||
|
||
#[async_trait] | ||
impl SwapClient for SonicClient { | ||
async fn deposit_account(&self) -> CallResult<Account> { | ||
self.deposit_account().await | ||
} | ||
|
||
async fn deposit(&self, amount: u128) -> CallResult<()> { | ||
self.deposit(amount).await.map(|_| ()) | ||
} | ||
|
||
async fn swap(&self, amount: u128, min_amount_out: u128) -> CallResult<Result<u128, String>> { | ||
self.swap(amount, min_amount_out).await | ||
} | ||
|
||
async fn withdraw(&self, successful_swap: bool, amount: u128) -> CallResult<u128> { | ||
self.withdraw(successful_swap, amount).await | ||
} | ||
} |
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 |
---|---|---|
|
@@ -156,6 +156,9 @@ | |
switch (dex) { | ||
case "icpswap": | ||
return "ICPSwap"; | ||
case "sonic": | ||
return "Sonic"; | ||
} | ||
} | ||
|
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
22 changes: 22 additions & 0 deletions
22
frontend/openchat-agent/src/services/dexes/sonic/swaps/candid/can.did
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,22 @@ | ||
// This is a trimmed down version of the full candid file which can be found here - | ||
// https://dashboard.internetcomputer.org/canister/3xwpq-ziaaa-aaaah-qcn4a-cai | ||
|
||
type PairInfoExt = | ||
record { | ||
blockTimestampLast: int; | ||
creator: principal; | ||
id: text; | ||
kLast: nat; | ||
lptoken: text; | ||
price0CumulativeLast: nat; | ||
price1CumulativeLast: nat; | ||
reserve0: nat; | ||
reserve1: nat; | ||
token0: text; | ||
token1: text; | ||
totalSupply: nat; | ||
}; | ||
service : { | ||
getAllPairs: () -> (vec PairInfoExt) query; | ||
getPair: (principal, principal) -> (opt PairInfoExt) query; | ||
} |
5 changes: 5 additions & 0 deletions
5
frontend/openchat-agent/src/services/dexes/sonic/swaps/candid/idl.d.ts
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,5 @@ | ||
import type { IDL } from "@dfinity/candid"; | ||
import { PairInfoExt, _SERVICE } from "./types"; | ||
export { PairInfoExt as ApiPairInfo, _SERVICE as SonicSwapsService }; | ||
|
||
export const idlFactory: IDL.InterfaceFactory; |
25 changes: 25 additions & 0 deletions
25
frontend/openchat-agent/src/services/dexes/sonic/swaps/candid/idl.js
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,25 @@ | ||
export const idlFactory = ({ IDL }) => { | ||
const PairInfoExt = IDL.Record({ | ||
'id' : IDL.Text, | ||
'price0CumulativeLast' : IDL.Nat, | ||
'creator' : IDL.Principal, | ||
'reserve0' : IDL.Nat, | ||
'reserve1' : IDL.Nat, | ||
'lptoken' : IDL.Text, | ||
'totalSupply' : IDL.Nat, | ||
'token0' : IDL.Text, | ||
'token1' : IDL.Text, | ||
'price1CumulativeLast' : IDL.Nat, | ||
'kLast' : IDL.Nat, | ||
'blockTimestampLast' : IDL.Int, | ||
}); | ||
return IDL.Service({ | ||
'getAllPairs' : IDL.Func([], [IDL.Vec(PairInfoExt)], ['query']), | ||
'getPair' : IDL.Func( | ||
[IDL.Principal, IDL.Principal], | ||
[IDL.Opt(PairInfoExt)], | ||
['query'], | ||
), | ||
}); | ||
}; | ||
export const init = ({ IDL }) => { return []; }; |
24 changes: 24 additions & 0 deletions
24
frontend/openchat-agent/src/services/dexes/sonic/swaps/candid/types.d.ts
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,24 @@ | ||
import type { Principal } from '@dfinity/principal'; | ||
import type { ActorMethod } from '@dfinity/agent'; | ||
import type { IDL } from '@dfinity/candid'; | ||
|
||
export interface PairInfoExt { | ||
'id' : string, | ||
'price0CumulativeLast' : bigint, | ||
'creator' : Principal, | ||
'reserve0' : bigint, | ||
'reserve1' : bigint, | ||
'lptoken' : string, | ||
'totalSupply' : bigint, | ||
'token0' : string, | ||
'token1' : string, | ||
'price1CumulativeLast' : bigint, | ||
'kLast' : bigint, | ||
'blockTimestampLast' : bigint, | ||
} | ||
export interface _SERVICE { | ||
'getAllPairs' : ActorMethod<[], Array<PairInfoExt>>, | ||
'getPair' : ActorMethod<[Principal, Principal], [] | [PairInfoExt]>, | ||
} | ||
export declare const idlFactory: IDL.InterfaceFactory; | ||
export declare const init: (args: { IDL: typeof IDL }) => IDL.Type[]; |
32 changes: 32 additions & 0 deletions
32
frontend/openchat-agent/src/services/dexes/sonic/swaps/mappers.ts
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 @@ | ||
import type { ApiPairInfo } from "./candid/idl"; | ||
import type { TokenSwapPool } from "openchat-shared"; | ||
import { optional } from "../../../../utils/mapping"; | ||
|
||
export function getAllPairsResponse(candid: ApiPairInfo[], canisterId: string): TokenSwapPool[] { | ||
return candid.map((p) => ({ | ||
dex: "sonic", | ||
canisterId, | ||
token0: p.token0, | ||
token1: p.token1, | ||
})); | ||
} | ||
|
||
export function getPairResponse(candid: [ApiPairInfo] | []): TokenPair | undefined { | ||
return optional(candid, pair); | ||
} | ||
|
||
function pair(candid: ApiPairInfo): TokenPair { | ||
return { | ||
token0: candid.token0, | ||
reserve0: candid.reserve0, | ||
token1: candid.token1, | ||
reserve1: candid.reserve1, | ||
}; | ||
} | ||
|
||
export type TokenPair = { | ||
token0: string; | ||
reserve0: bigint; | ||
token1: string; | ||
reserve1: bigint; | ||
}; |
Oops, something went wrong.