ethworks / doppelganger Goto Github PK
View Code? Open in Web Editor NEWSmart contract mocking tool
Home Page: https://getdoppelganger.io
License: MIT License
Smart contract mocking tool
Home Page: https://getdoppelganger.io
License: MIT License
The only dependency needed is ethers
Following the readme to instantiate a new Doppelganger object via new Doppelganger(abi)
I get a TypeError: Doppelganger is not a constructor
The current API is not ideal and has several problems including:
deploy
,.returns(...)
before deploy()
,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,doppelganger.methods
object,name#hex
is used for overloaded methodsHowever 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).
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.