Skip to content

Commit

Permalink
fix: removed icp_account call on init
Browse files Browse the repository at this point in the history
  • Loading branch information
veeso committed Dec 18, 2023
1 parent edbc820 commit b0e72da
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 54 deletions.
5 changes: 1 addition & 4 deletions src/declarations/fly/fly.did.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ export interface FlyInitData {
'admins' : Array<Principal>,
'total_supply' : bigint,
}
export interface LiquidityPoolAccounts {
'icp' : Uint8Array | number[],
'ckbtc' : Account,
}
export interface LiquidityPoolAccounts { 'icp' : Account, 'ckbtc' : Account }
export interface LiquidityPoolBalance { 'icp' : bigint, 'ckbtc' : bigint }
export type MetadataValue = { 'Int' : bigint } |
{ 'Nat' : bigint } |
Expand Down
2 changes: 1 addition & 1 deletion src/declarations/fly/fly.did.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export const idlFactory = ({ IDL }) => {
});
const Result_5 = IDL.Variant({ 'Ok' : IDL.Nat, 'Err' : TransferFromError });
const LiquidityPoolAccounts = IDL.Record({
'icp' : IDL.Vec(IDL.Nat8),
'icp' : Account,
'ckbtc' : Account,
});
const LiquidityPoolBalance = IDL.Record({
Expand Down
2 changes: 1 addition & 1 deletion src/did/src/fly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ pub struct LiquidityPoolAccounts {
/// The account that holds the pool for the CKBTC token.
pub ckbtc: Account,
/// The account that holds the pool for the ICP tokens.
pub icp: Vec<u8>,
pub icp: Account,
}

/// The balance of the liquidity pool
Expand Down
2 changes: 1 addition & 1 deletion src/fly/fly.did
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type FlyInitData = record {
admins : vec principal;
total_supply : nat;
};
type LiquidityPoolAccounts = record { icp : vec nat8; ckbtc : Account };
type LiquidityPoolAccounts = record { icp : Account; ckbtc : Account };
type LiquidityPoolBalance = record { icp : nat; ckbtc : nat };
type MetadataValue = variant {
Int : int;
Expand Down
28 changes: 20 additions & 8 deletions src/fly/src/app/liquidity_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ use crate::utils;

thread_local! {
/// ICP ledger account
static ICP_ACCOUNT: RefCell<StableCell<Vec<u8>, VirtualMemory<DefaultMemoryImpl>>> =
static ICP_ACCOUNT: RefCell<StableCell<StorableAccount, VirtualMemory<DefaultMemoryImpl>>> =
RefCell::new(StableCell::new(MEMORY_MANAGER.with(|mm| mm.get(LIQUIDITY_POOL_ACCOUNT_MEMORY_ID)),
vec![]).unwrap()
Account {
owner: Principal::anonymous(),
subaccount: None,
}.into()).unwrap()
);

/// Pool map is an association between a contract-id and the account which holds the pool for that contract.
Expand Down Expand Up @@ -60,25 +63,32 @@ impl LiquidityPool {
)
.unwrap();
});
// get account from ICP ledger
let icp_account = IcpLedger::account_identifier(utils::id(), None).await;
// generate CkBTC account
ICP_ACCOUNT.with_borrow_mut(|account| {
account.set(icp_account).unwrap();
account
.set(
Account {
owner: utils::id(),
subaccount: None,
}
.into(),
)
.unwrap();
});
}

/// Get liquidity pool accounts
pub fn accounts() -> LiquidityPoolAccounts {
LiquidityPoolAccounts {
icp: ICP_ACCOUNT.with_borrow(|account| account.get().clone()),
icp: ICP_ACCOUNT.with_borrow(|account| account.get().clone()).0,
ckbtc: CKBTC_ACCOUNT.with_borrow(|account| account.get().clone()).0,
}
}

/// Get liquidity pool balance
pub async fn balance() -> FlyResult<LiquidityPoolBalance> {
let accounts = Self::accounts();
let icp = IcpLedger::account_balance(accounts.icp).await?;
let icp = IcpLedger::icrc1_balance_of(accounts.icp).await?;
let ckbtc = CkBtc::icrc1_balance_of(accounts.ckbtc).await?;

Ok(LiquidityPoolBalance { icp, ckbtc })
Expand Down Expand Up @@ -181,13 +191,15 @@ mod test {
async fn test_should_init_and_get_accounts() {
LiquidityPool::init().await;
let account = LiquidityPool::accounts();
assert_eq!(account.ckbtc.owner, utils::id());
assert_eq!(account.icp.owner, utils::id());
assert_eq!(
account.ckbtc,
CKBTC_ACCOUNT.with_borrow(|account| account.get().clone()).0
);
assert_eq!(
account.icp,
ICP_ACCOUNT.with_borrow(|account| account.get().clone())
ICP_ACCOUNT.with_borrow(|account| account.get().clone()).0
);
}

Expand Down
47 changes: 8 additions & 39 deletions src/fly/src/app/liquidity_pool/icp_ledger.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use candid::Nat;
#[cfg(target_arch = "wasm32")]
use candid::CandidType;
use candid::{Nat, Principal};
use candid::{CandidType, Principal};
use did::fly::FlyResult;
#[cfg(target_arch = "wasm32")]
use icrc::icrc1;
Expand All @@ -13,52 +13,21 @@ use crate::Account;
pub struct IcpLedger;

impl IcpLedger {
/// Get account identifier
#[allow(unused_variables)]
pub async fn account_identifier(id: Principal, subaccount: Option<Vec<u8>>) -> Vec<u8> {
#[cfg(not(target_arch = "wasm32"))]
{
vec![
0x44, 0x49, 0x44, 0x4c, 0x01, 0x6d, 0x7b, 0x01, 0x00, 0x20, 0x21, 0xa9, 0x95, 0x49,
0xe7, 0x92, 0x90, 0x7c, 0x5e, 0x27, 0x5e, 0x54, 0x51, 0x06, 0x8d, 0x4d, 0xdf, 0x4d,
0x43, 0xee, 0x8d, 0xca, 0xb4, 0x87, 0x56, 0x23, 0x1a, 0x8f, 0xb7, 0x71, 0x31, 0x23,
]
}
#[cfg(target_arch = "wasm32")]
{
let result: (Vec<u8>,) = ic_cdk::call(
Self::icp_ledger_canister(),
"account_identifier",
(AccountIdentifierRequest {
owner: id,
subaccount,
},),
)
.await
.unwrap();

result.0
}
}

/// Get account balance
#[allow(unused_variables)]
pub async fn account_balance(account: Vec<u8>) -> FlyResult<Nat> {
pub async fn icrc1_balance_of(account: Account) -> FlyResult<Nat> {
#[cfg(not(target_arch = "wasm32"))]
{
Ok(1_216_794_022.into())
}
#[cfg(target_arch = "wasm32")]
{
let result: (AccountBalanceResponse,) = ic_cdk::call(
Self::icp_ledger_canister(),
"account_balance",
(AccountBalanceRequest { account },),
)
.await
.map_err(|(code, err)| did::fly::FlyError::CanisterCall(code, err))?;
let result: (Nat,) =
ic_cdk::call(Self::icp_ledger_canister(), "icrc1_balance_of", (account,))
.await
.map_err(|(code, err)| did::fly::FlyError::CanisterCall(code, err))?;

Ok(result.0.e8s)
Ok(result.0)
}
}

Expand Down

0 comments on commit b0e72da

Please sign in to comment.