-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsolidity1.sol
More file actions
189 lines (155 loc) · 5.34 KB
/
solidity1.sol
File metadata and controls
189 lines (155 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
contract test{
uint256 public counter;
address private owner;
// constructor que me permite mandarle ethers y establece como owner del contrato a quien lo crea
constructor() payable {
owner=msg.sender;
}
// Funcion para ver los limites de un tipo
function intLimits() external pure returns (int8,int8) {
return ( (type(int8).min),(type(int8).max) );
}
// generacion de un underflow
function underflow() external pure returns (int8) {
return ( type(int8).min / (-1) );
}
// estudio de side effects
function Counter() external {
if (1 == 0 && count()){} // Que pasa con counter? se incrementa o no?
}
function count() internal returns(bool) {
counter++;
return(true);
}
// Alguien sabría decir por que falla?
function underflow2() external pure returns (uint16) {
uint16 a= 255 + (true ? 1 : 0) ;
return a;
}
// cualquiera saca los ethers
function transferEtherAny() external {
address payable _to;
_to = payable (msg.sender); // por que debo hacer este casteo?
_to.transfer(address(this).balance);
}
// solo el owner saca los ethers. Utilizo send para ver la diferencia con transfer. Call y delegateCall los veremos luego
modifier onlyOwner(){
require(owner==msg.sender,"no eres el owner");
_;
}
function transferEtherOwner() external onlyOwner {
address payable _to;
_to = payable (msg.sender);
if( _to.send(address(this).balance) == false ){
revert("fallo el envio");
}
}
// structs + mapping + variables publicas
struct Person{
string name;
uint256 id;
}
mapping (address => Person) public myInfo;
function cargarDatos(string calldata _name, uint256 _id) external {
myInfo[msg.sender].name = _name;
myInfo[msg.sender].id = _id;
}
// struct + array + variables publicas (Ver diferencia de gas con la anterior)
Person[] public myInfoArray;
function cargarDatosArray(string calldata _name, uint256 _id) external {
Person memory aux;
aux.name = _name;
aux.id = _id;
myInfoArray.push(aux);
}
// Devolver todo el aray myInfoArray (el default getter no me devuelve todo el array, esto si)
function getMyInfoArray() external view returns (Person[] memory) {
return myInfoArray;
}
//bytes
bytes1 public _bytes1="a";
bytes2 public _bytes2="ab";
bytes public _bytesArray;//0x606162 => igual a string no alineado a 256
function _bytes3Setter(bytes calldata _byteArray) external { // ver memory vs calldata
_bytesArray = _byteArray;
}
//operaciones sobre literales
function sumaConstante() external pure returns(uint256){
uint256 a=1;
return ( 2.5 + 2.5 + a );
// return ( 2.5 + a + 2.5 ); // Por que esta mal?
}
function compararTiempo(bool _choice) external pure returns(bool)
{
if(_choice){
return(60 minutes == 1 hours);
}
else{
return(61 minutes == 1 hours);
}
}
function compararMoneda(bool _choice) external pure returns(bool)
{
if(_choice){
return(1000000000000000000 wei == 1 ether);
}
else{
return(1400000000000000000 wei == 1 ether);
}
}
// vimos hasta aca el 17/1/2023
//Enumeraciones y manejo de estados
enum Estado{ // Maxio puede tener 256 miembros y eso es porque está contenido en un unit8
prendido,
apagado
}
Estado public state;
function toggleState() external {
if(state==Estado.prendido) {
state=Estado.apagado;
}
else {
state=Estado.prendido;
}
}
// push para arrays del tipo storage
uint16[] public valuePushed;
function agregarValor(uint16 _agregar) public {
valuePushed.push(_agregar); // push() es de costo fijo != push(algo)... Pop es variable en funcion de lo que le pasemos como push(algo)
}
// Copia por referencia (memoria a memoria o storage a storage)
// Ojo con esto y evitar dangling references porque no se sabe como se comportara
uint32[] public referencia;
function modificarReferencia(uint32 _number) external {
uint32[] storage _referencia = referencia;
_referencia.push(_number);
}
// string por bytes
string public curso="Blockchain";
function modificarCurso(bytes1 _letra) external {
//curso[0]=_letra; // no se puede
bytes(curso)[0]=_letra; // 0x63
// bytes es cmo si fuera un bytes1[] pero optimiza memoria no alineandola a 256bits
}
//comparacion de strings (La comparacion no puede ser en strings y por eso se hashea)
function comparar(string calldata a, string calldata b) pure external returns (string memory) {
if( keccak256(bytes(a)) == keccak256(bytes(b)) ) {
return ("iguales");
}
else {
return ("distintos");
}
}
}
// trabajando al contrato como si fuera su propio tipo
contract testSup{
test contrato;
constructor(address _addr){
contrato=test(_addr);
}
function callContrato() external view returns(int8, int8){
return contrato.intLimits();
}
}