Skip to content

Commit

Permalink
refactor: get-l1-balance works with any ERC20 token
Browse files Browse the repository at this point in the history
  • Loading branch information
alexghr committed Jul 24, 2024
1 parent 7bba9a0 commit a262e69
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 18 deletions.
18 changes: 3 additions & 15 deletions yarn-project/cli/src/cmds/l1/get_l1_balance.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,16 @@
import { type EthAddress } from '@aztec/circuits.js';
import { createEthereumChain } from '@aztec/ethereum';
import { type DebugLogger, type LogFn } from '@aztec/foundation/log';
import { type LogFn } from '@aztec/foundation/log';
import { PortalERC20Abi } from '@aztec/l1-artifacts';

import { createPublicClient, getContract, http } from 'viem';

import { createCompatibleClient } from '../../client.js';

export async function getL1Balance(
who: EthAddress,
rpcUrl: string,
l1RpcUrl: string,
chainId: number,
log: LogFn,
debugLogger: DebugLogger,
) {
const client = await createCompatibleClient(rpcUrl, debugLogger);
const { l1ContractAddresses } = await client.getNodeInfo();

export async function getL1Balance(who: EthAddress, token: EthAddress, l1RpcUrl: string, chainId: number, log: LogFn) {
const chain = createEthereumChain(l1RpcUrl, chainId);
const publicClient = createPublicClient({ chain: chain.chainInfo, transport: http(chain.rpcUrl) });

const gasL1 = getContract({
address: l1ContractAddresses.gasTokenAddress.toString(),
address: token.toString(),
abi: PortalERC20Abi,
client: publicClient,
});
Expand Down
6 changes: 3 additions & 3 deletions yarn-project/cli/src/cmds/l1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,18 @@ export function injectCommands(program: Command, log: LogFn, debugLogger: DebugL

program
.command('get-l1-balance')
.description('Gets the balance of gas tokens in L1 for the given Ethereum address.')
.description('Gets the balance of an ERC token in L1 for the given Ethereum address.')
.argument('<who>', 'Ethereum address to check.', parseEthereumAddress)
.requiredOption(
'--l1-rpc-url <string>',
'Url of the ethereum host. Chain identifiers localhost and testnet can be used',
ETHEREUM_HOST,
)
.addOption(pxeOption)
.requiredOption('-t, --token <string>', 'The address of the token to check the balance of', parseEthereumAddress)
.addOption(l1ChainIdOption)
.action(async (who, options) => {
const { getL1Balance } = await import('./get_l1_balance.js');
await getL1Balance(who, options.rpcUrl, options.l1RpcUrl, options.l1ChainId, log, debugLogger);
await getL1Balance(who, options.token, options.l1RpcUrl, options.l1ChainId, log);
});

return program;
Expand Down

0 comments on commit a262e69

Please sign in to comment.