Skip to content

Commit

Permalink
feat: ckbtc and icp-ledger arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Jan 20, 2024
1 parent e097b75 commit fd5b4b4
Show file tree
Hide file tree
Showing 24 changed files with 278 additions and 22 deletions.
53 changes: 51 additions & 2 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use pocket_ic::{PocketIc, WasmResult};
use serde::de::DeserializeOwned;

use self::wasm::Canister;
use crate::wasm::Icrc2InitArgs;

const DEFAULT_CYCLES: u128 = 2_000_000_000_000;

Expand All @@ -26,6 +27,7 @@ pub struct TestEnv {
pub pic: PocketIc,
pub deferred_id: Principal,
pub fly_id: Principal,
pub icp_ledger_id: Principal,
pub marketplace_id: Principal,
pub xrc_id: Principal,
}
Expand Down Expand Up @@ -83,26 +85,67 @@ impl TestEnv {
let pic = PocketIc::new();

// create canisters
let icp_ledger_id = pic.create_canister();
let ckbtc_id = pic.create_canister();
let xrc_id = pic.create_canister();
let deferred_id = pic.create_canister();
let fly_id = pic.create_canister();
let marketplace_id = pic.create_canister();

// install deferred canister
Self::install_icrc2(&pic, icp_ledger_id, "ICP", "Internet Computer", 8);
Self::install_icrc2(&pic, ckbtc_id, "ckBTC", "ckBTC", 8);
Self::install_deferred(&pic, deferred_id, fly_id, marketplace_id);
Self::install_fly(&pic, fly_id, deferred_id, marketplace_id, xrc_id);
Self::install_marketplace(&pic, marketplace_id, deferred_id, fly_id, xrc_id);
Self::install_fly(
&pic,
fly_id,
deferred_id,
marketplace_id,
xrc_id,
icp_ledger_id,
ckbtc_id,
);
Self::install_marketplace(
&pic,
marketplace_id,
deferred_id,
fly_id,
xrc_id,
icp_ledger_id,
);
Self::install_xrc(&pic, xrc_id);

TestEnv {
pic,
deferred_id,
icp_ledger_id,
fly_id,
marketplace_id,
xrc_id,
}
}

fn install_icrc2(pic: &PocketIc, id: Principal, symbol: &str, name: &str, decimals: u8) {
pic.add_cycles(id, DEFAULT_CYCLES);
let wasm_bytes = Self::load_wasm(Canister::Icrc2);
let init_arg = Encode!(&Icrc2InitArgs {
name: name.to_string(),
symbol: symbol.to_string(),
decimals,
fee: 10,
logo: "https://ic0.app/img/logo.png".to_string(),
minting_account: actor::minting_account(),
total_supply: Nat::from(21_268_400_889_u64),
accounts: vec![
(actor::alice_account(), Nat::from(1_000_000_000)),
(actor::bob_account(), Nat::from(1_000_000_000)),
],
})
.unwrap();

pic.install_canister(id, wasm_bytes, init_arg, None);
}

