Skip to content

Commit

Permalink
feat: add chain name flag used to display on the frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
iczc committed Sep 13, 2021
1 parent 4db255e commit f198d69
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 15 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ docker run -d -p 8080:8080 -e WEB3_PROVIDER="rpc endpoint" -e KEYSTORE="keystore

| flag | Description | Default Value
| ----------- | ------------------------------------------------ | -------------
| -httpport | Listener port to serve HTTP connection | 8080
| -chainname | Network name to display on the frontend | testnet
| -httpport | Listener port to serve HTTP connection | 8080
| -interval | Number of minutes to wait between funding rounds | 1440
| -payout | Number of Ethers to transfer per user request | 1
| -proxycount | Count of reverse proxies in front of the server | 0
Expand Down
22 changes: 15 additions & 7 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/ecdsa"
"flag"
"fmt"
"math/big"
"os"
"os/signal"

Expand All @@ -13,12 +14,15 @@ import (
"github.com/chainflag/eth-faucet/internal/server"
)

var chainIDMap = map[string]int{"mainnet": 1, "ropsten": 3, "rinkeby": 4, "goerli": 5, "kovan": 42}

var (
httpPortFlag = flag.Int("httpport", 8080, "Listener port to serve HTTP connection")
intervalFlag = flag.Int("interval", 1440, "Number of minutes to wait between funding rounds")
payoutFlag = flag.Int("payout", 1, "Number of Ethers to transfer per user request")
proxyCntFlag = flag.Int("proxycount", 0, "Count of reverse proxies in front of the server")
queueCapFlag = flag.Int("queuecap", 100, "Maximum transactions waiting to be sent")
chainNameFlag = flag.String("chainname", "testnet", "Network name to display on the frontend")
httpPortFlag = flag.Int("httpport", 8080, "Listener port to serve HTTP connection")
intervalFlag = flag.Int("interval", 1440, "Number of minutes to wait between funding rounds")
payoutFlag = flag.Int("payout", 1, "Number of Ethers to transfer per user request")
proxyCntFlag = flag.Int("proxycount", 0, "Count of reverse proxies in front of the server")
queueCapFlag = flag.Int("queuecap", 100, "Maximum transactions waiting to be sent")
)

func init() {
Expand All @@ -30,9 +34,13 @@ func Execute() {
if err != nil {
panic(fmt.Errorf("failed to read private key: %w", err))
}
var chainID *big.Int
if value, ok := chainIDMap[*chainNameFlag]; ok {
chainID = big.NewInt(int64(value))
}

txBuilder := chain.NewTxBuilder(os.Getenv("WEB3_PROVIDER"), privateKey, nil)
config := server.NewConfig(*httpPortFlag, *intervalFlag, *payoutFlag, *proxyCntFlag, *queueCapFlag)
txBuilder := chain.NewTxBuilder(os.Getenv("WEB3_PROVIDER"), privateKey, chainID)
config := server.NewConfig(*chainNameFlag, *httpPortFlag, *intervalFlag, *payoutFlag, *proxyCntFlag, *queueCapFlag)
go server.NewServer(txBuilder, config).Run()

c := make(chan os.Signal, 1)
Expand Down
4 changes: 3 additions & 1 deletion internal/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ import (
)

type Config struct {
chainName string
httpPort int
interval time.Duration
payout *big.Int
proxyCount int
queueCap int
}

func NewConfig(httpPort, interval, payout, proxyCount, queueCap int) *Config {
func NewConfig(chainName string, httpPort, interval, payout, proxyCount, queueCap int) *Config {
ether := new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)
return &Config{
chainName: chainName,
httpPort: httpPort,
interval: time.Duration(interval),
payout: new(big.Int).Mul(big.NewInt(int64(payout)), ether),
Expand Down
10 changes: 6 additions & 4 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,9 @@ func (s *Server) handleClaim() http.HandlerFunc {

func (s *Server) handleInfo() http.HandlerFunc {
type info struct {
Account string `json:"account"`
Payout string `json:"payout"`
Account string `json:"account"`
ChainName string `json:"network"`
Payout string `json:"payout"`
}
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
Expand All @@ -112,8 +113,9 @@ func (s *Server) handleInfo() http.HandlerFunc {

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(info{
Account: s.Sender().String(),
Payout: s.cfg.payout.String(),
Account: s.Sender().String(),
ChainName: s.cfg.chainName,
Payout: s.cfg.payout.String(),
})
}
}
2 changes: 1 addition & 1 deletion web/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width,initial-scale=1'>

<title>Svelte app</title>
<title>ETH Testnet Faucet</title>

<link rel='icon' type='image/png' href='/favicon.png'>
<link rel='stylesheet' href='/build/bundle.css'>
Expand Down
1 change: 0 additions & 1 deletion web/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
</script>

<svelte:head>
<title>ETH Testnet Faucet</title>
<link
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"
rel="stylesheet"
Expand Down
11 changes: 11 additions & 0 deletions web/src/Faucet.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
let address = null;
let faucetInfo = {
account: '0x0000000000000000000000000000000000000000',
network: 'Testnet',
payout: 1,
};
onMount(async () => {
const res = await fetch('/api/info');
faucetInfo = await res.json();
faucetInfo.network = capitalize(faucetInfo.network);
faucetInfo.payout = parseInt(formatEther(faucetInfo.payout));
});
Expand Down Expand Up @@ -42,8 +44,17 @@
let type = res.ok ? 'is-success' : 'is-warning';
toast({ message, type });
}
function capitalize(str) {
const lower = str.toLowerCase();
return str.charAt(0).toUpperCase() + lower.slice(1);
}
</script>

<svelte:head>
<title>ETH {faucetInfo.network} Faucet</title>
</svelte:head>

<main>
<section class="hero is-info is-fullheight">
<div class="hero-head">
Expand Down

0 comments on commit f198d69

Please sign in to comment.