主要是指的是用NodeJs調用 提供接口供前端使用 用戶查詢和轉帳javascript
外部函數的調用前端
參與人如何保存本身已經參與的項目java
定義數據: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]; } }
把數據放到新的合約中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); } }
存數據: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); } }