Giter Club home page Giter Club logo

doppelganger's People

Contributors

marekkirejczyk avatar sz-piotr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

doppelganger's Issues

API improvements

The current API is not ideal and has several problems including:

  • Function names from abi can override doppelganger properties and methods e.g. deploy,
  • Overloaded methods are not supported on the Doppelganger object,
  • It is possible to cause exceptions because the user called Doppelganger methods in an incorrect order (calling .returns(...) before deploy(),
  • Setup requires calling multiple methods.

To address some of those issues the following API can be used:

const abi = [
  'function transfer(address,uint256) public returns(bool)',
  'function foo() public returns(uint256,string)',
  'function bar(uint256 x) public pure returns (uint256)',
  'function bar(bool x) public pure returns (bool)',
];

async function example() {
  const doppelganger = new Doppelganger(abi);
  const { address } = await doppelganger.deploy();
  await doppelganger.methods.transfer.returns(true);
  await doppelganger.methods.transfer.returns(42, 'hello');
  // support for overloaded names. Requires identifier
  await doppelganger.methods['bar#0423a132'].returns(1337);
}

The changes are as follows:

  • deploy returns the contract instance, making it easy to get the address and the contract safely,
  • all of the abi methods names now reside in the doppelganger.methods object,
  • a special syntax name#hex is used for overloaded methods

However I believe some further improvements can be made if we allow some bigger changes. Let's look at the following example:

const abi = [
  'function transfer(address,uint256) public returns(bool)',
  'function foo() public returns(uint256,string)',
  'function bar(uint256 x) public pure returns (uint256)',
  'function bar(bool x) public pure returns (bool)',
];

async function example() {
  // create initially. Deploys and sets up methods
  const doppelganger = await Doppelganger.from(abi, {
    transfer: true,
    foo: [42, 'hello'],
    'bar#0423a132': 1337, // support for overloaded names. Requires identifier
  });

  // access members
  const usedAbi = doppelganger.abi;
  const contract = doppelganger.contract;
  const address = doppelganger.address;

  // change something later
  await doppelganger.mockReturn('transfer', false);
  await doppelganger.mockReturn('foo', [21, ':37']);
}

This approach addresses all of the pain points listed at the start of this issue, by deploying the contract in the constructor (that is no longer strictly class based).

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.