Last active
September 17, 2019 15:44
-
-
Save thealmarty/f4e027371fad8a7ec86de832b560109c to your computer and use it in GitHub Desktop.
A simplified token contract written in Liquidity, for deployment in the Tezos ledger.
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 = { | |
balance : nat; | |
allowances : (address, nat) map; | |
} | |
type storage = { | |
accounts : (address, account) big_map; | |
version : nat; | |
totalSupply : nat; | |
name : string; | |
symbol : string; | |
owner : address; | |
} | |
let get_account (a, (accounts : (address, account) big_map)) = | |
match Map.find a accounts with | |
| None -> { balance = 0p; allowances = Map [] } | |
| Some account -> account | |
let perform_transfer | |
(from, dest, tokens, storage) = | |
let accounts = storage.accounts in | |
let account_sender = get_account (from, accounts) in | |
let new_account_sender = match is_nat (account_sender.balance - tokens) with | |
| None -> | |
failwith ("Not enough tokens for transfer", account_sender.balance) | |
| Some b -> account_sender.balance <- b in | |
let accounts = Map.add from new_account_sender accounts in | |
let account_dest = get_account (dest, accounts) in | |
let new_account_dest = | |
account_dest.balance <- account_dest.balance + tokens in | |
let accounts = Map.add dest new_account_dest accounts in | |
[], storage.accounts <- accounts | |
let%entry transfer (dest, tokens) storage = | |
perform_transfer (Current.sender (), dest, tokens, storage) | |
let%entry multiTransfer list_of_transfers storage = | |
List.fold (fun ((dest, tokens), (_ops, storage)) -> | |
perform_transfer (Current.sender (), dest, tokens, storage) | |
) list_of_transfers ([], storage) | |
let%entry approve (spender, tokens) storage = | |
let account_sender = get_account (Current.sender (), storage.accounts) in | |
let account_sender = | |
account_sender.allowances <- | |
if tokens = 0p then | |
Map.remove spender account_sender.allowances | |
else | |
Map.add spender tokens account_sender.allowances in | |
let storage = storage.accounts <- | |
Map.add (Current.sender ()) account_sender storage.accounts in | |
[], storage | |
let%entry transferFrom (from, dest, tokens) storage = | |
let account_from = get_account (from, storage.accounts) in | |
let new_allowances_from = | |
match Map.find dest account_from.allowances with | |
| None -> failwith ("Not allowed to spend from", from) | |
| Some allowed -> | |
match is_nat (allowed - tokens) with | |
| None -> | |
failwith ("Not enough allowance for transfer", allowed) | |
| Some allowed -> | |
if allowed = 0p then | |
Map.remove dest account_from.allowances | |
else | |
Map.add dest allowed account_from.allowances in | |
let account_from = account_from.allowances <- new_allowances_from in | |
let storage = storage.accounts <- | |
Map.add from account_from storage.accounts in | |
perform_transfer (from, dest, tokens, storage) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment