Skip to content

Commit

Permalink
Misc edits
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed May 9, 2024
1 parent cee721a commit ef80828
Showing 1 changed file with 38 additions and 38 deletions.
76 changes: 38 additions & 38 deletions convex-core/src/main/cvx/convex/asset.cvx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"For more information, see those functions defined in this library whose purpose is to delegate to asset implementations in a generic way."]
:name "Convex Asset API library"}))

;;;;;;;;;; Forward declarations
(declare balance transfer)
;;;;;;;;;; Forward declarations
(declare balance transfer)

;;;;;;;;;; Private

Expand All @@ -41,7 +41,7 @@
^{:doc {:description ["Accepts asset from sender."
"If asset contains multiple assets, accepts each in turn. MUST fail if the asset cannot be accepted."]
:examples [{:code "(accept sender [fungible-token-address 1000])"}]
:signature [{:params [sender asset]}
:signature [{:params [sender asset]}
{:params [sender asset-path quantity]}]
:errors {:STATE "If there is no sufficient offer to accept."
:FUNDS "If sender has insufficient balance."}}}
Expand All @@ -59,14 +59,14 @@
path
(accept sender path quantity)))
{}
asset)

;; a nil quantity can always be accepted
(nil? asset) nil

(fail "Bad asset argument")))

([sender path quantity]
asset)

;; a nil quantity can always be accepted
(nil? asset) nil

(fail "Bad asset argument")))

([sender path quantity]
(call path (accept sender quantity))))


Expand Down Expand Up @@ -102,7 +102,7 @@
"May result in an error if the asset does not support open offers."]
:examples [{:code "(offer receiver [fungible-token-address 1000])"}
{:code "(offer receiver non-fungible-token-address #{1 4 6})"}]
:signature [{:params [receiver asset]}
:signature [{:params [receiver asset]}
{:params [receiver asset-path quantity]}]}}

([receiver asset]
Expand All @@ -116,9 +116,9 @@
(reduce (fn [m [path quantity]]
(assoc m path (offer receiver path quantity)))
{}
asset)))

([receiver path quantity]
asset)))

([receiver path quantity]
(call path (offer receiver quantity))))


Expand All @@ -132,9 +132,9 @@
([receiver asset]
(cond
(vector? asset)
(let [[path quantity] asset]
(recur receiver path quantity nil))

(let [[path quantity] asset]
(recur receiver path quantity nil))

(map? asset)
(reduce (fn [m [path quantity]]
(assoc m
Expand All @@ -146,21 +146,21 @@
:else
(fail "Invalid asset")))

([receiver path quantity]
(recur receiver path quantity nil))
([receiver path quantity]
(recur receiver path quantity nil))

([receiver path quantity data]
(cond
;; First check if receiver has a callable receive-asset function. If so, use this
(callable? receiver 'receive-asset)
(cond
;; First check if receiver has a callable receive-asset function. If so, use this
(callable? receiver 'receive-asset)
(do
(offer receiver path quantity) ;; Offer correct quantity
(call receiver
(receive-asset path quantity data)))

;; An actor without a receive-asset function is not a valid receiver?
(actor? receiver) (fail :STATE "Target Actor does not have receive-asset function")

(call receiver
(receive-asset path quantity data)))

;; An actor without a receive-asset function is not a valid receiver?
(actor? receiver) (fail :STATE "Target Actor does not have receive-asset function")

(call path (direct-transfer receiver quantity)))))


Expand All @@ -183,17 +183,17 @@
(defn balance
^{:doc {:description ["Returns asset balance for a specified owner, or for the current address if not supplied."
"Return value will be in the quantity format as specified by the asset type."]
:examples [{:code "(balance asset-address)"}
:examples [{:code "(balance asset-address)"}
{:code "(balance [asset-address asset-id] owner)"}]
:signature [{:params [asset-path]}
{:params [asset-path owner]}]}}
([path]
(recur path *address*))

([path owner]
(query ;; external call, so use query to avoid re-entrant secuity risks
(call path (balance owner)))))

(query ;; external call, so use query to avoid re-entrant secuity risks
(call path (balance owner)))))

(defn burn
^{:doc {:description "Burns a quantity of the given asset, if allowed by the implementation. Amount must be a valid quantity owned by the user."
:examples [{:code "(burn [my-token-address :FOO] 1000)"}]
Expand Down Expand Up @@ -240,11 +240,11 @@

;; Interpret nil as the 'zero' asset, which everybody owns
(nil? asset)
true)

([owner path quantity]
(query ;; query because this is an external call
(let [bal (balance path owner)]
true)

([owner path quantity]
(query ;; query because this is an external call
(let [bal (balance path owner)]
(call path
(quantity-subset? quantity bal))))))

Expand Down

0 comments on commit ef80828

Please sign in to comment.