Skip to content

Commit

Permalink
refactor: add EtherToWei function
Browse files Browse the repository at this point in the history
  • Loading branch information
iczc committed Jan 29, 2022
1 parent c104a27 commit fa2d800
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 39 deletions.
7 changes: 7 additions & 0 deletions internal/chain/address.go → internal/chain/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package chain

import (
"math/big"

"github.com/ethereum/go-ethereum/common"
)

Expand All @@ -10,3 +12,8 @@ func IsValidAddress(address string, checksummed bool) bool {
}
return !checksummed || common.HexToAddress(address).Hex() == address
}

func EtherToWei(amount int64) *big.Int {
ether := new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)
return new(big.Int).Mul(big.NewInt(amount), ether)
}
23 changes: 22 additions & 1 deletion internal/chain/address_test.go → internal/chain/util_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package chain

import "testing"
import (
"math/big"
"reflect"
"testing"
)

func TestIsValidAddress(t *testing.T) {
type args struct {
Expand All @@ -27,3 +31,20 @@ func TestIsValidAddress(t *testing.T) {
})
}
}

func TestEtherToWei(t *testing.T) {
tests := []struct {
name string
amount int64
want *big.Int
}{
{name: "1ether", amount: 1, want: new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := EtherToWei(tt.amount); !reflect.DeepEqual(got, tt.want) {
t.Errorf("EtherToWei() = %v, want %v", got, tt.want)
}
})
}
}
14 changes: 4 additions & 10 deletions internal/server/config.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
package server

import (
"math/big"
"time"
)

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

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),
interval: interval,
payout: payout,
proxyCount: proxyCount,
queueCap: queueCap,
}
Expand Down
8 changes: 4 additions & 4 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewServer(builder chain.TxBuilder, cfg *Config) *Server {
func (s *Server) setupRouter() *http.ServeMux {
router := http.NewServeMux()
router.Handle("/", http.FileServer(web.Dist()))
limiter := NewLimiter(s.cfg.proxyCount, s.cfg.interval*time.Minute)
limiter := NewLimiter(s.cfg.proxyCount, time.Duration(s.cfg.interval)*time.Minute)
router.Handle("/api/claim", negroni.New(limiter, negroni.Wrap(s.handleClaim())))
router.Handle("/api/info", s.handleInfo())

Expand Down Expand Up @@ -66,7 +66,7 @@ func (s *Server) consumeQueue() {
defer s.mutex.Unlock()
for len(s.queue) != 0 {
address := <-s.queue
txHash, err := s.Transfer(context.Background(), address, s.cfg.payout)
txHash, err := s.Transfer(context.Background(), address, chain.EtherToWei(int64(s.cfg.payout)))
if err != nil {
log.WithError(err).Error("Failed to handle transaction in the queue")
} else {
Expand Down Expand Up @@ -104,7 +104,7 @@ func (s *Server) handleClaim() http.HandlerFunc {

ctx, cancel := context.WithTimeout(r.Context(), 5*time.Second)
defer cancel()
txHash, err := s.Transfer(ctx, address, s.cfg.payout)
txHash, err := s.Transfer(ctx, address, chain.EtherToWei(int64(s.cfg.payout)))
s.mutex.Unlock()
if err != nil {
log.WithError(err).Error("Failed to send transaction")
Expand Down Expand Up @@ -136,7 +136,7 @@ func (s *Server) handleInfo() http.HandlerFunc {
json.NewEncoder(w).Encode(info{
Account: s.Sender().String(),
ChainName: s.cfg.chainName,
Payout: s.cfg.payout.String(),
Payout: strconv.Itoa(s.cfg.payout),
})
}
}
18 changes: 0 additions & 18 deletions web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
},
"dependencies": {
"@ethersproject/address": "^5.4.0",
"@ethersproject/units": "^5.4.0",
"animate.css": "^4.1.1",
"bulma": "^0.9.2",
"bulma-toast": "^2.4.0",
Expand Down
7 changes: 2 additions & 5 deletions web/src/Faucet.svelte
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
<script>
import { onMount } from 'svelte';
import { getAddress } from '@ethersproject/address';
import { formatEther } from '@ethersproject/units';
import { setDefaults as setToast, toast } from 'bulma-toast';
let address = null;
let faucetInfo = {
account: '0x0000000000000000000000000000000000000000',
network: 'Testnet',
network: 'testnet',
payout: 1,
};
$: document.title = `ETH ${faucetInfo.network} Faucet`;
$: document.title = `ETH ${capitalize(faucetInfo.network)} Faucet`;
onMount(async () => {
const res = await fetch('/api/info');
faucetInfo = await res.json();
faucetInfo.network = capitalize(faucetInfo.network);
faucetInfo.payout = parseInt(formatEther(faucetInfo.payout));
});
setToast({
Expand Down

0 comments on commit fa2d800

Please sign in to comment.