Skip to content

Commit

Permalink
feat: added xrc canister as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Jan 20, 2024
1 parent 6330f45 commit c2608b3
Show file tree
Hide file tree
Showing 25 changed files with 167 additions and 16 deletions.
Binary file added .dfx/local/canisters/xrc/xrc.wasm
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ target/
**/.DS_Store

# dfx temp files
!.dfx/local/canisters/xrc/xrc.wasm
.dfx/

integration-tests/pocket-ic

1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions scripts/deploy_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ deploy_fly() {
total_supply = $TOTAL_SUPPLY;
initial_balances = $INITIAL_BALANCES;
minting_account = $MINTING_ACCOUNT;
xrc_canister = principal \"uf6dk-hyaaa-aaaaq-qaaaq-cai\";
})"

dfx deploy --mode=$INSTALL_MODE --yes --network="$NETWORK" --argument="$fly_init_args" fly
Expand All @@ -63,6 +64,7 @@ deploy_marketplace() {
marketplace_init_args="(record {
deferred_canister = principal \"$DEFERRED_PRINCIPAL\";
fly_canister = principal \"$FLY_PRINCIPAL\";
xrc_canister = principal \"uf6dk-hyaaa-aaaaq-qaaaq-cai\";
admins = vec { $(for admin in $ADMINS; do echo "principal \"$admin\";"; done) };
})"

