use std::sync::Arc;
use async_trait::async_trait;
use candid::{Nat, Principal};
use ex3_canister_client::canister_call_wrapper::call;
use crate::types::Subaccount;
use crate::{BlockResult, CanisterResult, OperationResult, QueryBlocksResult, DFT};
type CanisterId = Principal;
type BlockHeight = Nat;
#[derive(Debug)]
pub struct DFTClient {
pub token_id: Arc<CanisterId>,
}
#[allow(dead_code)]
impl DFTClient {
pub fn new(dft_id: CanisterId) -> Self {
Self {
token_id: Arc::new(dft_id),
}
}
}
#[async_trait]
impl DFT for DFTClient {
async fn block_by_height(&self, block_height: &BlockHeight) -> CanisterResult<BlockResult> {
let args = (block_height,);
call(&self.token_id, "blockByHeight", args).await
}
async fn blocks_by_query(
&self,
start: &BlockHeight,
count: &usize,
) -> CanisterResult<QueryBlocksResult> {
let args = (start, count);
call(&self.token_id, "blocksByQuery", args).await
}
async fn transfer_from(
&self,
spender_sub_account: Option<Subaccount>,
from: String,
to: String,
value: Nat,
created_at: Option<u64>,
) -> CanisterResult<OperationResult> {
let args = (spender_sub_account, from, to, value, created_at);
call(&self.token_id, "transferFrom", args).await
}
async fn transfer(
&self,
from_sub_account: Option<Subaccount>,
to: String,
value: Nat,
created_at: Option<u64>,
) -> CanisterResult<OperationResult> {
let args = (from_sub_account, to, value, created_at);
call(&self.token_id, "transfer", args).await
}
async fn balance_of(&self, token_holder: String) -> CanisterResult<Nat> {
let args = (token_holder,);
call(&self.token_id, "balanceOf", args).await
}
}