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

truffle-object-mapper

v1.5.1

Published

Get all the values from a contract in the blockchain, optionally transformed as desired

Downloads

28

Readme

Truffle Object Mapper

Quickly and painless dump all the values from a deployed contract. Includes all read-able values, including defined getters.

See Object Mapper for full details on output mapping syntax.

Quick Usage

const Mapper = require('truffle-object-mapper');

// automatically loads truffle.js and contracts from build/contracts
const mapper = new Mapper();

(async () => {
  const values = await mapper.map('MetaCoin', '0x254dffcd3277C0b1660F6d42EFbB754edaBAbC2B');
  console.log(values);
})();

Examples

Customize Output for Types

const mapper = new Mapper({
  types: {
    // convert ALL int types to string
    int: function (val) {
      return Web3.utils.BN(val).toString();
    },
    // convert all uint256 to string; default is BN
    uint256: function(val) {
      return Web3.utils.BN(val).toString();
    }
  },
});
const values = await mapper.map('MetaCoin', '0x254dffcd3277C0b1660F6d42EFbB754edaBAbC2B');

Provide Custom Output Mapping

See Object Mapper for full details on output mapping.


// all calls to .map will apply these transformations
const mapper = new Mapper({
  mapping: {
    // example: 'getState' is a method that returns an integer
    // which represents possible values in a struct
    //
    // This mapping will output two keys:
    //  'state' - the integer itself from getState
    //  'stateName' - the string representing the struct
    state: [
      {
        key: 'stateName',
        transform: function (val) {
          const index = Number(val.toString());
          const states = ['Funding', 'Active', 'Matured'];
          return states[index];
        },
      },
      'state',
    ],
  },
});

Change Output Mapping Each Time


// this custom mapping only applies to this call of .map
const values = await mapper.map('Token', '0x254dffcd3277C0b1660F6d42EFbB754edaBAbC2B', {
  // map the property 'name' to the output key 'tokenName'
  name: 'tokenName',
  // use a custom transformation
  status: {
    key: 'status',
    transform: function(val) {
      const statuses = {
        0: 'New',
        1: 'Active',
        2: 'Closed'
      };
      const index = val.toString();
      return statuses[index];
    }
  },
  // send a single value to multiple output values
  symbol: ['tokenSymbol', {
    key: 'symbol',
    transform: function(val) {
      return `XYZ:${val}`;
    }
  }],
});

Methods

map

  • (async) contractName - Name of the contract corresponding to one of the ABI files
  • (async) at - Address of deployed contract
  • (async) mapping - Hash of src:dest object mapping rules with optional transformations, applied only to this method invocation. See Mapping. Can also be provided to constructor to apply to all calls.

Options

See also: lib/options.schema.json

networkName

Type: string

Network name corresponding to truffle config network name. Corresponds to truffle 'network' parameter

const mapper = new Mapper({
  networkName: 'mainnet'
});

workingDirectory

Type: string

Base directory to search for truffle config and contracts. Creates a truffle instance based on the contents of this directory using Truffle's auto-detection.

const mapper = new Mapper({
  workingDirectory: path.join(__dirname, '/../my-truffle-project')
});

types

Type: object

Hash of data type and how retrieved values of this type should be converted. Uses ABI definition to convert all properties of the specified type.

const mapper = new Mapper({
  types: {
    // all 'int' types will be converted to string
    int: function (val) {
      return Web3.utils.BN(val).toString();
    }
  }
});

mapping

Type: string|object|array

Hash of src:dest mapping; see the Node Object Mapper Docs for more information on object mapping. The src property will be mapped to one ore more dest properties.

string

Provide a simple string dest key:

const mapper = new Mapper({
  mapping: {
    // value from contract property 'bytes32Example'
    // will be mapped to 'newPropertyName'
    'bytes32Example': 'newPropertyName'
  }
});

object

Provide an object with dest key and optional transformations:

const mapper = new Mapper({
  mapping: {
    // value from contract property 'bytes32Example'
    // will be mapped to 'newPropertyName'
    'bytes32Example': {
      key: 'newPropertyName'
    },
    'intExample': {
      key: 'intExample',
      transform: function (val) {
        return Web3.utils.BN(val).toString();
      }
    }
  }
});

array

Use a list of destination mappings, which can be a mix of strings and/or objects:

const mapper = new Mapper({
  mapping: {
    // value from contract property 'bytes32Example'
    // will be mapped to 'newPropertyName'
    'bytes32Example': [{
      key: 'newDestPropertyName'
    }, {
      key: 'otherDestPropertyName',
      transform: (val) => `${val}_modified_string`
    }]
  }
});

provider

A pre-configured Web3 provider.

const TruffleProvider = require('@truffle/provider');
const TruffleConfig = require('@truffle/config');
const mapper = new Mapper({
  provider: TruffleProvider.create(TruffleConfig.detect({
    network: 'development',
    workingDirectory: __dirname,
  })),
});

contracts

Type: string|array|object

Glob path, array of paths, or pre-loaded ABI objects for the contracts to use.

string

Single glob path to the ABI contract files. A glob pattern to where contract ABI files are stored. Defaults to: [workingDirectory]/build/contracts/*.json

const mapper = new Mapper({
  contracts: path.join(__dirname, '/../my-truffle-project/build/**/*.json'),
});

array

List of glob path to the ABI contract files

const mapper = new Mapper({
  contracts: [
    path.join(__dirname, '/../my-truffle-project/build/**/*.json'),
    path.join(__dirname, '/../other-project/build/**/*.json'),
  ],
});

object

A hash of contract name and the pre-loaded ABI. Use this if you don't need/want automatic contract detection and loading.

const MyContract = require('./build/contracts/MetaCoin');
const mapper = new Mapper({
  contracts: { MyContract }
});