Skip to content

Instantly share code, notes, and snippets.

@joelburget
Last active June 18, 2018 18:07
Show Gist options
  • Save joelburget/4fdcdd512c3b8f3ce88f60269b586127 to your computer and use it in GitHub Desktop.
Save joelburget/4fdcdd512c3b8f3ce88f60269b586127 to your computer and use it in GitHub Desktop.
(update accounts from { "balance": (- from-bal amount) })
(update accounts to { "balance": (+ to-bal amount) })))
;;
(update accounts "brian" { "balance": (- previous-balance 1000000) })
(update accounts "brian" { "balance": (+ previous-balance 1000000) })
(enforce-pact-version "2.4.1")
(define-keyset 'accounts-admin-keyset
(read-keyset "accounts-admin-keyset"))
(module accounts 'accounts-admin-keyset
"Demo module"
(defschema account
("account"
(invariant (>= balance 0)))
balance:integer
ks:keyset)
(deftable accounts:{account})
(defun transfer (from:string to:string amount:integer)
("Transfer money between accounts"
(properties [
(row-enforced 'accounts 'ks from)
(= (column-delta 'accounts 'balance) 0)
]))
(let ((from-bal (at 'balance (read accounts from)))
(from-ks (at 'ks (read accounts from)))
(to-bal (at 'balance (read accounts to))))
(enforce-keyset from-ks)
(enforce (> amount 0))
(enforce (>= from-bal amount) "Insufficient Funds")
(enforce (!= to from))
(update accounts from { "balance": (- from-bal amount) })
(update accounts to { "balance": (+ to-bal amount) }))))
(env-keys ["accountadmin" "user123" "user456"])
(env-data { "accounts-admin-keyset": ["accountadmin"] })
(begin-tx)
(load "test.pact")
(commit-tx)
(verify 'accounts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment