Last active
November 14, 2016 18:48
-
-
Save killerstorm/7684a469b49dab59ba4449ceb2d0f48f to your computer and use it in GitHub Desktop.
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
;setup environment: module is submitted with keyset definition ... | |
(env-data { "keyset": { "keys": ["ABCD"] , "pred": "keys-all" } }) | |
;... and is signed by ABCD key | |
(env-keys "ABCD") | |
;define keyset to guard module | |
(define-keyset 'module-keyset (read-keyset "keyset")) | |
;define smart-contract code | |
(module payments 'module-keyset | |
(defun create-account (id initial-balance) | |
"Create a new account for ID with INITIAL-BALANCE funds" | |
(insert 'payments-table id { "balance": initial-balance })) | |
(defun get-balance (id) (read 'payments-table id 'balance)) | |
(defun pay-tax (from transfer-amount) | |
(let ((to "IRS") | |
(amount (* transfer-amount 0.10))) | |
(with-read 'payments-table from { "balance":= from-bal } | |
(with-read 'payments-table to { "balance":= to-bal } | |
(enforce (>= from-bal amount) "Insufficient Funds") | |
(update 'payments-table from | |
{ "balance": (- from-bal amount) }) | |
(update 'payments-table to | |
{ "balance": (+ to-bal amount) }) | |
(format "{} paid {} {}" from to amount))))) | |
(defun pay (from to amount) | |
(with-read 'payments-table from { "balance":= from-bal } | |
(with-read 'payments-table to { "balance":= to-bal } | |
(enforce (>= from-bal amount) "Insufficient Funds") | |
(pay-tax from amount) | |
(update 'payments-table from | |
{ "balance": (- from-bal amount) }) | |
(update 'payments-table to | |
{ "balance": (+ to-bal amount) }) | |
(format "{} paid {} {}" from to amount)))) | |
) | |
;define table and "guard" with module | |
(create-table 'payments-table 'payments) | |
;create accounts | |
(create-account "Sarah" 100.25) | |
(create-account "James" 250.0) | |
(create-account "IRS" 0.0) | |
(pay "Sarah" "James" 25.0) | |
(format "{} {} {}" (get-balance "Sarah") (get-balance "James") (get-balance "IRS")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment