Skip to content

Instantly share code, notes, and snippets.

@zigguratt
Last active January 17, 2018 08:24
Show Gist options
  • Save zigguratt/539c8acdcec50b3d69375d5cbc99741f to your computer and use it in GitHub Desktop.
Save zigguratt/539c8acdcec50b3d69375d5cbc99741f to your computer and use it in GitHub Desktop.
Macros for the structure contract
;;;; --------------------------------------------------------------------------
;;;; @title Macros for the structure contract.
;;;; @author Daniel Ellison <daniel@syrinx.net>
(seq
;; Memory layout.
(def '*scratch* 0x00)
(def '*funcid* 0x20)
;; Storage layout.
(def '*parameter* 0x00)
;; The single function hash for this contract.
(def '*get-constructor-parameter* 0x32cafedd)
;; Shifts the leftmost 4 bytes of a 32-byte number right by 28 bytes.
(def 'shift-right (input)
(div input (exp 2 224)))
;; Gets the function ID and stores it in memory for reference.
(def 'get-function-id
(mstore *funcid* (shift-right (calldataload 0x00))))
;; Takes a function hash and a sequence of code, compares that hash to the
;; hash supplied by the caller (which has been stored in *funcid*), and
;; executes the given code if they match.
(def 'function (function-hash code-body)
(when (= (mload *funcid*) function-hash)
code-body))
;; Modifier macro to prevent unintended payments.
(def 'unpayable
(when (callvalue)
(panic))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment