链上链下交互|链上链下交互 以太坊Dapp接口开发

主要是指的是用NodeJs调用 提供接口供前端使用 用户查询和转账
以太坊Dapp项目 众筹项目 功能需求

  • 路人
    • 查看所有众筹项目, 2 @ OK
    • 根据众筹项目的address获取该众筹的详情 (参与人数/已筹金额/目标金额/结束时间/参与人数),2.5 @ OK
    • 参与众筹项目, 3 @ OK
  • 众筹发起人
    • 创建众筹, 1 @ OK
    • 查看自己创建的众筹, 5 @ OK
    • 发起付款请求, 6
  • 众筹参与者
    • 查看已参与的众筹项目, 4 @ OK
    • 审批付款请求, 7
  • 获取当前account @ OK
智能合约及测试
  • 外部函数的调用
    参与人如何保存自己已经参与的项目
    1. 定义数据:
      // 用于保存 参与者的所有参与的项目,每一个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; returnplayersFundings[sender]; }}

      ?
    2. 把数据放到新的合约中PlayerToFundings。
      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); } }

  • demo
    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
【链上链下交互|链上链下交互 以太坊Dapp接口开发】转载于:https://www.cnblogs.com/xiaocongcong888/p/9553300.html

    推荐阅读