Skip to content

Commit

Permalink
Support payout in Gwei
Browse files Browse the repository at this point in the history
Pass background and logo through command line
  • Loading branch information
iszubok committed Dec 5, 2024
1 parent d152b8d commit e9b2ecc
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 31 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The faucet is a web application with the goal of distributing small amounts of E
### Installation

1. Clone the repository and navigate to the app’s directory

```bash
git clone https://github.com/chainflag/eth-faucet.git
cd eth-faucet
Expand All @@ -35,7 +36,8 @@ cd eth-faucet
go generate
```

3. Build Go project
3. Build Go project

```bash
go build -o eth-faucet
```
Expand Down Expand Up @@ -71,6 +73,7 @@ echo "your_keystore_password" > `pwd`/password.txt
```

Then run the faucet application without the wallet command-line flags:

```bash
./eth-faucet -httpport 8080
```
Expand All @@ -83,12 +86,14 @@ The following are the available command-line flags(excluding above wallet flags)
|-------------------|--------------------------------------------------|---------------|
| -httpport | Listener port to serve HTTP connection | 8080 |
| -proxycount | Count of reverse proxies in front of the server | 0 |
| -faucet.amount | Number of Ethers to transfer per user request | 1.0 |
| -faucet.amount | Number of Gwei to transfer per user request | 1000000000 |
| -faucet.minutes | Number of minutes to wait between funding rounds | 1440 |
| -faucet.name | Network name to display on the frontend | testnet |
| -faucet.symbol | Token symbol to display on the frontend | ETH |
| -hcaptcha.sitekey | hCaptcha sitekey | |
| -hcaptcha.secret | hCaptcha secret | |
| -frontend.logo | Logo URL to display on the frontend | /gatewayfm-logo.svg |
| -frontend.background | Background to display on the frontend | /background.jpg |

### Docker deployment

Expand Down
17 changes: 7 additions & 10 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"math/big"
"os"
"os/signal"
"strings"

"github.com/ethereum/go-ethereum/crypto"
Expand All @@ -24,10 +23,12 @@ var (
proxyCntFlag = flag.Int("proxycount", 0, "Count of reverse proxies in front of the server")
versionFlag = flag.Bool("version", false, "Print version number")

payoutFlag = flag.Float64("faucet.amount", 1, "Number of Ethers to transfer per user request")
intervalFlag = flag.Int("faucet.minutes", 1440, "Number of minutes to wait between funding rounds")
netnameFlag = flag.String("faucet.name", "testnet", "Network name to display on the frontend")
symbolFlag = flag.String("faucet.symbol", "ETH", "Token symbol to display on the frontend")
payoutFlag = flag.Int64("faucet.amount", 1000000000, "Number of Gwei to transfer per user request")
intervalFlag = flag.Int("faucet.minutes", 1440, "Number of minutes to wait between funding rounds")
netnameFlag = flag.String("faucet.name", "testnet", "Network name to display on the frontend")
symbolFlag = flag.String("faucet.symbol", "ETH", "Token symbol to display on the frontend")
logoFlag = flag.String("frontend.logo", "/gatewayfm-logo.svg", "Logo to display on the frontend")
backgroundFlag = flag.String("frontend.background", "/background.jpg", "Background to display on the frontend")

keyJSONFlag = flag.String("wallet.keyjson", os.Getenv("KEYSTORE"), "Keystore file to fund user requests with")
keyPassFlag = flag.String("wallet.keypass", "password.txt", "Passphrase text file to decrypt keystore")
Expand Down Expand Up @@ -60,14 +61,10 @@ func Execute() {
if err != nil {
panic(fmt.Errorf("cannot connect to web3 provider: %w", err))
}
config := server.NewConfig(*netnameFlag, *symbolFlag, *httpPortFlag, *intervalFlag, *proxyCntFlag, *payoutFlag, *hcaptchaSiteKeyFlag, *hcaptchaSecretFlag)
config := server.NewConfig(*netnameFlag, *symbolFlag, *httpPortFlag, *intervalFlag, *payoutFlag, *proxyCntFlag, *hcaptchaSiteKeyFlag, *hcaptchaSecretFlag, *logoFlag, *backgroundFlag)
go server.NewServer(txBuilder, config).Run()

c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
}

func getPrivateKeyFromFlags() (*ecdsa.PrivateKey, error) {
if *privKeyFlag != "" {
hexkey := *privKeyFlag
Expand Down
5 changes: 5 additions & 0 deletions internal/chain/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ func EtherToWei(amount float64) *big.Int {
return wei
}

func GweiToWei(amount int64) *big.Int {
multiplier := new(big.Int).Exp(big.NewInt(10), big.NewInt(9), nil)
return new(big.Int).Mul(big.NewInt(amount), multiplier)
}

func Has0xPrefix(str string) bool {
return len(str) >= 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X')
}
Expand Down
8 changes: 6 additions & 2 deletions internal/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ type Config struct {
symbol string
httpPort int
interval int
payout float64
payout int64
proxyCount int
hcaptchaSiteKey string
hcaptchaSecret string
logoUrl string
backgroundUrl string
}

func NewConfig(network, symbol string, httpPort, interval, proxyCount int, payout float64, hcaptchaSiteKey, hcaptchaSecret string) *Config {
func NewConfig(network, symbol string, httpPort, interval, proxyCount int, payout int64, hcaptchaSiteKey, hcaptchaSecret, logoUrl, backgroundUrl string) *Config {
return &Config{
network: network,
symbol: symbol,
Expand All @@ -21,5 +23,7 @@ func NewConfig(network, symbol string, httpPort, interval, proxyCount int, payou
proxyCount: proxyCount,
hcaptchaSiteKey: hcaptchaSiteKey,
hcaptchaSecret: hcaptchaSecret,
logoUrl: logoUrl,
backgroundUrl: backgroundUrl,
}
}
2 changes: 2 additions & 0 deletions internal/server/dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type infoResponse struct {
Payout string `json:"payout"`
Symbol string `json:"symbol"`
HcaptchaSiteKey string `json:"hcaptcha_sitekey,omitempty"`
LogoUrl string `json:"logo_url"`
BackgroundUrl string `json:"background_url"`
}

type malformedRequest struct {
Expand Down
6 changes: 4 additions & 2 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (s *Server) handleClaim() http.HandlerFunc {
address, _ := readAddress(r)
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
txHash, err := s.Transfer(ctx, address, chain.EtherToWei(s.cfg.payout))
txHash, err := s.Transfer(ctx, address, chain.GweiToWei(s.cfg.payout))
if err != nil {
log.WithError(err).Error("Failed to send transaction")
renderJSON(w, claimResponse{Message: err.Error()}, http.StatusInternalServerError)
Expand All @@ -81,8 +81,10 @@ func (s *Server) handleInfo() http.HandlerFunc {
Account: s.Sender().String(),
Network: s.cfg.network,
Symbol: s.cfg.symbol,
Payout: strconv.FormatFloat(s.cfg.payout, 'f', -1, 64),
Payout: strconv.FormatInt(s.cfg.payout, 10),
HcaptchaSiteKey: s.cfg.hcaptchaSiteKey,
LogoUrl: s.cfg.logoUrl,
BackgroundUrl: s.cfg.backgroundUrl,
}, http.StatusOK)
}
}
6 changes: 1 addition & 5 deletions web/.env.example
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# BRANDING
# BRANDING

VITE_FAVICON_PATH=/favicon.png
VITE_LOGO_PATH=/gpt-logo-white-transparent.svg
VITE_BACKGROUND_PATH=/gpt-background.jpg

# OTHER

VITE_SYMBOL=ETH
3 changes: 3 additions & 0 deletions web/public/gatewayfm-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 36 additions & 10 deletions web/src/Faucet.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,48 @@
import { CloudflareProvider } from '@ethersproject/providers';
import { setDefaults as setToast, toast } from 'bulma-toast';
const symbol = import.meta.env.VITE_SYMBOL
let input = null;
let faucetInfo = {
account: '0x0000000000000000000000000000000000000000',
network: 'testnet',
payout: 1,
symbol: symbol,
payout: 1000000000,
symbol: 'ETH',
hcaptcha_sitekey: '',
logo_url: '/gatewayfm-logo.svg',
background_url: 'background.jpg'
};
let mounted = false;
let hcaptchaLoaded = false;
const logoPath = import.meta.env.VITE_LOGO_PATH
const backgroundPath = import.meta.env.VITE_BACKGROUND_PATH
function gweiToEth(gwei) {
let str = gwei.toString();
let len = str.length;
// Add leading zeros if necessary
if (len <= 9) {
str = '0'.repeat(9 - len) + str;
len = str.length;
}
// Insert decimal point
str = str.slice(0, len - 9) + '.' + str.slice(len - 9);
// Add leading zero if necessary
if (str.startsWith('.')) {
str = '0' + str;
}
// Remove trailing zeros
str = str.replace(/0+$/, '');
// Remove trailing decimal point
if (str.endsWith('.')) {
str = str.slice(0, -1);
}
return str;
}
onMount(async () => {
const res = await fetch('/api/info');
Expand Down Expand Up @@ -123,14 +149,14 @@
</svelte:head>

<main>
<section class="hero is-info is-fullheight" style='background-image: url({backgroundPath})'>
<section class="hero is-info is-fullheight" style='background-image: url({faucetInfo.background_url})'>
<div class="hero-head">
<nav class="navbar">
<div class="container">
<div class="navbar-brand">
<a class="navbar-item" href="../..">
<span class="icon icon-brand">
<img src={logoPath} alt="logo"/>
<img src={faucetInfo.logo_url} alt="logo"/>
</span>
<span><b>{faucetInfo.symbol} Faucet</b></span>
</a>
Expand Down Expand Up @@ -158,7 +184,7 @@
<div class="container has-text-centered">
<div class="column is-6 is-offset-3">
<h1 class="title">
Receive {faucetInfo.payout}
Receive {gweiToEth(faucetInfo.payout)}
{faucetInfo.symbol} per request
</h1>
<h2 class="subtitle">
Expand Down Expand Up @@ -194,7 +220,7 @@
<style>
.icon-brand {
width: 5rem;
width: 5rem;
margin: 1rem;
}
Expand Down

0 comments on commit e9b2ecc

Please sign in to comment.