Ir ao contido principal

Axude a traducir esta p谩xina


Est谩 a ver esta p谩xina en ingl茅s porque a铆nda non a traducimos. Ax煤denos a traducir este contido.

Traducir p谩xina

No bugs here!馃悰

This page is not being translated. We've intentionally left this page in English for now.

Deploying your first smart contract

smart contractsremixsoliditydeploying
馃搯3 de abril de 2020
鈴憋笍4 minute read

Tip author 0x19dE91Af973F404EDF5B4c093983a7c6E3EC8ccE

I guess you are as excited as us to deploy and interact with your first smart contract on the Ethereum blockchain.

Don鈥檛 worry, as it鈥檚 our first smart contract, we鈥檒l deploy it on a local test network so it does not cost anything for you to deploy and play as much as you鈥檇 like with it.

Writing our contract

First step is to visit Remix and create a new file. On the upper left part of the Remix interface add a new file and enter the file name you want.

Adding a new file in the Remix interface

In the new file, we鈥檒l paste the following code.

1// SPDX-License-Identifier: MIT
2pragma solidity >=0.5.17;
4contract Counter {
6 // Public variable of type unsigned int to keep the number of counts
7 uint256 public count = 0;
9 // Function that increments our counter
10 function increment() public {
11 count += 1;
12 }
14 // Not necessary getter to get the count value
15 function getCount() public view returns (uint256) {
16 return count;
17 }
Mostrar todo
馃搵 Copiar

If you鈥檙e used to programming you can easily guess what this program does. Here is an explainer line by line:

  • Line 4: We define a contract with the name Counter.
  • Line 7: Our contract stores one unsigned integer named count starting at 0.
  • Line 10: The first function will modify the state of the contract and increment() our variable count.
  • Line 15: The second function is just a getter to be able to read the value of the count variable outside of the smart contract. Note that, as we defined our count variable as public this is not necessary but is shown as an example.

This is all for our first simple smart contract. As you may know, it looks like a class from OOP (Object-Oriented Programming) languages like Java or C++. It鈥檚 now time to play with our contract.

Deploying our contract

As we wrote our very first smart contract, we鈥檒l now deploy it to the blockchain to be able to play with it.

Deploying the smart contract on the blockchain is actually just sending a transaction containing the code of the compiled smart contract without specifying any recipients.

We鈥檒l first compile the contract by clicking on the compile icon on the left hand side:

The compile icon in the Remix toolbar

Then click on the compile button:

The compile button in the Remix solidity compiler

You can choose to select the 鈥淎uto compile鈥 option so the contract will always be compiled when you save the content on the text editor.

Then navigate to the deploy and run transactions screen:

The deploy icon in the Remix toolbar

Once you are on the "deploy and run" transactions screen, double check that your contract name appears and click on Deploy. As you can see on the top of the page, the current environment is 鈥淛avaScript VM鈥 that means that we鈥檒l deploy and interact with our smart contract on a local test blockchain to be able to test faster and without any fees.

The deploy button in the Remix solidity compiler

Once you've clicked the 鈥淒eploy鈥 button, you鈥檒l see your contract appear on the bottom. Click the arrow on the left to expand it so we鈥檒l see the content of our contract. This is our variable counter, our increment() function and the getter getCounter().

If you click on the count or getCount button, it will actually retrieve the content of the contract鈥檚 count variable and display it. As we did not called the increment function yet, it should display 0.

The function button in the Remix solidity compiler

Let鈥檚 now call the increment function by clicking on the button. You鈥檒l see logs of the transactions that are made appearing on the bottom of the window. You鈥檒l see that the logs are different when you鈥檙e pressing the button to retrieve the data instead of the increment button. It鈥檚 because reading data on the blockchain does not need any transactions (writing) or fees. Because only modifying the state of the blockchain requires to make a transaction:

A log of transactions

After pressing the increment button that will generate a transaction to call our increment() function if we click back on the count or getCount buttons we鈥檒l read the newly updated state of our smart contract with the count variable being bigger than 0.

Newly updated state of the smart contract

In the next tutorial, we鈥檒l cover how you can add events to your smart contracts. Logging events is a convenient way to debug your smart contract and understand what is happening while calling a function.

脷ltima edici贸n: , Invalid DateTime
Editar p谩xina

Was this page helpful?