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 |
// SPDX-License-Identifier: MIT pragma solidity >=0.6.12 <0.9.0; /** * @title WordSubmission * @dev A contract where two users submit a word and are notified when both have submitted. */ contract WordSubmission { address public user1; address public user2; string public word1; string public word2; bool public user1Submitted; bool public user2Submitted; bool public wordsMatch; // Variable para indicar si las palabras son iguales o no event WordsSubmitted(string word1, string word2, bool wordsMatch); // Se agrega la variable wordsMatch al evento /** * @dev Initializes the contract with two users. * @param _user1 The address of the first user. * @param _user2 The address of the second user. */ constructor(address _user1, address _user2) { user1 = _user1; user2 = _user2; user1Submitted = false; user2Submitted = false; wordsMatch = false; // Se inicializa en falso } /** * @dev Allows the first user to submit a word. * @param _word The word to be submitted. */ function submitWord1(string memory _word) public { require(msg.sender == user1, "Only the first user can submit this word."); word1 = _word; user1Submitted = true; checkCompletion(); } /** * @dev Allows the second user to submit a word. * @param _word The word to be submitted. */ function submitWord2(string memory _word) public { require(msg.sender == user2, "Only the second user can submit this word."); word2 = _word; user2Submitted = true; checkCompletion(); } /** * @dev Checks if both users have submitted their words and emits the event with the result of the comparison. */ function checkCompletion() internal { if (user1Submitted && user2Submitted) { // Realiza la comparación de las cadenas if (keccak256(abi.encodePacked(word1)) == keccak256(abi.encodePacked(word2))) { wordsMatch = true; } else { wordsMatch = false; } // Emite el evento con el resultado de la comparación emit WordsSubmitted(word1, word2, wordsMatch); } } } |
Capturas de pantalla del funcionamiento

Cadenas distintas

Cadenas iguales
