So at this point, we have a single user that owns all the tokens for the contract. However, it's not really a useful token unless you can transfer them to other people...
Let's do that!
The transfer
function does exactly what you might expect: it allows the user calling the contract
to transfer some funds they own to another user.
You will notice in our template code there is a public function transfer
and an internal function
transfer_from_to
. We have done this because in the future, we will be reusing the logic for a
token transfer when we enable third party allowances and spending on-behalf-of.
fn transfer_from_to(&mut self, from: AccountId, to: AccountId, value: Balance) -> bool {
/* --snip-- */
}
The transfer_from_to
function will be built without any authorization checks. Because it is an
internal function we fully control when it gets called. However, it will have all logical checks
around managing the balances between accounts.
Really we just need to check for one thing: make sure that the from
account has enough funds to
send to the to
account, something likes the following:
if balance_from < value {
return false
}
Remember that the transfer
function and other public functions return a bool to indicate success.
If the from
account does not have enough balance to satisfy the transfer, we will exit early and
return false, not making any changes to the contract state. Our transfer_from_to
will simply
forward the "success" bool
up to the function that calls it.
#[ink(message)]
pub fn transfer(&mut self, to: AccountId, value: Balance) -> bool {
/* --snip-- */
}
Finally, the transfer
function will simply call into the transfer_from_to
with the from
parameter automatically set to the self.env().caller()
. This is our "authorization check" since
the contract caller is always authorized to move their own funds.
There really is not much to say about the simple math executed within a token transfer.
- First we get the current balance of both the
from
andto
account, making sure to use ourbalance_of_or_zero()
getter. - Then we make the logic check mentioned above to ensure the
from
balance has enough funds to sendvalue
. - Finally, we subtract that
value
from thefrom
balance and add it to theto
balance and insert those new values back in.
Follow the ACTION
s in the template code to build your transfer function.
Remember to run cargo +nightly test
to test your work.