Ethereum 개발 프롬프트
블록체인 메신저 요구사항을 넣으면 메시지 저장, 공개 읽기, 배포자만 쓰기, 업데이트 횟수 계산을 담은 Solidity 코드와 설명을 받습니다.
| 분류 | 개발 › 코딩 |
|---|---|
| 태그 | 초안작성개발자코드 |
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.
어떤 프롬프트인가
Ethereum 스마트 계약 예제를 만들 때 쓰는 개발 프롬프트다. 메시지 공개 읽기와 배포자 전용 쓰기, 수정 횟수 추적이라는 조건이 본문에 고정되어 있다.
모델별 실제 결과
답 전문 보기
같은 입력을 세 모델에 넣어 받은 답을 그대로 옮겼습니다. 각 CLI(에이전트 하네스)에서 실행했습니다. ChatGPT·Claude 앱이나 웹에서는 답이 다를 수 있습니다.
아래 계약은 다음 요구사항을 충족합니다.
- 누구나 메시지를 읽을 수 있음
- 계약 배포자만 메시지를 변경할 수 있음
- 메시지가 변경된 횟수를 저장함
- 메시지 변경 이벤트를 기록함
// 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 상태 변수도 블록체인에 기록되므로 완전한 비밀 저장소로 사용할 수 없습니다.
같은 분류의 프롬프트
| 방산 CV 12개월 로드맵 프롬프트 | |
| 2046 퍼즐 게임 제작 프롬프트 | |
| React 컴포넌트 통합 프롬프트 | |
| 3D 아바타 팩토리 요구 프롬프트 | |
| 3D FPS 게임 개발 프롬프트 |