以太坊DAPP[3]-博彩-react特性与博彩合约实例

栏目: 服务器 · 发布时间: 6年前

内容简介:在之前的课程中,我们介绍了如何编译与部署合约。现在,我们假设已经把合约部署到了ropsten的网络之上。得到了地址。现在,我们需要通过合约的ABI接口与地址来构建博彩合约的实例。新建文件:src/lottery.js:

博彩实例

在之前的课程中,我们介绍了如何编译与部署合约。现在,我们假设已经把合约部署到了ropsten的网络之上。得到了地址。

现在,我们需要通过合约的ABI接口与地址来构建博彩合约的实例。

新建文件:src/lottery.js:

import web3 from './web3';
const address = '0x94A261B507c4388f2ADcFfaA5685ffdF53ebcba5';

const abi = [{"constant":true,"inputs":[],"name":"manager","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"getPlayers","outputs":[{"name":"","type":"address[]"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"enetr","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[],"name":"pickwiner","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"players","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"}];


export default new web3.eth.Contract(abi,address);

src/App.js:

componentDidMount为react class的生命周期函数,其在reander之后执行。state是react中特殊的属性,其只是存在于class component中,用于存储属性。

当state中的状态变化之后,都会重新的执行reander函数,从而让页面展现出不同的效果。下面获取了管理者的地址并存储在了state状态变量中。在页面中显示了管理者的地址。

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import web3 from './web3';
import lottery from './lottery';

class App extends Component {

   state = {
     manager:'',
   }

  async componentDidMount(){
      const manager = await lottery.methods.manager().call();
        this.setState({manager});
  }


  render() {
    return (
      <div>
        <h1>lottery管理者地址:</h1>
        <p>this is manager by  {this.state.manager}</p>
      </div>
    );
  }
}
export default App;

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Agile Web Application Development with Yii 1.1 and PHP5

Agile Web Application Development with Yii 1.1 and PHP5

Jeffrey Winesett / Packt Publishing / 2010-08-27

In order to understand the framework in the context of a real-world application, we need to build something that will more closely resemble the types of applications web developers actually have to bu......一起来看看 《Agile Web Application Development with Yii 1.1 and PHP5》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具