Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Static call and function calls doesn't match whats in the function property #590

Closed
ShravanSunder opened this issue Jan 1, 2022 · 5 comments

Comments

@ShravanSunder
Copy link
Contributor

ShravanSunder commented Jan 1, 2022

Issue with type generation

Using ethersv5 and typechain and hardhat

Below of is a snippet of a simple test contract:

contract YourContract {
  event SetPurpose(address sender, string purpose);
  string _purpose = 'Building Unstoppable Apps';

  constructor() {
    // what should we do on deploy?
  }

  function purpose() public view returns (string memory) {
    return _purpose;
  }

  function setPurpose(string memory newPurpose) public payable {

    _purpose = newPurpose;
    console.log(msg.sender, 'set purpose to', _purpose);
    emit SetPurpose(msg.sender, _purpose);
  }
}

Typechain generates a purpose function that has multiple return types. string and [string] as shown below.

This is what typechain generates

...
  functions: {
    purpose(overrides?: CallOverrides): Promise<[string]>;

    setPurpose(
      newPurpose: string,
      overrides?: PayableOverrides & { from?: string | Promise<string> }
    ): Promise<ContractTransaction>;
  };

  purpose(overrides?: CallOverrides): Promise<string>;

  setPurpose(
    newPurpose: string,
    overrides?: PayableOverrides & { from?: string | Promise<string> }
  ): Promise<ContractTransaction>;

  callStatic: {
    purpose(overrides?: CallOverrides): Promise<string>;

    setPurpose(newPurpose: string, overrides?: CallOverrides): Promise<void>;
  };
...

The return type of

functions: {
    purpose(overrides?: CallOverrides): Promise<[string]>;
}

does not match the static call and function call return type

  purpose(overrides?: CallOverrides): Promise<string>;

  callStatic: {
    purpose(overrides?: CallOverrides): Promise<string>;

    setPurpose(newPurpose: string, overrides?: CallOverrides): Promise<void>;
  };

Possible Cause

It seems to have to do with something with the target-ethers-v5 implementation of generateOutputTypes

https://github.com/ShravanSunder/TypeChain/blob/c0e175b3b6e8176d66bbbcc7f44e00987dd0e615/packages/target-ethers-v5/src/codegen/types.ts#L22-L28

export function generateOutputTypes(options: GenerateTypeOptions, outputs: Array<AbiOutputParameter>): string {
  if (!options.returnResultObject && outputs.length === 1) {
    return generateOutputType(options, outputs[0].type)
  } else {
    return generateOutputComplexType(outputs, options)
  }
}
@ShravanSunder
Copy link
Contributor Author

@krzkaczor it looks like strings are considered objects and outputted as array of 1.

@ShravanSunder
Copy link
Contributor Author

this is the log of what a string output variable in solidity is seen as in generateFunction

{
 |   name: 'purpose',
 |   inputs: [],
 |   outputs: [ { name: '', type: [Object] } ],
 |   stateMutability: 'view',
 |   documentation: undefined
 | } undefined
 |  { returnResultObject: true, useStructs: true } [ { name: '', type: { type: 'string', originalType: 'string' } } ]

this is only for functions that are under the property in a contract type

functions: {
...
}

ShravanSunder added a commit to ShravanSunder/TypeChain that referenced this issue Jan 5, 2022
@ShravanSunder
Copy link
Contributor Author

here is a link to a reproduction: https://github.com/dethcrypto/TypeChain/pull/593/files#r778495210

@krzkaczor
Copy link
Member

krzkaczor commented Jan 8, 2022

@ShravanSunder have you tested it? I just verified it and it's how ethers work -- for some reason functions. wrap result in an array. We have tests covering this behavior: https://github.com/dethcrypto/TypeChain/blob/master/packages/target-ethers-v5-test/test/DataTypesInput.test.ts#L47

I am gonna close it for now. Feel free to reopen if I am wrong.

and btw. this is controlled by this switch: https://github.com/dethcrypto/TypeChain/blob/master/packages/target-ethers-v5/src/codegen/index.ts#L81

@krzkaczor
Copy link
Member

Please repoen if this is still a problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@krzkaczor @ShravanSunder and others