Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getBalance method to Network type #56

Merged
merged 6 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/green-ghosts-hammer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@delvtech/evm-client-ethers": minor
"@delvtech/evm-client-viem": minor
"@delvtech/evm-client": minor
---

Add a getBalance method to the Network interface for fetching native currency balances (e.g. ETH)
9 changes: 9 additions & 0 deletions packages/evm-client-ethers/src/network/createNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ import { Provider } from 'ethers';

export function createNetwork(provider: Provider): Network {
return {
async getBalance(account, options = {}) {
const { blockHash, blockNumber, blockTag } = options;

return provider.getBalance(
account,
blockHash || blockNumber || blockTag || 'latest',
);
},

async getBlock(options = {}) {
const { blockHash, blockNumber, blockTag } = options;

Expand Down
26 changes: 25 additions & 1 deletion packages/evm-client-viem/src/network/createNetwork.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
import { Network } from '@delvtech/evm-client';
import { PublicClient, TransactionLegacy, rpcTransactionType } from 'viem';
import {
GetBalanceParameters,
PublicClient,
TransactionLegacy,
rpcTransactionType,
} from 'viem';

export function createNetwork(publicClient: PublicClient): Network {
return {
async getBalance(account, options) {
const { blockHash, blockNumber, blockTag } = options ?? {};

const parameters: Partial<GetBalanceParameters> = {
address: account,
};

if (blockNumber) {
parameters.blockNumber = blockNumber;
} else if (blockTag) {
parameters.blockTag = blockHash;
} else if (blockHash) {
const block = await publicClient.getBlock({ blockHash });
parameters.blockNumber = block.number;
}

return publicClient.getBalance(parameters as GetBalanceParameters);
},

async getBlock(args) {
const block = await publicClient.getBlock(args);

Expand Down
2 changes: 2 additions & 0 deletions packages/evm-client/src/exports/network.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export type { Block, BlockTag } from 'src/network/types/Block';
export type {
Network,
NetworkGetBalanceArgs,
NetworkGetBlockArgs,
NetworkGetBlockOptions,
NetworkGetTransactionArgs,
NetworkWaitForTransactionArgs,
} from 'src/network/types/Network';
export type {
MinedTransaction,
Expand Down
33 changes: 33 additions & 0 deletions packages/evm-client/src/network/stubs/NetworkStub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { SinonStub, stub } from 'sinon';
import { Block } from 'src/network/types/Block';
import {
Network,
NetworkGetBalanceArgs,
NetworkGetBlockArgs,
NetworkGetTransactionArgs,
NetworkWaitForTransactionArgs,
Expand All @@ -13,13 +14,36 @@ import { Transaction, TransactionReceipt } from 'src/network/types/Transaction';
* testing.
*/
export class NetworkStub implements Network {
protected getBalanceStub:
| SinonStub<[NetworkGetBalanceArgs?], Promise<bigint>>
| undefined;
protected getBlockStub:
| SinonStub<[NetworkGetBlockArgs?], Promise<Block | undefined>>
| undefined;
protected getTransactionStub:
| SinonStub<[NetworkGetTransactionArgs?], Promise<Transaction | undefined>>
| undefined;

stubGetBalance({
args,
value,
}: {
args?: NetworkGetBalanceArgs | undefined;
value: bigint;
}): void {
if (!this.getBalanceStub) {
this.getBalanceStub = stub();
}

// Account for dynamic args if provided
if (args) {
this.getBalanceStub.withArgs(args).resolves(value);
return;
}

this.getBalanceStub.resolves(value);
}

stubGetBlock({
args,
value,
Expand Down Expand Up @@ -60,6 +84,15 @@ export class NetworkStub implements Network {
this.getTransactionStub.resolves(value);
}

getBalance(...args: NetworkGetBalanceArgs): Promise<bigint> {
if (!this.getBalanceStub) {
throw new Error(
`The getBalance function must be stubbed first:\n\tcontract.stubGetBalance()`,
);
}
return this.getBalanceStub(args);
}

getBlock(...args: NetworkGetBlockArgs): Promise<Block | undefined> {
if (!this.getBlockStub) {
throw new Error(
Expand Down
10 changes: 10 additions & 0 deletions packages/evm-client/src/network/types/Network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import { Transaction, TransactionReceipt } from 'src/network/types/Transaction';
* An interface representing data the SDK needs to get from the network.
*/
export interface Network {
/**
* Get the balance of native currency for an account.
*/
getBalance(...args: NetworkGetBalanceArgs): Promise<bigint>;

/**
* Get a block from a block tag, number, or hash. If no argument is provided,
* the latest block is returned.
Expand Down Expand Up @@ -45,6 +50,11 @@ export type NetworkGetBlockOptions =
blockTag?: BlockTag;
};

export type NetworkGetBalanceArgs = [
address: `0x${string}`,
block?: NetworkGetBlockOptions,
];

export type NetworkGetBlockArgs = [options?: NetworkGetBlockOptions];

export type NetworkGetTransactionArgs = [hash: `0x${string}`];
Expand Down
Loading