question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature: auto-generated types for solidity structs and events

See original GitHub issue

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:closed
  • Created 2 years ago
  • Reactions:15
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
krzkaczorcommented, Oct 6, 2021

@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 method
  • will NOT appear if structs are only used internally or only in public storage variables (this case is quite unexpected)
1reaction
krzkaczorcommented, Sep 12, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

Master Solidity Variables, Data Types, and Structs ... - YouTube
Become an in-demand blockchain MASTER:https://dappuniversity.com/bootcamp Get the FREE Step-By-Step ...
Read more >
Types — Solidity 0.8.17 documentation
Types . Solidity is a statically typed language, which means that the type of each variable (state and local) needs to be specified....
Read more >
How To Use Events In Solidity | HackerNoon
Solidity defines events with the event keyword. After events are called, their arguments are placed in the blockchain. To use events first, you ......
Read more >
adjudicator - Go Packages
Call invokes the (constant) contract method with params as input values and sets the output to result. The result type might be a...
Read more >
How to receive a value returned by a Solidity smart contract ...
Values returned from a transaction are not available outside of EVM. You can either emit an event, or create a public view getter...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found