Skip to content

Instantly share code, notes, and snippets.

@rosalogia
Created September 2, 2022 00:52
Show Gist options
  • Save rosalogia/bae21c4ca32427210de9c6d5f00b362a to your computer and use it in GitHub Desktop.
Save rosalogia/bae21c4ca32427210de9c6d5f00b362a to your computer and use it in GitHub Desktop.
WIP experimental/educational implementation of TZIP-12 in CameLIGO
type token_id = nat
type amount = nat
type balance_key = {
owner : address;
token_id : token_id
}
type balance_map = (balance_key, amount) map
type storage = balance_map
type parameter =
| Transfer of address * address * token_id * amount
type return = operation list * storage
let get_balance (key, map : balance_key * balance_map): amount =
match Map.find_opt key map with
| Some balance -> balance
| None -> 0n
// Two entrypoints
let transfer (store, from, to, token_id, amount : storage * address * address * token_id * amount) : storage =
let from_key : balance_key = { owner = from ; token_id = token_id } in
let to_key : balance_key = { owner = to ; token_id = token_id } in
let from_balance : nat = get_balance (from_key, store) in
let to_balance : nat = get_balance (to_key, store) in
if from_balance < amount then
store // Nothing changes
else
let new_from_balance : nat = abs (from_balance - amount) in
let store = Map.update from_key (Some (new_from_balance)) store in
let store = Map.update to_key (Some (to_balance + amount)) store in
store
let main (action, store : parameter * storage) : return =
([] : operation list), // No operations
(match action with
| Transfer (from, to, token_id, amount) -> transfer (store, from, to, token_id, amount)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment