use candid::{CandidType, Deserialize};
use crate::identity::CanisterId;
use super::CallError;
#[derive(CandidType, Deserialize, Debug, Clone)]
pub struct LedgerName {
name: String,
}
#[allow(unused)]
pub async fn name(canister_id: CanisterId) -> LedgerName {
let _call_result: Result<(LedgerName,), CallError> =
ic_cdk::call(canister_id, "name", ()).await;
_call_result.unwrap().0
}
#[allow(unused)]
pub async fn name_by(canister_id: CanisterId) -> String {
name(canister_id).await.name
}
#[derive(CandidType, Deserialize, Debug, Clone)]
pub struct LedgerSymbol {
symbol: String,
}
#[allow(unused)]
pub async fn symbol(canister_id: CanisterId) -> LedgerSymbol {
let _call_result: Result<(LedgerSymbol,), CallError> =
ic_cdk::call(canister_id, "symbol", ()).await;
_call_result.unwrap().0
}
#[allow(unused)]
pub async fn symbol_by(canister_id: CanisterId) -> String {
symbol(canister_id).await.symbol
}
#[derive(CandidType, Deserialize, Debug, Clone)]
pub struct LedgerDecimals {
decimals: u32,
}
#[allow(unused)]
pub async fn decimals(canister_id: CanisterId) -> LedgerDecimals {
let _call_result: Result<(LedgerDecimals,), CallError> =
ic_cdk::call(canister_id, "decimals", ()).await;
_call_result.unwrap().0
}
#[allow(unused)]
pub async fn decimals_by(canister_id: CanisterId) -> u32 {
decimals(canister_id).await.decimals
}
#[derive(CandidType, Deserialize, Debug, Clone)]
pub struct LedgerBinaryAccountBalanceArgs {
pub account: LedgerAccountIdentifier,
}
#[derive(CandidType, Deserialize, Debug, Clone)]
pub struct LedgerTokens {
pub e8s: u64, }
#[allow(unused)]
pub async fn account_balance(
canister_id: CanisterId,
args: LedgerBinaryAccountBalanceArgs,
) -> LedgerTokens {
let _call_result: Result<(LedgerTokens,), CallError> =
ic_cdk::call(canister_id, "account_balance", (args,)).await;
_call_result.unwrap().0
}
#[allow(unused)]
pub async fn account_balance_by(canister_id: CanisterId, account: LedgerAccountIdentifier) -> u64 {
account_balance(canister_id, LedgerBinaryAccountBalanceArgs { account })
.await
.e8s
}
#[derive(CandidType, Deserialize, Debug, Clone)]
pub struct LedgerTransferFeeArg {}
#[derive(CandidType, Deserialize, Debug, Clone)]
pub struct LedgerTransferFee {
pub transfer_fee: LedgerTokens,
}
#[allow(unused)]
pub async fn transfer_fee(
canister_id: CanisterId,
args: LedgerTransferFeeArg,
) -> LedgerTransferFee {
let _call_result: Result<(LedgerTransferFee,), CallError> =
ic_cdk::call(canister_id, "transfer_fee", (args,)).await;
_call_result.unwrap().0
}
#[allow(unused)]
pub async fn transfer_fee_by(canister_id: CanisterId) -> u64 {
transfer_fee(canister_id, LedgerTransferFeeArg {})
.await
.transfer_fee
.e8s
}
pub type LedgerMemo = u64; #[derive(CandidType, Deserialize, Debug, Clone)]
pub struct LedgerTimestamp {
pub timestamp_nanos: u64, }
pub type LedgerAccountIdentifier = Vec<u8>;
pub type LedgerSubaccount = Vec<u8>;
#[derive(CandidType, Deserialize, Debug, Clone)]
pub struct LedgerTransferArgs {
pub from_subaccount: Option<LedgerSubaccount>,
pub amount: LedgerTokens,
pub to: LedgerAccountIdentifier,
pub fee: LedgerTokens,
pub memo: LedgerMemo,
pub created_at_time: Option<LedgerTimestamp>,
}
pub type LedgerBlockIndex = u64;
#[derive(CandidType, Deserialize, Debug, Clone)]
pub enum LedgerTransferError {
BadFee { expected_fee: LedgerTokens },
InsufficientFunds { balance: LedgerTokens },
TxTooOld { allowed_window_nanos: u64 },
TxCreatedInFuture,
TxDuplicate { duplicate_of: LedgerBlockIndex },
}
pub type LedgerTransferResult = Result<LedgerBlockIndex, LedgerTransferError>;
#[allow(unused)]
pub async fn transfer(canister_id: CanisterId, args: LedgerTransferArgs) -> LedgerTransferResult {
let _call_result: Result<(LedgerTransferResult,), CallError> =
ic_cdk::call(canister_id, "transfer", (args,)).await;
_call_result.unwrap().0
}
#[allow(unused)]
pub async fn transfer_by(
canister_id: CanisterId,
amount: u64,
to: LedgerAccountIdentifier,
fee: u64,
memo: u64,
) -> LedgerTransferResult {
transfer(
canister_id,
LedgerTransferArgs {
from_subaccount: None,
amount: LedgerTokens { e8s: amount },
to,
fee: LedgerTokens { e8s: fee },
memo,
created_at_time: None,
},
)
.await
}