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

feat: bridge devcoin #7595

Merged
merged 3 commits into from
Jul 24, 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
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;
}
35 changes: 35 additions & 0 deletions yarn-project/cli/src/cmds/l1/bridge_erc20.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { type AztecAddress, type EthAddress } from '@aztec/circuits.js';
import { createEthereumChain, createL1Clients } from '@aztec/ethereum';
import { type DebugLogger, type LogFn } from '@aztec/foundation/log';

import { ERC20PortalManager } from '../../portal_manager.js';

export async function bridgeERC20(
amount: bigint,
recipient: AztecAddress,
l1RpcUrl: string,
chainId: number,
privateKey: string | undefined,
mnemonic: string,
tokenAddress: EthAddress,
portalAddress: EthAddress,
mint: boolean,
log: LogFn,
debugLogger: DebugLogger,
) {
// Prepare L1 client
const chain = createEthereumChain(l1RpcUrl, chainId);
const { publicClient, walletClient } = createL1Clients(chain.rpcUrl, privateKey ?? mnemonic, chain.chainInfo);

// Setup portal manager
const portal = await ERC20PortalManager.create(tokenAddress, portalAddress, publicClient, walletClient, debugLogger);
const { secret } = await portal.prepareTokensOnL1(amount, amount, recipient, mint);

if (mint) {
log(`Minted ${amount} tokens on L1 and pushed to L2 portal`);
} else {
log(`Bridged ${amount} tokens to L2 portal`);
}
log(`claimAmount=${amount},claimSecret=${secret}\n`);
log(`Note: You need to wait for two L2 blocks before pulling them from the L2 side`);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createEthereumChain, createL1Clients } from '@aztec/ethereum';
import { type DebugLogger, type LogFn } from '@aztec/foundation/log';

import { createCompatibleClient } from '../../client.js';
import { GasPortalManagerFactory } from '../../gas_portal.js';
import { FeeJuicePortalManager } from '../../portal_manager.js';

export async function bridgeL1Gas(
amount: bigint,
Expand All @@ -12,6 +12,7 @@ export async function bridgeL1Gas(
l1RpcUrl: string,
chainId: number,
mnemonic: string,
mint: boolean,
log: LogFn,
debugLogger: DebugLogger,
) {
Expand All @@ -23,14 +24,8 @@ export async function bridgeL1Gas(
const client = await createCompatibleClient(rpcUrl, debugLogger);

// Setup portal manager
const manager = await GasPortalManagerFactory.create({
pxeService: client,
publicClient: publicClient,
walletClient: walletClient,
logger: debugLogger,
});

const { secret } = await manager.prepareTokensOnL1(amount, amount, recipient, false);
const portal = await FeeJuicePortalManager.create(client, publicClient, walletClient, debugLogger);
const { secret } = await portal.prepareTokensOnL1(amount, amount, recipient, mint);

log(`Minted ${amount} gas tokens on L1 and pushed to L2 portal`);
log(`claimAmount=${amount},claimSecret=${secret}\n`);
Expand Down
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}`);
}
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
54 changes: 49 additions & 5 deletions yarn-project/cli/src/cmds/l1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function injectCommands(program: Command, log: LogFn, debugLogger: DebugL
});

program
.command('bridge-l1-gas')
.command('bridge-fee-juice')
.description('Mints L1 gas tokens and pushes them to L2.')
.argument('<amount>', 'The amount of gas tokens to mint and bridge.', parseBigint)
.argument('<recipient>', 'Aztec address of the recipient.', parseAztecAddress)
Expand All @@ -100,36 +100,80 @@ export function injectCommands(program: Command, log: LogFn, debugLogger: DebugL
'The mnemonic to use for deriving the Ethereum address that will mint and bridge',
'test test test test test test test test test test test junk',
)
.option('--mint', 'Mint the tokens on L1', false)
.addOption(pxeOption)
.addOption(l1ChainIdOption)
.action(async (amount, recipient, options) => {
const { bridgeL1Gas } = await import('./bridge_l1_gas.js');
const { bridgeL1Gas } = await import('./bridge_fee_juice.js');
await bridgeL1Gas(
amount,
recipient,
options.rpcUrl,
options.l1RpcUrl,
options.l1ChainId,
options.mnemonic,
options.mint,
log,
debugLogger,
);
});

program
.command('bridge-erc20')
.description('Bridges ERC20 tokens to L2.')
.argument('<amount>', 'The amount of gas tokens to mint and bridge.', parseBigint)
.argument('<recipient>', 'Aztec address of the recipient.', parseAztecAddress)
.requiredOption(
'--l1-rpc-url <string>',
'Url of the ethereum host. Chain identifiers localhost and testnet can be used',
ETHEREUM_HOST,
)
.option(
'-m, --mnemonic <string>',
'The mnemonic to use for deriving the Ethereum address that will mint and bridge',
'test test test test test test test test test test test junk',
)
.option('--mint', 'Mint the tokens on L1', false)
.addOption(l1ChainIdOption)
.requiredOption('-t, --token <string>', 'The address of the token to bridge', parseEthereumAddress)
.requiredOption('-p, --portal <string>', 'The address of the portal contract', parseEthereumAddress)
.option('-k, --private-key <string>', 'The private key to use for deployment', PRIVATE_KEY)
.action(async (amount, recipient, options) => {
const { bridgeERC20 } = await import('./bridge_erc20.js');
await bridgeERC20(
amount,
recipient,
options.l1RpcUrl,
options.l1ChainId,
options.privateKey,
options.mnemonic,
options.token,
options.portal,
options.mint,
log,
debugLogger,
);
});

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.')
.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
Loading
Loading