This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Big = require('big.js') | |
const BN = require('bn.js') | |
const MathBN = { | |
sub: (a, b) => { | |
const aBN = new Big(a || '0') | |
const bBN = new Big(b || '0') | |
return aBN.sub(bBN).toString(10) | |
}, | |
add: (a, b) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is an implementation of the FA1.2 specification in PascaLIGO | |
type amount is nat; | |
type account is record | |
balance : amount; | |
allowances: map(address, amount); | |
end | |
type action is |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(address :from, (address :to, nat :value)) %transfer | |
view (address :owner) nat %getBalance | |
view unit nat %getTotalSupply |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function natToTz(const val: nat): tez is val * 1tz | |
function natToMutez(const val: nat): tez is val * 1mutez | |
function tezToNatWithMutez(const val: tez): nat is val / 1mutez | |
function tezToNatWithTz(const val: tez): nat is val / 1tz | |
function natToInt(const val: nat): int is val + 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function main (const action : tokenAction ; const store : store) : return is | |
block { | |
if amount =/= 0tz then failwith ("This contract do not accept token amount"); | |
else skip; | |
} with case action of | |
GetAllowance(n) -> allowance(n.0, n.1, n.2, store) | |
| Transfer(n) -> transfer(n.0, n.1, n.2, store) | |
| Approve(n) -> approve(n.0, n.1, store) | |
| GetBalance(n) -> balanceOf(n.0, n.1, store) | |
| GetTotalSupply(n) -> totalSupply(n.1, store) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type tokenAction is | |
| GetAllowance of (address * address * contract(nat)) | |
| Transfer of (address * address * nat) | |
| Approve of (address * nat) | |
| GetBalance of (address * contract(nat)) | |
| GetTotalSupply of (unit * contract(nat)) | |
| Mint of (nat) | |
| Burn of (nat) | |
| Symbol of (unit * contract(string)) | |
| Name of (unit * contract(string)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type account is record | |
balance: nat; | |
allowances: map(address, nat); | |
end | |
type store is record | |
owners: set(address); | |
decimals: nat; // Added this property used in the erc20 ethereum specification | |
symbol: string; // Added this property used in the erc20 ethereum specification | |
name: string; // Added this property used in the erc20 ethereum specification |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mint (const value : nat ; var store : store) : return is | |
block { | |
// Fail if is not an owner | |
if not isOwner(sender, store) then failwith("You must be an owner of the contract to mint tokens"); | |
else block { | |
var ownerAccount: account := record | |
balance = 0n; | |
allowances = (map end : map(address, nat)); | |
end; | |
case store.accounts[sender] of |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function approve (const addressSpender : address; const value : nat; var store : store) : return is | |
block { | |
// If sender is the spender approving is not necessary | |
if sender = addressSpender then skip; | |
else block { | |
const senderAccount: account = getAccount(sender, store.accounts); | |
var allowed: nat := getAllowance(addressSpender, senderAccount.allowances); | |
// Changing allowance value from non-zero value to a non-zero value is forbidden to prevent the corresponding attack vector. | |
if allowed =/= 0n then failwith("UnsafeAllowanceChange"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function transfer (const addressFrom : address; const addressTo : address; const value : nat; var store : store) : return is | |
block { | |
if addressFrom = addressTo then skip; | |
else block { | |
case isAllowed(addressFrom, addressTo, value, store) of | |
| False -> block { | |
failwith ("NotEnoughAllowance"); | |
} | |
| True -> skip | |
end; |