-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable human readable abi output (#114)
* Enable human readable abi output
- Loading branch information
1 parent
5adbb3a
commit 999e1e2
Showing
9 changed files
with
451 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
export function getHumanReadableAbi(abi: any[]) { | ||
return abi | ||
.filter((entry) => ['function', 'constructor', 'event'].includes(entry.type)) | ||
.map((entry) => { | ||
switch (entry.type) { | ||
case 'function': return encodeFunction(entry); | ||
case 'constructor': return encodeConstructor(entry); | ||
case 'event': return encodeEvent(entry); | ||
default: throw new TypeError('Unrecognized entry type'); | ||
} | ||
}); | ||
} | ||
|
||
function encodeFunction(entry: any) { | ||
let declaration = `function ${entry.name}(${encodeInputs(entry.inputs)})`; | ||
if (entry.stateMutability !== 'nonpayable') { | ||
declaration += ` ${entry.stateMutability}`; | ||
} | ||
return declaration + encodeOutputs(entry.outputs); | ||
} | ||
|
||
function encodeConstructor(entry: any) { | ||
return `constructor(${encodeInputs(entry.inputs)})`; | ||
} | ||
|
||
function encodeEvent(entry: any) { | ||
return `event ${entry.name}(${encodeInputs(entry.inputs)})`; | ||
} | ||
|
||
function encodeInputs(inputs: any[] | undefined) { | ||
if (!inputs || inputs.length === 0) { | ||
return ''; | ||
} | ||
return inputs | ||
.map((input) => { | ||
if (input.indexed) { | ||
return `${input.type} indexed ${input.name}`; | ||
} | ||
return `${input.type} ${input.name}`; | ||
}) | ||
.join(', '); | ||
} | ||
|
||
function encodeOutputs(outputs: any[] | undefined) { | ||
if (!outputs || outputs.length === 0) { | ||
return ''; | ||
} | ||
const returns = outputs | ||
.map((output) => { | ||
if (output.name !== '') { | ||
return `${output.type} ${output.name}`; | ||
} | ||
return output.type; | ||
}) | ||
.join(', '); | ||
return ` returns(${returns})`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import fsx from 'fs-extra'; | ||
import {join} from 'path'; | ||
import {expect} from 'chai'; | ||
import {compileProject} from '../../lib/compiler/compiler'; | ||
import {readFileContent} from '../../lib/utils'; | ||
import { utils } from 'ethers'; | ||
|
||
describe('E2E: humanReadableAbi', () => { | ||
before(() => { | ||
process.chdir('test/projects/humanReadableAbi'); | ||
}); | ||
|
||
beforeEach(() => { | ||
fsx.removeSync('build'); | ||
}); | ||
|
||
it(`"outputHumanReadableAbi" makes output contain humanReadableAbi`, async () => { | ||
await compileProject('config.json'); | ||
|
||
const filePath = join('./build', 'MyContract.json'); | ||
const { humanReadableAbi } = JSON.parse(readFileContent(filePath)); | ||
|
||
expect(humanReadableAbi.sort()).to.deep.equal([ | ||
'constructor(uint256 argOne)', | ||
'event Bar(bool argOne, uint256 indexed argTwo)', | ||
'event FooEvent()', | ||
'function noArgs() view returns(uint200)', | ||
'function oneArg(bool argOne)', | ||
'function threeArgs(string argOne, bool argTwo, uint256[] argThree) view returns(bool, uint256)', | ||
'function twoReturns(bool argOne) view returns(bool, uint256)' | ||
]); | ||
expect(() => new utils.Interface(humanReadableAbi)).not.to.throw(); | ||
}); | ||
|
||
it(`By default output does not contain humanReadableAbi`, async () => { | ||
await compileProject('config2.json'); | ||
|
||
const filePath = join('./build', 'MyContract.json'); | ||
const { humanReadableAbi } = JSON.parse(readFileContent(filePath)); | ||
|
||
expect(humanReadableAbi).to.equal(undefined); | ||
}); | ||
|
||
after(async () => { | ||
process.chdir('../../..'); | ||
}); | ||
}); |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"outputHumanReadableAbi": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
pragma solidity ^0.5.1; | ||
|
||
|
||
|
||
contract MyContract { | ||
event FooEvent (); | ||
event Bar (bool argOne, uint indexed argTwo); | ||
|
||
uint200 boo; | ||
constructor (uint argOne) public { | ||
boo = uint200(argOne); | ||
} | ||
|
||
function () external { | ||
} | ||
|
||
function noArgs() public view returns (uint200) { | ||
return boo; | ||
} | ||
|
||
function oneArg(bool argOne) public { | ||
boo = argOne ? 1 : 0; | ||
} | ||
|
||
function twoReturns(bool argOne) public view returns (bool, uint) { | ||
return (argOne, boo); | ||
} | ||
|
||
function threeArgs(string memory argOne, bool argTwo, uint[] memory argThree) public view returns (bool, uint) { | ||
argOne; | ||
argTwo; | ||
return (boo > 1, argThree[1]); | ||
} | ||
|
||
function bar() private returns (uint200) { | ||
return boo++; | ||
} | ||
} |