Lesson 5 of 14
In Progress

Writing and Deploying Smart Contracts with Solidity

Smart contracts are self-executing contracts with the terms of the agreement between buyer and seller being directly written into lines of code. They are one of the key components of the Ethereum blockchain, and they allow developers to build decentralized applications (DApps) that can execute automatically and transparently.

In this article, we will cover the basics of writing and deploying smart contracts with Solidity, the most popular programming language for writing smart contracts on the Ethereum blockchain.

What is Solidity?

Solidity is a programming language for writing smart contracts on the Ethereum blockchain. It was developed by the Ethereum Foundation and is designed to be used for creating and executing smart contracts on the Ethereum Virtual Machine (EVM).

Solidity is a high-level, object-oriented language that is similar to JavaScript and C++, and it is easy to learn for developers who are familiar with these languages.

Hello World Smart Contract

To get started with Solidity, we will write a simple “Hello World” smart contract that prints a message to the console.

Here is the code for our “Hello World” smart contract:

pragma solidity ^0.7.0;

contract HelloWorld {
  function greet() public pure returns (string memory) {
    return "Hello, World!";
  }
}

This smart contract consists of a single function called greet() that returns the string “Hello, World!”. The public keyword indicates that the function is public and can be called by anyone, and the pure keyword indicates that the function does not modify the state of the contract or access external resources.

Compiling and Deploying Smart Contracts

Now that we have written our “Hello World” smart contract, we need to compile it and deploy it to the Ethereum blockchain.

To compile our smart contract, we can use a Solidity compiler, such as Remix (https://remix.ethereum.org). Remix is an online Solidity compiler and debugger that allows you to compile and deploy smart contracts to the Ethereum blockchain.

To compile our smart contract with Remix, follow these steps:

  1. Open Remix in your web browser.
  2. Paste the code for your smart contract into the Solidity compiler.
  3. Click the “Compile” button to compile your smart contract.

If the compilation is successful, you will see the compiled bytecode for your smart contract. The bytecode is the machine-readable code that is executed on the Ethereum Virtual Machine (EVM).

To deploy our smart contract to the Ethereum blockchain, we will need to use a Web3 provider, such as Infura (https://infura.io). Infura is a cloud-based Ethereum node service that allows you to access the Ethereum mainnet and other Ethereum networks from anywhere.

To deploy our smart contract to the Ethereum mainnet with Infura, we will need to do the following:

  1. Create an Infura account and retrieve your API keys.
  2. Connect to the Ethereum mainnet with Web3.js and Infura.
  3. Create an instance of our smart contract with Web3.js.
  4. Deploy the smart contract to the Ethereum mainnet with Web3.js.

Here is an example of how to deploy our “Hello World” smart contract to the Ethereum mainnet with Infura and Web3.js:

const Web3 = require('web3');

const web3 = new Web3('https://mainnet.infura.io/v3/YOUR-API-KEY');

const contract = new web3.eth.Contract(ABI, bytecode);

contract.deploy({
  data: bytecode,
  arguments: []
}).send({
  from: 'YOUR-ACCOUNT-ADDRESS',
  gas: 1500000,
  gasPrice: '30000000000000'
}, (error, transactionHash) => {
  console.log(transactionHash);
}).on('error', (error) => {
  console.error(error);
}).then((newContractInstance) => {
  console.log(newContractInstance.options.address);
});

This code creates a new instance of our smart contract with the web3.eth.Contract constructor, and deploys it to the Ethereum mainnet with the contract.deploy() method. The from parameter specifies the address of the account that will be used to deploy the contract, and the gas and gasPrice parameters specify the amount of gas and the gas price that will be used to execute the deployment.

Once the contract is deployed, the transaction hash and contract address will be output to the console. You can use the transaction hash to track the status of the deployment on the Ethereum blockchain, and you can use the contract address to interact with the contract.

Interacting with Smart Contracts

Now that our smart contract is deployed to the Ethereum blockchain, we can interact with it using the Web3.js API.

To call a function on our smart contract, we can use the contract.methods.functionName() method. For example, to call the greet() function on our “Hello World” smart contract, we can use the following code:

contract.methods.greet().call().then((result) => {
  console.log(result);
});

This code will output the result of the greet() function to the console. In this case, it will output the string “Hello, World!”.

You can also use the contract.methods.functionName() method to send transactions to your smart contract. Transactions are used to modify the state of a smart contract, and they require gas to execute.

For example, to send a transaction to our “Hello World” smart contract, we can use the following code:

contract.methods.functionName(functionArguments).send({
  from: 'YOUR-ACCOUNT-ADDRESS',
  gas: 1500000,
  gasPrice: '30000000000000'
}, (error, transactionHash) => {
  console.log(transactionHash);
}).on('error', (error) => {
  console.error(error);
});

This code sends a transaction to the functionName() function of our smart contract, with the specified functionArguments. The from parameter specifies the address of the account that will be used to send the transaction, and the gas and gasPrice parameters specify the amount of gas and the gas price that will be used to execute the transaction.

Conclusion

Writing and deploying smart contracts with Solidity is an essential skill for any blockchain developer working with the Ethereum blockchain. By following the steps outlined in this article, you can easily get started with Solidity and deploy your own smart contracts to the Ethereum blockchain. With a little bit of practice and the right tools, you will be well on your way to becoming a proficient blockchain developer.

Exercises

To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

What is Solidity and what is it used for?

Solidity is a programming language for writing smart contracts on the Ethereum blockchain. It is used by developers to build decentralized applications (DApps) that can execute automatically and transparently on the Ethereum blockchain.

What is a smart contract?

A smart contract is a self-executing contract with the terms of the agreement between buyer and seller being directly written into lines of code. They are used to facilitate, verify, and enforce the negotiation or performance of a contract.

How do you compile a Solidity smart contract?

To compile a Solidity smart contract, you can use a Solidity compiler, such as Remix (https://remix.ethereum.org). To compile a smart contract with Remix, paste the code for your smart contract into the Solidity compiler and click the “Compile” button.

How do you deploy a smart contract to the Ethereum blockchain?

To deploy a smart contract to the Ethereum blockchain, you will need to use a Web3 provider, such as Infura (https://infura.io). To deploy a smart contract with Infura, you will need to create an Infura account and retrieve your API keys, connect to the Ethereum mainnet with Web3.js and Infura, create an instance of your smart contract with Web3.js, and use the contract.deploy() method to deploy your smart contract to the Ethereum blockchain.

How do you interact with a smart contract on the Ethereum blockchain?

To interact with a smart contract on the Ethereum blockchain, you can use the contract.methods.functionName() method of the Web3.js API. To call a function on your smart contract, use the contract.methods.functionName().call() method, and to send a transaction to your smart contract, use the contract.methods.functionName().send() method.