Skip to content

Commit

Permalink
👌 Applied suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
nagdahimanshu committed Jun 21, 2024
1 parent 556db9b commit 780fb98
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 22 deletions.
6 changes: 6 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
WEB3_PROVIDER=
PRIVATE_KEY=
KEYSTORE=
ERC20_TOKEN_ADDRESS=
HCAPTCHA_SITEKEY=
HCAPTCHA_SECRET=
13 changes: 10 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@

include .env

APP_NAME = lsk-faucet
PKGS=$(shell go list ./... | grep -v "/vendor/")
BLUE = \033[1;34m
GREEN = \033[1;32m
COLOR_END = \033[0;39m
BINDING_OUTPUT_DIR=$(realpath ./internal/bindings)

validate-env: # Validates if the necessary environment variables are set
ifndef ERC20_CONTRACT_FILE_PATH
$(error `ERC20_CONTRACT_FILE_PATH` environment variable is undefined)
endif

build: build-frontend build-backend

build-backend: # Builds the application and create a binary at ./bin/
Expand All @@ -21,7 +28,7 @@ build-frontend: # Builds the frontned application
run: # Runs the application, use `make run FLAGS="--help"`
./bin/${APP_NAME} ${FLAGS}

generate-abi:
generate-abi: validate-env
@echo "$(BLUE)Generating abi file... $(COLOR_END)"
solc --overwrite --abi $(ERC20_CONTRACT_FILE_PATH) -o $(BINDING_OUTPUT_DIR)
@echo "$(GREEN)ABI file generated successfully$(COLOR_END)"
Expand Down Expand Up @@ -49,9 +56,9 @@ build-image: # Builds docker image
docker-start: # Runs docker image
@echo "$(BLUE)Starting docker container $(APP_NAME)...$(COLOR_END)"
ifdef PRIVATE_KEY
docker run --name $(APP_NAME) -p 8080:8080 -d -e WEB3_PROVIDER=$(WEB3_PROVIDER) -e PRIVATE_KEY=$(PRIVATE_KEY) $(APP_NAME)
docker run --name $(APP_NAME) -p 8080:8080 -d --env-file .env $(APP_NAME)
else ifdef KEYSTORE
docker run --name $(APP_NAME) -p 8080:8080 -d -e WEB3_PROVIDER=$(WEB3_PROVIDER) -e KEYSTORE=$(KEYSTORE) -v $(KEYSTORE)/keystore:/app/keystore -v $(KEYSTORE)/password.txt:/app/password.txt $(APP_NAME)
docker run --name $(APP_NAME) -p 8080:8080 -d --env-file .env -v $(KEYSTORE)/keystore:/app/keystore -v $(KEYSTORE)/password.txt:/app/password.txt $(APP_NAME)
endif

docker-stop:
Expand Down
17 changes: 4 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ LSK faucet is a web application that can be configured and deployed to get ETH a

* Go (1.22 or later)
* Node.js
* Solidity compiler (Solc)
* Solidity compiler (solc)
* Abigen

### Installation
Expand Down Expand Up @@ -113,25 +113,16 @@ The following are the available command-line flags(excluding above wallet flags)
#### Build docker image
Run the following command to build docker image:
```bash
make build
make build-image
```

#### Run faucet
Run the following command to start the application:
Set the appropriate configuration in `.env` file and run the following command to start the application:

```bash
make docker-start WEB3_PROVIDER=<rpc-endpoint> PRIVATE_KEY=<hex-private-key>
make docker-start

```
**NOTE**: Please replace `<rpc-endpoint>` and `<hex-private-key>` with appropriate values.

or

```bash
make docker-start WEB3_PROVIDER=<rpc-endpoint> KEYSTORE=<keystore-path>
```

**NOTE**: Please replace `<rpc-endpoint>` and `<keystore-path>` with appropriate values.

## License

Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/jellydator/ttlcache/v2 v2.11.1
github.com/kataras/hcaptcha v0.0.2
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
github.com/urfave/negroni v1.0.0
golang.org/x/crypto v0.22.0
)
Expand Down Expand Up @@ -68,6 +69,7 @@ require (
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.12.0 // indirect
github.com/prometheus/client_model v0.2.1-0.20210607210712-147c58e9608a // indirect
github.com/prometheus/common v0.32.1 // indirect
Expand All @@ -92,5 +94,6 @@ require (
golang.org/x/time v0.5.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
9 changes: 7 additions & 2 deletions internal/chain/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ import (
"golang.org/x/crypto/sha3"
)

var (
gasLimitInitializedAccount uint64 = 35000
gasLimitNonInitializedAccount uint64 = 55000
)

type TxBuilder interface {
Sender() common.Address
GetContractInstance() *bindings.Token
Expand Down Expand Up @@ -145,9 +150,9 @@ func (b *TxBuild) TransferERC20(ctx context.Context, to string, value *big.Int,

var gasLimit uint64
if balance.BitLen() == 0 {
gasLimit = 55000
gasLimit = gasLimitNonInitializedAccount
} else {
gasLimit = 35000
gasLimit = gasLimitInitializedAccount
}

tx := types.NewTransaction(nonce, tokenAddress, big.NewInt(0), gasLimit, gasPrice, data)
Expand Down
40 changes: 37 additions & 3 deletions internal/chain/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient/simulated"
"github.com/stretchr/testify/assert"
)

func TestTxBuilder_TransferETH(t *testing.T) {
Expand Down Expand Up @@ -89,7 +90,9 @@ func TestTxBuilder_TransferERC20(t *testing.T) {
bgCtx := context.Background()
toAddress := common.HexToAddress("0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B")
value := big.NewInt(1000)
txHash, err := txBuilder.TransferERC20(bgCtx, toAddress.Hex(), value, big.NewInt(30000))

// Transaction when recipient is initialized
txHashInitAcc, err := txBuilder.TransferERC20(bgCtx, toAddress.Hex(), value, big.NewInt(30000))
if err != nil {
t.Errorf("could not add tx to pending block: %v", err)
}
Expand All @@ -99,9 +102,40 @@ func TestTxBuilder_TransferERC20(t *testing.T) {
if err != nil {
t.Errorf("could not get block at height 1: %v", err)
}
if txHash != block.Transactions()[0].Hash() {
t.Errorf("did not commit sent transaction. expected hash %v got hash %v", block.Transactions()[0].Hash(), txHash)
if txHashInitAcc != block.Transactions()[0].Hash() {
t.Errorf("did not commit sent transaction. expected hash %v got hash %v", block.Transactions()[0].Hash(), txHashInitAcc)
}

txInitAcc, _, err := simBackend.Client().TransactionByHash(bgCtx, txHashInitAcc)
if err != nil {
t.Errorf("could not get transaction by hash: %v", err)
}

gasLimit := txInitAcc.Gas()
assert.Equal(t, gasLimit, gasLimitInitializedAccount)

// Transaction when recipient is not initialized
txHashNonInitAcc, err := txBuilder.TransferERC20(bgCtx, toAddress.Hex(), value, big.NewInt(0))
if err != nil {
t.Errorf("could not add tx to pending block: %v", err)
}
simBackend.Commit()

block, err = simBackend.Client().BlockByNumber(bgCtx, big.NewInt(2))
if err != nil {
t.Errorf("could not get block at height 1: %v", err)
}
if txHashNonInitAcc != block.Transactions()[0].Hash() {
t.Errorf("did not commit sent transaction. expected hash %v got hash %v", block.Transactions()[0].Hash(), txHashNonInitAcc)
}

txNonInitAcc, _, err := simBackend.Client().TransactionByHash(bgCtx, txHashNonInitAcc)
if err != nil {
t.Errorf("could not get transaction by hash: %v", err)
}

gasLimit = txNonInitAcc.Gas()
assert.Equal(t, gasLimit, gasLimitNonInitializedAccount)

// TODO: Verify ERC20 token balance of recipient if possible in future with the simulator
}
3 changes: 2 additions & 1 deletion internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"context"
"fmt"
"math/big"
"net/http"
"strconv"
"time"
Expand Down Expand Up @@ -60,7 +61,7 @@ func (s *Server) handleClaim() http.HandlerFunc {
currBalance, err := s.GetContractInstance().BalanceOf(&bind.CallOpts{}, common.HexToAddress(address))
if err != nil {
log.WithError(err).Error("Failed to fetch recipient balance")
return
currBalance = big.NewInt(0)
}

txHash, err := s.TransferERC20(ctx, address, chain.LSKToWei(int64(s.cfg.payout)), currBalance)
Expand Down

0 comments on commit 780fb98

Please sign in to comment.