Skip to content

Commit

Permalink
Merge pull request #4 from gateway-fm:support-gwei-payout
Browse files Browse the repository at this point in the history
Support payout in Gwei
  • Loading branch information
iszubok authored Mar 18, 2024
2 parents 8db8215 + 5ba003a commit 01bc9ed
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 26 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@ 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
```

2. Bundle Frontend web with Vite

```bash
go generate
```

3. Build Go project
3. Build Go project

```bash
go build -o eth-faucet
```
Expand All @@ -57,6 +60,7 @@ go build -o eth-faucet
### Configuration

You can configure the funder by using environment variables instead of command-line flags as follows:

```bash
export WEB3_PROVIDER=rpc endpoint
export PRIVATE_KEY=hex private key
Expand All @@ -71,6 +75,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 +88,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 |
| -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
12 changes: 7 additions & 5 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,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.Int("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,7 +62,7 @@ func Execute() {
if err != nil {
panic(fmt.Errorf("cannot connect to web3 provider: %w", err))
}
config := server.NewConfig(*netnameFlag, *symbolFlag, *httpPortFlag, *intervalFlag, *payoutFlag, *proxyCntFlag, *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)
Expand Down
5 changes: 5 additions & 0 deletions internal/chain/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ func EtherToWei(amount int64) *big.Int {
return new(big.Int).Mul(big.NewInt(amount), ether)
}

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 int
payout int64
proxyCount int
hcaptchaSiteKey string
hcaptchaSecret string
logoUrl string
backgroundUrl string
}

func NewConfig(network, symbol string, httpPort, interval, payout, proxyCount int, hcaptchaSiteKey, hcaptchaSecret string) *Config {
func NewConfig(network, symbol string, httpPort, interval int, payout int64, proxyCount int, 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, payout, proxyCount in
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(int64(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.Itoa(s.cfg.payout),
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 01bc9ed

Please sign in to comment.