Module 0xa6::coin_bag
use 0x1::ascii;
use 0x1::option;
use 0x1::type_name;
use 0x1::vector;
use 0x2::address;
use 0x2::bag;
use 0x2::balance;
use 0x2::hash;
use 0x2::tx_context;
Struct CoinBag
struct CoinBag has store
Fields
-
bag: bag::Bag
-
Constants
#[error]
const EKeyNotExist: vector<u8> = b"key not found";
#[error]
const ENotEnoughBalance: vector<u8> = b"not enough balance";
Function new
public(friend) fun new(ctx: &mut tx_context::TxContext): coin_bag::CoinBag
Implementation
public(package) fun new(ctx: &mut TxContext): CoinBag {
CoinBag {
bag: bag::new(ctx),
}
}
Function store_balance
public(friend) fun store_balance<T>(self: &mut coin_bag::CoinBag, balance: balance::Balance<T>)
Implementation
public(package) fun store_balance<T>(self: &mut CoinBag, balance: Balance<T>) {
let key = balance_key<T>();
if (self.bag.contains(key)) {
self
.bag
.borrow_mut<address, Balance<T>>(key)
.join(
balance,
);
} else {
self
.bag
.add(
key,
balance,
)
}
}
Function balance
public(friend) fun balance<T>(self: &mut coin_bag::CoinBag): option::Option<balance::Balance<T>>
Implementation
public(package) fun balance<T>(self: &mut CoinBag): Option<Balance<T>> {
let key = balance_key<T>();
if (self.bag.contains(key)) {
option::some(self.bag.remove<address, Balance<T>>(key))
} else {
option::none<Balance<T>>()
}
}
Function exact_balance
public(friend) fun exact_balance<T>(self: &mut coin_bag::CoinBag, amount: u64): balance::Balance<T>
Implementation
public(package) fun exact_balance<T>(
self: &mut CoinBag,
amount: u64,
): Balance<T> {
let key = balance_key<T>();
assert!(self.bag.contains(key), EKeyNotExist);
let balance = self.bag.borrow_mut<address, Balance<T>>(key);
assert!(balance.value() >= amount, ENotEnoughBalance);
balance.split(amount)
}
Function balance_amount
public(friend) fun balance_amount<T>(self: &coin_bag::CoinBag): u64
Implementation
public(package) fun balance_amount<T>(self: &CoinBag): u64 {
let key = balance_key<T>();
if (self.bag.contains(key)) {
self.bag.borrow<address, Balance<T>>(key).value()
} else {
0
}
}
Function store_estimate
public(friend) fun store_estimate<T>(self: &mut coin_bag::CoinBag, estimate: u64)
Implementation
public(package) fun store_estimate<T>(self: &mut CoinBag, estimate: u64) {
let key = estimate_key<T>();
if (self.bag.contains(key)) {
let previous = self.bag.borrow_mut<address, u64>(key);
*previous = *previous + estimate;
} else {
self
.bag
.add(
key,
estimate,
)
}
}
Function estimate
public(friend) fun estimate<T>(self: &mut coin_bag::CoinBag): u64
Implementation
public(package) fun estimate<T>(self: &mut CoinBag): u64 {
let key = estimate_key<T>();
if (self.bag.contains(key)) {
self.bag.remove<address, u64>(key)
} else {
0
}
}
Function estimate_amount
public(friend) fun estimate_amount<T>(self: &coin_bag::CoinBag): u64
Implementation
public(package) fun estimate_amount<T>(self: &CoinBag): u64 {
let key = estimate_key<T>();
if (self.bag.contains(key)) {
*self.bag.borrow<address, u64>(key)
} else {
0
}
}
Function destroy
public(friend) fun destroy(self: coin_bag::CoinBag)
Implementation
public(package) fun destroy(self: CoinBag) {
let CoinBag { bag } = self;
bag.destroy_empty();
}
Function balance_key
fun balance_key<T>(): address
Implementation
fun balance_key<T>(): address {
let mut data = vector[0];
data.append(type_name::get<T>().into_string().into_bytes());
address::from_bytes(keccak256(&data))
}
Function estimate_key
fun estimate_key<T>(): address
Implementation
fun estimate_key<T>(): address {
let mut data = vector[1];
data.append(type_name::get<T>().into_string().into_bytes());
address::from_bytes(keccak256(&data))
}