Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
543
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. ;; accounts module, admin keyset, and table
  2. ; (load "examples/verified-accounts/accounts.repl")
  3.  
  4. (enforce-pact-version "2.4.1")
  5.  
  6. (define-keyset 'accounts-admin-keyset
  7. (read-keyset "accounts-admin-keyset"))
  8.  
  9. (module accounts 'accounts-admin-keyset
  10. @doc
  11. "Accounts module demonstrating row-level keysets, private pacts, and escrow. \
  12. \ Version: 0.2 \
  13. \ Author: Stuart Popejoy"
  14. @model
  15. [(defproperty conserves-mass
  16. (= (column-delta accounts 'balance) 0.0))
  17. (defproperty auth-required
  18. (authorized-by 'accounts-admin-keyset))
  19.  
  20. ; we have two admin functions
  21. (property auth-required
  22. {'only: [read-account-admin, fund-account]})
  23.  
  24. ; every function should conserve mass except for the admin fund-account,
  25. ; and debit / credit which should be private
  26. (property conserves-mass
  27. {'except: [fund-account, debit, credit]})
  28.  
  29. ; reading functions do not write
  30. (property (not (table-written accounts))
  31. {'only: [read-account-user, read-account-admin, check-balance]})
  32.  
  33. (property (not (table-read accounts))
  34. {'only: [create-account]})
  35. ]
  36.  
  37. (defschema account
  38. @doc "Row type for accounts table."
  39. @model [(invariant (>= balance 0.0))]
  40. balance:decimal
  41. amount:decimal
  42. ccy:string
  43. auth:string ;; AUTH_KEYSET for keysets, pact id for pacts
  44. )
  45.  
  46. (deftable accounts:{account}
  47. "Main table for accounts module.")
  48.  
  49. (defconst AUTH_KEYSET 'K
  50. "Indicates keyset-governed account")
  51.  
  52. (defconst PACT_REF "ref")
  53.  
  54. (defun create-account (address:string ccy)
  55. (insert accounts address
  56. { "balance": 0.0
  57. , "amount": 0.0
  58. , "ccy": ccy
  59. , "auth": AUTH_KEYSET
  60. }
  61. ))
  62.  
  63. (defun transfer (src:string dest:string amount:decimal)
  64. "transfer AMOUNT from SRC to DEST"
  65. (debit src amount)
  66. (credit dest amount))
  67.  
  68. (defun read-account-user (id)
  69. "Read data for account ID"
  70. (with-read accounts id
  71. { "balance":= b
  72. , "ccy":= c
  73. , "auth" := auth }
  74. ; TODO: we can't handle this object yet
  75. ; { "balance": b, "ccy": c }
  76. b
  77. ))
  78.  
  79. (defun read-account-admin (id)
  80. "Read data for account ID, admin version"
  81. (enforce-keyset 'accounts-admin-keyset)
  82. (read accounts id ['balance 'ccy 'amount]))
  83.  
  84. (defun check-balance (balance:decimal amount:decimal)
  85. (enforce (<= amount balance) "Insufficient funds"))
  86.  
  87. (defun fund-account (address amount)
  88. (enforce-keyset 'accounts-admin-keyset)
  89. (enforce (>= amount 0.0) "amount must be non-negative")
  90. (update accounts address
  91. { "balance": amount
  92. , "amount": amount }
  93. ))
  94.  
  95. (defun debit (acct amount)
  96. "Debit AMOUNT from ACCT balance"
  97. (with-read accounts acct
  98. { "balance":= balance
  99. , "auth" := auth
  100. }
  101. (check-balance balance amount)
  102. (update accounts acct
  103. { "balance": (- balance amount)
  104. , "amount": (- amount)
  105. }
  106. )))
  107.  
  108. (defun credit (acct amount)
  109. "Credit AMOUNT to ACCT balance"
  110. (enforce (>= amount 0.0) "amount must be non-negative")
  111. (with-read accounts acct
  112. { "balance":= balance }
  113. (update accounts acct
  114. { "balance": (+ balance amount)
  115. , "amount": amount
  116. }
  117. )))
  118. )
  119.  
  120. (create-table accounts)
  121. ;done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement