Skip to content

Commit

Permalink
Add getChainId to Network interface, improve testing (#68)
Browse files Browse the repository at this point in the history
* Add getChainId to Network interface

* Update Network stub

* Update Network tests

* Update bindings

* README nit

* Add changeset
  • Loading branch information
ryangoree authored May 2, 2024
1 parent 919f525 commit 93531e6
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .changeset/curly-tomatoes-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@delvtech/evm-client-ethers": patch
"@delvtech/evm-client-viem": patch
"@delvtech/evm-client": patch
---

Added getChainId method to the Network interface.
2 changes: 1 addition & 1 deletion packages/evm-client-ethers/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @delvtech/evm-client-ethers

Ethers bindings for [@delvtech/evm-client](https://github.com/delvtech/evm-client/tree/main/packages/evm-client)
Ethers implementations of [@delvtech/evm-client](https://github.com/delvtech/evm-client/tree/main/packages/evm-client).

```ts
import { createCachedReadContract } from '@delvtech/evm-client-ethers';
Expand Down
5 changes: 5 additions & 0 deletions packages/evm-client-ethers/src/network/createNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export function createNetwork(provider: Provider): Network {
};
},

async getChainId() {
const network = await provider.getNetwork();
return Number(network.chainId);
},

async getTransaction(hash) {
const transaction = await provider.getTransaction(hash);

Expand Down
2 changes: 1 addition & 1 deletion packages/evm-client-viem/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @delvtech/evm-client-viem

Viem bindings for [@delvtech/evm-client](https://github.com/delvtech/evm-client/tree/main/packages/evm-client)
Viem implementations of [@delvtech/evm-client](https://github.com/delvtech/evm-client/tree/main/packages/evm-client).

```ts
import { createCachedReadContract } from '@delvtech/evm-client-viem';
Expand Down
4 changes: 4 additions & 0 deletions packages/evm-client-viem/src/network/createNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export function createNetwork(publicClient: PublicClient): Network {
return { blockNumber: block.number, timestamp: block.timestamp };
},

getChainId() {
return publicClient.getChainId();
},

async getTransaction(hash) {
const {
blockHash,
Expand Down
65 changes: 65 additions & 0 deletions packages/evm-client/src/network/stubs/NetworkStub.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,75 @@
import { ALICE } from 'src/base/testing/accounts';
import {
NetworkStub,
transactionToReceipt,
} from 'src/network/stubs/NetworkStub';
import { describe, expect, it } from 'vitest';
import { Transaction } from '../types/Transaction';

describe('NetworkStub', () => {
it('stubs getBalance', async () => {
const network = new NetworkStub();

network.stubGetBalance({
args: [ALICE],
value: 100n,
});

const balance = await network.getBalance(ALICE);

expect(balance).toEqual(100n);
});

it('stubs getBlock', async () => {
const network = new NetworkStub();

const block = {
blockNumber: 1n,
timestamp: 1000n,
};
network.stubGetBlock({
args: [{ blockNumber: 1n }],
value: block,
});

const blockResponse = await network.getBlock({ blockNumber: 1n });

expect(blockResponse).toEqual(block);
});

it('stubs getChainId', async () => {
const network = new NetworkStub();

network.stubGetChainId(42069);

const chainId = await network.getChainId();

expect(chainId).toEqual(42069);
});

it('stubs getTransaction', async () => {
const network = new NetworkStub();

const txHash = '0x123abc';
const tx: Transaction = {
gas: 100n,
gasPrice: 100n,
input: '0x456def',
nonce: 0,
type: '0x0',
value: 0n,
};

network.stubGetTransaction({
args: [txHash],
value: tx,
});

const transaction = await network.getTransaction(txHash);

expect(transaction).toEqual(tx);
});

it('waits for stubbed transactions', async () => {
const network = new NetworkStub();

Expand Down
18 changes: 18 additions & 0 deletions packages/evm-client/src/network/stubs/NetworkStub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class NetworkStub implements Network {
protected getBlockStub:
| SinonStub<[NetworkGetBlockArgs?], Promise<Block | undefined>>
| undefined;
protected getChainIdStub: SinonStub<[], Promise<number>> | undefined;
protected getTransactionStub:
| SinonStub<[NetworkGetTransactionArgs?], Promise<Transaction | undefined>>
| undefined;
Expand Down Expand Up @@ -64,6 +65,14 @@ export class NetworkStub implements Network {
this.getBlockStub.resolves(value);
}

stubGetChainId(id: number): void {
if (!this.getChainIdStub) {
this.getChainIdStub = stub();
}

this.getChainIdStub.resolves(id);
}

stubGetTransaction({
args,
value,
Expand Down Expand Up @@ -102,6 +111,15 @@ export class NetworkStub implements Network {
return this.getBlockStub(args);
}

getChainId(): Promise<number> {
if (!this.getChainIdStub) {
throw new Error(
`The getChainId function must be stubbed first:\n\tcontract.stubGetChainId()`,
);
}
return this.getChainIdStub();
}

getTransaction(
...args: NetworkGetTransactionArgs
): Promise<Transaction | undefined> {
Expand Down
5 changes: 5 additions & 0 deletions packages/evm-client/src/network/types/Network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export interface Network {
*/
getBlock(...args: NetworkGetBlockArgs): Promise<Block | undefined>;

/**
* Get the chain ID of the network.
*/
getChainId(): Promise<number>;

/**
* Get a transaction from a transaction hash.
*/
Expand Down

0 comments on commit 93531e6

Please sign in to comment.