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

chore: Move gas bridge initialization into L1 contracts deployment #6912

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
43 changes: 5 additions & 38 deletions yarn-project/aztec/src/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,11 @@ import {
} from '@aztec/l1-artifacts';
import { GasTokenContract } from '@aztec/noir-contracts.js/GasToken';
import { KeyRegistryContract } from '@aztec/noir-contracts.js/KeyRegistry';
import { getCanonicalGasToken } from '@aztec/protocol-contracts/gas-token';
import { GasTokenAddress, getCanonicalGasToken } from '@aztec/protocol-contracts/gas-token';
import { getCanonicalKeyRegistry } from '@aztec/protocol-contracts/key-registry';
import { type PXEServiceConfig, createPXEService, getPXEServiceConfig } from '@aztec/pxe';

import {
type HDAccount,
type PrivateKeyAccount,
createPublicClient,
getContract,
http as httpViemTransport,
} from 'viem';
import { type HDAccount, type PrivateKeyAccount, createPublicClient, http as httpViemTransport } from 'viem';
import { mnemonicToAccount } from 'viem/accounts';
import { foundry } from 'viem/chains';

Expand Down Expand Up @@ -127,43 +121,16 @@ export async function deployContractsToL1(
};

const l1Contracts = await waitThenDeploy(aztecNodeConfig, () =>
deployL1Contracts(aztecNodeConfig.rpcUrl, hdAccount, localAnvil, contractDeployLogger, l1Artifacts),
deployL1Contracts(aztecNodeConfig.rpcUrl, hdAccount, localAnvil, contractDeployLogger, l1Artifacts, {
l2GasTokenAddress: GasTokenAddress,
}),
);
await initL1GasPortal(l1Contracts, getCanonicalGasToken().address);

aztecNodeConfig.l1Contracts = l1Contracts.l1ContractAddresses;

return aztecNodeConfig.l1Contracts;
}

/**
* Initializes the portal between L1 and L2 used to pay for gas.
* @param l1Data - The deployed L1 data.
*/
async function initL1GasPortal(
{ walletClient, l1ContractAddresses }: DeployL1Contracts,
l2GasTokenAddress: AztecAddress,
) {
const gasPortal = getContract({
address: l1ContractAddresses.gasPortalAddress.toString(),
abi: GasPortalAbi,
client: walletClient,
});

await gasPortal.write.initialize(
[
l1ContractAddresses.registryAddress.toString(),
l1ContractAddresses.gasTokenAddress.toString(),
l2GasTokenAddress.toString(),
],
{} as any,
);

logger.info(
`Initialized Gas Portal at ${l1ContractAddresses.gasPortalAddress} to bridge between L1 ${l1ContractAddresses.gasTokenAddress} to L2 ${l2GasTokenAddress}`,
);
}

/**
* Deploys the contract to pay for gas on L2.
*/
Expand Down
16 changes: 11 additions & 5 deletions yarn-project/cli/src/gas_portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ class GasPortalManager {
}

async mintTokensOnL1(amount: bigint) {
this.logger.info('Minting tokens on L1');
this.logger.info(
`Minting tokens on L1 for ${this.ethAccount.toString()} in contract ${this.underlyingERC20.address}`,
);
await this.publicClient.waitForTransactionReceipt({
hash: await this.underlyingERC20.write.mint([this.ethAccount.toString(), amount]),
});
Expand All @@ -123,16 +125,20 @@ class GasPortalManager {
}

async sendTokensToPortalPublic(bridgeAmount: bigint, l2Address: AztecAddress, secretHash: Fr) {
this.logger.info(
`Approving erc20 tokens for the TokenPortal at ${this.tokenPortalAddress.toString()} ${this.tokenPortal.address}`,
);
await this.publicClient.waitForTransactionReceipt({
hash: await this.underlyingERC20.write.approve([this.tokenPortalAddress.toString(), bridgeAmount]),
});

// Deposit tokens to the TokenPortal
this.logger.info('Sending messages to L1 portal to be consumed publicly');
this.logger.info(
`Simulating token portal deposit configured for token ${await this.tokenPortal.read.l2TokenAddress()} with registry ${await this.tokenPortal.read.registry()} to retrieve message hash`,
);
const args = [l2Address.toString(), bridgeAmount, secretHash.toString()] as const;
const { result: messageHash } = await this.tokenPortal.simulate.depositToAztecPublic(args, {
account: this.ethAccount.toString(),
} as any);
const { result: messageHash } = await this.tokenPortal.simulate.depositToAztecPublic(args);
this.logger.info('Sending messages to L1 portal to be consumed publicly');
await this.publicClient.waitForTransactionReceipt({
hash: await this.tokenPortal.write.depositToAztecPublic(args),
});
Expand Down
5 changes: 4 additions & 1 deletion yarn-project/cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
PortalERC20Abi,
PortalERC20Bytecode,
} from '@aztec/l1-artifacts';
import { GasTokenAddress } from '@aztec/protocol-contracts/gas-token';

import TOML from '@iarna/toml';
import { CommanderError, InvalidArgumentError } from 'commander';
Expand Down Expand Up @@ -101,7 +102,9 @@ export async function deployAztecContracts(
contractBytecode: GasPortalBytecode,
},
};
return await deployL1Contracts(chain.rpcUrl, account, chain.chainInfo, debugLogger, l1Artifacts);
return await deployL1Contracts(chain.rpcUrl, account, chain.chainInfo, debugLogger, l1Artifacts, {
l2GasTokenAddress: GasTokenAddress,
});
}

