Truffle contract Interaction-reading and writing of Ethernet square data on the web end of the ether square

Source: Internet
Author: User
Tags button type getmessage
1. Initialization of truffle
Truffle init Webpack
1 2

can refer to: here 2. Write a contract

Here is a simple contract.

pragma solidity ^0.4.2;

Contract Credit {

  event CreateRecord (address indexed _address, string identity, uint category, uint price);

  string A;
  UINT B;
  UINT C;

    function Create (string identity, uint category, uint256 price)
  {
      a = identity;
      b = category;
      c = Price;
      CreateRecord (Msg.sender, identity, category, price);
  }

  function All () returns (strings, uint, uint) {return
      (A, B, c);
  }

}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23-24 3. Deployment Contract

Mentioned in the previous article: here 4. To modify an HTML file

This assumes that you already have a web base.

Write 3 input, namely identity, classification and price. Notice the IDs of them

and 2 buttons: Commit: Will call the Savemessage function and write the data to the ether. Get information: Call the GetMessage function, read the data from the Ethernet square

<! DOCTYPE html> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28-29
5. Modify the App.js file 

Note the introduction of the file.

Import the page ' s CSS.
Webpack'll know what to does with it. Import "..

/stylesheets/app.css ";
Import Libraries we need.
Import {default as Web3} from ' web3 '; Import {default as Contract} from ' Truffle-contract '//Import we contract artifacts and turn them into usable abstrac
tions. Import credit_artifacts from '. /..
/build/contracts/credit.json '//Metacoin are our usable abstraction, which we'll use through the code below.

var credit = contract (credit_artifacts);
Show the interaction with the contract var accounts;

var account; Window.

    APP = {start:function () {var self = this;
    Bootstrap the credit abstraction for use.

    Credit.setprovider (Web3.currentprovider); Gets the initial account web3.eth.getAccounts (function (err, ACCS) {if (err!= null) {alert ("There was" error Fetchin
        G your accounts. ");
      Return } if (accs.length = = 0) {alert ("couldn ' t get any accounts!
        Make sure your Ethereum the client is configured correctly. ");
      Return} accounts = ACCs;
    account = Accounts[0];
    Self.refreshbalance ();
  });
    //Set page text information setstatus:function (message) {var status = document.getElementById ("status");
  status.innerhtml = message;
    //Get information from ether Getmessage:function () {var self = this;

    var meta;

    var a,b,c;
        Credit.deployed (). Then (function (instance) {meta = instance;
    Return Meta.all.call ({from:account});
    }). Then (function (value) {self.setstatus (value);
      ). catch (function (e) {Console.log (e)); Self.setstatus ("Error getting message.")
      See log. ");},//write data to Etheric square savemessage:function () {var self = this;

      var meta;
      var identity = document.getElementById ("Identity"). Value;
      var category = parseint (document.getElementById ("category"). value);

      var price = parseint (document.getElementById ("price"). Value);
         Credit.deployed (). Then (function (instance) {meta = instance; Return Meta.create (identity, category, price, {from:account});
      }). Then (function () {self.setstatus (identity);
        ). catch (function (e) {Console.log (e));
    Self.setstatus ("Error");
  });

}
}; The following paragraph should be done without the control of copying. Web page load is invoked.
But in fact I don't understand, to be solved. Window.addeventlistener (' Load ', function () {if (typeof web3!== ' undefined ') {Console.warn ("Using web3 detected F Rom external source. If you find this your accounts don ' t appear or you have 0 metacoin, ensure your ' ve configured that source properly. If using Metamask, the following link. Feel free to delete this warning. :) Http://truffleframework.com/tutorials/truffle-and-metamask ")//Use Mist/metamask ' s provider window.web3 = new
  WEB3 (Web3.currentprovider); else {Console.warn ("No web3 detected. Falling back to http://localhost:8545. You are should remove this fallback if you are deploy live, as it's inherently insecure. Consider switching to metamask for development. More info here:http://trUffleframework.com/tutorials/truffle-and-metamask "); Fallback-use your fallback strategy (local node/hosted node + in-dapp ID mgmt/fail) window.web3 = new Web3 (NE
  W Web3.providers.HttpProvider ("http://localhost:8545"));
} app.start (); });
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 The 23 24 25 26 27 28 29 30 31 32 33 34 35 36

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.