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.

Obscure ways how to select certain types

See original GitHub issue

Hi, (and again, thanks for this library! It’s awesome).

This is a more of a suggestion than a bug. The generated files currently export 2 things per contract - the factory and the contract instance type. However, it’s a bit harder to do the following:

  1. Parse logs of a contract without instantiating a contract instance. With ethers you can just do this:
    const contractInterface = new ethers.utils.Interface(Contract.abi);
    const parsedLog = contractInterface.parseLog(log);

I would like to do the same with TypeChain - however, this is not possible as of now.

  1. I want to get some of the generated types. E.g. the events. For now I have to “rape” the TS with following:
type ClientRequestCreatedEvent = ReturnType<InstanceType<typeof Contract>['filters']['ClientRequestCreated']>
// or to mock the available functions
type MockedContractFunctions = { [key in keyof InstanceType<typeof Contract>['functions']]: jest.Mock };

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Siegriftcommented, Sep 18, 2021

Hi,

actually https://github.com/dethcrypto/TypeChain/releases/tag/%40typechain%2Fethers-v5%407.1.0 implements the third point. The first two are not really needed.

0reactions
Siegriftcommented, May 2, 2021

Actually, I was a bit mistaken with my example in https://github.com/ethereum-ts/TypeChain/issues/376#issue-870686826 and also https://github.com/ethereum-ts/TypeChain/issues/376#issuecomment-829155140. The problem is that the “advanced types” I created don’t give me the TypedEvent but rather TypedEventFilter - the actual TypedEvent which I want can be queryFilter function on the generated contract.

Unfortunately, because TypedEventFilter is not exported and there is no way to extract the generic arguments without the original interface I was forced to modify the generated code and export this interface from index.ts file.

So for completeness:

// AirnodeRrp - is generated contract by TypeChain
// TypedEventFilter - is the interface created by TypeChain which I exported by modifying the generated code
import { AirnodeRrp, TypedEventFilter } from '@airnode/protocol';

// NOTE: I am also ignoring the typed tupple and only extracting the typed event object.
type ExtractTypedEvent<T> = T extends TypedEventFilter<any, infer EventArgsObject> ? EventArgsObject : never;
// I am only interested in certain events from the generated contract
export type AirnodeRrpEvents = Pick<
  InstanceType<typeof AirnodeRrp>['filters'],
  | 'ClientRequestCreated'
  | 'ClientFullRequestCreated'
  | 'ClientRequestFulfilled'
  | 'ClientRequestFailed'
  | 'WithdrawalRequested'
  | 'WithdrawalFulfilled'
>;
export type AirnodeRrpLog<T extends keyof AirnodeRrpEvents> = ExtractTypedEvent<ReturnType<AirnodeRrpEvents[T]>>;

// And I want the ethers `LogDescription`, but I want the args to be typed
export type AirnodeLogDescription<T> = Omit<ethers.utils.LogDescription, 'args'> & { args: T };


// I can then parse the logs and return typed response with
export function parseAirnodeRrpLog<T extends keyof AirnodeRrpEvents>(
  log: ethers.providers.Log
): AirnodeLogDescription<AirnodeRrpLog<T>> {
  const airnodeRrpInterface = new ethers.utils.Interface(AirnodeRrpArtifact.abi);
  const parsedLog = airnodeRrpInterface.parseLog(log);
  return parsedLog as AirnodeLogDescription<AirnodeRrpLog<T>>;
}

I then use it in other parts of codebase like this:

const event = fixtures.evm.logs.buildClientRequest(); // creates a mock of the request
const parsedLog = parseAirnodeRrpLog<'ClientRequestCreated'>(event);
    
// and I can strongly type the parsed logs with
type WithdrawalRequestLog = AirnodeLogDescription<AirnodeRrpLog<'WithdrawalFulfilled'>>

What I propose is to export the interface of generated code. This includes:

  • Types defined in commons.d.ts
  • Export generated interface for smart contracts (e.g. AirnodeRrpInterface in my case), such that one can type the following code:
const airnodeRrpInterface: AirnodeRrpInterface = new ethers.utils.Interface(AirnodeRrpArtifact.abi);
  • Maybe generate a method for parsing typed events on the contract interface - similarly to how my parseAirnodeRrpLog works?
Read more comments on GitHub >

github_iconTop Results From Across the Web

Select specific cells or ranges - Microsoft Support
Press F5 or CTRL+G to launch the Go To dialog. In the Go to list, click the name of the cell or range...
Read more >
Striking a Balance Between Native and Custom Select Elements
Here's the plan! We're going to build a styled select element. Not just the outside, but the inside too. Total styling control.
Read more >
How to make selections in Illustrator - Adobe Support
To select all objects of a certain kind, deselect all artwork, click Select > Object, and then choose an object type (Brush Strokes,...
Read more >
CSS selectors - Learn web development | MDN
Next, we'll take a closer look at type, class, and ID selectors. ... This group of selectors gives you different ways to select...
Read more >
How to Select Multiple Files on a Windows 10 PC at Once
It's easy to select multiple files on a Windows 10 computer from a folder or ... files on a Windows 10 computer at...
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