Ethereum Developer
Imagine you are an experienced Ethereum developer tasked with creating a smart contract for a blockchain messenger.
| Category | Development › Coding |
|---|---|
| Tags | DraftingDeveloperCode |
Imagine you are an experienced Ethereum developer tasked with creating a smart contract for a blockchain messenger. The objective is to save messages on the blockchain, making them readable (public) to everyone, writable (private) only to the person who deployed the contract, and to count how many times the message was updated. Develop a Solidity smart contract for this purpose, including the necessary functions and considerations for achieving the specified goals. Please provide the code and any relevant explanations to ensure a clear understanding of the implementation.
What this prompt does
This is a development prompt for creating an Ethereum smart contract example. It is scoped to public message reading, deployer-only writing, and tracking how many times the message is updated.
Real outputs by model
Read full answers
We gave three models the same input and copied their answers unedited. Each ran in its CLI (an agent harness), and answers in the ChatGPT or Claude apps or on the web may differ. Outputs are in Korean.
아래 계약은 다음 요구사항을 충족합니다.
- 누구나 메시지를 읽을 수 있음
- 계약 배포자만 메시지를 변경할 수 있음
- 메시지가 변경된 횟수를 저장함
- 메시지 변경 이벤트를 기록함
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract BlockchainMessenger {
address public immutable owner;
string private message;
uint256 public updateCount;
error NotOwner();
error EmptyMessage();
event MessageUpdated(
address indexed updater,
string newMessage,
uint256 updateCount
);
constructor(string memory initialMessage) {
if (bytes(initialMessage).length == 0) {
revert EmptyMessage();
}
owner = msg.sender;
message = initialMessage;
updateCount = 0;
}
modifier onlyOwner() {
if (msg.sender != owner) {
revert NotOwner();
}
_;
}
/// @notice 현재 메시지를 누구나 읽을 수 있습니다.
function getMessage() external view returns (string memory) {
return message;
}
/// @notice 계약 배포자만 메시지를 변경할 수 있습니다.
function updateMessage(string calldata newMessage)
external
onlyOwner
{
if (bytes(newMessage).length == 0) {
revert EmptyMessage();
}
message = newMessage;
updateCount++;
emit MessageUpdated(msg.sender, newMessage, updateCount);
}
}
주요 구성
owner
- 생성자에서
msg.sender를 저장합니다. immutable이므로 배포 후 변경할 수 없습니다.- 계약을 배포한 주소가 소유자가 됩니다.
getMessage()
external view함수이므로 누구나 호출할 수 있습니다.- 블록체인 상태를 변경하지 않으므로 가스 비용 없이 읽을 수 있습니다.
updateMessage()
onlyOwnermodifier를 사용해 배포자만 실행할 수 있습니다.- 메시지를 변경할 때마다
updateCount가 1씩 증가합니다.
updateCount
- 메시지 변경 횟수를 저장합니다.
- 최초 메시지 설정은 업데이트로 계산하지 않으며,
0부터 시작합니다. - 최초 설정도 횟수에 포함하려면 생성자에서
updateCount = 1로 설정하면 됩니다.
MessageUpdated 이벤트
- 메시지가 변경될 때마다 로그를 남깁니다.
- 프론트엔드나 블록체인 탐색기가 변경 이력을 추적할 때 사용할 수 있습니다.
중요한 점은 블록체인의 데이터는 기본적으로 공개된다는 것입니다. 여기서 “쓰기 권한이 비공개”라는 의미는 배포자만 수정할 수 있다는 뜻이며, 메시지 내용 자체가 비밀이라는 뜻은 아닙니다. private 상태 변수도 블록체인에 기록되므로 완전한 비밀 저장소로 사용할 수 없습니다.
More in this category
| 12-Month AI and Computer Vision Roadmap for Defense Applications | |
| 2046 Puzzle Game Challenge | |
| 21st.dev component prompt | |
| 3D FACTORY | |
| 3D FPS Game |