A Step-by-Step Guide to Writing Your First Move Smart Contract on Aptos

A Step-by-Step Guide to Writing Your First Move Smart Contract on Aptos

Aptos is one of the independent layer 1 laying focus on scalability, security, and reliability among other blockchain. It supports smart contracts which its smart contract is written in move programming. The blockchain network utilizes Proof of Stake as the consensus mechanism We will create a simple smart contract a waste management system for people in a community. Aptos blockchain has maintained high-level security features and reduced transaction costs. We should delve into how to build a smart with move building waste management system.

To delve into move smart contract, you can either use remix or your local code editor such as vs for the sake of this tutorial we will utilize remix

Open your Remix IDE

Move aptos write smart contract

Look at the left corner of your screen you see a plugin icon click

Learn how to deploy smart on APtos move smart contract

A sidebar will pop up find CODE BY WELLDONE STUDIO active in the code studio

Image description

Select Aptos

You need to install a wallet to be able to interact with your smart contract. Visit this link wallet connect Aptos learning move to download the wallet from chrome extension.

Move

Setup your wallet account to interact with your wallet

Image description

Click Aptos to create a wallet after creating you can visit claim fuacet Aptos to claim testnet or devnet copy your address.
After you finish setting up the account, copy your seed phrase somewhere save.

After successfully claim


Let move to our smart contract using waste manager as an example

In your remix IDE on your left sidebar, create a new create project input the name of your project

waste_manager aptos

Locate your Move.toml
paste this code

[package]
name = "Examples"
version = "0.0.0" [addresses]
wastes_Insured_addr = "paste your account address" [dependencies]
AptosFramework = { git = "https://github.com/aptos-labs/aptos-core.git", subdir = "aptos-move/framework/aptos-framework/", rev = "aptos-node-v1.13.1"}
Enter fullscreen mode
Exit fullscreen mode

TO get address generated for this account navigate to .aptos/config.yaml you will find generated account with the public key and private key

profiles: default: private_key: "0xee8f387ef0b4bb0018c4b91d1c0f71776a9b85935b4c6ec2823d6c0022fbf5cb" public_key: "0xc6c07218d79a806380ca67761905063ec7a78d41f79619f4562462a0f8b6be11" account: cbddf398841353776903dbab2fdaefc54f181d07e114ae818b1a67af28d1b018 rest_url: "https://api.devnet.aptoslabs.com" faucet_url: "https://faucet.devnet.aptoslabs.com" 
Enter fullscreen mode
Exit fullscreen mode

To get started with the smart contract, you need to define a module that is placed under account address which

module <account-address>::<module-name> { }
Enter fullscreen mode
Exit fullscreen mode

We will define a module, this module will be the container for our entire smart contract

module wastes_Insured_addr::wastes-Insured { }
Enter fullscreen mode
Exit fullscreen mode

We import our library which we are going to use in this smart contract.
Event is for emitting any event that occurred in our smart contract when a function is triggered.
Table: we utilize table to define our table for the smart contract input data
Signer is who is calling the smart contract at a particular time.
An account is associated with our smart contract.

 use aptos_framework::event; use std::string::String; use aptos_std::table::{Self, Table}; use aptos_framework::account; use std::signer;
Enter fullscreen mode
Exit fullscreen mode

We will define a struct that will hold our typed fields, it has the capability to store, drop, and copy.

struct Waste has store, drop, copy, { wast_id: u64, wasteType:String, collectionLocation: String, weigth: u64, isRecorded: bool, isValidated: bool }
Enter fullscreen mode
Exit fullscreen mode

We will define our WasteList which will take waste array, new events emit when waste is recorded, and counter which will serve as the length of the waste store.

struct WasteList has key { waste: Table<u64, Waste>, waste_count: u64 } 
Enter fullscreen mode
Exit fullscreen mode

Next, we create a list function which is the first an account must have, it’s essential for submitting transactions which we will associate with a signer

public entry fun create_list(account: &signer) { let waste_holder = WasteList { waste: table::new(), set_waste_event: account::event::new_event_handle<Waste>(account), waste_count: 0 }; move_to(account, waste_holder); }
Enter fullscreen mode
Exit fullscreen mode

We create a waste add function that submits the new waste transaction to the blockchain. We need to know the user submitting the transaction to the chain. it will accept all the fields typed in our struct which looks like this

public entry fun register_waste(account: &signer, wasteType: String, collectionLocation: String, weight: u64, wasteAmount: u64, hospitalAddress: address) acquires WasteList {
// we are getting the signer address let signer_address = signer::address_of(account);
// we check if the signer has been initialized or not assert!(exists<WasteList>(signer_address), E_NOT_INITIALIZED);
//We are getting waste list resources let waste_list = borrow_global_mut<WasteList>(signer_address);
// we increament the count of waste recorded let counter = waste_list.waste_count + 1;
// record a new waste let new_record_waste = Waste { wast_id: counter, wasteType:String, collectionLocation: String, weigth: u64, isRecorded: bool, isValidated: }; table::upsert(&mut waste_list.waste, counter, new_record_waste); waste_list.waste_count = counter; event::emit(new_record_waste); } 
Enter fullscreen mode
Exit fullscreen mode

Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top