/**
Expand Down
31 changes: 5 additions & 26 deletions yarn-project/end-to-end/src/fixtures/setup_l1_contracts.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
type DebugLogger,
type DeployL1Contracts,
type L1ContractArtifactsForDeployment,
deployL1Contracts,
} from '@aztec/aztec.js';
import { type DebugLogger, type L1ContractArtifactsForDeployment, deployL1Contracts } from '@aztec/aztec.js';
import {
AvailabilityOracleAbi,
AvailabilityOracleBytecode,
Expand All @@ -22,7 +17,7 @@ import {
} from '@aztec/l1-artifacts';
import { GasTokenAddress } from '@aztec/protocol-contracts/gas-token';

import { type HDAccount, type PrivateKeyAccount, getContract } from 'viem';
import { type HDAccount, type PrivateKeyAccount } from 'viem';
import { foundry } from 'viem/chains';

export { deployAndInitializeTokenAndBridgeContracts } from '../shared/cross_chain_test_harness.js';
Expand Down Expand Up @@ -63,25 +58,9 @@ export const setupL1Contracts = async (
},
};

const l1Data = await deployL1Contracts(l1RpcUrl, account, foundry, logger, l1Artifacts);
await initGasBridge(l1Data);
const l1Data = await deployL1Contracts(l1RpcUrl, account, foundry, logger, l1Artifacts, {
l2GasTokenAddress: GasTokenAddress,
});

return l1Data;
};

async function initGasBridge({ walletClient, l1ContractAddresses }: DeployL1Contracts) {
const gasPortal = getContract({
address: l1ContractAddresses.gasPortalAddress.toString(),
abi: GasPortalAbi,
client: walletClient,
});

await gasPortal.write.initialize(
[
l1ContractAddresses.registryAddress.toString(),
l1ContractAddresses.gasTokenAddress.toString(),
GasTokenAddress.toString(),
],
{} as any,
);
}
22 changes: 3 additions & 19 deletions yarn-project/end-to-end/src/fixtures/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,29 +124,13 @@ export const setupL1Contracts = async (
},
};

const l1Data = await deployL1Contracts(l1RpcUrl, account, foundry, logger, l1Artifacts);
await initGasBridge(l1Data);
const l1Data = await deployL1Contracts(l1RpcUrl, account, foundry, logger, l1Artifacts, {
l2GasTokenAddress: GasTokenAddress,
});

return l1Data;
};

async function initGasBridge({ walletClient, l1ContractAddresses }: DeployL1Contracts) {
const gasPortal = getContract({
address: l1ContractAddresses.gasPortalAddress.toString(),
abi: GasPortalAbi,
client: walletClient,
});

await gasPortal.write.initialize(
[
l1ContractAddresses.registryAddress.toString(),
l1ContractAddresses.gasTokenAddress.toString(),
GasTokenAddress.toString(),
],
{} as any,
);
}

/**
* Sets up Private eXecution Environment (PXE).
* @param aztecNode - An instance of Aztec Node.
Expand Down
21 changes: 21 additions & 0 deletions yarn-project/ethereum/src/deploy_l1_contracts.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type AztecAddress } from '@aztec/foundation/aztec-address';
import { EthAddress } from '@aztec/foundation/eth-address';
import { type DebugLogger } from '@aztec/foundation/log';

Expand Down Expand Up @@ -121,6 +122,7 @@ export function createL1Clients(
* @param chain - The chain instance to deploy to.
* @param logger - A logger object.
* @param contractsToDeploy - The set of L1 artifacts to be deployed
* @param args - Arguments for initialization of L1 contracts
* @returns A list of ETH addresses of the deployed contracts.
*/
export const deployL1Contracts = async (
Expand All @@ -129,6 +131,7 @@ export const deployL1Contracts = async (
chain: Chain,
logger: DebugLogger,
contractsToDeploy: L1ContractArtifactsForDeployment,
args: { l2GasTokenAddress: AztecAddress },
): Promise<DeployL1Contracts> => {
logger.debug('Deploying contracts...');

Expand Down Expand Up @@ -224,6 +227,24 @@ export const deployL1Contracts = async (

logger.info(`Deployed Gas Portal at ${gasPortalAddress}`);

const gasPortal = getContract({
address: gasPortalAddress.toString(),
abi: contractsToDeploy.gasPortal.contractAbi,
client: walletClient,
});

await publicClient.waitForTransactionReceipt({
hash: await gasPortal.write.initialize([
registryAddress.toString(),
gasTokenAddress.toString(),
args.l2GasTokenAddress.toString(),
]),
});

logger.info(
`Initialized Gas Portal at ${gasPortalAddress} to bridge between L1 ${gasTokenAddress} to L2 ${args.l2GasTokenAddress}`,
);

// fund the rollup contract with gas tokens
const gasToken = getContract({
address: gasTokenAddress.toString(),
Expand Down
Loading