TypeChain icon indicating copy to clipboard operation
TypeChain copied to clipboard

Events and structs should be exported additionally in a simpler format

Open krzkaczor opened this issue 4 years ago • 7 comments

Currently we generate event types like this:

export type Event3_bool_uint256_Event = TypedEvent<
  [boolean, BigNumber],
  { value1: boolean; value2: BigNumber }
>;

This is fine if user wants to use this for for filters etc. but often events (and structs) doubles as "models" in a different part of the software. I propose we export them additionally in a more user friendly format that enables to reuse them in different part of the sourcecode:

export type Event3_bool_uint256_EventObject = { 
	value1: boolean; 
	value2: BigNumber
}

krzkaczor avatar Dec 02 '21 16:12 krzkaczor

that would be awesome. would that be possible to use the same name of the Struct defined in the solidity contract?

StErMi avatar Dec 19 '21 06:12 StErMi

Is it currently not possible to import structs exported from typechain?

mrwillis avatar Mar 02 '22 23:03 mrwillis

@mrwillis it is. This issue is about dropping TypedEvent thing.

krzkaczor avatar Mar 02 '22 23:03 krzkaczor

@krzkaczor sorry for the abuse of this thread. I'm unable to import structs for example in internal libraries, not linked. The default behaviour seems to export the contract type in index.ts

export type { BaseFillOrder} from "./BaseFillOrder";

but this file BaseFillOrder.ts has an internal library called LibOrder.sol which is represented in a namespace like

export declare namespace LibOrder {
  export type OrderStruct = {
    marketHash: BytesLike;
    baseToken: string;
    totalBetSize: BigNumberish;
    percentageOdds: BigNumberish;
    expiry: BigNumberish;
    salt: BigNumberish;
    maker: string;
    executor: string;
    isMakerBettingOutcomeOne: boolean;
  };

  export type OrderStructOutput = [
    string,
    string,
    BigNumber,
    BigNumber,
    BigNumber,
    BigNumber,
    string,
    string,
    boolean
  ] & {
    marketHash: string;
    baseToken: string;
    totalBetSize: BigNumber;
    percentageOdds: BigNumber;
    expiry: BigNumber;
    salt: BigNumber;
    maker: string;
    executor: string;
    isMakerBettingOutcomeOne: boolean;
  };
}

which actually does not get exported.

If the export looked like in index.ts

export type { BaseFillOrder, LibOrder} from "./BaseFillOrder";

It is importable into consuming code. Does it make sense? I can put together a repro repo

mrwillis avatar Mar 07 '22 18:03 mrwillis

I have the same issue as @mrwillis

jonbeckman avatar Jun 07 '22 01:06 jonbeckman

There is an issue that if event does not have some params name declared, named props for them are not created by ethers e.g. event Transfer(address indexed from, address indexed, uint) is decoded as:

{
  ...
  args: [
    '0x362fA9D0bCa5D19f743Db50738345ce2b40eC99f',
    '0x5777d92f208679DB4b9778590Fa3CAB3aC9e2168',
    BigNumber { value: "1195383028069068722922", hex: "0x40cd45a29e3ba452ea" },
    from: '0x362fA9D0bCa5D19f743Db50738345ce2b40eC99f' // only from is available 
  ]
}

zemse avatar Jun 19 '22 11:06 zemse

I want to share my workaround to anyone landing on this page after struggling to use a typechain struct type as model : contract : struct NFToken { IERC721 implem; uint256 id; } type generated : type NFTokenStructOutput = [string, BigNumber] & { implem: string; id: BigNumber; } How I create my model : type NFT = Omit<NFTokenStructOutput, keyof [string, BigNumber]> Hope that helps

npasquie avatar Jan 03 '23 15:01 npasquie