Skip to content

Commit

Permalink
feat: faucet takes mnemonic (#7546)
Browse files Browse the repository at this point in the history
This PR updates the faucet to support accounts from mnemonics, not just
private keys.
  • Loading branch information
alexghr authored Jul 22, 2024
1 parent 609a68f commit dbd054f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
11 changes: 7 additions & 4 deletions .github/workflows/devnet-deploys.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ env:
TF_VAR_API_KEY: ${{ secrets.FORK_API_KEY }}
TF_VAR_FORK_MNEMONIC: ${{ secrets.FORK_MNEMONIC }}
TF_VAR_INFURA_API_KEY: ${{ secrets.INFURA_API_KEY }}
TF_VAR_FAUCET_ACCOUNT_INDEX: 5

jobs:
setup:
Expand Down Expand Up @@ -127,6 +128,12 @@ jobs:
run: |
terraform apply -input=false -auto-approve -replace="aws_efs_file_system.node_data_store"
- name: Deploy Faucet
working-directory: ./yarn-project/aztec/aztec-faucet
run: |
terraform init -input=false -backend-config="key=${{ env.DEPLOY_TAG }}/aztec-faucet"
terraform apply -input=false -auto-approve
- name: Deploy Provers
working-directory: ./yarn-project/aztec/terraform/prover
run: |
Expand Down Expand Up @@ -187,17 +194,13 @@ jobs:
--rpc-url https://api.aztec.network/${{ env.DEPLOY_TAG }}/aztec-pxe/${{ secrets.FORK_API_KEY }} \
--args $AZTEC_ADDRESS DevCoin DEV 18 \
--private-key $AZTEC_PRIVATE_KEY \
--public-deployment \
--class-registration \
| tee ./token_contract.txt
source ./.github/scripts/extract_output.sh l2-contract ./token_contract.txt TOKEN_CONTRACT_ADDRESS
docker run aztecprotocol/aztec:${{ env.DEPLOY_TAG }} deploy FPCContract \
--rpc-url https://api.aztec.network/${{ env.DEPLOY_TAG }}/aztec-pxe/${{ secrets.FORK_API_KEY }} \
--args $TOKEN_CONTRACT_ADDRESS $FEE_JUICE_ADDRESS \
--private-key $AZTEC_PRIVATE_KEY \
--public-deployment \
--class-registration \
| tee ./fpc_contract.txt
source ./.github/scripts/extract_output.sh l2-contract ./fpc_contract.txt FPC_CONTRACT_ADDRESS
Expand Down
37 changes: 33 additions & 4 deletions yarn-project/aztec-faucet/src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@ import http from 'http';
import Koa from 'koa';
import cors from 'koa-cors';
import Router from 'koa-router';
import { type Hex, http as ViemHttp, createPublicClient, createWalletClient, parseEther } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
import {
type Hex,
type LocalAccount,
http as ViemHttp,
createPublicClient,
createWalletClient,
parseEther,
} from 'viem';
import { mnemonicToAccount, privateKeyToAccount } from 'viem/accounts';

const {
FAUCET_PORT = 8082,
API_PREFIX = '',
RPC_URL = '',
L1_CHAIN_ID = '',
FORK_MNEMONIC = '',
FAUCET_ACCOUNT_INDEX = '',
PRIVATE_KEY = '',
INTERVAL = '',
ETH_AMOUNT = '',
Expand All @@ -24,7 +33,6 @@ const logger = createDebugLogger('aztec:faucet');

const rpcUrl = RPC_URL;
const l1ChainId = +L1_CHAIN_ID;
const privateKey: Hex = PRIVATE_KEY ? createHex(PRIVATE_KEY) : NULL_KEY;
const interval = +INTERVAL;
const mapping: { [key: Hex]: Date } = {};

Expand Down Expand Up @@ -53,14 +61,35 @@ function checkThrottle(address: Hex) {
}
}

/**
* Get the account to use for sending ETH
* @returns The account to use for sending ETH
*/
function getFaucetAccount(): LocalAccount {
let account: LocalAccount;
if (FORK_MNEMONIC) {
const accountIndex = Number.isNaN(+FAUCET_ACCOUNT_INDEX) ? 0 : +FAUCET_ACCOUNT_INDEX;
account = mnemonicToAccount(FORK_MNEMONIC, {
accountIndex,
});
} else if (PRIVATE_KEY) {
account = privateKeyToAccount(PRIVATE_KEY as `0x${string}`);
} else {
logger.warn('No mnemonic or private key provided, using null key');
account = privateKeyToAccount(NULL_KEY);
}

return account;
}

/**
* Helper function to send some ETH to the given address
* @param address - Address to receive some ETH
*/
async function transferEth(address: string) {
const chain = createEthereumChain(rpcUrl, l1ChainId);

const account = privateKeyToAccount(privateKey);
const account = getFaucetAccount();
const walletClient = createWalletClient({
account: account,
chain: chain.chainInfo,
Expand Down
8 changes: 8 additions & 0 deletions yarn-project/aztec-faucet/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ resource "aws_ecs_task_definition" "aztec-faucet" {
{
"name": "ETH_AMOUNT",
"value": "1.0"
},
{
"name": "FAUCET_ACCOUNT_INDEX",
"value": "${var.FAUCET_ACCOUNT_INDEX}"
},
{
"name": "FORK_MNEMONIC",
"value": "${var.FORK_MNEMONIC}"
}
],
"logConfiguration": {
Expand Down
8 changes: 8 additions & 0 deletions yarn-project/aztec-faucet/terraform/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ variable "FAUCET_PRIVATE_KEY" {
variable "DOCKERHUB_ACCOUNT" {
type = string
}

variable "FORK_MNEMONIC" {
type = string
}

variable "FAUCET_ACCOUNT_INDEX" {
type = string
}

0 comments on commit dbd054f

Please sign in to comment.