August 16, 2023 - 5 min read
The old saying goes that you have to learn how to crawl before you learn to walk. So, we will start with the basics of Web3 development here now. In this tutorial, you’ll learn how to build a “Hello World” smart contract by using Move, a new smart contract development language aimed at strengthening the limitations presented by using Solidity.
Since we’re keeping things simple, you won’t need any serious technical knowledge to follow along — as this tutorial is beginner-friendly. Even if you are not a developer, you can follow the step-by-step instructions to create your first smart contract using the Move language. By the end, you’ll know how to spin up a quick example contract using one of the newest, most cutting-edge smart contract languages.
First off, a few definitions for context. Smart contracts are automated programs published and executed in blockchain environments. Two parties agree to transact based on certain conditions being met and automate the process by cryptographically signing their agreements.
As they run on blockchains, they can be operated without a central party or server. Once a smart contract transaction is settled, due to the immutable nature of blockchains, it can’t be modified. However, data can be updated if the contract has been designed to allow it.
Smart contracts follow the rules of the programming used to write them in addition to obeying the parameters of the blockchains on which they run. Move is a statically-typed, resource-oriented language that allows developers to write programs that flexibly manage and transfer assets, while providing the security and protections against attacks on those assets.
Move takes its cue from Rust by using resource types with move (hence the name) semantics as an explicit representation of digital assets, such as currency. It introduces the concept of “resources,” which are special types that are linearly typed to ensure they are not copied or discarded unintentionally. This ensures that digital assets are handled safely and transparently.
The Move Code Playground is a browser-based, integrated development environment for the Move language. It allows users to write, compile, and test Move modules and scripts without having to download any software.
The Move Code Playground was developed by Pontem Labs. Pontem is a product development studio working toward global financial inclusion powered by blockchains. To start your journey with Move, head over to the Move Code Playground to get started.
Once you are in the Move Code Playground:
‘
Hello_World’
and click save.Well done, now let’s copy and paste this example:
module hello::hello_world {
public entry fun hello_world() {
let vect: vector<u8> = b"Hello World";
let str: std::string::String = std::string::utf8(vect);
std::debug::print(&str); // [debug] "Hello World"
}
}
module hello::hello_world {
The line above declares a new module named hello_world within the hello
namespace. A module in Move is similar to a class or contract in other languages; it’s a collection of functions and data structures.
public entry fun hello_world() {
This defines a public entry function named hello_world
. The entry keyword means that this function can be called from outside this module, making it an entry point.
let vect: vector<u8> = b"Hello World";
This line initializes a new variable called vect
of type vector<u8>
. The type vector<u8>
is a dynamic array of unsigned 8-bit integers. The b"Hello World"
is a byte literal representation of the string “Hello World,” which assigns the byte values of each character in the string to the vector.
let str: std::string::String = std::string::utf8(vect);
Here, the code is converting the byte vector vect
into a string. The function std::string::utf8
from the standard library is used to achieve this conversion, turning the vector<u8>
into a std::string::String
.
std::debug::print(&str);
This line utilizes the std::debug::print
function from the standard library. It’s used to print the string str
to the debug output. The &str
syntax means a reference to the string variable is being passed to the print function.
In essence, the hello_world
function within the hello::hello_world
module, when called, will print the message “Hello World” to the debug output.
Before we deploy our smart contract, we have to do two things in the Move Code Playground. In the move.toml file, add this at the end:
[addresses]
hello = "_"
The move.toml file should look like this:
[package]
name = 'Hello_World'
version = '1.0.0'
[dependencies.AptosFramework]
git = 'https://github.com/aptos-labs/aptos-core.git'
rev = 'mainnet'
subdir = 'aptos-move/framework/aptos-framework'
[addresses]
hello = "_"
Next, click the Profiles icon on the left sidebar. Click the ‘+’ icon to add a new profile. Type in your profile name and click ‘ADD’.
To deploy your smart contract, click the ‘Deploy’ icon on the top left of the screen, and voila – you have just deployed your first smart contract in the Move language on the Aptos blockchain. Congratulations on making it this far!
To run your smart contract, click ‘Run’ in the top left corner. From the dropdown menu, select the module and function that you would like to run. In our case, the module and function name are both ‘hello_world,’ and then click ‘Run.’ You should see an output similar to this:
Running the function
0x736006466087c8930225e42144fa248583cccf733393c699bf8f8c3195922deb
transaction_hash: 0x736006466087c8930225e42144fa248583cccf733393c699bf8f8c3195922deb
gas_used: 2
gas_unit_price: 100
sender: ce754ec515824ebc2a31d703eb04f2b02d1ffd1636cc4ad3d41953936b59670d
sequence_number: 12
success: true
timestamp_us: 1692131475229315
version: 2886944
vm_status: Executed successfully
As you can see in the vm_status
, the function was executed successfully. Great job!
Now that you’ve created your “Hello World” smart contract using the Move language, there are many possibilities available to you. You could deploy your contract to a testnet or even a mainnet, change the message, create a state variable to store the message, create a function to update the message, or save a message forever to a blockchain!
Learn more about Supra by taking a deep dive into our documentation. For discussions or integrations, don’t hesitate to reach out to our business development team today.
RECENT POSTS
註冊Supra新聞通訊,獲取最新消息、更新、行業洞察等內容。
©2024 Supra | Entropy基金會(瑞士註冊號:CHE.383.364.961)。保留所有權利。