Skip to content
This repository has been archived by the owner on Oct 29, 2020. It is now read-only.

API improvements #4

Open
sz-piotr opened this issue Dec 14, 2018 · 0 comments
Open

API improvements #4

sz-piotr opened this issue Dec 14, 2018 · 0 comments

Comments

@sz-piotr
Copy link
Contributor

sz-piotr commented Dec 14, 2018

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).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant