鏈上鍊下交互 以太坊Dapp接口開發

主要是指的是用NodeJs調用 提供接口供前端使用 用戶查詢和轉帳javascript

以太坊Dapp項目

衆籌項目

功能需求

  • 路人
    • 查看全部衆籌項目, 2 @ OK
    • 根據衆籌項目的address獲取該衆籌的詳情 (參與人數/已籌金額/目標金額/結束時間/參與人數),2.5 @ OK
    • 參與衆籌項目, 3 @ OK
  • 衆籌發起人
    • 建立衆籌, 1 @ OK
    • 查看本身建立的衆籌, 5 @ OK
    • 發起付款請求, 6
  • 衆籌參與者
    • 查看已參與的衆籌項目, 4 @ OK
    • 審批付款請求, 7
  • 獲取當前account @ OK

智能合約及測試

  • 外部函數的調用前端

    參與人如何保存本身已經參與的項目java

    1. 定義數據:web

      // 用於保存 參與者的全部參與的項目,每個FundingFactory只有一個
      contract PlayerToFundings {
      
          mapping(address => address[]) playersFundings;
      
          // 添加接口 (在Funding的support中被調用)
          function joinFunding(address funding, address sender) public{
              playersFundings[sender].push(funding);
          }
      
          // 查詢接口 (在FundingFactory中被調用)
          function getFundings(address sender) public view returns(address[] fundings){
              // return msg.sender;
              return  playersFundings[sender];
          }
      
      }

    2. 把數據放到新的合約中PlayerToFundings。app

      mapping(address => address[]) private playerToFunings;函數

      contract FundingFactory {
      
          PlayerToFundings playerToFundings;
          // 初始化PlayerToFundings合約
          constructor() public {
              address playerToFundingsAddress = new PlayerToFundings();
              playerToFundings = PlayerToFundings(playerToFundingsAddress);
          }
      
          // * 提供獲取合約中數據的函數
          function getPlayerFoundings() public view returns(address[]){
              return playerToFundings.getFundings(msg.sender);
          }
      
      }

    3. 存數據:Funding support時候存數據測試

      contract Funding {
      
        // 在factory建立Funding時,把PlayerToFundings傳進來。
          constructor (string _projectName, uint _supportMoney, uint _goalMoney, PlayerToFundings _p2f,address _address) public {
            ...
              p2f = _p2f;
          }
      
      
        // * 調用p2f, 把參與者參與的項目存到PlayerToFundings的mapping中
          funtion support() public payable {
            ...
              players.push(msg.sender);
              p2f.joinFunding(address(this), msg.sender);
          }
      }
  • demoui

    pragma solidity ^0.4.17;
    
    contract PlayerToFundings {
    
        uint count = 100;
    
        function setFundingsCount(uint _count) public {
            count = _count;
        }
    
        function getFundingsCount() public view returns(uint){
            return count;
        }
    
    }
    
    contract Factory {
    
        PlayerToFundings p2f;
    
        address[] public fundings;
        // function Test(address p2fAddress) public{
        //     p2f = PlayerToFundings(p2fAddress);
        // }
        function Factory() public{
            address p2fAddress = new PlayerToFundings();
            p2f = PlayerToFundings(p2fAddress);
    
        }
    
        function createFounding() public {
            address funding = new Funding(p2f);
            fundings.push(funding);
        }
    
        function setCount(uint count) public {
            p2f.setFundingsCount(count);
        }
    
        function getCount() public view returns(uint){
            return p2f.getFundingsCount();
        }
    }
    
    contract Funding {
        PlayerToFundings p2f;
    
        function Funding(PlayerToFundings _p2f) public{
            p2f = _p2f;
        }
    
        function support() public {
            p2f.setFundingsCount(999);
        }
    }

智能合約及測試(web3.js)

  • interaction.js智能合約代碼封裝
    • 建立合約 createFunding
相關文章
相關標籤/搜索