fn install_deferred(
pic: &PocketIc,
deferred_id: Principal,
Expand All @@ -128,6 +171,8 @@ impl TestEnv {
deferred_id: Principal,
marketplace_id: Principal,
xrc_canister: Principal,
icp_ledger_canister: Principal,
ckbtc_canister: Principal,
) {
pic.add_cycles(fly_id, DEFAULT_CYCLES);
let wasm_bytes = Self::load_wasm(Canister::Fly);
Expand All @@ -144,6 +189,8 @@ impl TestEnv {
marketplace_canister: marketplace_id,
swap_account: actor::swap_account(),
xrc_canister,
icp_ledger_canister,
ckbtc_canister,
};
let init_arg = Encode!(&init_arg).unwrap();

Expand All @@ -156,6 +203,7 @@ impl TestEnv {
deferred_id: Principal,
fly_id: Principal,
xrc_canister: Principal,
icp_ledger_canister: Principal,
) {
pic.add_cycles(marketplace_id, DEFAULT_CYCLES);
let wasm_bytes = Self::load_wasm(Canister::Marketplace);
Expand All @@ -165,6 +213,7 @@ impl TestEnv {
deferred_canister: deferred_id,
fly_canister: fly_id,
xrc_canister,
icp_ledger_canister,
};
let init_arg = Encode!(&init_arg).unwrap();

Expand Down
18 changes: 18 additions & 0 deletions integration-tests/src/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
use std::path::Path;

use candid::{CandidType, Nat};
use icrc::icrc1::account::Account;
use serde::Deserialize;

#[derive(Debug, Clone, CandidType, Deserialize)]
pub struct Icrc2InitArgs {
pub accounts: Vec<(Account, Nat)>,
pub decimals: u8,
pub fee: u64,
pub logo: String,
pub minting_account: Account,
pub name: String,
pub symbol: String,
pub total_supply: Nat,
}

pub enum Canister {
Deferred,
Fly,
Icrc2,
Marketplace,
Xrc,
}
Expand All @@ -14,6 +31,7 @@ impl Canister {
Canister::Fly => Path::new("fly/fly.wasm"),
Canister::Marketplace => Path::new("marketplace/marketplace.wasm"),
Canister::Xrc => Path::new("test/xrc.wasm"),
Canister::Icrc2 => Path::new("test/icrc2-template-canister.wasm"),
}
}
}
3 changes: 3 additions & 0 deletions scripts/deploy_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ deploy_fly() {
initial_balances = $INITIAL_BALANCES;
minting_account = $MINTING_ACCOUNT;
xrc_canister = principal \"uf6dk-hyaaa-aaaaq-qaaaq-cai\";
ckbtc_canister = principal \"mxzaz-hqaaa-aaaar-qaada-cai\";
icp_ledger_canister = principal \"ryjl3-tyaaa-aaaaa-aaaba-cai\";
})"

dfx deploy --mode=$INSTALL_MODE --yes --network="$NETWORK" --argument="$fly_init_args" fly
Expand All @@ -66,6 +68,7 @@ deploy_marketplace() {
fly_canister = principal \"$FLY_PRINCIPAL\";
xrc_canister = principal \"uf6dk-hyaaa-aaaaq-qaaaq-cai\";
admins = vec { $(for admin in $ADMINS; do echo "principal \"$admin\";"; done) };
icp_ledger_canister = principal \"ryjl3-tyaaa-aaaaa-aaaba-cai\";
})"

