Last active
April 20, 2020 18:58
-
-
Save mariano-aguero/932521db91f31f2ca67114b6c78670f6 to your computer and use it in GitHub Desktop.
fa12.ligo
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; | |
| const addressFromAccount: account = getAccount(addressFrom, store.accounts); | |
| if value > addressFromAccount.balance | |
| then failwith ("NotEnoughBalance"); | |
| else skip; | |
| // Update balances | |
| const newFromBalance :nat = abs(addressFromAccount.balance - value); // ensure non negative | |
| patch addressFromAccount with record [balance = newFromBalance]; | |
| store.accounts[addressFrom] := addressFromAccount; | |
| const addressToAccount: account = getAccount(addressTo, store.accounts); | |
| const newToBalance :nat = addressToAccount.balance + value; // ensure non negative | |
| patch addressToAccount with record [balance = newToBalance]; | |
| store.accounts[addressTo] := addressToAccount; | |
| // Update allowances | |
| case store.accounts[addressFrom] of | |
| | None -> skip | |
| | Some(account) -> block { | |
| case account.allowances[addressTo] of | |
| | None -> skip | |
| | Some(allowanceAmount) -> block { | |
| account.allowances[addressTo] := abs(allowanceAmount - value); | |
| store.accounts[addressFrom] := record balance = addressFromAccount.balance; allowances = account.allowances; end; | |
| } | |
| end; | |
| } | |
| end; | |
| } | |
| } with (emptyOps, store); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment