[BlockChain] DApp 구축 기초

HYEOB KIM·2023년 2월 28일
0

BlockChain

목록 보기
3/3

DApp 구축 기초

유언장 스마트 계약 예제

pragma solidity >=0.7.0 <0.9.0;

// 유언장 스마트 계약 : 어떤 할아버지가 돌아가시면서 증손자에게는 20 이더를, 증손녀에게는 10 이더를 주기로 했다면?
contract Will {
    // 확인 사항 3가지
    // 1. 할아버지가 정말로 돌아가셨는지, 2. 증여하는 유산은 어느 정도인지, 3. 증여자의 주소(지갑 주소)

    bool    deceased;
    uint    fortune;
    address owner;

    // 생성자 : Solidity 스마트 계약을 배포할 때 실행되는 특별한 함수로 생성자를 통해 오브젝트를 만든다. 초기값을 설정할 수 있다.
    // payable : 함수가 이더를 보내고 받을 수 있게 만든다.
    constructor() payable public {
        owner = msg.sender; // msg sender represents address that is being called
        fortune = msg.value; // msg value tells us how much ether is being sent
        deceased = false;
    }

    // 제어자(modifier) : 함수에 사용하는 애드온으로 추가적인 논리를 생성할 수 있게 한다. 조건문.
    // create modifier so that only person who can call the contract is the owner.
    modifier onlyOwner {
        require(msg.sender == owner);
        _; // 밑줄 문자를 입력하면 함수가 계속됨.
    }

    // 1. 할아버지가 정말로 돌아가셨는지
    // create modifier so that we only allocate funds if friend's gramps deceased  
    modifier mustBeDeceased {
        require(deceased == true);
        _;
    }

    // array : []
    // 3. 증여자의 주소(지갑 주소)
    // list of family wallets : 가족의 모든 지갑
    address payable[] familyWallets;

    // key-value
    // 2. 증여하는 유산은 어느 정도인지
    // map through inheritance : 누가 얼만큼의 유산을 받을지 기록되어 있음
    mapping(address => uint) inheritance;

    // set inheritance for each address : 누가 얼만큼의 유산을 받을지 설정
    function setInheritance(address payable wallet, uint amount) public {
        // to add wallets to the family wallets : .push
        familyWallets.push(wallet);
        inheritance[wallet] = amount;
    }     

    // pay each family member based on their wallet address
    function payout() private mustBeDeceased {
        // with a for loop you can loop through things and set conditions
        for(uint i=0; i<familyWallets.length; i++) {
            // transfering the funds contract address to receiver address
            familyWallets[i].transfer(inheritance[familyWallets[i]]);
        }
    }
}

탈중앙화 은행 예제

탈중앙화 은행에 투자자 지갑을 추가한 다음 자금을 할당(지불)할 수 있는 스마트 계약을 구축하는 겁니다.

스마트 계약을 완료하고 디버그와 컴파일했으면 계속해서 계약을 배포하고 테스트하세요.

성공하면 테스트 계정(IDE)에서 다른 계정으로 payInvestors 함수를 통해 자금을 보낼 수 있습니다. 선택한 자금을 몇 개의 계정에 지불하고 완료되면 checkInvestors 테스팅 함수를 실행하세요.

코드가 작동하면 트랜잭션이 성공적일 뿐만 아니라 checkInvestors 함수가 은행에 추가된 투자자 지갑 수를 반환해야 합니다!

pragma solidity >=0.7.0 <0.9.0;

contract payInventory {
    /*
    address owner;
    uint    fortune;

    constructor() payable public {
        owner = msg.sender;
        fortune = msg.value;
    }

    modifier onlyOwner {
        require(owner == msg.sender);
        _;
    }
    */
    
    address payable[] customer;

    mapping (address => uint) customerWallets;

    function payInvestors(address payable wallet, uint amount) public {
        customer.push(wallet);
        customerWallets[wallet] = amount;
    }

    function checkInvestors() public view returns (uint) {
        return customer.length;
    }
}
profile
Devops Engineer

0개의 댓글