FA 1.2
Implements TZIP-7 norm for fungible token.
A fungible token is basically an association table between the token owner (the account address) and the amount of tokens (a natural integer). It is possible to allow another account to transfer tokens on the token owner's behalf.
Parameters
Contract is declared with two constant parameters:
initial_holderaddress owning the total supply of tokenstotal_supplytotal supply of tokensmetadata_coinhex encoded ipfs URI of token metadata
Note that in this template version of FA 1.2, the total supply of tokens is a contract invariant since there is no mint nor burn entrypoint.
Code
archetype fa1_2(const initial_holder : address, const total_supply : nat, const metadata_coin : bytes)
with metadata ""
Storage
ledger
Associates an address to:
tokensthe number of tokens it ownsallowancethat associates a spender address to the quantity it is allowed to spend
Code
asset ledger identified by holder to big_map {
holder : address;
tokens : nat = 0;
allowance : map<address, nat> = [];
} initialized with {
{ holder = caller; tokens = total_supply }
}
Type
Related
Entrypoints
transfer(from, to, value)
Transfers value tokens from from to to.
If the caller is not equal to from, then caller must have been allowed by from to transfer this amount to to
Approved amount is decreased by value if applicable.
Code
entry %transfer (%from : address, %to : address, value : nat) {
require {
r1 : ledger[%from].tokens >= value otherwise "NotEnoughBalance";
}
effect {
if caller <> %from then begin
const current = ledger[%from].allowance[caller] ? the : 0;
const new_value ?=
int_to_nat(current - value) : ("NotEnoughAllowance", ((value, current)));
ledger[%from].allowance.put(caller, new_value);
end;
ledger.update(%from, { tokens -= value });
ledger.add_update(%to, { tokens += value });
}
}
entry address nat require []. >= effect if caller <> begin const ?: ?= into_to_nat put update add_update
Parameters
Fails with
NotEnoughBalance
from is less than value("ASSET_NOT_FOUND", "allowance")
caller is not allowed by from to transfer tokensNotEnoughAllowance
caller's allowed transfer is less than valueRelated
approve(spender, value)
Approves spender to transfer value tokens owned by caller.
Code
entry approve(spender : address, value : nat) {
const previous = ledger[caller] ? (the.allowance[spender] ? the : 0) : 0;
do_fail_if(previous > 0 and value > 0, (("UnsafeAllowanceChange", previous)));
ledger.add_update(caller, { allowance += [(spender, value)] });
}
Parameters
Fails with
("UnsafeAllowanceChange", previous)
spender is already approved a non-zero amount of tokens (previous)Related
getAllowance(owner, spender)
Getter of the allowed value spender can transfer on behalf of owner.
Code
getter getAllowance (owner : address, spender : address) : nat {
return (ledger[owner] ? (the.allowance[spender] ? the : 0) : 0)
}
Parameters
Returns
Fails with
does not fail
getBalance(owner)
Getter of the number of tokens owned by owner.
Code
getter getBalance (owner : address) : nat {
return (ledger[owner] ? the.tokens : 0)
}
Parameter
Returns
Fails with
does not fail
getTotalSupply()
Returns
Fails with
does not fail