Wallet
The following example implements a wallet that utilizes the base asset.
ABI
The interface
contains a function which tracks the amount of the base asset received and a function to transfer the funds.
abi Wallet {
#[storage(read, write)]
fn receive();
#[storage(read, write)]
fn send(amount: u64, recipient: Identity);
}
Implementation
When receiving funds we assert that the wallet accepts the base asset and we track the amount sent. When transferring funds out of the wallet we assert that only the owner can perform the transfer.
use std::{
call_frames::msg_asset_id,
constants::BASE_ASSET_ID,
context::msg_amount,
asset::transfer,
};
storage {
balance: u64 = 0,
}
const OWNER = Address::from(0x8900c5bec4ca97d4febf9ceb4754a60d782abbf3cd815836c1872116f203f861);
impl Wallet for Contract {
#[storage(read, write)]
fn receive() {
assert(msg_asset_id() == BASE_ASSET_ID);
storage.balance.write(storage.balance.read() + msg_amount());
}
#[storage(read, write)]
fn send(amount: u64, recipient: Identity) {
assert(msg_sender().unwrap() == Identity::Address(OWNER));
storage.balance.write(storage.balance.read() - amount);
transfer(recipient, BASE_ASSET_ID, amount);
}
}