0

How to Write a Digital Marketplace Smart Contract on Algorand Using Python

Today, we will embark on a journey to create a smart contract for a digital marketplace on the Algorand blockchain, using the Python programming language and the Algokit tool.

Introduction to Algokit

Algokit is an all-in-one toolkit designed for decentralized application (dApp) development on the Algorand blockchain. Algokit provides a convenient programming environment, including localnet, various contract templates, and many other utilities, making development easier and more accessible.

Previously, writing smart contracts on Algorand often involved using the PyTEAL language. However, recently, Algorand introduced Algokit 2.0, which allows writing smart contracts in native Python.

To start a new project, we use the command:

algokit init

Then, choose the template as "Smart Contract" and the language as "Python". Next, fill in the required information, and Algokit will create a new project for us.

Writing the Smart Contract

After creating the project, we navigate to the Smart Contract directory and open the Contract.py file to begin writing the smart contract.

Create Application Method

from algopy import *

class Digital_marketplace(ARC4Contract):
    assetId: UInt64
    unitaryPrice: UInt64

    # create the app
    @arc4.abimethod(allow_actions=["NoOp"], create="require")
    def createApplication(self, assetId: Asset, unitaryPrice: UInt64) -> None:
        self.assetId = assetId.id
        self.unitaryPrice = unitaryPrice

This method is responsible for creating an application for selling an asset in the digital marketplace. It takes two parameters: assetId, which is the ID of the asset to be sold, and unitaryPrice, which is the price of the asset. The method sets the assetId and unitaryPrice attributes of the contract.

Update the listing price

@arc4.abimethod
def setPrice(self, unitaryPrice: UInt64) -> None:
    assert Txn.sender == Global.creator_address
    self.unitaryPrice = unitaryPrice

This method allows the creator of the contract to update the listing price of the asset. It takes unitaryPrice as a parameter and updates the unitaryPrice attribute of the contract. This method can only be called by the creator of the contract.

Opt in to the asset that will be sold

@arc4.abimethod
def optInToAsset(self, mbrPay: gtxn.PaymentTransaction) -> None:
    assert Txn.sender == Global.creator_address
    assert not Global.current_application_address.is_opted_in(Asset(self.assetId))
    assert mbrPay.receiver == Global.current_application_address
    assert mbrPay.amount == Global.min_balance + Global.asset_opt_in_min_balance
    itxn.AssetTransfer(
        xfer_asset=self.assetId,
        asset_receiver=Global.current_application_address,
        asset_amount=0,
    ).submit()

This method enables users to opt in to the asset that will be sold in the marketplace. It takes a payment transaction mbrPay as a parameter and ensures that the sender is the creator of the contract, the asset has not been opted in before, and the payment amount is sufficient. Upon successful validation, it opts in the user to the asset.

Buy the asset

@arc4.abimethod
def buy(self, buyerTxn: gtxn.PaymentTransaction, quantity: UInt64) -> None:
    assert self.unitaryPrice != UInt64(0)
    assert Txn.sender == buyerTxn.sender
    assert Txn.receiver == Global.current_application_address
    assert buyerTxn.amount == self.unitaryPrice * quantity

    itxn.AssetTransfer(
        xfer_asset=self.assetId,
        asset_receiver=Txn.sender,
        asset_amount=quantity,
    ).submit()

This method facilitates the purchase of the asset by a buyer. It takes a payment transaction buyerTxn and the quantity of the asset to be purchased as parameters. It verifies that the price is set and matches the payment amount. Upon successful validation, it transfers the asset to the buyer.

Delete Application

@arc4.abimethod(allow_actions=["DeleteApplication"])
def deleteApplication(self) -> None:
    assert Txn.sender == Global.creator_address
    itxn.AssetTransfer(
        xfer_asset=self.assetId,
        asset_receiver=Global.creator_address,
        asset_amount=0,
        asset_close_to=Global.creator_address,
    ).submit()

    itxn.Payment(
        receiver=Global.creator_address,
        amount=0,
        close_remainder_to=Global.creator_address,
    ).submit()

This method allows the creator of the contract to delete the application and reclaim any remaining assets and funds. It ensures that the sender is the creator of the contract. Upon successful validation, it transfers the remaining assets and funds to the creator and deletes the application.

Full-code

https://github.com/algorand-bootcamp/py-dm-beginner-en

Building the Smart Contract

After writing the smart contract, we need to build the contract to deploy it to the Algorand blockchain. Use the following command:

algokit project run build

Screen Shot 2024-04-25 at 10.55.20.png

Conclusion

Through this article, we have gained an overview of writing smart contracts on Algorand using the Python language and the Algokit tool. Hopefully, you have acquired additional knowledge and are ready to pursue your projects on the Algorand blockchain.

Don't hesitate to join the Algorand community and continue exploring this exciting blockchain technology!


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí