Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Proposed changes for multi-testnet faucet option #32

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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: 10 additions & 0 deletions src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ console.log('Acting as faucet for address:', config.address)
//

// ProviderEngine based caching layer, with fallback to geth
//
// need to be able to dynamically change rpcOrigin based on network requesting funds
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the provider stack is quite, old it should be replace by https://github.com/MetaMask/eth-json-rpc-middleware and the json-rpc-engine stack

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the provider stack is quite, old it should be replace by https://github.com/MetaMask/eth-json-rpc-middleware and the json-rpc-engine stack

@kumavis Will that change the syntax here much?

const engine = rpcWrapperEngine({
rpcUrl: config.rpcOrigin,
addressHex: config.address,
Expand Down Expand Up @@ -85,6 +87,11 @@ function startServer () {
async function handleRequest (req, res) {
try {
// parse address
// can parse for network here.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

server must have an explicit allowlist (not a denylist) for network ids it permits to send from. that faucet occasionally accidentally holds real world assets on mainnet and bsc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kumavis Got it, what does that mean for the parsing here?

// can either be json object or
// inserted in a combined rawdata string with address, and then parsed out here
// depending on what the network is, make sure ethQuery engine knows when
// testing balance and sending transaction
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

go with json

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good!

let targetAddress = req.body
if (!targetAddress || typeof targetAddress !== 'string') {
return didError(res, new Error(`Address parse failure - request body empty`))
Expand All @@ -110,13 +117,16 @@ function startServer () {
const alignedIpAddress = ipAddress.padStart(15, ' ')
const requestorMessage = `${flag} ${alignedIpAddress} requesting for ${targetAddress}`
// check for greediness
//
// need to change network here:
const balance = await ethQuery.getBalance(targetAddress, 'pending')
const balanceTooFull = balance.gt(MAX_BALANCE)
if (balanceTooFull) {
console.log(`${requestorMessage} - already has too much ether`)
return didError(res, new Error('User is greedy - already has too much ether'))
}
// send value
// need ot change network here as well:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we're currently doing eip155 sigs here, we should fix that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also going to need infura api keys for each network

@kumavis I don't think we will if we're using Infura the way it normally is. As you can see here, you can change the Ethereum network from an Infura API simply by changing the subdomain:

Screen Shot 2021-06-07 at 3 39 11 PM

Screen Shot 2021-06-07 at 3 39 03 PM

The one issue I'm having is how to dynamically change rpcUrl of the engine based on user's request. The relevant code is here:

https://github.com/ConsenSys-Academy/eth-faucet/blob/07dc818c341b2103175420295c068f25278a0e58/src/server/index.js#L40

Do you have any ideas of how to do this?

const txHash = await ethQuery.sendTransaction({
to: targetAddress,
from: config.address,
Expand Down
31 changes: 27 additions & 4 deletions src/webapp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ function getNetwork () {
if (res.error) return console.res.error(res.error)
var network = res.result
state.network = network

// set networkName for use in etherscan URL
switch(network) {
case '3':
state.networkName = 'ropsten';
break
case '42':
state.networkName = 'kovan';
break
case '4':
state.networkName = 'rinkeby';
break
case '5':
state.networkName = 'goerli';
break
// not sure about the best way to handle this, but
// need to have something if they're on non-public testnet
default:
state.networkName = 'unknown';
break
}
renderApp()
})
}
Expand Down Expand Up @@ -209,7 +230,7 @@ function renderApp () {
}
}, (
state.transactions.map((txHash) => {
return link(`https://ropsten.etherscan.io/tx/${txHash}`, txHash)
return link(`https://${state.networkName}.etherscan.io/tx/${txHash}`, txHash)
})
))
])
Expand All @@ -232,16 +253,18 @@ async function getEther () {
if (!account) return

var uri = `${window.location.href}v0/request`
var data = account
var data = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may have to json serialize this first. verify that the fetch api supports providing an object

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kumavis just a quick search makes it seem like I may have to use JSON.stringify(), does that sound right?

'account' : account,
'network' : state.network,
}

let res, body, err

try {
res = await fetch(uri, {
method: 'POST',
body: data,
headers: {
'Content-Type': 'application/rawdata'
'Content-Type': 'application/json'
}
})
body = await res.text()
Expand Down