ERC20

Introduction#

caution

It is strongly suggested to use the FA 1.2 norm for fungible token.

API#

Storage#

NameTypeDescription
totalnattotal number of unit tokens.
onetokennatnumber of units for one token.
ledgercollectionAssociation between token holder and number of tokens.
allowancecollectionAssociation between the pair owner and spender and the allowed amount.

Entrypoints#

NameParametersDescription
transferto, valueTransfers value tokens from caller to to.
approvespender, valueApproves spender to transfer value tokens from caller.
transferFromfrom, to, valueTransfers value tokens from from to to. It requires that caller have been allowed by from to transfer this amount to to.

Originate#

Deploy the contract from Archetype code below with the following Completium CLI example command:

completium-cli deploy erc20.arl --init '(1_000_000_000_000_000, 1_000_000, @tz1LLJ3nxbpGGMLmjzcp9sTMYui87tycG6nG)'

The command sets:

  • total variable to 10 millions
  • onetoken variable to 1 million
  • initialholder constant to tz1LLJ3nxbpGGMLmjzcp9sTMYui87tycG6nG

Code#

erc20.arl
archetype erc20(total : nat, onetoken: nat, const initialowner : address)
asset allowance identified by owner spender {
owner : address;
spender : address;
amount : nat;
}
asset ledger identified by holder {
holder : address;
tokens : nat = 0;
} initialized by {
{ holder = initialowner; tokens = total }
}
entry %transfer (%to : pkey<ledger>, value : nat) {
require {
d0 : ledger[caller].tokens >= value
}
effect {
ledger.addupdate(%to, { tokens += value });
ledger.update(caller, { tokens -= value })
}
}
entry approve(ispender : address, value : nat) {
require {
d1 : ledger[caller].tokens >= value;
}
effect {
allowance.addupdate((caller, ispender), { amount = value });
}
}
entry transferFrom(%from : address, %to : address, value : nat) {
require {
d3: allowance[(%from,caller)].amount >= value;
d4: ledger[%from].tokens >= value
}
effect {
(* update allowance *)
allowance.update((%from,caller), { amount -= value });
(* update token *)
ledger.addupdate(%to, { tokens += value });
ledger.update(%from, { tokens -= value });
}
}