Feature: auto-generated types for solidity structs and events
See original GitHub issue-
“ethers”: “5.3.0”
-
“@typechain/ethers-v5”: “7.0.0”
-
“typechain”: “5.0.0”
Typechain is generating this for a solidity function that is accepting a struct as input and also returns an array of structs as output:
Solidity:
struct CanExecData {
uint256 id;
address user;
uint256 minimumHF;
address colToken;
address spender;
}
struct CanExecResult {
uint256 id;
string isPositionSafe;
string isATokenAllowed;
string message;
}
function multiCanExecute(CanExecData[] memory _canExecDatas)
external
view
returns (CanExecResult[] memory)
Typechain:
multiCanExecute(
_canExecDatas: {
id: BigNumberish;
user: string;
minimumHF: BigNumberish;
colToken: string;
spender: string;
}[],
overrides?: CallOverrides
): Promise<
([BigNumber, string, string, string] & {
id: BigNumber;
isPositionSafe: string;
isATokenAllowed: string;
message: string;
})[]
>;
It would be really useful if typechain also generated and exported interface for every solidity struct with correct field names.
Otherwise we have to resort to defining those types ourselves:
// DO NOT MODIFY FIELDS: must match solidity struct
export interface CanExecData {
id: string;
user: string;
minimumHF: BigNumber;
colToken: string;
spender: string;
}
// DO NOT MODIFY FIELDS: must match function return type
export interface CanExecResult {
id: string;
isPositionSafe: string;
isATokenAllowed: string;
message: string;
}
And then do an unsafe cast via unknown
to those types:
export const multiCanExecute = async (
canExecDatas: CanExecData[]
): Promise<CanExecResult[]> => {
return refinanceResolver.multiCanExecute(
canExecDatas
) as unknown as CanExecResult[];
};
I suppose that if typechain correctly auto-generates the CanExecResult
struct type, then the cast via unknown
would not be necessary because it intersects
in the ethers.js
function result type
Promise<
([BigNumber, string, string, string] & {
id: BigNumber;
isPositionSafe: string;
isATokenAllowed: string;
message: string;
})[]
>
Maybe typechain could also make use of the auto-generated struct type internally in its definitions like so (if it doesnt cause trouble with ethers.js compatibility)?
multiCanExecute(
_canExecDatas: CanExecData[],
overrides?: CallOverrides
): Promise<
([BigNumber, string, string, string] & CanExecResult)[]
>;
Either way it would be super useful if typechain also auto-generated and exported types for Solidity structs, to avoid human error.
The same can be said for solidity events.
Thanks for this amazing tool @krzkaczor ! We use it every day and love it!
Issue Analytics
- State:
- Created 2 years ago
- Reactions:15
- Comments:8 (5 by maintainers)
Top GitHub Comments
@zemse pointed out that sometimes structs appear in abi 👏
I did some testing and it appears that:
"internalType": "struct StructName"
will be generated if the struct is used as input or output of an exposed methodSupport for events released in https://github.com/dethcrypto/TypeChain/releases/tag/%40typechain%2Fethers-v5%407.1.0
h/t @zemse