Skip to content

Commit

Permalink
fix: retrieve events & errors by sig (not name)
Browse files Browse the repository at this point in the history
  • Loading branch information
b00ste committed Sep 13, 2023
1 parent a12e509 commit ce03a3c
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 33 deletions.
4 changes: 2 additions & 2 deletions examples/docs/contracts/Bar.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
}
},
"events": {
"Transfer": {
"Transfer(uint256)": {
"code": "event Transfer(uint256 foo)",
"inputs": {
"foo": {
Expand All @@ -124,7 +124,7 @@
}
},
"errors": {
"Doh": {
"Doh(bool)": {
"code": "error Doh(bool yay)",
"inputs": {
"yay": {
Expand Down
6 changes: 3 additions & 3 deletions examples/docs/contracts/ExampleContract.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
}
},
"events": {
"DoSomething": {
"DoSomething(address,uint256)": {
"code": "event DoSomething(address indexed a, uint256 b)",
"inputs": {
"a": {
Expand All @@ -117,7 +117,7 @@
"notice": "Emitted when the function doSomething is called.",
"details": "More info about the event can be added here."
},
"NewEvent": {
"NewEvent(uint256)": {
"code": "event NewEvent(uint256 newNumber)",
"inputs": {
"newNumber": {
Expand All @@ -131,7 +131,7 @@
}
},
"errors": {
"RandomError": {
"RandomError(address,address)": {
"code": "error RandomError(address expected, address actual)",
"inputs": {
"expected": {
Expand Down
4 changes: 2 additions & 2 deletions examples/docs/contracts/IBar.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
}
},
"events": {
"Transfer": {
"Transfer(uint256)": {
"code": "event Transfer(uint256 foo)",
"inputs": {
"foo": {
Expand All @@ -37,7 +37,7 @@
}
},
"errors": {
"Doh": {
"Doh(bool)": {
"code": "error Doh(bool yay)",
"inputs": {
"yay": {
Expand Down
6 changes: 3 additions & 3 deletions examples/docs/contracts/IExampleContract.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
}
},
"events": {
"DoSomething": {
"DoSomething(address,uint256)": {
"code": "event DoSomething(address indexed a, uint256 b)",
"inputs": {
"a": {
Expand All @@ -82,7 +82,7 @@
"notice": "Emitted when the function doSomething is called.",
"details": "More info about the event can be added here."
},
"NewEvent": {
"NewEvent(uint256)": {
"code": "event NewEvent(uint256 newNumber)",
"inputs": {
"newNumber": {
Expand All @@ -96,7 +96,7 @@
}
},
"errors": {
"RandomError": {
"RandomError(address,address)": {
"code": "error RandomError(address expected, address actual)",
"inputs": {
"expected": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"methods": {},
"events": {
"NewEvent": {
"NewEvent(uint256)": {
"code": "event NewEvent(uint256 newNumber)",
"inputs": {
"newNumber": {
Expand Down
16 changes: 13 additions & 3 deletions src/abiDecoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export function getCodeFromAbi(element: AbiElement): string {
return code;
}

function getSigFromAbi(element: AbiElement): string {
return `${element.name}(${element.inputs.map((input) => input.type)})`;
}

export function decodeAbi(abi: AbiElement[]): Doc {
const doc: Doc = {
methods: {},
Expand Down Expand Up @@ -97,6 +101,8 @@ export function decodeAbi(abi: AbiElement[]): Doc {
}

if (el.type === 'function') {
const funcSig = getSigFromAbi(el);

const func: Method = {
stateMutability: el.stateMutability,
code: getCodeFromAbi(el),
Expand All @@ -120,10 +126,12 @@ export function decodeAbi(abi: AbiElement[]): Doc {
};
});

doc.methods[`${el.name}(${el.inputs ? el.inputs.map((inp) => inp.type).join(',') : ''})`] = func;
doc.methods[funcSig] = func;
}

if (el.type === 'event') {
const eventSig = getSigFromAbi(el);

const event: Event = {
code: getCodeFromAbi(el),
inputs: {},
Expand All @@ -138,10 +146,12 @@ export function decodeAbi(abi: AbiElement[]): Doc {
};
});

doc.events[el.name] = event;
doc.events[eventSig] = event;
}

if (el.type === 'error') {
const errorSig = getSigFromAbi(el);

const error: Error = {
code: getCodeFromAbi(el),
inputs: {},
Expand All @@ -155,7 +165,7 @@ export function decodeAbi(abi: AbiElement[]): Doc {
};
});

doc.errors[el.name] = error;
doc.errors[errorSig] = error;
}
}

Expand Down
29 changes: 12 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,69 +125,64 @@ async function generateDocumentation(hre: HardhatRuntimeEnvironment): Promise<vo

// Fetches info from userdoc
for (const errorSig in info.userdoc?.errors) {
const [errorName] = errorSig.split('(');
const error = info.userdoc?.errors[errorSig][0];

if (doc.errors[errorName] !== undefined) doc.errors[errorName].notice = error?.notice;
if (doc.errors[errorSig] !== undefined) doc.errors[errorSig].notice = error?.notice;
}

for (const eventSig in info.userdoc?.events) {
const [eventName] = eventSig.split('(');
const event = info.userdoc?.events[eventSig];

if (doc.events[eventName] !== undefined) doc.events[eventName].notice = event?.notice;
if (doc.events[eventSig] !== undefined) doc.events[eventSig].notice = event?.notice;
}

for (const methodSig in info.userdoc?.methods) {
// const [methodName] = methodSig.split('(');
const method = info.userdoc?.methods[methodSig];

if (doc.methods[methodSig] !== undefined) doc.methods[methodSig].notice = method?.notice;
}

// Fetches info from devdoc
for (const errorSig in info.devdoc?.errors) {
const [errorName] = errorSig.split('(');
const error = info.devdoc?.errors[errorSig][0];

if (doc.errors[errorName] !== undefined) doc.errors[errorName].details = error?.details;
if (doc.errors[errorSig] !== undefined) doc.errors[errorSig].details = error?.details;

for (const param in error?.params) {
if (doc.errors[errorName]?.inputs[param]) {
doc.errors[errorName].inputs[param].description = error?.params[param];
if (doc.errors[errorSig]?.inputs[param]) {
doc.errors[errorSig].inputs[param].description = error?.params[param];
}
}

for (const value in error) {
if (value.startsWith('custom:')) {
const strippedValue = value.replace('custom:', '');
if (strippedValue.length > 0) {
if (doc.errors[errorName]) {
doc.errors[errorName][`custom:${strippedValue}`] = error[`custom:${strippedValue}`];
if (doc.errors[errorSig]) {
doc.errors[errorSig][`custom:${strippedValue}`] = error[`custom:${strippedValue}`];
}
}
}
}
}

for (const eventSig in info.devdoc?.events) {
const [eventName] = eventSig.split('(');
const event = info.devdoc?.events[eventSig];

if (doc.events[eventName] !== undefined) doc.events[eventName].details = event?.details;
if (doc.events[eventSig] !== undefined) doc.events[eventSig].details = event?.details;

for (const param in event?.params) {
if (doc.events[eventName]?.inputs[param]) {
doc.events[eventName].inputs[param].description = event?.params[param];
if (doc.events[eventSig]?.inputs[param]) {
doc.events[eventSig].inputs[param].description = event?.params[param];
}
}

for (const value in event) {
if (value.startsWith('custom:')) {
const strippedValue = value.replace('custom:', '');
if (strippedValue.length > 0) {
if (doc.events[eventName]) {
doc.events[eventName][`custom:${strippedValue}`] = event[`custom:${strippedValue}`];
if (doc.events[eventSig]) {
doc.events[eventSig][`custom:${strippedValue}`] = event[`custom:${strippedValue}`];
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/template.sqrl
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
## Events

{{@foreach(it.events) => key, val}}
### {{key}}
### {{key.split('(')[0]}}


```solidity
Expand Down Expand Up @@ -151,7 +151,7 @@
## Errors

{{@foreach(it.errors) => key, val}}
### {{key}}
### {{key.split('(')[0]}}


```solidity
Expand Down

0 comments on commit ce03a3c

Please sign in to comment.