Marketplace contract
Simple marketplace contract in Archetype
Last updated
Simple marketplace contract in Archetype
Last updated
This one will be created from scratch.
Create a new file ./contracts/market.arl
The first line is the name declaration:
Next, we'll re-define the FA2 transfer parameter types that the contract will need to use to send transfer calls to the FA2 contract. They are very specific to the .
The marketplace contract will accept sale orders from collectors. For simplicity, the contract will accept any order, without checking the ownership of the token. Transfers will be enforced during the purchase and will fail if the seller does not own the tokens. The user interface will need to filter out invalid sale orders.
Orders are identified by a unique order id.
Parameters include the FA2 contract address (making this marketplace token-agnostic), the seller address, the token id, how many tokens are for sale, the price for each token, and an expiry date.
We need to keep track of orders:
Let's now create an entrypoint to list a token item for sale. The only conditions are that the amount is non-zero and that the expiry date is in the future. These conditions will prevent some invalid sale orders to be published.
The seller should be able to change their mind and cancel the order. Note how we check that only the seller can call this entrypoint.
Now, buyers will call another entrypoint, that will do the following:
Check that the bought amount is non-zero: r_buy_amount
Check that the order exists and that there are still tokens remaining to be sold: r_order
Check that the amount sent with the transaction matches the price: r_value
Check that the order didn't expire: r_expired
Prepare the records required to call the %transfer
entrypoint on the contract of the listed token
Update the orders big map with the new amount
Call the FA2 contract to transfer the token from the seller to the buyer
Transfer the tez coins from the buyer to the seller
And our marketplace contract is now ready đŽâđ¨
However, the marketplace contract will not validate the amount of tokens owned by the seller. This would require tracking balance updates and add too much complexity to the smart contract. Instead, the orders will be filtered by .
A more robust solution would be to run an off-chain indexer (or integrate an existing one such as ), that will keep track of all the created and expired orders.