- vừa được xem lúc

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

0 0 7

Người đăng: Henry Pham

Theo Viblo Asia

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!

Bình luận

Bài viết tương tự

- vừa được xem lúc

Lớp trong Python

. Hôm ni, mình học tiếp về bạn “Lớp(class) trong python”, bài blog tiếp theo nằm trong series “Khám phá Đại Bản Doanh Python”(nội dung trong bài series này từ chủ yếu mình lấy từ python.org rồi viết lại hoặc dịch lại theo ngôn ngữ của mình).

0 0 17

- vừa được xem lúc

Tổng quan về Recommender System [Recommender System cơ bản - Phần 1]

Giới thiệu. Recommender System là một trong những ứng dụng phổ biến nhất của khoa học dữ liệu ngày nay.

0 0 354

- vừa được xem lúc

Gotchas trong Python

Python là một ngôn ngữ quen thuộc của hầu hết những người mới lập trình. Phần lớn bởi vì Python khá đơn giản trong cấu trúc, có nhu cầu cao và đặc biệt Python là một ngôn ngữ được ứng dụng cực kì rộng

0 0 22

- vừa được xem lúc

Hướng dẫn cơ bản framework FastAPI từ A -> Z (Phần 1)

Lời mở đầu. Chào các bạn, hôm nay tôi xin giới thiệu với các bạn về 1 framework API mà tôi mới vọc vạch mấy tuần trước.

0 0 96

- vừa được xem lúc

[Python Library Series] Pandas Tutorial for Beginners Part 1

Pandas là thư viện rất quan trọng đối với các lập trình viên Python hiện nay. Thư viện này được ví như backbone của hầu hết các dự án dữ liệu. . Note:.

0 0 32

- vừa được xem lúc

[Python Library Series] Pandas Tutorial for Beginners Part 2

Ở Part 1 chúng ta đã đi qua các bước hướng dẫn cách cài đặt Pandas, cách tạo và xem thông tin của một Dataframe. Như đã đề cập ở phần trước thì nội dung trong Part 2 này giúp chúng ta làm quen các tha

0 0 27