64 lines
1.4 KiB
Rust
64 lines
1.4 KiB
Rust
//! 账户管理模块
|
|
|
|
use crate::address::StructuredAddress;
|
|
use crate::key_manager::KeyPair;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
/// 账户
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
pub struct Account {
|
|
/// 地址
|
|
pub address: StructuredAddress,
|
|
/// 密钥对(可选,离线模式可能没有)
|
|
#[serde(skip)]
|
|
pub keypair: Option<KeyPair>,
|
|
/// XTZH余额
|
|
pub xtzh_balance: u64,
|
|
/// XIC余额
|
|
pub xic_balance: u64,
|
|
/// Nonce
|
|
pub nonce: u64,
|
|
}
|
|
|
|
impl Account {
|
|
/// 创建新账户
|
|
pub fn new(address: StructuredAddress) -> Self {
|
|
Self {
|
|
address,
|
|
keypair: None,
|
|
xtzh_balance: 0,
|
|
xic_balance: 0,
|
|
nonce: 0,
|
|
}
|
|
}
|
|
|
|
/// 创建带密钥对的账户
|
|
pub fn with_keypair(address: StructuredAddress, keypair: KeyPair) -> Self {
|
|
Self {
|
|
address,
|
|
keypair: Some(keypair),
|
|
xtzh_balance: 0,
|
|
xic_balance: 0,
|
|
nonce: 0,
|
|
}
|
|
}
|
|
|
|
/// 更新余额
|
|
pub fn update_balance(&mut self, xtzh: u64, xic: u64) {
|
|
self.xtzh_balance = xtzh;
|
|
self.xic_balance = xic;
|
|
}
|
|
|
|
/// 更新nonce
|
|
pub fn update_nonce(&mut self, nonce: u64) {
|
|
self.nonce = nonce;
|
|
}
|
|
|
|
/// 获取下一个nonce
|
|
pub fn next_nonce(&mut self) -> u64 {
|
|
let nonce = self.nonce;
|
|
self.nonce += 1;
|
|
nonce
|
|
}
|
|
}
|