Skip to content

Instantly share code, notes, and snippets.

@johnchandlerburnham
Created June 22, 2021 13:42
Show Gist options
  • Save johnchandlerburnham/5d2703a5dd92f0e0db8225bb9e3121e8 to your computer and use it in GitHub Desktop.
Save johnchandlerburnham/5d2703a5dd92f0e0db8225bb9e3121e8 to your computer and use it in GitHub Desktop.
A sketch of smart contracts as dependent records in Yatima
package example_contract
open introit
open contract
lang contract
where
// pure datatypes are defined outside of contracts
data SimpleToken {
value: Integer,
mintedBy: Address,
}
// A contract is a heterogenous-map/dependent-record/module
contract myContract {
// State is "rows" in the module, values initialized at runtime
owner: Address,
storedString: Text,
messages: List Text,
tokens: Map Address SimpleToken,
// Methods are static rows, defined at compile time, with some kind of exception-handling
def requireOwner (errorMessage: Text) : Method Bool = do {
owner <- get "owner";
caller <- get "caller"; // Contract module implicitly has "caller" row, etc.
require (equals caller owner) errorMessage
}
def setOwner (newOwner: Address): MethodMut Unit = do {
requireOwner "Only the current owner can change ownership";
set "owner" newOwner;
}
def storeString (toStore: Text): MethodMut Text = do {
set "storedString" toStore;
log "Message stored {}" toStore;
}
def mintToken (address: Address) (amount: Integer): MethodMut Unit = do {
requireOwner "Only the current owner can mint tokens";
tokens <- get "tokens";
let token = case Map.get address tokens {
Some(token) => SimpleToken (add value.token amount) address
None => SimpleToken value address
};
set "tokens" (Map.set address token)
}
def getLastMessages: View Text = do {
map (take 3) (get "messages")
}
def concatMessages: View Text = do {
map (String.intercalate "\n") (get "messages")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment