-
Notifications
You must be signed in to change notification settings - Fork 307
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: Faucet #2856
Merged
Merged
feat: Faucet #2856
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1ccd811
Initial Faucet
PhilWindle a0bafd2
Lock file
PhilWindle bb0d7c1
Allow min and max peer and transaction config
PhilWindle 3b4fb50
Fix
PhilWindle 477718e
Updated build manifest
PhilWindle 6d1340d
Image name fix
PhilWindle 056b7a9
Reverted unrelated change
PhilWindle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpicky but I think we should name this just
faucet
rather thanaztec-faucet