forked from AmazingAng/WTF-Solidity
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/AmazingAng/WTF-Solidity
- Loading branch information
Showing
97 changed files
with
2,640 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
te | ||
loath | ||
loath | ||
amountIn |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.21; | ||
contract Overload { | ||
function saySomething() public pure returns(string memory){ | ||
return("Nothing"); | ||
} | ||
|
||
function saySomething(string memory something) public pure returns(string memory){ | ||
return(something); | ||
} | ||
|
||
function f(uint8 _in) public pure returns (uint8 out) { | ||
out = _in; | ||
} | ||
|
||
function f(uint256 _in) public pure returns (uint256 out) { | ||
out = _in; | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
--- | ||
Título: 16. Sobrecarga | ||
tags: | ||
- solidity | ||
- advanced | ||
- wtfacademy | ||
- overloading | ||
--- | ||
# Tutorial WTF Solidity: 16. Sobrecarga | ||
|
||
Recientemente, he estado revisando Solidity y escribiendo tutoriales en "WTF Solidity" para principiantes. | ||
|
||
Twitter: [@0xAA_Science](https://twitter.com/0xAA_Science) | [@WTFAcademy_](https://twitter.com/WTFAcademy_) | ||
|
||
Comunidad: [Discord](https://discord.gg/5akcruXrsk)|[Wechat](https://docs.google.com/forms/d/e/1FAIpQLSe4KGT8Sh6sJ7hedQRuIYirOoZK_85miz3dw7vA1-YjodgJ-A/viewform?usp=sf_link)|[Sitio web wtf.academy](https://wtf.academy) | ||
|
||
La traducción al español ha sido realizada por Jonathan Díaz con el objetivo de hacer estos recursos accesibles a la comunidad de habla hispana. | ||
|
||
Twitter: [@jonthdiaz](https://twitter.com/jonthdiaz) | ||
|
||
Los códigos y tutoriales están como código abierto en GitHub: [github.com/AmazingAng/WTFSolidity](https://github.com/AmazingAng/WTFSolidity) | ||
|
||
----- | ||
|
||
## Sobrecarga | ||
`solidity` permite la sobrecarga de funciones (`overloading`). Es decir, funciones con el mismo nombre pero diferentes tipos de parámetros de entrada pueden existir al mismo tiempo, | ||
y se consideran funciones diferentes. | ||
Note que `solidity` no permite la sobrecarga de modificadores (`modifier`). | ||
|
||
### Sobrecarga de Funciones | ||
Por ejemplo, se podría definir dos funciones llamadas `saySomething()`: | ||
una sin argumentos y que devuelva `"Nothing"`, la otra que tome un argumento de tipo `string` y devuelva un `string`. | ||
|
||
```solidity | ||
function saySomething() public pure returns(string memory){ | ||
return("Nothing"); | ||
} | ||
function saySomething(string memory something) public pure returns(string memory){ | ||
return(something); | ||
} | ||
``` | ||
|
||
Después de compilar, todas las funciones de sobrecarga se convierten en selectores de funciones diferentes debido a los diferentes tipos de parámetros. | ||
Para ver el contenido específico del selector de una función, consultar [WTF Solidity Tutorial: 29. Function Selector](https://github.com/AmazingAng/WTFSolidity/tree/main/29_Selector) | ||
|
||
Tomando el contrato `Overloading.sol` como ejemplo, después de compilar y desplegar en Remix. | ||
Después de llamar a las funciones de sobrecarga `saySomething()` y `saySomething(string memory something)` respectivamente, | ||
se pueden ver diferentes resultados, ya que las funciones se consideran diferentes. | ||
|
||
![](./img/16-1.jpeg) | ||
|
||
### Coincidencia de Argumentos | ||
|
||
Cuando se llama a la función de sobrecarga, el tipo de variable se emparejará entre el parámetro de entrada y los parámetros de la función. | ||
Se generará un error si hay múltiples funciones de sobrecarga coincidentes, | ||
El siguiente ejemplo tiene dos funciones llamadas `f()`, una tiene el parámetro `uint8` y la otra tiene `uint256`: | ||
|
||
```solidity | ||
function f(uint8 _in) public pure returns (uint8 out) { | ||
out = _in; | ||
} | ||
function f(uint256 _in) public pure returns (uint256 out) { | ||
out = _in; | ||
} | ||
``` | ||
El número `50` se puede convertir tanto a `uint8` como a `uint256`, por lo que se generará un error si se llama a `f(50)`. | ||
|
||
## Resumen | ||
En esta lección, se presento el uso básico de la función de sobrecarga en `solidity`: | ||
las funciones con el mismo nombre pero diferentes tipos de parámetros de entrada pueden existir al mismo tiempo, | ||
y se consideran funciones diferentes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.21; | ||
|
||
library Strings { | ||
bytes16 private constant _HEX_SYMBOLS = "0123456789abcdef"; | ||
|
||
/** | ||
* @dev Convierte un `uint256` a su representación decimal ASCII `string`. | ||
*/ | ||
function toString(uint256 value) public pure returns (string memory) { | ||
// Inspirado en la implementación de OraclizeAPI - licencia MIT | ||
// https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol | ||
|
||
if (value == 0) { | ||
return "0"; | ||
} | ||
uint256 temp = value; | ||
uint256 digits; | ||
while (temp != 0) { | ||
digits++; | ||
temp /= 10; | ||
} | ||
bytes memory buffer = new bytes(digits); | ||
while (value != 0) { | ||
digits -= 1; | ||
buffer[digits] = bytes1(uint8(48 + uint256(value % 10))); | ||
value /= 10; | ||
} | ||
return string(buffer); | ||
} | ||
|
||
/** | ||
* @dev Convierte un `uint256` a su representación hexadecimal ASCII `string`. | ||
*/ | ||
function toHexString(uint256 value) public pure returns (string memory) { | ||
if (value == 0) { | ||
return "0x00"; | ||
} | ||
uint256 temp = value; | ||
uint256 length = 0; | ||
while (temp != 0) { | ||
length++; | ||
temp >>= 8; | ||
} | ||
return toHexString(value, length); | ||
} | ||
|
||
/** | ||
* @dev Convierte un `uint256` a su representación hexadecimal ASCII `string` con longitud fija. | ||
*/ | ||
function toHexString(uint256 value, uint256 length) public pure returns (string memory) { | ||
bytes memory buffer = new bytes(2 * length + 2); | ||
buffer[0] = "0"; | ||
buffer[1] = "x"; | ||
for (uint256 i = 2 * length + 1; i > 1; --i) { | ||
buffer[i] = _HEX_SYMBOLS[value & 0xf]; | ||
value >>= 4; | ||
} | ||
require(value == 0, "Strings: insuficiente longitud hexadecimal"); | ||
return string(buffer); | ||
} | ||
} | ||
|
||
|
||
// Llamando a otro contrato de biblioteca con una función | ||
contract UseLibrary{ | ||
// Usando la biblioteca con el comando "using for" | ||
using Strings for uint256; | ||
function getString1(uint256 _number) public pure returns(string memory){ | ||
// Las funciones de la biblioteca se agregan automáticamente como miembros de las variables de tipo uint256 | ||
return _number.toHexString(); | ||
} | ||
|
||
// Llamado directamente por el nombre del contrato | ||
function getString2(uint256 _number) public pure returns(string memory){ | ||
return Strings.toHexString(_number); | ||
} | ||
} |
Oops, something went wrong.