dethcrypto / TypeChain

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

Events and structs should be exported additionally in a simpler format #569

Open krzkaczor opened 2 years ago

krzkaczor commented 2 years ago

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
}
StErMi commented 2 years ago

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

mrwillis commented 2 years ago

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

krzkaczor commented 2 years ago

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

mrwillis commented 2 years ago

@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

unibeck commented 2 years ago

I have the same issue as @mrwillis

zemse commented 2 years ago

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 
  ]
}
npasquie commented 1 year ago

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