Solidity로 간단한 블로그 엔진 만들기
우리가 할 일에 대한 간략한 개요.
Remix은 스마트 계약을 작성하고 배포하는 데 사용할 수 있는 온라인 견고성 편집기입니다. 우리는 오늘 그것을 사용할 것입니다.
리믹스로 이동하면 아래와 같은 내용이 있어야 합니다.

계약 디렉토리를 클릭하고 파일을 생성합니다
Blog.sol.이 스니펫을 파일에 추가하십시오.
// SPDX-License-Identifier: unlicensed
pragma solidity ^0.8.0;
contract Blog {}
SPDX 라이선스 식별자는 무엇입니까?
SPDX License Identifiers can be used to indicate relevant license information at any level, from package to the source code file level
pragma solidity ^0.8.0는 EVM(Ethereum Virtual Machine)에 우리가 사용 중인 견고성의 버전을 알려줍니다. 모든 것이 예상대로 작동하는지 확인하려면 solidity 컴파일러로 이동하여 파란색 버튼compile Blog.sol을 클릭하면 제대로 작동합니다.다음으로 게시물의 모양을 정의해 보겠습니다.
     uint256 _id = 1;
    struct Post{
       uint256 id;
       string  title;
       address owner;
       string body;
       bool isPublished;
       uint256 createdAt;
       uint256 publishedAt;
    }
    mapping(uint256 => Post) posts;
여기에는 카운터 역할을 하는 변수 _id와 새로운 데이터 유형을 정의하는 데 사용되는 구조체가 있습니다. 매핑은 키-값 쌍인 Javascript의 객체와 매우 유사합니다.
게시물 작성
게시물을 작성하려면 아래 스니펫을 추가하세요.
    function createPost(string memory _title, string memory _body, bool _isPublished) public {
        Post storage newPost = post[_id];
        newPost.id = _id;
        newPost.title = _title;
        newPost.owner = msg.sender;
        newPost.body = _body;
        newPost.isPublished = _isPublished;
        newPost.createdAt = block.timestamp;
        if (_isPublished){
            newPost.publishedAt = block.timestamp;
        }
        _id++;
    }
그래서 우리는 공개적으로 사용 가능한 방법을 만들었습니다.
msg.sender 이것은 현재 사용자가 계약과 상호 작용할 수 있도록 블록체인에서 액세스할 수 있는 전역 변수입니다. Solidity에서 block.timestamp는 현재 시간을 제공합니다.참고
memory 임시 값을 저장하는 데 사용됩니다. (외부) 함수 호출 간에 지워지며 사용 비용이 저렴합니다.게시물 받기
단일 게시물을 얻으려면 아래와 같이 게시물의
id를 사용하여 찾을 것입니다.
    function getPost(uint256  id) public view  returns ( Post memory) {
        return post[id];
    }
위의 함수는 public의 가시성을 가지며
view EVM에 상태가 수정자가 아님을 알리고 Post를 반환하는 getter 메서드입니다.게시물 나열
모든 게시물의 목록을 반환할 예정입니다.
function listPost() public view returns (Post [] memory){
        Post[] memory posts = new Post[](_id);
        for (uint i = 0; i < _id; i++) {
            posts[i] = post[_id];
        }
        return posts;
    }
우리는 id 카운터의 길이 목록을 생성한 매핑을 반환할 수 없기 때문에 게시물 목록만 반환하고 있습니다.
게시물 게시
여기에서 게시물의 상태를 업데이트하고 게시 날짜를 추가합니다.
    function publishPost(uint256 id, bool isPublished) public  returns (Post memory){
        // error handling
        require(posts[id].owner == msg.sender, "Not allowed");
        posts[id].isPublished = isPublished;
        posts[id].publishedAt = block.timestamp;
        return posts[id];
    }
당신은 지금이 같은 것을해야합니다
// SPDX-License-Identifier: unlicensed
pragma solidity ^0.8.0;
contract Blog {
    uint256 _id;
    struct Post{
       uint256 id;
       string  title;
       address owner;
       string body;
       bool isPublished;
       uint256 createdAt;
       uint256 publishedAt;
    }
    mapping(uint256 => Post) posts;
    function createPost(string memory _title, string memory _body, bool _isPublished) public {
        Post storage newPost = posts[_id];
        newPost.id = _id;
        newPost.title = _title;
        newPost.owner = msg.sender;
        newPost.body = _body;
        newPost.isPublished = _isPublished;
        newPost.createdAt = block.timestamp;
        if (_isPublished){
            newPost.publishedAt = block.timestamp;
        }
        _id++;
    }
    function getPost(uint256  id) public view  returns ( Post memory) {
        return posts[id];
    }
    function listPost() public view returns (Post [] memory){
        Post[] memory result = new Post[](_id);
        for (uint i = 0; i < _id; i++) {
            result[i] = posts[_id];
        }
        return result;
    }
    function publishPost(uint256 id, bool isPublished) public  returns (Post memory){
        // error handling
        require(posts[id].owner == msg.sender, "Not allowed");
        posts[id].isPublished = isPublished;
        posts[id].publishedAt = block.timestamp;
        return posts[id];
    }
}
이제 스마트 계약을 테스트하려면 배치 및 스마트 계약 탭으로 이동하여 테스트할 수 있도록 배치하십시오. 끝까지 버티셨다면 축하드립니다.
Reference
이 문제에 관하여(Solidity로 간단한 블로그 엔진 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/6igo/creating-a-simple-blog-engine-with-solidity-4lb9텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)