#![deny(missing_docs)]
#![doc = include_str!("../README.md")]
mod call;
mod genesis;
#[cfg(feature = "native")]
pub mod query;
mod token;
pub mod utils;
pub use call::CallMessage;
use sov_modules_api::{CallResponse, Error, ModuleInfo};
use sov_state::WorkingSet;
use token::Token;
pub use token::{Amount, Coins};
pub use utils::{get_genesis_token_address, get_token_address};
pub struct TokenConfig<C: sov_modules_api::Context> {
pub token_name: String,
pub address_and_balances: Vec<(C::Address, u64)>,
pub authorized_minters: Vec<C::Address>,
pub salt: u64,
}
pub struct BankConfig<C: sov_modules_api::Context> {
pub tokens: Vec<TokenConfig<C>>,
}
#[cfg_attr(feature = "native", derive(sov_modules_api::ModuleCallJsonSchema))]
#[derive(ModuleInfo, Clone)]
pub struct Bank<C: sov_modules_api::Context> {
#[address]
pub(crate) address: C::Address,
#[state]
pub(crate) tokens: sov_state::StateMap<C::Address, Token<C>>,
}
impl<C: sov_modules_api::Context> sov_modules_api::Module for Bank<C> {
type Context = C;
type Config = BankConfig<C>;
type CallMessage = call::CallMessage<C>;
fn genesis(
&self,
config: &Self::Config,
working_set: &mut WorkingSet<C::Storage>,
) -> Result<(), Error> {
Ok(self.init_module(config, working_set)?)
}
fn call(
&self,
msg: Self::CallMessage,
context: &Self::Context,
working_set: &mut WorkingSet<C::Storage>,
) -> Result<sov_modules_api::CallResponse, Error> {
match msg {
call::CallMessage::CreateToken {
salt,
token_name,
initial_balance,
minter_address,
authorized_minters,
} => {
self.create_token(
token_name,
salt,
initial_balance,
minter_address,
authorized_minters,
context,
working_set,
)?;
Ok(CallResponse::default())
}
call::CallMessage::Transfer { to, coins } => {
Ok(self.transfer(to, coins, context, working_set)?)
}
call::CallMessage::Burn { coins } => {
Ok(self.burn_from_eoa(coins, context, working_set)?)
}
call::CallMessage::Mint {
coins,
minter_address,
} => {
self.mint_from_eoa(&coins, &minter_address, context, working_set)?;
Ok(CallResponse::default())
}
call::CallMessage::Freeze { token_address } => {
Ok(self.freeze(token_address, context, working_set)?)
}
}
}
}