Skip to content

Commit

Permalink
require buyer_fungible_account at map-escrowed-buy
Browse files Browse the repository at this point in the history
  • Loading branch information
ggobugi27 committed Aug 3, 2023
1 parent a56f400 commit 47cfcf5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
3 changes: 2 additions & 1 deletion pact/concrete-policies/royalty-policy/royalty-policy-v1.repl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"fungible": marmalade.abc
,"price": 2.0
,"amount": 1.0
,"seller-account": {
,"seller-fungible-account": {
"account": "k:account"
,"guard": {"keys": ["account"], "pred": "keys-all"}
}
Expand All @@ -116,6 +116,7 @@

(env-data {
"buyer": "k:buyer"
,"buyer_fungible_account": "k:buyer"
,"buyer-guard": {"keys": ["buyer"], "pred": "keys-all"}
})

Expand Down
16 changes: 10 additions & 6 deletions pact/policy-manager/policy-manager.pact
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@

(use kip.token-policy-v2 [token-info])
(use marmalade.quote-manager)
(use marmalade.quote-manager [quote-spec quote-msg])
(use marmalade.quote-manager [quote-spec quote-msg fungible-account])

(defconst QUOTE-MSG-KEY "quote"
@doc "Payload field for quote spec")

(defconst UPDATE-QUOTE-PRICE-MSG-KEY "update_quote_price"
@doc "Payload field for quote spec")

(defconst BUYER-FUNGIBLE-ACCOUNT-MSG-KEY "buyer_fungible_account"
@doc "Payload field for buyer's fungible account")

(defcap POLICY_MANAGER:bool ()
@doc "Ledger module guard for policies to be able to validate access to policy operations."
true
Expand Down Expand Up @@ -246,12 +249,13 @@
(quote:object{quote-schema} (get-quote-info sale-id))
(spec:object{quote-spec} (at 'spec quote))
(fungible:module{fungible-v2} (at 'fungible spec))
(seller-account:object{fungible-account} (at 'seller-account spec))
(buyer-fungible-account-name:string (read-msg BUYER-FUNGIBLE-ACCOUNT-MSG-KEY))
(seller-fungible-account:object{fungible-account} (at 'seller-fungible-account spec))
(price:decimal (at 'price spec))
(sale-price:decimal (floor (* price amount) (fungible::precision)))
)
;; transfer fungible to escrow account
(fungible::transfer-create buyer (at 'account escrow-account) (at 'guard escrow-account) sale-price)
(fungible::transfer-create buyer-fungible-account-name (at 'account escrow-account) (at 'guard escrow-account) sale-price)

(with-capability (ESCROW sale-id)
;; Run policies::enforce-buy
Expand All @@ -260,8 +264,8 @@
(let (
(balance:decimal (fungible::get-balance (at 'account escrow-account)))
)
(install-capability (fungible::TRANSFER (at 'account escrow-account) (at 'account seller-account) balance))
(fungible::transfer (at 'account escrow-account) (at 'account seller-account) balance)
(install-capability (fungible::TRANSFER (at 'account escrow-account) (at 'account seller-fungible-account) balance))
(fungible::transfer (at 'account escrow-account) (at 'account seller-fungible-account) balance)
)
)
true
Expand All @@ -284,7 +288,7 @@
@doc "Checks env-data field and see if the msg is a object"
(= (take 6 (typeof (try false (read-msg msg)))) "object")
)

(defun token-init (token:object{token-info} policy:module{kip.token-policy-v2})
(policy::enforce-init token))

Expand Down
11 changes: 6 additions & 5 deletions pact/policy-manager/policy-manager.repl
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@
"fungible": marmalade.abc
,"price": 2.0
,"amount": 1.0
,"seller-account": {
,"seller-fungible-account": {
"account": "k:account"
,"guard": {"keys": ["account"], "pred": "keys-all"}
}
Expand Down Expand Up @@ -293,7 +293,7 @@
{"name": "marmalade.ledger.OFFER","params": [(read-msg 'token-id) "k:account" 1.0 "2023-07-22T11:26:35Z"]}
{"name": "marmalade.ledger.SALE","params": [(read-msg 'token-id) "k:account" 1.0 "2023-07-22T11:26:35Z" "C1vw1eMf_DZV3oZjyinRIKrQV2rPMBMh3lydeduo8yg"]}
{"name": "marmalade.quote-manager.QUOTE","params": ["C1vw1eMf_DZV3oZjyinRIKrQV2rPMBMh3lydeduo8yg" (read-msg 'token-id)
{"amount": 1.0,"fungible": marmalade.abc,"price": 2.0,"seller-account": {"account": "k:account","guard": (read-keyset 'seller-guard) }}]}
{"amount": 1.0,"fungible": marmalade.abc,"price": 2.0,"seller-fungible-account": {"account": "k:account","guard": (read-keyset 'seller-guard) }}]}
{"name": "marmalade.quote-manager.QUOTE_GUARDS","params": ["C1vw1eMf_DZV3oZjyinRIKrQV2rPMBMh3lydeduo8yg" (read-msg 'token-id) (read-keyset 'seller-guard) []]}
{"name": "marmalade.ledger.ACCOUNT_GUARD","params": [(read-msg 'token-id) "c:kojW5oDBdlmFg0jVWz_mdj1DxlL8OgeH-1OILa1YzzE" (create-capability-pact-guard (SALE_PRIVATE "C1vw1eMf_DZV3oZjyinRIKrQV2rPMBMh3lydeduo8yg"))]}
{"name": "marmalade.ledger.TRANSFER","params": [(read-msg 'token-id) "k:account" "c:kojW5oDBdlmFg0jVWz_mdj1DxlL8OgeH-1OILa1YzzE" 1.0]}
Expand All @@ -302,9 +302,10 @@
)

(env-data {
"buyer": "k:buyer"
,"buyer-guard": {"keys": ["buyer"], "pred": "keys-all"}
,"market-guard": {"keys": ["market"], "pred": "keys-all"}
"buyer_fungible_account": "k:buyer"
,"buyer": "k:buyer"
,"buyer-guard": {"keys": ["buyer"], "pred": "keys-all"}
,"market-guard": {"keys": ["market"], "pred": "keys-all"}
})

(marmalade.abc.create-account "k:buyer" (read-keyset 'buyer-guard))
Expand Down
18 changes: 9 additions & 9 deletions pact/quote-manager/quote-manager.pact
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
(defschema quote-spec
@doc "Quote spec of the sale"
fungible:module{fungible-v2}
seller-account:object{fungible-account}
seller-fungible-account:object{fungible-account}
price:decimal
amount:decimal
)
Expand Down Expand Up @@ -162,14 +162,14 @@
(bind quote-spec {
"fungible":= fungible
,"amount":= amount
,"seller-account":= fungible-account
,"seller-fungible-account":= fungible-account
}
(update quotes sale-id {
"spec": {
"fungible": fungible
, "amount": amount
, "price": price
, "seller-account": fungible-account
, "seller-fungible-account": fungible-account
}
}))
)
Expand All @@ -183,20 +183,20 @@
)

;; Validate functions
(defun validate-fungible-account (fungible:module{fungible-v2} seller-account:object{fungible-account})
(let ((seller-details (fungible::details (at 'account seller-account))))
(defun validate-fungible-account (fungible:module{fungible-v2} account:object{fungible-account})
(let ((seller-details (fungible::details (at 'account account))))
(enforce (=
(at 'guard seller-details) (at 'guard seller-account))
"Seller guard does not match"))
(at 'guard seller-details) (at 'guard account))
"Account guard does not match"))
)

(defun validate-quote:bool (quote-spec:object{quote-spec})
(let* ( (fungible:module{fungible-v2} (at 'fungible quote-spec) )
(seller-account:object{fungible-account} (at 'seller-account quote-spec))
(seller-fungible-account:object{fungible-account} (at 'seller-fungible-account quote-spec))
(amount:decimal (at 'amount quote-spec))
(price:decimal (at 'price quote-spec))
(sale-price:decimal (* amount price)) )
(validate-fungible-account fungible seller-account)
(validate-fungible-account fungible seller-fungible-account)
(fungible::enforce-unit sale-price)
(enforce (< 0.0 price) "Offer price must be positive")
true)
Expand Down
3 changes: 2 additions & 1 deletion pact/quote-manager/quote-manager.repl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
}),
"quote": {
"spec": {
"seller-account": {
"seller-fungible-account": {
"account": "k:account"
,"guard": {"keys": ["account"], "pred": "keys-all"}
}
Expand Down Expand Up @@ -112,6 +112,7 @@
,"buyer-guard": {"keys": ["bidder"], "pred": "keys-all"}
,"market-guard": {"keys": ["market"], "pred": "keys-all"}
,"update_quote_price": 20.0
,"buyer_fungible_account": "k:bidder"
})

(env-sigs [
Expand Down

0 comments on commit 47cfcf5

Please sign in to comment.