pub use crate::{
psp22,
psp22::extensions::wrapper,
traits::psp22::{
extensions::wrapper::*,
*,
},
};
use ink::{
env::CallFlags,
prelude::vec::Vec,
};
use openbrush::traits::{
AccountId,
Balance,
Storage,
ZERO_ADDRESS,
};
pub use psp22::Internal as _;
pub use wrapper::Internal as _;
pub const STORAGE_KEY: u32 = openbrush::storage_unique_key!(Data);
#[derive(Debug)]
#[openbrush::upgradeable_storage(STORAGE_KEY)]
pub struct Data {
pub underlying: AccountId,
pub _reserved: Option<()>,
}
impl Default for Data {
fn default() -> Self {
Self {
underlying: ZERO_ADDRESS.into(),
_reserved: Default::default(),
}
}
}
impl<T: Storage<psp22::Data> + Storage<Data>> PSP22Wrapper for T {
default fn deposit_for(&mut self, account: AccountId, amount: Balance) -> Result<(), PSP22Error> {
self._deposit(amount)?;
self._mint_to(account, amount)
}
default fn withdraw_to(&mut self, account: AccountId, amount: Balance) -> Result<(), PSP22Error> {
self._burn_from(Self::env().caller(), amount)?;
self._withdraw(account, amount)
}
}
pub trait Internal {
fn _recover(&mut self, account: AccountId) -> Result<Balance, PSP22Error>;
fn _deposit(&mut self, amount: Balance) -> Result<(), PSP22Error>;
fn _withdraw(&mut self, account: AccountId, amount: Balance) -> Result<(), PSP22Error>;
fn _underlying_balance(&mut self) -> Balance;
fn _init(&mut self, underlying: AccountId);
fn _underlying(&mut self) -> &mut PSP22Ref;
}
impl<T: Storage<psp22::Data> + Storage<Data>> Internal for T {
default fn _recover(&mut self, account: AccountId) -> Result<Balance, PSP22Error> {
let value = self._underlying_balance() - self.total_supply();
self._mint_to(account, value)?;
Ok(value)
}
default fn _deposit(&mut self, amount: Balance) -> Result<(), PSP22Error> {
self._underlying()
.transfer_from_builder(Self::env().caller(), Self::env().account_id(), amount, Vec::<u8>::new())
.call_flags(CallFlags::default().set_allow_reentry(true))
.try_invoke()
.unwrap()
.unwrap()
}
default fn _withdraw(&mut self, account: AccountId, amount: Balance) -> Result<(), PSP22Error> {
self._underlying()
.transfer_builder(account, amount, Vec::<u8>::new())
.call_flags(CallFlags::default().set_allow_reentry(true))
.try_invoke()
.unwrap()
.unwrap()
}
default fn _underlying_balance(&mut self) -> Balance {
self._underlying().balance_of(Self::env().account_id())
}
default fn _init(&mut self, underlying: AccountId) {
self.data::<Data>().underlying = underlying;
}
default fn _underlying(&mut self) -> &mut PSP22Ref {
&mut self.data::<Data>().underlying
}
}