Search code examples
pact

Coin.transfer twice in single function with one user confirmation


Is possible to call coin.transfer twice (to two different address) in the same function and with one user confirmation? I’m thinking about the secondary sales of a marketplace… how does the contract keep the fee and transfer the other part of the sale to the seller?

This is what I'm trying to do (not tested yet)

(defun buy-nft (id:string newowner:string)
    @doc "buy an nft from marketplace"
    (let (
            (data (read nfts id ['listed 'owner 'price]))
        )
        (enforce (= (at "listed" data) true) "this nft is not listed")
        (let (
                (fee (/ (* (get-fee) (at "price" data)) 100))
            )
            (coin.transfer newowner (at "owner" data) (- (at "price" data) fee))
            (coin.transfer newowner ADMIN_ADDRESS fee)
            (insert hmarket (int-to-str 10 (get-count MARKET_PURCHASE_COUNT)) {
                "id": id, 
                "transactionTime": (at "block-time" (chain-data)), 
                "newowner": newowner, 
                "oldowner": (at "owner" data),
                "price": (at "price" data)
            })
            (update nfts id {
              "owner": newowner, 
              "listed": false, 
              "price": 0.0
            })
            (with-capability (PRIVATE)
                (increase-count MARKET_PURCHASE_COUNT)
                (increase-volume-by VOLUME_PURCHASE_COUNT (at "price" data))
            )
        )
    )
)

Solution

  • Yes. When doing coin transfer, you present the coin.TRANSFER capability for the user to sign. You can present multiple capabilities to the user during the signing stage.

    So if you want to do transfers to multiple addresses, just supply multiple coin.TRANSFER capabilities to be signed during the transaction. The user will see it as one screen