code example for Intelligent contract client and web-side invocation of event events for intelligent contracts __ Smart Contracts

Source: Internet
Author: User
code example for invoking event events for intelligent contracts on the client and web side Web Truffle

According to the official website example

Http://truffleframework.com/boxes/pet-shop

Truffle as a running test framework, it is also web3 to make calls to smart contracts.

The location where the file is located src/js/app.js

  Initweb3:function () {

      //Web3 entry
      if (typeof web3!== ' undefined ') {
          app.web3provider = Web3.currentprovider;
      else {
          //test network
          App.web3provider = new Web3.providers.HttpProvider (' http://localhost:8588 '); Here is the port I specified
      web3 = new Web3 (app.web3provider);

    return App.initcontract ();
  },

When the smart contract was initialized, it was invoked using the JSON file that truffle itself compiled

  Initcontract:function () {
      //load Adoption.json
      $.getjson (' Adoption.json ', function (data) {
          //Smart Contract instantiation
          var adoptionartifact = data;
          App.contracts.Adoption = Trufflecontract (adoptionartifact); Assign value to node data under App.contracts

          //Set contract provider
          App.contracts.Adoption.setProvider (app.web3provider);

          Retrieval Operation return
          app.markadopted ();
      });

    return app.bindevents ();//execute event binding
  },

This is how event bindings are implemented.

  Bindevents:function () {
    $ (document). On (' click ', '. btn-adopt ', app.handleadopt);
  },

Where btn-adopt corresponds to a button in the src/index.html

<button class= "btn btn-default btn-adopt" type= "button" data-id= "0" >Adopt</button>

After the environment is run, an element check is made and the button is found to bind the event

The bound method is defined below the JS file.

 Handleadopt:function () {event.preventdefault ();//This method notifies the Web browser not to perform the default action associated with the event (if there is such an action)

      var Petid = parseint ($ (event.target). Data (' ID '));

      var adoptioninstance;
          Web3.eth.getAccounts (function (error, accounts) {//essence or WEB3 call if (error) {Console.log (error);

          var account = Accounts[0];

              App.contracts.Adoption.deployed (). Then (function (instance) {adoptioninstance = instance;
          Execute adopt as a transaction by sending account return Adoptioninstance.adopt (Petid, {from:account}); }). Then (function (Result) {return app.markadopted ();////disable button disabled}
          {Console.log (err.message);
      });
  }); }
  Markadopted:function (adopters, account) {
      //instance
      var adoptioninstance;


      App.contracts.Adoption.deployed (). Then (function (instance) {
          adoptioninstance = instance;

          return AdoptionInstance.getAdopters.call ();
      }). Then (function (adopters) {for
          (i = 0; i < adopters.length; i++) {
              if (adopters[i)!== ' 0x0000000000000000000000 000000000000000000 ') {
                  $ ('. Panel-pet '). EQ (i). Find (' button '). Text (' Success '). attr (' disabled ', true);
              }
      }). catch (function (err) {
          console.log (err.message);
      });
  

Summary: The intelligent contract on the Web side uses the technology stack for Node + truffle + web3 to complete the writing, testing, and deployment. Client Java web3j

Project Open Source address is https://github.com/web3j/web3j

Because the role of WEB3J is the same as WEB3, it is a deployment call to a smart contract. Client Java qtum-android

The quantum chain official wallet client does not use WEB3J, but rather writes its own library.

Project Address is https://github.com/qtumproject/qtum-android

File as location App/src/main/java/org/qtum/wallet/utils/contractbuilder.java

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.