Skip to content

Commit

Permalink
feat: integrate faucet into cli
Browse files Browse the repository at this point in the history
  • Loading branch information
alexghr committed Jul 24, 2024
1 parent 3f13db1 commit 4935ff8
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 3 deletions.
10 changes: 8 additions & 2 deletions yarn-project/aztec-faucet/src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ if (EXTRA_ASSETS) {
});
}

class ThrottleError extends Error {
constructor(address: string) {
super(`Not funding address ${address}, please try again later`);
}
}

/**
* Checks if the requested asset is something the faucet can handle.
* @param asset - The asset to check
Expand Down Expand Up @@ -88,7 +94,7 @@ function checkThrottle(asset: 'eth' | AssetName, address: Hex) {
const current = new Date();
const diff = (current.getTime() - last.getTime()) / 1000;
if (diff < interval) {
throw new Error(`Not funding address ${address}, please try again later`);
throw new ThrottleError(address);
}
}

Expand Down Expand Up @@ -254,7 +260,7 @@ async function main() {
await next();
} catch (err: any) {
logger.error(err);
ctx.status = 400;
ctx.status = err instanceof ThrottleError ? 429 : 400;
ctx.body = { error: err.message };
}
};
Expand Down
15 changes: 15 additions & 0 deletions yarn-project/cli/src/cmds/devnet/faucet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { type EthAddress } from '@aztec/circuits.js';
import { type LogFn } from '@aztec/foundation/log';

export async function dripFaucet(faucetUrl: string, asset: string, account: EthAddress, log: LogFn): Promise<void> {
const url = new URL(`${faucetUrl}/drip/${account.toString()}`);
url.searchParams.set('asset', asset);
const res = await fetch(url);
if (res.status === 200) {
log(`Dripped ${asset} for ${account.toString()}`);
} else if (res.status === 429) {
log(`Rate limited when dripping ${asset} for ${account.toString()}`);
} else {
log(`Failed to drip ${asset} for ${account.toString()}`);
}
}
13 changes: 12 additions & 1 deletion yarn-project/cli/src/cmds/devnet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type DebugLogger, type LogFn } from '@aztec/foundation/log';

import { type Command } from 'commander';

import { ETHEREUM_HOST, l1ChainIdOption, pxeOption } from '../../utils/commands.js';
import { ETHEREUM_HOST, l1ChainIdOption, parseEthereumAddress, pxeOption } from '../../utils/commands.js';

export function injectCommands(program: Command, log: LogFn, debugLogger: DebugLogger) {
program
Expand Down Expand Up @@ -36,5 +36,16 @@ export function injectCommands(program: Command, log: LogFn, debugLogger: DebugL
);
});

program
.command('drip-faucet')
.description('Drip the faucet')
.requiredOption('-u, --faucet-url <string>', 'Url of the faucet', 'http://localhost:8082')
.requiredOption('-t, --token <string>', 'The asset to drip', 'eth')
.requiredOption('-a, --address <string>', 'The Ethereum address to drip to', parseEthereumAddress)
.action(async options => {
const { dripFaucet } = await import('./faucet.js');
await dripFaucet(options.faucetUrl, options.token, options.address, log);
});

return program;
}
11 changes: 11 additions & 0 deletions yarn-project/cli/src/cmds/l1/create_l1_account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { type LogFn } from '@aztec/foundation/log';

import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';

export function createL1Account(log: LogFn) {
const privateKey = generatePrivateKey();
const account = privateKeyToAccount(privateKey);

log(`Private Key: ${privateKey}`);
log(`Address: ${account.address}`);
}
5 changes: 5 additions & 0 deletions yarn-project/cli/src/cmds/l1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ export function injectCommands(program: Command, log: LogFn, debugLogger: DebugL
);
});

program.command('create-l1-account').action(async () => {
const { createL1Account } = await import('./create_l1_account.js');
createL1Account(log);
});

program
.command('get-l1-balance')
.description('Gets the balance of gas tokens in L1 for the given Ethereum address.')
Expand Down

0 comments on commit 4935ff8

Please sign in to comment.