Article cover

25.05.2022

13

Like

128

Views

Smart Contract Project with Near Protocol

Hello my name is sercan and i will be explain my raffle demo project. I builded during the course i attented from patika.dev platform. I used singleton design pattern, there is function in one export class.

I have two type of roles in my project; admin and user. Admin can create, edit cancel or complete raffles. The user can buy a ticket and participate in the raffle. I used two class called raffle and ticket as a model. There are vectors created from these models to store datas to the blockchain.

Now let’s introduce you to the basic functions of the project for Admin:

Creating Raffle :


Creating Raffle


It creates a Raffle model object with the parameters it receives and pushes it to “raffles” PersistentVector.

How to call this function : near call \$CONTRACT createRaffle ‘{“title”:”$title”, “name”:”$name”, “category”:”$category”, “description”:”$description”, “price”:”$price”, “maxTicketCount”:$maxTicketCount}’ --accountId \$OWNER

Modifying Raffle :

Modifying Raffle

As in Create method, it creates an object of Raffle model with the parameters it receives and replaces the element in the index in raffles PersistentVector with the replace method.

How to call this function : near call \$CONTRACT modifyRaffle ‘{“raffleId”:”$raffleId”, “title”:”$title”, “name”:”$name”, “category”:”$category”, “description”:”$description”, “price”:”$price”, “maxTicketCount”:$maxTicketCount}’ --accountId \$OWNER

Completing Raffle :

Completing Raffle

Tickets purchased in raffle, which will be drawn, are added to an array and the winning ticket is determined with a randomly generated number according to the indexes in that series, then the winner ticket and completed tickets of the raffle are updated and replaced.

How To Call This Function : near call $CONTRACT completeRaffle ‘{“raffleId”:’$raffleId’}’ --accountId $OWNER --gas=$gas

Canceling Raffle :

Canceling Raffle

The function makes the isActive value belongs to the index false and replaces it with its new value using the replace method.

An User Function, Buying a Ticket :

Buying Ticket

If an user has enough deposit, the money transferred from the account and a ticket is added to the tickets PersistentVector on behalf of the account ID.

How to call this function : near call $CONTRACT buyTicket ‘{“raffleId”:’$raffleId’}’ --accountId $USER --deposit $attachmentDeposit

As an example, let’s examine two get methods.

getRaffleByRaffleId as a simple example:

Getting Raffle by Raffle Id

It takes raffle id as a parameter and returns the value in that index in the Persistent Vector.

How to call this function : near view $CONTRACT getAllTickets ‘{“raffleId”:’$raffleId’}’

More complex getRafflesByAccountId :

Getting Raffles by Account Id

It adds the raffles created by that accountId to an array and returns an array as a result.

How to call this function : near view $CONTRACT getAllTickets ‘{“sender”:’$sender’}’

Asset Functions :

Is Deposit Enough?

It checks whether the deposit is sufficient for the ticket price so that the user can buy a ticket. If the first parameter is false, it outputs the second parameter as an error.

Logging :


Logging

After each transaction, we added logs indicating the status of the transaction. For example, it logs the ticket was purchased, the price of the ticket and who bought it.

Thanks to Near Developer Course and Patika.dev, I learned about blockchain technology and smart contracts and completed my first project.

I hope that has been revealing.

My project : https://github.com/sercangur/eRaffle-NearProtocol

You can contact me at [email protected].


Web3 Fundamentals and NEAR

Comments

You need to log in to be able to comment!

Kazım Sercan Gür

Location

Ankara, TR

© 2021 Patika Dev

facebook
twitter
instagram
youtube
linkedin