pub use crate::{
psp22::*,
traits::psp22::extensions::wrapper::*,
};
use brush::{
declare_storage_trait,
traits::{
AccountId,
Balance,
},
};
pub use derive::PSP22WrapperStorage;
use ink_env::CallFlags;
use ink_prelude::vec::Vec;
pub const STORAGE_KEY: [u8; 32] = ink_lang::blake2x256!("brush::PSP22WrapperData");
#[derive(Default, Debug)]
#[brush::storage(STORAGE_KEY)]
pub struct PSP22WrapperData {
pub underlying: AccountId,
pub _reserved: Option<()>,
}
declare_storage_trait!(PSP22WrapperStorage, PSP22WrapperData);
impl<T: PSP22 + PSP22WrapperStorage + PSP22Internal> PSP22Wrapper for T {
default fn deposit_for(&mut self, account: AccountId, amount: Balance) -> Result<(), PSP22Error> {
self._deposit(amount)?;
self._mint(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 PSP22WrapperInternal {
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: PSP22 + PSP22Internal + PSP22WrapperStorage> PSP22WrapperInternal for T {
default fn _recover(&mut self, account: AccountId) -> Result<Balance, PSP22Error> {
let value = self._underlying_balance() - self.total_supply();
self._mint(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))
.fire()
.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))
.fire()
.unwrap()
}
default fn _underlying_balance(&mut self) -> Balance {
self._underlying().balance_of(Self::env().account_id())
}
default fn _init(&mut self, underlying: AccountId) {
PSP22WrapperStorage::get_mut(self).underlying = underlying;
}
default fn _underlying(&mut self) -> &mut PSP22Ref {
&mut PSP22WrapperStorage::get_mut(self).underlying
}
}