dfx deploy --mode=$INSTALL_MODE --yes --network="$NETWORK" --argument="$marketplace_init_args" marketplace
Expand Down
4 changes: 4 additions & 0 deletions src/declarations/fly/fly.did.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ export type FlyError = { 'Configuration' : ConfigurationError } |
{ 'Icrc2Transfer' : TransferFromError };
export interface FlyInitData {
'deferred_canister' : Principal,
'icp_ledger_canister' : Principal,
'minting_account' : Account,
'ckbtc_canister' : Principal,
'initial_balances' : Array<[Account, bigint]>,
'swap_account' : Account,
'xrc_canister' : Principal,
Expand Down Expand Up @@ -145,6 +147,8 @@ export interface _SERVICE {
'admin_burn' : ActorMethod<[bigint], Result>,
'admin_cycles' : ActorMethod<[], bigint>,
'admin_remove_role' : ActorMethod<[Principal, Role], Result>,
'admin_set_ckbtc_canister' : ActorMethod<[Principal], undefined>,
'admin_set_icp_ledger_canister' : ActorMethod<[Principal], undefined>,
'admin_set_role' : ActorMethod<[Principal, Role], undefined>,
'admin_set_swap_account' : ActorMethod<[Account], undefined>,
'admin_set_xrc_canister' : ActorMethod<[Principal], undefined>,
Expand Down
6 changes: 6 additions & 0 deletions src/declarations/fly/fly.did.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ export const idlFactory = ({ IDL }) => {
});
const FlyInitData = IDL.Record({
'deferred_canister' : IDL.Principal,
'icp_ledger_canister' : IDL.Principal,
'minting_account' : Account,
'ckbtc_canister' : IDL.Principal,
'initial_balances' : IDL.Vec(IDL.Tuple(Account, IDL.Nat)),
'swap_account' : Account,
'xrc_canister' : IDL.Principal,
Expand Down Expand Up @@ -173,6 +175,8 @@ export const idlFactory = ({ IDL }) => {
'admin_burn' : IDL.Func([IDL.Nat], [Result], []),
'admin_cycles' : IDL.Func([], [IDL.Nat], ['query']),
'admin_remove_role' : IDL.Func([IDL.Principal, Role], [Result], []),
'admin_set_ckbtc_canister' : IDL.Func([IDL.Principal], [], []),
'admin_set_icp_ledger_canister' : IDL.Func([IDL.Principal], [], []),
'admin_set_role' : IDL.Func([IDL.Principal, Role], [], []),
'admin_set_swap_account' : IDL.Func([Account], [], []),
'admin_set_xrc_canister' : IDL.Func([IDL.Principal], [], []),
Expand Down Expand Up @@ -215,7 +219,9 @@ export const init = ({ IDL }) => {
});
const FlyInitData = IDL.Record({
'deferred_canister' : IDL.Principal,
'icp_ledger_canister' : IDL.Principal,
'minting_account' : Account,
'ckbtc_canister' : IDL.Principal,
'initial_balances' : IDL.Vec(IDL.Tuple(Account, IDL.Nat)),
'swap_account' : Account,
'xrc_canister' : IDL.Principal,
Expand Down
2 changes: 2 additions & 0 deletions src/declarations/marketplace/marketplace.did.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export type MarketplaceError = { 'Buy' : BuyError } |
{ 'Icrc2Transfer' : TransferFromError };
export interface MarketplaceInitData {
'deferred_canister' : Principal,
'icp_ledger_canister' : Principal,
'fly_canister' : Principal,
'xrc_canister' : Principal,
'admins' : Array<Principal>,
Expand Down Expand Up @@ -118,6 +119,7 @@ export interface _SERVICE {
'admin_set_admins' : ActorMethod<[Array<Principal>], Result>,
'admin_set_deferred_canister' : ActorMethod<[Principal], undefined>,
'admin_set_fly_canister' : ActorMethod<[Principal], Result>,
'admin_set_icp_ledger_canister' : ActorMethod<[Principal], undefined>,
'admin_set_interest_rate_for_buyer' : ActorMethod<[number], undefined>,
'admin_set_xrc_canister' : ActorMethod<[Principal], undefined>,
'buy_token' : ActorMethod<[bigint, [] | [Uint8Array | number[]]], Result>,
Expand Down
3 changes: 3 additions & 0 deletions src/declarations/marketplace/marketplace.did.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const idlFactory = ({ IDL }) => {
const MarketplaceInitData = IDL.Record({
'deferred_canister' : IDL.Principal,
'icp_ledger_canister' : IDL.Principal,
'fly_canister' : IDL.Principal,
'xrc_canister' : IDL.Principal,
'admins' : IDL.Vec(IDL.Principal),
Expand Down Expand Up @@ -144,6 +145,7 @@ export const idlFactory = ({ IDL }) => {
'admin_set_admins' : IDL.Func([IDL.Vec(IDL.Principal)], [Result], []),
'admin_set_deferred_canister' : IDL.Func([IDL.Principal], [], []),
'admin_set_fly_canister' : IDL.Func([IDL.Principal], [Result], []),
'admin_set_icp_ledger_canister' : IDL.Func([IDL.Principal], [], []),
'admin_set_interest_rate_for_buyer' : IDL.Func([IDL.Float64], [], []),
'admin_set_xrc_canister' : IDL.Func([IDL.Principal], [], []),
'buy_token' : IDL.Func([IDL.Nat, IDL.Opt(IDL.Vec(IDL.Nat8))], [Result], []),
Expand All @@ -153,6 +155,7 @@ export const idlFactory = ({ IDL }) => {
export const init = ({ IDL }) => {
const MarketplaceInitData = IDL.Record({
'deferred_canister' : IDL.Principal,
'icp_ledger_canister' : IDL.Principal,
'fly_canister' : IDL.Principal,
'xrc_canister' : IDL.Principal,
'admins' : IDL.Vec(IDL.Principal),
Expand Down
4 changes: 4 additions & 0 deletions src/did/src/fly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,16 @@ pub type PicoFly = Nat;
#[derive(Debug, Clone, CandidType, Deserialize)]
pub struct FlyInitData {
pub admins: Vec<Principal>,
/// The canister ID of the CKBTC canister
pub ckbtc_canister: Principal,
/// Total supply of $picofly tokens
pub total_supply: PicoFly,
/// Initial balances (wallet subaccount -> picofly)
pub initial_balances: Vec<(Account, PicoFly)>,
/// Deferred canister
pub deferred_canister: Principal,
/// ICP ledger canister
pub icp_ledger_canister: Principal,
/// Marketplace canister
pub marketplace_canister: Principal,
/// Swap account
Expand Down
2 changes: 2 additions & 0 deletions src/did/src/marketplace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ pub struct MarketplaceInitData {
pub deferred_canister: Principal,
/// Fly canister
pub fly_canister: Principal,
/// ICP ledger canister
pub icp_ledger_canister: Principal,
/// XRC canister
pub xrc_canister: Principal,
}
Expand Down
4 changes: 4 additions & 0 deletions src/fly/fly.did
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ type FlyError = variant {
};
type FlyInitData = record {
deferred_canister : principal;
icp_ledger_canister : principal;
minting_account : Account;
ckbtc_canister : principal;
initial_balances : vec record { Account; nat };
swap_account : Account;
xrc_canister : principal;
Expand Down Expand Up @@ -132,6 +134,8 @@ service : (FlyInitData) -> {
admin_burn : (nat) -> (Result);
admin_cycles : () -> (nat) query;
admin_remove_role : (principal, Role) -> (Result);
admin_set_ckbtc_canister : (principal) -> ();
admin_set_icp_ledger_canister : (principal) -> ();
admin_set_role : (principal, Role) -> ();
admin_set_swap_account : (Account) -> ();
admin_set_xrc_canister : (principal) -> ();
Expand Down
45 changes: 43 additions & 2 deletions src/fly/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ impl FlyCanister {
Configuration::set_minting_account(data.minting_account);
// set swap account
Configuration::set_swap_account(data.swap_account);
// set xrc canister
// set canisters
Configuration::set_xrc_canister(data.xrc_canister);
Configuration::set_ckbtc_canister(data.ckbtc_canister);
Configuration::set_icp_ledger_canister(data.icp_ledger_canister);
// init liquidity pool
LiquidityPool::init();
// set roles
Expand All @@ -73,7 +75,7 @@ impl FlyCanister {
fn set_timers() {
#[cfg(target_family = "wasm")]
async fn swap_icp_to_btc_timer() {
let xrc_principal = Configuration::set_xrc_canister();
let xrc_principal = Configuration::get_xrc_canister();
let _ = LiquidityPool::swap_icp_to_btc(xrc_principal).await;
}

Expand Down Expand Up @@ -198,6 +200,22 @@ impl FlyCanister {
}
Configuration::set_xrc_canister(canister_id);
}

/// Set ckbtc canister
pub fn admin_set_ckbtc_canister(canister_id: Principal) {
if !Inspect::inspect_is_admin(utils::caller()) {
ic_cdk::trap("Unauthorized");
}
Configuration::set_ckbtc_canister(canister_id);
}

/// Set icp ledger canister
pub fn admin_set_icp_ledger_canister(canister_id: Principal) {
if !Inspect::inspect_is_admin(utils::caller()) {
ic_cdk::trap("Unauthorized");
}
Configuration::set_icp_ledger_canister(canister_id);
}
}

impl Icrc1 for FlyCanister {
Expand Down Expand Up @@ -498,6 +516,11 @@ mod test {

// swap account
assert_eq!(Configuration::get_swap_account(), bob_account());

// check canisters
assert_eq!(Configuration::get_xrc_canister(), caller());
assert_eq!(Configuration::get_ckbtc_canister(), caller());
assert_eq!(Configuration::get_icp_ledger_canister(), caller());
}

#[tokio::test]
Expand Down Expand Up @@ -732,6 +755,22 @@ mod test {
assert_eq!(Configuration::get_xrc_canister(), canister_id);
}

#[test]
fn test_should_set_ckbtc_canister() {
init_canister();
let canister_id = Principal::from_str("aaaaa-aa").unwrap();
FlyCanister::admin_set_ckbtc_canister(canister_id);
assert_eq!(Configuration::get_ckbtc_canister(), canister_id);
}

#[test]
fn test_should_set_icp_ledger_canister() {
init_canister();
let canister_id = Principal::from_str("aaaaa-aa").unwrap();
FlyCanister::admin_set_icp_ledger_canister(canister_id);
assert_eq!(Configuration::get_icp_ledger_canister(), canister_id);
}

#[tokio::test]
async fn test_should_get_balance_of() {
init_canister();
Expand Down Expand Up @@ -1106,6 +1145,8 @@ mod test {
(caller_account(), fly_to_picofly(100_000)),
],
xrc_canister: caller(),
ckbtc_canister: caller(),
icp_ledger_canister: caller(),
};
FlyCanister::init(data);
}
Expand Down
Loading

0 comments on commit fd5b4b4

Please sign in to comment.