contract WavePortal {
uint256 totalWaves;
//use o Google para entender o que são eventos em Solidity!
event NewWave(address indexed from, uint256 timestamp, string message);
//struct = tipo de dado customizado
//customizo o que quero armazenar dentro dele
struct Wave {
address waver; // Endereço do usuário que deu tchauzinho
string message; // Mensagem que o usuário envio
uint256 timestamp; // Data/hora de quando o usuário tchauzinhou.
}
//variável para armazenar array de structs, todos os tchauzinhos recebidos
Wave[] waves;
constructor() {
console.log("EU SOU UM CONTRATO INTELIGENTE. POG.");
}
//função do tchauzinho agora requer string chamada _message
//essa é a mensagem que o usuário enviou pelo frontend!
function wave(string memory _message) public {
totalWaves += 1;
console.log("%s tchauzinhou com a mensagem %s", msg.sender, _message);
//aqui é onde armazeno o tchauzinho no array
waves.push(Wave(msg.sender, _message, block.timestamp));
//use o Google para tentar entender o que tem de novo aqui! haha
emit NewWave(msg.sender, block.timestamp, _message);
}
//função que retorna os tchauzinhos
function getAllWaves() public view returns (Wave[] memory) {
return waves;
}
function getTotalWaves() public view returns (uint256) {
//opcional: adicione esta linha se você quer ver o contrato imprimir o valor!
console.log("Temos %d tchauzinhos no total!", totalWaves);
return totalWaves;
}
}