Skip to content

Commit

Permalink
✨ feat(ethers): Make events typesafe (#479)
Browse files Browse the repository at this point in the history
## Description

Make ethers events typesafe

## Testing

Explain the quality checks that have been done on the code changes

## Additional Information

- [ ] I read the [contributing docs](../docs/contributing.md) (if this
is your first contribution)

Your ENS/address:

Co-authored-by: Will Cory <[email protected]>
  • Loading branch information
roninjin10 and Will Cory authored Sep 24, 2023
1 parent b5c71c6 commit 2ac0b12
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-trainers-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@evmts/ethers": minor
---

Add typesafe event inputs. The input type will have autocomplete but still is widened to accept the ethers type. The return type is extremely difficult to override to being generic
20 changes: 0 additions & 20 deletions ethers/src/Contract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,24 +289,4 @@ describe('ethers.Contract', () => {
await c.balanceOf('0x32307adfFE088e383AFAa721b06436aDaBA47DBE'),
).toMatchInlineSnapshot('0n')
})

test('should work with custom address with chainId supplied even though the chainId is unnecessary', async () => {
const c = new Contract(addresses[420], abi, provider)
expect(c).toBeInstanceOf(Contract)
expect(await c.name({ blockTag: 12865720 })).toMatchInlineSnapshot(
'"OptimismUselessToken-1"',
)
expect(await c.symbol({ blockTag: 12865720 })).toMatchInlineSnapshot(
'"OUT-1"',
)
expect(await c.decimals({ blockTag: 12865720 })).toMatchInlineSnapshot(
'18n',
)
expect(await c.totalSupply({ blockTag: 12865720 })).toMatchInlineSnapshot(
'71000000000000000000000n',
)
expect(
await c.balanceOf('0x32307adfFE088e383AFAa721b06436aDaBA47DBE'),
).toMatchInlineSnapshot('0n')
})
})
31 changes: 29 additions & 2 deletions ethers/src/TypesafeEthersContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ import type { BaseContractMethod } from './BaseContractMethod'
import type {
Abi,
AbiParametersToPrimitiveTypes,
ExtractAbiEvent,
ExtractAbiEventNames,
ExtractAbiFunction,
ExtractAbiFunctionNames,
} from 'abitype'
import type { ContractTransactionResponse } from 'ethers'
import type { BaseContract } from 'ethers'
import type { Log } from 'ethers'
import type { EventLog } from 'ethers'
import type { BlockTag } from 'ethers'
import type {
BaseContract,
ContractEventName,
ContractTransactionResponse,
} from 'ethers'

export type TypesafeEthersContract<TAbi extends Abi> = BaseContract & {
// readonly methods
Expand Down Expand Up @@ -43,4 +51,23 @@ export type TypesafeEthersContract<TAbi extends Abi> = BaseContract & {
>[0],
ContractTransactionResponse
>
} & {
// events
queryFilter: <
TContractEventName extends
| Omit<ContractEventName, ExtractAbiEventNames<TAbi>>
| ExtractAbiEventNames<TAbi>,
>(
event: TContractEventName,
fromBlock?: BlockTag,
toBlock?: BlockTag,
// TODO this return type does not work
// this is extremely difficult to override the return type into being generic
) => Promise<
Array<
TContractEventName extends ExtractAbiEventNames<TAbi>
? ExtractAbiEvent<TAbi, TContractEventName>
: EventLog | Log
>
>
}

1 comment on commit 2ac0b12

@vercel
Copy link

@vercel vercel bot commented on 2ac0b12 Sep 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

evmts-docs – ./

evmts-docs-evmts.vercel.app
evmts-docs-git-main-evmts.vercel.app
evmts.dev

Please sign in to comment.