Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
15 Hrs
10 Min
23 Seg

C贸mo estructurar un contrato inteligente

2/15
Resources
Transcript

How to structure a smart contract in Solidity?

Structuring a smart contract in Solidity is essential to ensure that the code is efficient, understandable and maintainable. As you advance your skills as a blockchain developer, it is critical to get a handle on the key components of a contract, from initial definition to implementation, and understand how each contributes to the functioning of the code.

Which license to choose?

When developing a smart contract, the first element to consider is the license. The choice of a license determines whether the code will be free and how others will be able to reuse or share it. Some popular choices include GPL, MIT, or even no license at all. It is important to include a license comment at the beginning of the contract to avoid compiler warnings.

How to import?

Imports play a crucial role in allowing the inclusion of external contracts or libraries. This step is essential if your contract depends on external code and ensures that you are using tested and existing components.

How to choose a suitable name for the contract?

The contract name should be clear and descriptive. Make sure that when you read it, anyone can identify what it is about. While you can program in English or Spanish, English is usually preferable if you plan to share your code in public repositories. In this example, we use the name "visitor counter".

contract visit_counter { // Contract code here}

What are attributes or variables?

Attributes or variables represent the state of the contract and include information that will be stored and can be modified by users. In a basic contract, such as a visit counter, you define variables to store critical data, such as the current number of visits.

uint256 visits;

What is the function of the constructor?

The constructor is a special function that initializes the contract when it is implemented. This element allows you to set initial values, such as counting the number of visits already recorded.

constructor(uint256 initialvalue) { visits = initialvalue;}

How to develop functions and modify their behavior?

Functions bring logic and action to the contract. In a visit counter, a function incrementVisits can add to the number of visits. It can be public so that it can be accessed from outside, depending on your needs.

function incrementVisits() public { visits += 1;}

Modifiers, on the other hand, are useful tools for adding checks or restrictions to functions. In the case of the counter, you can make sure that only the address that implemented the contract can increment the counter.

modifier onlyImplementer() { require(msg.sender == implementer, "Account I don't implement the contract"); _; };}

How to organize and execute your contract in Solidity?

The Remix tool is essential for compiling and deploying contracts. Be sure to check for warnings and errors during compilation to avoid future problems. In addition, you can adjust the visibility of variables to facilitate testing or external access.

Why is it important to maintain order in the code?

Organizing your contracts is not only aesthetically pleasing; it makes them easier to maintain and improves their readability. Consider grouping functions and variables by their visibility or purpose. This will be useful not only for personal development but also when collaborating on larger projects, ensuring that any developer can follow the flow of your code.

Keep exploring and practicing. Each step brings you closer to mastering Solidity and getting the most out of your smart contract programming skills. Keep going!

Contributions 5

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

C贸mo estructurar un contrato inteligente?

Para escribir un contrato se debe tener en cuenta:

Licencia._ Se refiere al c贸digo libre, si se tiene que solicitar permisos, menciones, etc.

Importaciones._ si se debe acceder a componentes externos o librer铆as.

Nombre del contrato._ Se debe ser lo m谩s descriptivo posible para que las personas puedan entender f谩cilmente de que trata el contrato.

Variables o atributos._ El estado del contrato, la informaci贸n almacenada en el contrato.

Constructor._ Es una funci贸n. Se ejecuta al inicio del contrato, sirve para inicializar los valores.

Funciones._ Permiten agregar l贸gica al contrato ya sea para modificar valores o para hacer c谩lculos, puede o no retornar resultados.

Modificadores._ Tambi茅n son funciones, permiten de forma f谩cil agregar validaciones a la funciones.

Actualmente (Julio 2023) estamos en la versi贸n 0.8.21!
Solidity releases

Al dia de hoy (Nov 23) la version latest es la 0.8.23
Estructura recomendada para las funciones: Layout of Functions:聽 constructor 聽receive function (if exists)聽 fallback function (if exists)聽 external聽 public聽 internal聽 private internal & private view & pure functions聽 external & public view & pure functions
Estructura recomendada para los smart contracts: Layout of Contract:聽 version 聽imports聽 errors聽 interfaces, libraries, contracts聽 Type declarations聽 State variables聽 Events聽 Modifiers聽 Functions