Solidity Development: Creating our first smart contract

In the previous blog we discovered how to set up our environment. I prefer using the solution with Webstorm, Truffle and Ganache, but feel free to use what’s appropriate for you. Now we are going to write our first smart contract. To add some fun to the development process, let’s create a crypto game. As we know, everyone loves kitties and cookies. Although, we already have CryptoKitties, let’s make CryptoCookieMonsters instead.

Building the contract

Firstly, let’s create a Solidity file with .sol extension in your IDE. The first line of each Solidity file contains the information about the version of language, that we will use. The latest stable version is 0.4.21, so let’s use it:

pragma solidity ^0.4.21;

That’s how the first line of each Solidity file must look like. Now let’s create a contract for our application. And let’s make it faster, because Crypto Cookie Monsters don’t like to wait.

contract CryptoCookieMonsters {
  address public owner;
  function CryptoCookieMonsters() {
    owner = msg.sender;
  }
}

So, we’ve just defined the smart contract for our application. Let’s take a deeper look what is going on here. Inside of the contract body, we’ve defined the constructor function and owner field, with the data type address. This is a special type for storing the addresses of wallets or other contracts. The publicaccess modifier works slightly differently from other common languages. When you add the public modifier to a property, it automatically generates a getter for that property, avoiding setting the value directly. In this way,public properties are read-only.

Hooray, we’ve done with the explanation of the first line in our contract. But why we need that owner property?

It’s elementary! In this property we will store the address of the person who deployed the contract to the Ethereum network. Later, it could help us to restrict some of the functions, so they could be called only by the owner. The contract constructor is only called once, when the contract is deployed to the network, so that’s an ideal place for setting the owner.

What’s the msg?

As you can see, we get the owner address from some msg object. msg is an object, that comes with every transaction made through the network. It includes the following properties:

  • msg.data contains complete calldata
  • msg.sender tells us about the address, which ran the transaction
  • msg.value is the amount of WEI sent with the message
  • msg.gas indicates the remaining gas
  • msg.sig shows the first four bytes of the calldata

Note: Gas is the internal pricing for running a transaction or contract in the Ethereum network. Wei is the smallest denomination in the Ethereum. It’s like a penny for the pound. ETH costs 10¹⁸ wei.

Adding some logic

As we are going to create a game, we will need some functionality for generating new Crypto Cookie Monsters. Let’s define a function inside our contract body for it:

function createCookieMonster() external {
}

The external modifier specifies that this function can be called only outside of the smart contract. Now let’s create a model for our monsters. In Solidity we have a struct data type. That’s exactly what we need:

struct CookieMonster {
  string name;
  uint256 id;
  uint16 level;
}

Now we can create CookieMonster instances inside our createCookieMonster() function. But, before that, we have to think about how we will set new monster’s properties. Where would we get the id? Maybe, we have to add some kind of a counter in our contract, that we will increment after creating each new CookieMonster. We could use this value as an id and as a part of the name.

uint256 counter = 0;

Also, let’s create an array of all of Cookie Monsters, created in the contract. For this, we can define an array of CookieMonster structs inside our contract body:

CookieMonster[] public monsters; //public makes it readonly directly

Now we can finish with implementing createCookieMonster() function:

function createCookieMonster() external {
  CookieMonster memory newMonster = CookieMonster("Test Monster", counter++, 1);
  monsters.push(newMonster);
}

The full code can be found here

Conclusion

In this bloh we’ve discovered how to create a simple smart contract, using entities like: Arrays, Structs and Functions. Also, we’ve learned about the msg object and some access modifiers like public and external. In the next chapter, we will learn how to store relations between the user and his holdings (Crypto Monsters in our case), improve the generation of id, and learn about tje Events structure in the Solidity Language.

Related Blog Posts

We hope you’ve found this to be helpful and are walking away with some new, useful insights. If you want to learn more, here are a couple of related articles that others also usually find to be interesting:

Manage Your Windows Applications With Winget

Winget, Microsoft’s native package manager for Windows 10 (version 1709 and later) and Windows 11, offers a streamlined CLI for efficient application management. This blog post introduces Winget’s installation and basic commands for installing, updating, and removing software. It highlights the tool’s ability to manage non-Winget-installed apps and explores curated package lists for batch installations. The post also recommends top Winget packages, noting some may require a paid subscription.

Read More

Our Gear Is Packed and We're Excited to Explore With You

Ready to come with us? 

Together, we can map your company’s software journey and start down the right trails. If you’re set to take the first step, simply fill out our contact form. We’ll be in touch quickly – and you’ll have a partner who is ready to help your company take the next step on its software journey. 

We can’t wait to hear from you! 

Main Contact

This field is for validation purposes and should be left unchanged.

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the form below. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

Montage Portal

Montage Furniture Services provides furniture protection plans and claims processing services to a wide selection of furniture retailers and consumers.

Project Background

Montage was looking to build a new web portal for both Retailers and Consumers, which would integrate with Dynamics CRM and other legacy systems. The portal needed to be multi tenant and support branding and configuration for different Retailers. Trailhead architected the new Montage Platform, including the Portal and all of it’s back end integrations, did the UI/UX and then delivered the new system, along with enhancements to DevOps and processes.

Logistics

We’ve logged countless miles exploring the tech world. In doing so, we gained the experience that enables us to deliver your unique software and systems architecture needs. Our team of seasoned tech vets can provide you with:

Custom App and Software Development

We collaborate with you throughout the entire process because your customized tech should fit your needs, not just those of other clients.

Cloud and Mobile Applications

The modern world demands versatile technology, and this is exactly what your mobile and cloud-based apps will give you.

User Experience and Interface (UX/UI) Design

We want your end users to have optimal experiences with tech that is highly intuitive and responsive.

DevOps

This combination of Agile software development and IT operations provides you with high-quality software at reduced cost, time, and risk.

Trailhead stepped into a challenging project – building our new web architecture and redeveloping our portals at the same time the business was migrating from a legacy system to our new CRM solution. They were able to not only significantly improve our web development architecture but our development and deployment processes as well as the functionality and performance of our portals. The feedback from customers has been overwhelmingly positive. Trailhead has proven themselves to be a valuable partner.

– BOB DOERKSEN, Vice President of Technology Services
at Montage Furniture Services

Technologies Used

When you hit the trails, it is essential to bring appropriate gear. The same holds true for your digital technology needs. That’s why Trailhead builds custom solutions on trusted platforms like .NET, Angular, React, and Xamarin.

Expertise

We partner with businesses who need intuitive custom software, responsive mobile applications, and advanced cloud technologies. And our extensive experience in the tech field allows us to help you map out the right path for all your digital technology needs.

  • Project Management
  • Architecture
  • Web App Development
  • Cloud Development
  • DevOps
  • Process Improvements
  • Legacy System Integration
  • UI Design
  • Manual QA
  • Back end/API/Database development

We partner with businesses who need intuitive custom software, responsive mobile applications, and advanced cloud technologies. And our extensive experience in the tech field allows us to help you map out the right path for all your digital technology needs.

Our Gear Is Packed and We're Excited to Explore with You

Ready to come with us? 

Together, we can map your company’s tech journey and start down the trails. If you’re set to take the first step, simply fill out the contact form. We’ll be in touch – and you’ll have a partner who cares about you and your company. 

We can’t wait to hear from you! 

Thank you for reaching out.

You’ll be getting an email from our team shortly. If you need immediate assistance, please call (616) 371-1037.