Skip to main content

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.

Repository


Author


Norms

Parameters

Contract is declared with two constant parameters:

  • initial_holder address owning the total supply of tokens
  • total_supply total supply of tokens
info

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)
with metadata ""

Storage

Total number of tokens. Note that this value is a constant since there is no mint or burn mechanism in this version.

Type


Related

ledger

Associates an address to:

  • tokens the number of tokens it owns
  • allowance that 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 by {
{ holder = caller; tokens = total_supply }
}

asset address nat map caller

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


from :

Sending tokens address


to :

Receiving tokens address


value :

Number of tokens to transfer

Fails with

("ASSET_NOT_FOUND", "ledger")

When address from is not found in ledger


NotEnoughBalance

When number of tokens owned by from is less than value


("ASSET_NOT_FOUND", "allowance")

When caller is not allowed by from to transfer tokens


NotEnoughAllowance

When caller's allowed transfer is less than value


Related

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)] });
}

entry address nat const [] ?: do_fail_if > put

Parameters


spender :

Approved address to transfer tokens from caller


value :

Number of tokens approved for transfer

Fails with

("UnsafeAllowanceChange", previous)

When 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)
}

getter address nat [] ?:

Parameters


owner :

Tokens owner


spender :

Approved address to transfer tokens from owner

Returns


Number of tokens spender can transfer on behalf of owner

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)
}

getter address nat [] ?:

Parameter


owner :

Tokens owner

Returns


Number of tokens owned by owner

Fails with

does not fail

getTotalSupply()

Getter of total_supply.

Code


getter getTotalSupply () : nat {
return total_supply
}

getter nat

Returns

Fails with

does not fail