ink_sandbox/api/
balance_api.rs

1use crate::{
2    AccountIdFor,
3    OriginFor,
4    Sandbox,
5};
6use frame_support::{
7    sp_runtime::DispatchError,
8    traits::fungible::Mutate,
9};
10use pallet_revive::sp_runtime::traits::StaticLookup;
11
12type BalanceOf<R> = <R as pallet_balances::Config>::Balance;
13
14/// Balance API for the sandbox.
15pub trait BalanceAPI<T: Sandbox>
16where
17    T: Sandbox,
18    T::Runtime: pallet_balances::Config,
19{
20    /// Mint tokens to an account.
21    ///
22    /// # Arguments
23    ///
24    /// * `address` - The address of the account to add tokens to.
25    /// * `amount` - The number of tokens to add.
26    fn mint_into(
27        &mut self,
28        address: &AccountIdFor<T::Runtime>,
29        amount: BalanceOf<T::Runtime>,
30    ) -> Result<BalanceOf<T::Runtime>, DispatchError>;
31
32    /// Return the free balance of an account.
33    ///
34    /// # Arguments
35    ///
36    /// * `account` - The account id of the account to query.
37    fn free_balance(
38        &mut self,
39        account_id: &AccountIdFor<T::Runtime>,
40    ) -> BalanceOf<T::Runtime>;
41
42    fn transfer_allow_death(
43        &mut self,
44        origin: &OriginFor<T::Runtime>,
45        dest: &AccountIdFor<T::Runtime>,
46        value: BalanceOf<T::Runtime>,
47    ) -> Result<(), DispatchError>;
48}
49
50impl<T> BalanceAPI<T> for T
51where
52    T: Sandbox,
53    T::Runtime: pallet_balances::Config,
54{
55    fn mint_into(
56        &mut self,
57        address: &AccountIdFor<T::Runtime>,
58        amount: BalanceOf<T::Runtime>,
59    ) -> Result<BalanceOf<T::Runtime>, DispatchError> {
60        self.execute_with(|| {
61            pallet_balances::Pallet::<T::Runtime>::mint_into(address, amount)
62        })
63    }
64
65    fn free_balance(
66        &mut self,
67        account_id: &AccountIdFor<T::Runtime>,
68    ) -> BalanceOf<T::Runtime> {
69        self.execute_with(|| {
70            pallet_balances::Pallet::<T::Runtime>::free_balance(account_id)
71        })
72    }
73
74    fn transfer_allow_death(
75        &mut self,
76        origin: &OriginFor<T::Runtime>,
77        dest: &AccountIdFor<T::Runtime>,
78        value: BalanceOf<T::Runtime>,
79    ) -> Result<(), DispatchError> {
80        // Convert AccountId into the proper `Lookup::Source`
81        let dest =
82            <<T::Runtime as frame_system::Config>::Lookup as StaticLookup>::unlookup(
83                dest.clone(),
84            );
85
86        self.execute_with(|| {
87            pallet_balances::Pallet::<T::Runtime>::transfer_allow_death(
88                origin.clone(),
89                dest,
90                value,
91            )
92        })
93    }
94}
95
96#[cfg(test)]
97mod test {
98    use super::*;
99    use crate::DefaultSandbox;
100    #[test]
101    fn mint_works() {
102        let mut sandbox = DefaultSandbox::default();
103        let balance = sandbox.free_balance(&DefaultSandbox::default_actor());
104
105        sandbox
106            .mint_into(&DefaultSandbox::default_actor(), 100)
107            .unwrap();
108
109        assert_eq!(
110            sandbox.free_balance(&DefaultSandbox::default_actor()),
111            balance + 100
112        );
113    }
114}