Skip to content

Instantly share code, notes, and snippets.

@yoshihiro503
Created March 13, 2020 08:37
Show Gist options
  • Save yoshihiro503/e909bd59abbc2d569a32280242d25065 to your computer and use it in GitHub Desktop.
Save yoshihiro503/e909bd59abbc2d569a32280242d25065 to your computer and use it in GitHub Desktop.
scaml translation of the example on page 217 of the book "ブロックチェーンアプリケーション開発の教科書"
open SCaml
type balances = (address, nat) map
type storage = balances
type parameter =
| Activate
| Send of {to_ : address; amount : nat}
let activate addr (balances : balances) =
Map.update addr (Some (Nat 10000)) balances
let send sender receiver (amount:nat) (balances : balances)=
match Map.get sender balances with
| None -> failwith ""
| Some (n : nat) ->
begin match isnat (n -^ amount) with
| None -> failwith ""
| Some n' ->
let receivers_balance = match Map.get receiver balances with
| None -> Nat 0
| Some m -> m
in
Map.update receiver (Some (receivers_balance +^ amount))
(Map.update sender (Some n') balances)
end
let main (p : parameter) (s : storage) : (operation list * storage) =
match p with
| Activate ->
(([]: operation list), activate (Global.get_sender ()) s)
| Send {to_; amount} ->
(([]: operation list), send (Global.get_sender ()) to_ amount s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment