当前位置: 首页 > news >正文

truffle框架创建合约实例代码

truffle框架来创建合约实例的代码如下:

import React, { Component } from "react";
import SimpleStorageContract from "./contracts/SimpleStorage.json";
import getWeb3 from "./utils/getWeb3";
import truffleContract from "truffle-contract";

import "./App.css";

class App extends Component {
  state = { storageValue: 0, web3: null, accounts: null, contract: null };

  componentDidMount = async () => {
    try {
      // Get network provider and web3 instance.
      const web3 = await getWeb3();

      // Use web3 to get the user's accounts.
      const accounts = await web3.eth.getAccounts();

      // Get the contract instance.
      const Contract = truffleContract(SimpleStorageContract);
      Contract.setProvider(web3.currentProvider);
      const instance = await Contract.deployed();

      // Set web3, accounts, and contract to the state, and then proceed with an
      // example of interacting with the contract's methods.
      this.setState({ web3, accounts, contract: instance }, this.runExample);
    } catch (error) {
      // Catch any errors for any of the above operations.
      alert(
        `Failed to load web3, accounts, or contract. Check console for details.`
      );
      console.log(error);
    }
  };

  runExample = async () => {
    const { accounts, contract } = this.state;

    // Stores a given value, 5 by default.
    await contract.set(5, { from: accounts[0] });

    // Get the value from the contract to prove it worked.
    const response = await contract.get();

    // Update state with the result.
    this.setState({ storageValue: response.toNumber() });
  };

  render() {
    if (!this.state.web3) {
      return <div>Loading Web3, accounts, and contract...</div>;
    }
    return (
      <div className="App">
        <h1>Good to Go!</h1>
        <p>Your Truffle Box is installed and ready.</p>
        <h2>Smart Contract Example</h2>
        <p>
          If your contracts compiled and migrated successfully, below will show
          a stored value of 5 (by default).
        </p>
        <p>
          Try changing the value stored on <strong>line 37</strong> of App.js.
        </p>
        <div>The stored value is: {this.state.storageValue}</div>
      </div>
    );
  }
}

export default App;

相关文章:

  • truffle中test测试
  • web3.js
  • web3中eth转账函数
  • let definitions are not supported by current javascript
  • 笔记总结
  • json跨域原理及解决方法
  • solidity返回值太多,栈溢出
  • webpack在windows64位环境下使用
  • 利用朴素贝叶斯对名字进行性别预测
  • 机器学习神器:GBDT,XGBOOST
  • hyperledge简要命令
  • chaincde shim包的方法名称及其相应功能
  • Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.
  • docker重要细节
  • docker运行tomcat
  • 深入了解以太坊
  • 【跃迁之路】【444天】程序员高效学习方法论探索系列(实验阶段201-2018.04.25)...
  • 【跃迁之路】【463天】刻意练习系列222(2018.05.14)
  • 230. Kth Smallest Element in a BST
  • java取消线程实例
  • Lucene解析 - 基本概念
  • scala基础语法(二)
  • Vue ES6 Jade Scss Webpack Gulp
  • 爱情 北京女病人
  • 动态规划入门(以爬楼梯为例)
  • 更好理解的面向对象的Javascript 1 —— 动态类型和多态
  • 函数式编程与面向对象编程[4]:Scala的类型关联Type Alias
  • 扑朔迷离的属性和特性【彻底弄清】
  • 区块链分支循环
  • 微信小程序设置上一页数据
  • 06-01 点餐小程序前台界面搭建
  • 如何在 Intellij IDEA 更高效地将应用部署到容器服务 Kubernetes ...
  • 新年再起“裁员潮”,“钢铁侠”马斯克要一举裁掉SpaceX 600余名员工 ...
  • ​Distil-Whisper:比Whisper快6倍,体积小50%的语音识别模型
  • # 学号 2017-2018-20172309 《程序设计与数据结构》实验三报告
  • $.ajax()
  • (14)学习笔记:动手深度学习(Pytorch神经网络基础)
  • (2022版)一套教程搞定k8s安装到实战 | RBAC
  • (day 12)JavaScript学习笔记(数组3)
  • (补)B+树一些思想
  • (牛客腾讯思维编程题)编码编码分组打印下标(java 版本+ C版本)
  • .htaccess 强制https 单独排除某个目录
  • .net Application的目录
  • .NET C# 使用 SetWindowsHookEx 监听鼠标或键盘消息以及此方法的坑
  • .Net IE10 _doPostBack 未定义
  • .NET MVC之AOP
  • .NET 常见的偏门问题
  • .NET 中的轻量级线程安全
  • .NET/C# 获取一个正在运行的进程的命令行参数
  • .Net6 Api Swagger配置
  • .NetCore 如何动态路由
  • .NET面试题解析(11)-SQL语言基础及数据库基本原理
  • .NET与 java通用的3DES加密解密方法
  • @SuppressWarnings(unchecked)代码的作用
  • [BZOJ] 2006: [NOI2010]超级钢琴