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 |
// SPDX-License-Identifier: MIT pragma solidity >=0.6.12 <0.9.0; /** * @title HouseTransaction * @dev A contract to simulate a transaction between two buyers. One writes "casa" and the other transfers Ether. */ contract HouseTransaction { address public writer; address public buyer; string public writtenWord; uint public transferAmount; bool public wordWritten; bool public paymentMade; event TransactionCompleted(address writer, address buyer); /** * @dev Initializes the contract, setting the writer and buyer addresses. * @param _writer The address of the person who will write the word "casa". * @param _buyer The address of the person who will transfer Ether. */ constructor(address _writer, address _buyer) { writer = _writer; buyer = _buyer; wordWritten = false; paymentMade = false; } /** * @dev Allows the writer to write the word "casa". * @param _word The word to be written. */ function writeWord(string memory _word) public { require(msg.sender == writer, "Only the designated writer can write the word."); require(keccak256(bytes(_word)) == keccak256(bytes("casa")), "The word must be 'casa'."); writtenWord = _word; wordWritten = true; checkCompletion(); } /** * @dev Allows the buyer to make the payment. */ function makePayment() public payable { require(msg.sender == buyer, "Only the designated buyer can make the payment."); require(msg.value > 0, "Payment must be greater than 0."); transferAmount = msg.value; paymentMade = true; checkCompletion(); } /** * @dev Checks if both conditions are met and emits the completion event. */ function checkCompletion() internal { if (wordWritten && paymentMade) { emit TransactionCompleted(writer, buyer); } } } |

Explicación del Contrato:
- Variables de Estado:
writer
: Dirección del usuario que debe escribir la palabra «casa».buyer
: Dirección del usuario que debe realizar la transferencia de Ether.writtenWord
: Almacena la palabra escrita por elwriter
.transferAmount
: Cantidad de Ether transferida por elbuyer
.wordWritten
ypaymentMade
: Boleanos que indican si las acciones correspondientes se han completado.
- Constructor:
- Inicializa las direcciones de
writer
ybuyer
, y establece los indicadoreswordWritten
ypaymentMade
en falso.
- Inicializa las direcciones de
- Funciones:
writeWord
: Permite alwriter
escribir la palabra «casa». Verifica que la palabra escrita sea «casa» y que sea escrita por elwriter
.makePayment
: Permite albuyer
realizar una transferencia de Ether. Verifica que el remitente sea elbuyer
y que la cantidad transferida sea mayor que 0.checkCompletion
: Comprueba si ambas condiciones (wordWritten
ypaymentMade
) se cumplen y, si es así, emite un eventoTransactionCompleted
.
- Evento:
TransactionCompleted
: Se emite cuando ambas condiciones se han cumplido, notificando que la transacción está completa.