dethcrypto / TypeChain

🔌 TypeScript bindings for Ethereum smart contracts
MIT License
2.76k stars 364 forks source link

Truffle struct types are incorrect #343

Open forshtat opened 3 years ago

forshtat commented 3 years ago

Actual:

The generated types for calls to a solidity method that returns a struct make all integer fields be BN.

However, this is not what Truffle currently does with structs. It leaves these values be returned as strings, so generated types are not usable in our project.

For example , the following code:

struct Struct {
    uint256 a;
    uint256 b;
}

function getStruct() external view returns (Struct memory);

will result in the following type declaration:

getStruct(
  txDetails?: Truffle.TransactionDetails
): Promise<{
    a: BN;
    b: BN;
  }>;

but it is trivial to see that event the Truffle console does not treat these values as 'BN':

truffle(development)> c.getStruct()
[
  '150000',
  '100000',
  a: '150000',
  b: '100000',
]

So the expected type declaration must be

getStruct(
  txDetails?: Truffle.TransactionDetails
): Promise<{
    a: string;
    b: string;
  }>;
forshtat commented 3 years ago

In case this is to be viewed as an issue for the Truffle project itself also created an issue there: https://github.com/trufflesuite/truffle/issues/3914

quezak commented 3 years ago

Is this maybe related to the web3 issue discussed on this recent PR?

forshtat commented 3 years ago

Is this maybe related to the web3 issue discussed on this recent PR?

A little, but the mentioned issue is about the types of accepted inputs within structs, and this issue is about types of outputs when a struct is returned, so the issues are not identical.

quezak commented 3 years ago

OK. I think we can wait for some response on trufflesuite/truffle#3914 -- if it's a bug in truffle, we can just wait for them to fix it and then bump minimum typechain's truffle version.

Until that happens, we can fix Typechain for the current Truffle version too -- PRs welcome :)