-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR creates a small faucet application for retrieving eth. # Checklist: Remove the checklist to signal you've completed it. Enable auto-merge if the PR is ready to merge. - [ ] If the pull request requires a cryptography review (e.g. cryptographic algorithm implementations) I have added the 'crypto' tag. - [ ] I have reviewed my diff in github, line by line and removed unexpected formatting changes, testing logs, or commented-out code. - [ ] Every change is related to the PR description. - [ ] I have [linked](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue) this pull request to relevant issues (if any exist).
- Loading branch information
1 parent
c74311d
commit 5bad35f
Showing
20 changed files
with
615 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
data | ||
dest | ||
node_modules | ||
Dockerfile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('@aztec/foundation/eslint'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/data* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
FROM 278380418400.dkr.ecr.eu-west-2.amazonaws.com/yarn-project AS builder | ||
|
||
WORKDIR /usr/src/yarn-project/aztec-faucet | ||
|
||
# Productionify. See comment in yarn-project-base/Dockerfile. | ||
RUN yarn cache clean && yarn workspaces focus --production | ||
|
||
# Create final, minimal size image. | ||
FROM node:18-alpine | ||
COPY --from=builder /usr/src/ /usr/src/ | ||
WORKDIR /usr/src/yarn-project/aztec-faucet | ||
ENTRYPOINT ["yarn"] | ||
CMD [ "start" ] | ||
EXPOSE 8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Aztec Faucet | ||
|
||
This application allows someone to obtain a small amount of eth via a http endpoint. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
{ | ||
"name": "@aztec/aztec-faucet", | ||
"version": "0.1.0", | ||
"main": "dest/bin/index.js", | ||
"type": "module", | ||
"bin": "./dest/bin/index.js", | ||
"typedocOptions": { | ||
"entryPoints": [ | ||
"./src/bin/index.ts" | ||
], | ||
"name": "Aztec Faucet", | ||
"tsconfig": "./tsconfig.json" | ||
}, | ||
"scripts": { | ||
"start": "node --no-warnings ./dest/bin", | ||
"build": "yarn clean && tsc -b", | ||
"build:dev": "tsc -b --watch", | ||
"clean": "rm -rf ./dest .tsbuildinfo", | ||
"formatting": "run -T prettier --check ./src && run -T eslint ./src", | ||
"formatting:fix": "run -T prettier -w ./src", | ||
"test": "NODE_NO_WARNINGS=1 node --experimental-vm-modules $(yarn bin jest) --passWithNoTests" | ||
}, | ||
"inherits": [ | ||
"../package.common.json" | ||
], | ||
"jest": { | ||
"preset": "ts-jest/presets/default-esm", | ||
"moduleNameMapper": { | ||
"^(\\.{1,2}/.*)\\.m?js$": "$1" | ||
}, | ||
"testRegex": "./src/.*\\.test\\.(js|mjs|ts)$", | ||
"rootDir": "./src" | ||
}, | ||
"dependencies": { | ||
"@aztec/ethereum": "workspace:^", | ||
"@aztec/foundation": "workspace:^", | ||
"koa": "^2.14.2", | ||
"koa-cors": "^0.0.16", | ||
"koa-router": "^12.0.0", | ||
"viem": "^1.2.5" | ||
}, | ||
"devDependencies": { | ||
"@jest/globals": "^29.5.0", | ||
"@rushstack/eslint-patch": "^1.1.4", | ||
"@types/jest": "^29.5.0", | ||
"@types/node": "^18.7.23", | ||
"jest": "^29.5.0", | ||
"ts-jest": "^29.1.0", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^5.0.4" | ||
}, | ||
"files": [ | ||
"dest", | ||
"src", | ||
"!*.test.*" | ||
], | ||
"types": "./dest/index.d.ts", | ||
"engines": { | ||
"node": ">=18" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
#!/usr/bin/env -S node --no-warnings | ||
import { NULL_KEY, createEthereumChain } from '@aztec/ethereum'; | ||
import { EthAddress } from '@aztec/foundation/eth-address'; | ||
import { createDebugLogger } from '@aztec/foundation/log'; | ||
|
||
import http from 'http'; | ||
import Koa from 'koa'; | ||
import cors from 'koa-cors'; | ||
import Router from 'koa-router'; | ||
import { Hex, http as ViemHttp, createWalletClient, parseEther } from 'viem'; | ||
import { privateKeyToAccount } from 'viem/accounts'; | ||
|
||
const { | ||
FAUCET_PORT = 8082, | ||
API_PREFIX = '', | ||
API_KEY = '', | ||
RPC_URL = '', | ||
CHAIN_ID = '', | ||
PRIVATE_KEY = '', | ||
INTERVAL = '', | ||
ETH_AMOUNT = '', | ||
} = process.env; | ||
|
||
const logger = createDebugLogger('aztec:faucet'); | ||
|
||
const rpcUrl = RPC_URL; | ||
const apiKey = API_KEY; | ||
const chainId = +CHAIN_ID; | ||
const privateKey: Hex = PRIVATE_KEY ? createHex(PRIVATE_KEY) : NULL_KEY; | ||
const interval = +INTERVAL; | ||
const mapping: { [key: Hex]: Date } = {}; | ||
|
||
/** | ||
* Helper function to convert a string to a Hex value | ||
* @param hex - The string to convert | ||
* @returns The converted value | ||
*/ | ||
function createHex(hex: string) { | ||
return `0x${hex.replace('0x', '')}` as Hex; | ||
} | ||
|
||
/** | ||
* Function to throttle drips on a per address basis | ||
* @param address - Address requesting some ETH | ||
*/ | ||
function checkThrottle(address: Hex) { | ||
if (mapping[address] === undefined) { | ||
return; | ||
} | ||
const last = mapping[address]; | ||
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`); | ||
} | ||
} | ||
|
||
/** | ||
* 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, apiKey); | ||
|
||
const account = privateKeyToAccount(privateKey); | ||
const walletClient = createWalletClient({ | ||
account: account, | ||
chain: chain.chainInfo, | ||
transport: ViemHttp(chain.rpcUrl), | ||
}); | ||
const hexAddress = createHex(address); | ||
checkThrottle(hexAddress); | ||
try { | ||
const hash = await walletClient.sendTransaction({ | ||
account, | ||
to: hexAddress, | ||
value: parseEther(ETH_AMOUNT), | ||
}); | ||
mapping[hexAddress] = new Date(); | ||
logger.info(`Sent ${ETH_AMOUNT} ETH to ${hexAddress} in tx ${hash}`); | ||
} catch (error) { | ||
logger.error(`Failed to send eth to ${hexAddress}`); | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* Creates a router for the faucet. | ||
* @param apiPrefix - The prefix to use for all api requests | ||
* @returns - The router for handling status requests. | ||
*/ | ||
function createRouter(apiPrefix: string) { | ||
logger.info(`Creating router with prefix ${apiPrefix}`); | ||
const router = new Router({ prefix: `${apiPrefix}` }); | ||
router.get('/status', (ctx: Koa.Context) => { | ||
ctx.status = 200; | ||
}); | ||
router.get('/drip/:address', async (ctx: Koa.Context) => { | ||
const { address } = ctx.params; | ||
await transferEth(EthAddress.fromString(address).toChecksumString()); | ||
ctx.status = 200; | ||
}); | ||
return router; | ||
} | ||
|
||
/** | ||
* Create and start a new Aztec Node HTTP Server | ||
*/ | ||
async function main() { | ||
logger.info(`Setting up Aztec Faucet...`); | ||
|
||
const chain = createEthereumChain(rpcUrl, apiKey); | ||
if (chain.chainInfo.id !== chainId) { | ||
throw new Error(`Incorrect chain id, expected ${chain.chainInfo.id}`); | ||
} | ||
|
||
const shutdown = () => { | ||
logger.info('Shutting down...'); | ||
process.exit(0); | ||
}; | ||
|
||
process.once('SIGINT', shutdown); | ||
process.once('SIGTERM', shutdown); | ||
|
||
const app = new Koa(); | ||
app.on('error', error => { | ||
logger.error(`Error on API handler: ${error}`); | ||
}); | ||
const exceptionHandler = async (ctx: Koa.Context, next: () => Promise<void>) => { | ||
try { | ||
await next(); | ||
} catch (err: any) { | ||
logger.error(err); | ||
ctx.status = 400; | ||
ctx.body = { error: err.message }; | ||
} | ||
}; | ||
app.use(exceptionHandler); | ||
app.use(cors()); | ||
const apiRouter = createRouter(API_PREFIX); | ||
app.use(apiRouter.routes()); | ||
app.use(apiRouter.allowedMethods()); | ||
|
||
const httpServer = http.createServer(app.callback()); | ||
httpServer.listen(+FAUCET_PORT); | ||
logger.info(`Aztec Faucet listening on port ${FAUCET_PORT}`); | ||
await Promise.resolve(); | ||
} | ||
|
||
main().catch(err => { | ||
logger.error(err); | ||
process.exit(1); | ||
}); |
Oops, something went wrong.