Expand Down
2 changes: 2 additions & 0 deletions src/declarations/fly/fly.did.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface FlyInitData {
'minting_account' : Account,
'initial_balances' : Array<[Account, bigint]>,
'swap_account' : Account,
'xrc_canister' : Principal,
'marketplace_canister' : Principal,
'admins' : Array<Principal>,
'total_supply' : bigint,
Expand Down Expand Up @@ -146,6 +147,7 @@ export interface _SERVICE {
'admin_remove_role' : ActorMethod<[Principal, Role], Result>,
'admin_set_role' : ActorMethod<[Principal, Role], undefined>,
'admin_set_swap_account' : ActorMethod<[Account], undefined>,
'admin_set_xrc_canister' : ActorMethod<[Principal], undefined>,
'get_contract_reward' : ActorMethod<[bigint, bigint], Result_1>,
'get_transaction' : ActorMethod<[bigint], Result_2>,
'icrc1_balance_of' : ActorMethod<[Account], bigint>,
Expand Down
3 changes: 3 additions & 0 deletions src/declarations/fly/fly.did.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const idlFactory = ({ IDL }) => {
'minting_account' : Account,
'initial_balances' : IDL.Vec(IDL.Tuple(Account, IDL.Nat)),
'swap_account' : Account,
'xrc_canister' : IDL.Principal,
'marketplace_canister' : IDL.Principal,
'admins' : IDL.Vec(IDL.Principal),
'total_supply' : IDL.Nat,
Expand Down Expand Up @@ -174,6 +175,7 @@ export const idlFactory = ({ IDL }) => {
'admin_remove_role' : IDL.Func([IDL.Principal, Role], [Result], []),
'admin_set_role' : IDL.Func([IDL.Principal, Role], [], []),
'admin_set_swap_account' : IDL.Func([Account], [], []),
'admin_set_xrc_canister' : IDL.Func([IDL.Principal], [], []),
'get_contract_reward' : IDL.Func([IDL.Nat, IDL.Nat64], [Result_1], []),
'get_transaction' : IDL.Func([IDL.Nat64], [Result_2], ['query']),
'icrc1_balance_of' : IDL.Func([Account], [IDL.Nat], ['query']),
Expand Down Expand Up @@ -216,6 +218,7 @@ export const init = ({ IDL }) => {
'minting_account' : Account,
'initial_balances' : IDL.Vec(IDL.Tuple(Account, IDL.Nat)),
'swap_account' : Account,
'xrc_canister' : IDL.Principal,
'marketplace_canister' : IDL.Principal,
'admins' : IDL.Vec(IDL.Principal),
'total_supply' : IDL.Nat,
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 @@ -48,6 +48,7 @@ export type MarketplaceError = { 'Buy' : BuyError } |
export interface MarketplaceInitData {
'deferred_canister' : Principal,
'fly_canister' : Principal,
'xrc_canister' : Principal,
'admins' : Array<Principal>,
}
export type NftError = { 'UnauthorizedOperator' : null } |
Expand Down Expand Up @@ -118,6 +119,7 @@ export interface _SERVICE {
'admin_set_deferred_canister' : ActorMethod<[Principal], undefined>,
'admin_set_fly_canister' : ActorMethod<[Principal], Result>,
'admin_set_interest_rate_for_buyer' : ActorMethod<[number], undefined>,
'admin_set_xrc_canister' : ActorMethod<[Principal], undefined>,
'buy_token' : ActorMethod<[bigint, [] | [Uint8Array | number[]]], Result>,
'get_token_price_icp' : ActorMethod<[bigint], Result_1>,
}
3 changes: 3 additions & 0 deletions src/declarations/marketplace/marketplace.did.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const idlFactory = ({ IDL }) => {
const MarketplaceInitData = IDL.Record({
'deferred_canister' : IDL.Principal,
'fly_canister' : IDL.Principal,
'xrc_canister' : IDL.Principal,
'admins' : IDL.Vec(IDL.Principal),
});
const BuyError = IDL.Variant({
Expand Down Expand Up @@ -144,6 +145,7 @@ export const idlFactory = ({ IDL }) => {
'admin_set_deferred_canister' : IDL.Func([IDL.Principal], [], []),
'admin_set_fly_canister' : IDL.Func([IDL.Principal], [Result], []),
'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], []),
'get_token_price_icp' : IDL.Func([IDL.Nat], [Result_1], []),
});
Expand All @@ -152,6 +154,7 @@ export const init = ({ IDL }) => {
const MarketplaceInitData = IDL.Record({
'deferred_canister' : IDL.Principal,
'fly_canister' : IDL.Principal,
'xrc_canister' : IDL.Principal,
'admins' : IDL.Vec(IDL.Principal),
});
return [MarketplaceInitData];
Expand Down
2 changes: 2 additions & 0 deletions src/did/src/fly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ pub struct FlyInitData {
pub swap_account: Account,
/// Minting account, the account that can mint new tokens and burn them
pub minting_account: Account,
/// XRC canister
pub xrc_canister: Principal,
}

/// Fly user roles. Defines permissions
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,
/// XRC canister
pub xrc_canister: Principal,
}

/// Marketplace user roles. Defines permissions
Expand Down
2 changes: 2 additions & 0 deletions src/fly/fly.did
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type FlyInitData = record {
minting_account : Account;
initial_balances : vec record { Account; nat };
swap_account : Account;
xrc_canister : principal;
marketplace_canister : principal;
admins : vec principal;
total_supply : nat;
Expand Down Expand Up @@ -133,6 +134,7 @@ service : (FlyInitData) -> {
admin_remove_role : (principal, Role) -> (Result);
admin_set_role : (principal, Role) -> ();
admin_set_swap_account : (Account) -> ();
admin_set_xrc_canister : (principal) -> ();
get_contract_reward : (nat, nat64) -> (Result_1);
get_transaction : (nat64) -> (Result_2) query;
icrc1_balance_of : (Account) -> (nat) query;
Expand Down
24 changes: 23 additions & 1 deletion src/fly/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ impl FlyCanister {
Configuration::set_minting_account(data.minting_account);
// set swap account
Configuration::set_swap_account(data.swap_account);
// set xrc canister
Configuration::set_xrc_canister(data.xrc_canister);
// init liquidity pool
LiquidityPool::init();
// set roles
Expand All @@ -71,7 +73,8 @@ impl FlyCanister {
fn set_timers() {
#[cfg(target_family = "wasm")]
async fn swap_icp_to_btc_timer() {
let _ = LiquidityPool::swap_icp_to_btc().await;
let xrc_principal = Configuration::set_xrc_canister();
let _ = LiquidityPool::swap_icp_to_btc(xrc_principal).await;
}

#[cfg(target_family = "wasm")]
Expand Down Expand Up @@ -187,6 +190,14 @@ impl FlyCanister {
}
Configuration::set_swap_account(account);
}

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

impl Icrc1 for FlyCanister {
Expand Down Expand Up @@ -438,6 +449,8 @@ impl Icrc2 for FlyCanister {
#[cfg(test)]
mod test {

use std::str::FromStr as _;

use icrc::icrc1::transfer::TransferArg;
use icrc::icrc2::allowance::{Allowance, AllowanceArgs};
use icrc::icrc2::approve::ApproveArgs;
Expand Down Expand Up @@ -711,6 +724,14 @@ mod test {
);
}

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

#[tokio::test]
async fn test_should_get_balance_of() {
init_canister();
Expand Down Expand Up @@ -1084,6 +1105,7 @@ mod test {
(bob_account(), fly_to_picofly(50_000)),
(caller_account(), fly_to_picofly(100_000)),
],
xrc_canister: caller(),
};
FlyCanister::init(data);
}
Expand Down
36 changes: 34 additions & 2 deletions src/fly/src/app/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
use std::cell::RefCell;

use candid::Principal;
use did::StorableAccount;
use did::{StorableAccount, StorablePrincipal};
use ic_stable_structures::memory_manager::VirtualMemory;
use ic_stable_structures::{DefaultMemoryImpl, StableCell};
use icrc::icrc1::account::Account;

use crate::app::memory::{MEMORY_MANAGER, MINTING_ACCOUNT_MEMORY_ID, SWAP_ACCOUNT_MEMORY_ID};
use crate::app::memory::{
MEMORY_MANAGER, MINTING_ACCOUNT_MEMORY_ID, SWAP_ACCOUNT_MEMORY_ID, XRC_CANISTER_MEMORY_ID,
};

thread_local! {
/// Minting account
Expand All @@ -30,6 +32,12 @@ thread_local! {
subaccount: None
}.into()).unwrap()
);

/// Swap account
static XRC_CANISTER: RefCell<StableCell<StorablePrincipal, VirtualMemory<DefaultMemoryImpl>>> =
RefCell::new(StableCell::new(MEMORY_MANAGER.with(|mm| mm.get(XRC_CANISTER_MEMORY_ID)),
Principal::anonymous().into()).unwrap()
);
}

/// canister configuration
Expand Down Expand Up @@ -59,11 +67,26 @@ impl Configuration {
pub fn get_swap_account() -> Account {
SWAP_ACCOUNT.with(|sa| sa.borrow().get().0)
}

/// Set xrc canister address
pub fn set_xrc_canister(canister_id: Principal) {
XRC_CANISTER.with_borrow_mut(|cell| {
cell.set(canister_id.into()).unwrap();
});
}

/// Get xrc canister address
#[allow(dead_code)]
pub fn get_xrc_canister() -> Principal {
XRC_CANISTER.with(|xrc| xrc.borrow().get().0)
}
}

#[cfg(test)]
mod test {

use std::str::FromStr as _;

use pretty_assertions::assert_eq;

use super::*;
Expand All @@ -82,4 +105,13 @@ mod test {
Configuration::set_swap_account(swap_account);
assert_eq!(Configuration::get_swap_account(), swap_account);
}

#[test]
fn test_should_set_xrc_canister() {
let principal =
Principal::from_str("bs5l3-6b3zu-dpqyj-p2x4a-jyg4k-goneb-afof2-y5d62-skt67-3756q-dqe")
.unwrap();
Configuration::set_xrc_canister(principal);
assert_eq!(Configuration::get_xrc_canister(), principal);
}
}
4 changes: 2 additions & 2 deletions src/fly/src/app/liquidity_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ impl LiquidityPool {

/// Swap the current liquidity pool in ICP to BTC using the swap account
#[allow(dead_code)]
pub async fn swap_icp_to_btc() -> FlyResult<()> {
pub async fn swap_icp_to_btc(xrc_principal: Principal) -> FlyResult<()> {
// get the current exchange rate ICP/BTC
let rate = Xrc::get_icp_to_btc_rate().await?;
let rate = Xrc::get_icp_to_btc_rate(xrc_principal).await?;
// get current balance of swap account of CKBTC
let swap_account_balance =
CkBtc::icrc1_balance_of(Configuration::get_swap_account()).await?;
Expand Down
6 changes: 4 additions & 2 deletions src/fly/src/app/liquidity_pool/xrc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use candid::Principal;
#[cfg(target_arch = "wasm32")]
use did::fly::FlyError;
use did::fly::FlyResult;
Expand All @@ -10,14 +11,15 @@ impl Xrc {
/// Get the ICP to BTC rate
/// Then you can convert ICP to BTC with:
/// BTC = value * ExchangeRate
pub async fn get_icp_to_btc_rate() -> FlyResult<f64> {
#[allow(unused_variables)]
pub async fn get_icp_to_btc_rate(xrc_principal: Principal) -> FlyResult<f64> {
#[cfg(not(target_arch = "wasm32"))]
{
Ok(0.0002162)
}
#[cfg(target_arch = "wasm32")]
{
let xrc = XrcClient::default();
let xrc = XrcClient::new(xrc_principal);

// Base is the one on the left ICP/BTC
let base_asset = Asset {
Expand Down
1 change: 1 addition & 0 deletions src/fly/src/app/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub const SPEND_ALLOWANCE_MEMORY_ID: MemoryId = MemoryId::new(14);
pub const MINTING_ACCOUNT_MEMORY_ID: MemoryId = MemoryId::new(20);
pub const ROLES_MEMORY_ID: MemoryId = MemoryId::new(21);
pub const SWAP_ACCOUNT_MEMORY_ID: MemoryId = MemoryId::new(22);
pub const XRC_CANISTER_MEMORY_ID: MemoryId = MemoryId::new(23);

// Rewards
pub const RMC_MEMORY_ID: MemoryId = MemoryId::new(30);
Expand Down
6 changes: 6 additions & 0 deletions src/fly/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ pub fn admin_set_swap_account(account: Account) {
FlyCanister::admin_set_swap_account(account)
}

#[update]
#[candid_method(update)]
pub fn admin_set_xrc_canister(canister_id: Principal) {
FlyCanister::admin_set_xrc_canister(canister_id)
}

#[query]
#[candid_method(query)]
pub fn get_transaction(id: u64) -> FlyResult<Transaction> {
Expand Down
2 changes: 2 additions & 0 deletions src/marketplace/marketplace.did
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type MarketplaceError = variant {
type MarketplaceInitData = record {
deferred_canister : principal;
fly_canister : principal;
xrc_canister : principal;
admins : vec principal;
};
type NftError = variant {
Expand Down Expand Up @@ -128,6 +129,7 @@ service : (MarketplaceInitData) -> {
admin_set_deferred_canister : (principal) -> ();
admin_set_fly_canister : (principal) -> (Result);
admin_set_interest_rate_for_buyer : (float64) -> ();
admin_set_xrc_canister : (principal) -> ();
buy_token : (nat, opt vec nat8) -> (Result);
get_token_price_icp : (nat) -> (Result_1);
}
Loading

0 comments on commit c2608b3

Please sign in to comment.