Learn how to deploy contracts using the JavaScript VM. Learn how to build functions and to use the msg
object in Solidity.
By the end of this post, readers will be able to:
msg
object.So far we have written smart contracts with functions, used getters and setters, learned about memory and gas-fee structure, and explored variable types in Solidity. We have also learned how to write code in Solidity and create smart contracts using the Remix IDE. Now, we will learn the final step of creating a working smart contract: deploying a smart contract by using the Javascript VM and the Remix IDE.
It’s crucial to understand the requirements for deploying a smart contract.
When writing Solidity code, business logic errors might exist due to incorrect code implementation. Having a successfully compiled contract ensures that the EVM can run it, and that we can use it in any Ethereum-based network.
An execution environment is a valid blockchain network where the contract will reside and run. The Remix IDE offers the following execution environments:
Because we’ll use the JavaScript VM in this guide, we won’t incur any costs. However, we need to learn how to identify the appropriate amount of gas by analyzing the execution log of the contract. The execution log can be compared to the flight logbook of an aircraft.
The execution log records all the activity that relates to the contract. We can thus confirm the operations status of the contract and observe the gas expense for each operation that the smart contract made.
The three requirements for deploying a smart contract aren’t complex. Most of the complexity involves understanding the business needs and then writing the contract. Once we successfully compile a smart contract and choose an execution environment, we can deploy the contract in seconds.
View the sidebar in Remix IDE and click the “Deploy & run transactions” button to display the “Deploy & Run Transactions” pane. The “Deploy & Run Transactions” pane makes several configuration options available. There are two Javascript VM versions available - Berlin and London. The Berlin version of the chain uses the same protocols as the Ethereum mainnet, so we'll be using this option:
Click the Deploy button to deploy our smart contract. After a successful deployment, our deployed contract appears in the “Deploy & Run Transactions” pane and results in the “Remix IDE terminal” pane, as the following image shows:
Since the smart contract is deployed, we can start using its functions. Click the arrow that’s next to the deployed contract to display the functions that can be called. These functions are the ones that are publicly callable from the contract, as the following image shows:
For the contract, the withdraw
function has an input box. This box can be used to set the function arguments. Notice that the deposit
function doesn’t have an input box. This is because it doesn’t have any arguments. Instead, use the Value box, which is further up in the pane, to specify an amount of ETH.
Note: when we move ether in a blockchain network, we usually work with the smallest denomination of ether, which is wei. Recall that 1 ether is equivalent to 1 Ă— 10 to the 18th power. (That number is 1 followed by 18 zeros.) Remembering how to convert ether to wei and vice versa can be challenging! So, we can use a website like Ethereum Unit Converter to ease doing the conversion.
In the Remix IDE, the functions you observe in the smart contract may have buttons of different colors.
Solidity message (msg
) object and its attributes are important concepts for token smart contracts. So, it’s important to take a few minutes to review them.
msg
object is a global object. This means that it’s accessible from any smart contract that we write with Solidity—without defining the object within our code.msg
object always refers to the current message that’s being sent to the blockchain. That is, it’s the transaction that’s currently being executed or the smart contract that’s currently being deployed.msg
object has several attributes, including msg.sender
and msg.value
, which we’ll use in this module.msg.sender
attribute refers to the entity that’s currently sending a message. That is, it’s whoever is executing the current blockchain transaction or deploying the current smart contract.The msg object represents the transaction call (originated by an Ethereum address), or the message call (originated by a contract's address) that triggers a contract execution.
This object contains the following special attributes which allow access to the blockchain.
Until next time, here’s a twitter thread summary of this post: