Giter Club home page Giter Club logo

abi2lib's Introduction

abi2lib

An easy way to autogenerate boilerplate code for calling web3.js so your app can communicate with Ethereum.

Installation

Install globally via npm, yarn, or your preferred Javascript package manager:

npm install -g abi2lib

// OR

yarn global add abi2lib

If you are on Windows, you might need to refresh your path by restarting the terminal.

You can also install abi2lib in your own project

npm install --save abi2lib

// OR 

yarn global add abi2lib

Usage

CLI

Use in your terminal of choice by running:

abi2lib <contract_path> [options]

The options let you specify config values directly in the command, or specify a path to a config file. If you do not specify an output directory via an option, then it will be created in the working one. Run abi2lib --help to learn more.

Module

Use in your own nodejs scripts:

const abi2lib = require('abi2lib');

abi2lib.generate(contract_path, output_folder, config_options);

Output

Either of these methods will generate a directory named <ContractName>ContractLib which contains all of your autogenerated code. You can then use that code to communicate with your smart contract via:

const BaseController = require('<ContractName>ContractLib/<ContractName>Controller.js');

const ContractController = new BaseController();

// Async method, returns Promise; configures library with 
// network ID of currently active network.
await ContractController.detectNetwork();

// Perform transaction using config values & required inputs
ContractController.post_<fxnA>(fromAddr, [value if allowed], ...contractInputs);

// Query output of contract using required inputs, does not perform transaction
ContractController.get_<fxnB>(fromAddr, ...contractInputs);

Config

The optional config object includes constants for configuring web3 calls.

{
  "eth": {                                      
    "provider": "http://localhost:8545",       
    "default_gas": 0,                      
    "default_gasPrice": 40            
  }
}

Connector Methods

The Controller class will have methods corresponding to each of the function descriptions listed in the ABI. If the function can be called with inputs, then there will be a method shaped like:

post_<fxnName>(from, [value if fxn is payable], [fxnParams...]){
	// Properly configured call to contract's `send` function, triggers transaction
}

If the function also has outputs, then there will be a method shaped like:

get_<fxnName>(from, [inputParams...]){
	// Properly configured call to contract's `call` function, triggers no transaction
}

Output Structure

Outputs a directory named <ContractName>ContractLib which contains:

  1. GenericEthConnector.js : Generically converts ethereum read/write calls into web3 calls.
  2. <ContractName>Controller.js : Contains all generated methods corresponding to the contract.
  3. contract.json : A copy of your chosen smart contract ABI JSON
  4. config.json : A copy of your chosen config JSON

Licensing

abi2lib is developed & maintained by Eximchain, released for public use under the Apache-2.0 License.

Output from abi2lib uses the same license.

abi2lib's People

Contributors

john-osullivan avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

fcolon

abi2lib's Issues

Incorrect ref to contract address

Currently, the contract address is incorrectly hard-coded into GenericETHConnector.js. Those addresses are stored within the artifact in a shape like this:

{
...
"networks": {
    "[network1_id]": {
      "events": {},
      "links": {},
      "address": "[address_value]"
    },
    "[network2_id]": {...}
   ...
  }
}

Given that a contract can be deployed on multiple networks, we need to correctly infer which network is currently being used and then determine which address we want. We can use web3.shh.net.getId([callback]) to find that value, even throw a helpful little error message at init if the appropriate network isn't found.

Generate disconnect()

As the library stands now, there is a connect() function which creates the connection to the network, but there is never a disconnect(), meaning there are abandoned connection threads. This can become an issue at scale after enough calls. Please create a disconnect() function.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.