npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ethereum-abi-ui

v1.0.0

Published

Auto-generate UI form field definitions and associated validators from an Ethereum contract ABI

Downloads

5

Readme

ethereum-abi-ui

Build Status codecov Follow on Twitter

A convenience package that makes it easy to build UIs for interacting with Ethereum contracts.

  • Works with with any UI framework
  • Field sanitization and validation functions
  • Minimal dependencies

Example screenshot (from Meth):

Demo1

Installation

yarn add ethereum-abi-ui

or

npm install --save ethereum-abi-ui

## Example usage

This is a very basic example to illustrate how you use the API, it only uses jQuery, does not utilize any front-end framework, and is not production-quality code.

import $ from 'jquery'

import {
  FIELD_TYPES,
  canRenderMethodParams,
  renderMethodParams,
  canRenderMethodOutputs,
  renderMethodOutputs
} from 'ethereum-abi-ui'

const ABI = [ /* Solidity contract ABI definition */ ]
const form = $('method')
const output = $('outputs')

const fields = []

// render the input fields for the method params
if (canRenderMethodParams(ABI, 'approve')) {
  renderMethodParams(ABI, 'approve', (name, instance) => {
    switch (instance.fieldType()) {
      case FIELD_TYPES.NUMBER: {
        const input = $(`<input type="number" name="${name}" />`)
        input.instance = instance
        fields.push(input)
        form.append(input)
        break
      }
      case FIELD_TYPES.ADDRESS:
        // ...
        break
      // ...
    }
  })
}

form.submit(() => {
  const values = {}

  fields.forEach(input => {
    // sanitize entered value
    const val = input.instance.sanitize(input.val())

    // check that it's valid
    if (!input.instance.isValid(val)) {
      throw new Error('Please enter valid data')
    }

    // add to final values to send
    values[input.getAttribute('name')] = val
  })

  const results = doWeb3MethodCallUsingFormFieldValues(values)

  // now render the results
  if (canRenderMethodOutputs(ABI, 'approve')) {
    renderMethodOutputs(ABI, 'approve', results, (name, index, instance, result) => {
      output.append(`<p>${name}: ${result}</p>`)
    })
  }
})

API

canRenderMethodParams(abiJson, methodName): boolean

Get whether this library can render the input parameters for the given method of the given ABI.

renderMethodParams(abiJson, methodName, callback)

Render the input parameters for the given method of the given ABI.

The callback will be called for each input param with the following parameters:

  1. name - name of the parameter
  2. instance - an object instance representing the parameter type.

The instance object has the following methods:

  • fieldType() - returns one of the FIELD_TYPES (see below)
  • placeholderText() - suggested placeholder text to use for the input field representing this parameter
  • isValid() - function which tells you whether a given value is valid for this input parameter
  • sanitize() - function which can sanitize a given value entered for this parameter

canRenderMethodOutputs(abiJson, methodName): boolean

Get whether this library can render the output parameters for the given method of the given ABI.

renderMethodOutputs(abiJson, methodName, results, callback)

Render the output parameters for the given method of the given ABI.

The callback will be called for each output param with the following parameters:

  1. name - name of the parameter
  2. index - index of the output parameter in the list of outputs
  3. instance - an object instance representing the parameter type.
  4. result - the result obtained for this parameter (extracted from the results array passed in to the method)

The instance object has the following methods:

  • fieldType() - returns one of the FIELD_TYPES (see below)
  • placeholderText() - suggested placeholder text to use for the input field representing this parameter
  • isValid() - function which tells you whether a given value is valid for this input parameter
  • sanitize() - function which can sanitize a given value entered for this parameter

FIELD_TYPES

The supported form field types, at present: ADDRESS, TEXT, NUMBER, BOOL

PARAM_TYPES

The supported variable types, at present: address, string, uint, int, bool

Development

  • Lint: yarn lint
  • Test: yarn test
  • Build: yarn build

Contributors

All contributions welcome. Please see CONTRIBUTING.md

License

MIT