Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup faucet for sepolia env #1563

Merged
merged 7 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .github/workflows/manual-deploy-testnet-faucet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ on:
options:
- 'dev-testnet'
- 'testnet'
- 'sepolia-testnet'

workflow_call:
inputs:
Expand All @@ -45,12 +46,21 @@ jobs:
run: |
echo "FAUCET_BUILD_TAG=testnetobscuronet.azurecr.io/obscuronet/faucet_testnet:latest" >> $GITHUB_ENV
echo "TESTNET_ADDR=erpc.testnet.obscu.ro" >> $GITHUB_ENV
echo "DEFAULT_FAUCET_AMOUNT=100" >> $GITHUB_ENV

- name: 'Sets env vars for dev-testnet'
if: ${{ inputs.testnet_type == 'dev-testnet' }}
run: |
echo "FAUCET_BUILD_TAG=testnetobscuronet.azurecr.io/obscuronet/dev_faucet_testnet:latest" >> $GITHUB_ENV
echo "TESTNET_ADDR=erpc.dev-testnet.obscu.ro" >> $GITHUB_ENV
echo "DEFAULT_FAUCET_AMOUNT=100" >> $GITHUB_ENV

- name: 'Sets env vars for sepolia-testnet'
if: ${{ inputs.testnet_type == 'sepolia-testnet' }}
run: |
echo "FAUCET_BUILD_TAG=testnetobscuronet.azurecr.io/obscuronet/sepolia_faucet_testnet:latest" >> $GITHUB_ENV
echo "TESTNET_ADDR=erpc.sepolia-testnet.obscu.ro" >> $GITHUB_ENV
echo "DEFAULT_FAUCET_AMOUNT=0.5" >> $GITHUB_ENV

- name: 'Login to Azure docker registry'
uses: azure/docker-login@v1
Expand Down Expand Up @@ -79,7 +89,7 @@ jobs:
location: 'uksouth'
restart-policy: 'Never'
environment-variables: PORT=80
command-line: ./faucet --nodeHost ${{ env.TESTNET_ADDR }} --pk ${{ secrets.FAUCET_PK }} --jwtSecret ${{ secrets.FAUCET_JWT_SECRET }}
command-line: ./faucet --nodeHost ${{ env.TESTNET_ADDR }} --pk ${{ secrets.FAUCET_PK }} --jwtSecret ${{ secrets.FAUCET_JWT_SECRET }} --defaultAmount ${{ env.DEFAULT_FAUCET_AMOUNT }}
ports: '80'
cpu: 2
memory: 2
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ it for an allocation to an externally owned addressed e.g. for the account `0x0d
the following curl command can be used;

```bash
curl --location --request POST 'http://127.0.0.1:8080/fund/obx' \
curl --location --request POST 'http://127.0.0.1:8080/fund/eth' \
--header 'Content-Type: application/json' \
--data-raw '{ "address":"0x0d2166b7b3A1522186E809e83d925d7b0B6db084" }'
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
WHOST = '127.0.0.1'
LOWER = 0
UPPER = 100
FAUCET_URL = 'http://testnet-faucet.uksouth.azurecontainer.io/fund/obx'
FAUCET_URL = 'http://testnet-faucet.uksouth.azurecontainer.io/fund/eth'

guesser = '''
// SPDX-License-Identifier: MIT
Expand Down
2 changes: 1 addition & 1 deletion docs/_docs/testnet/faucet.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ the faucet server using the below;
1. Make a note of your wallet address or copy it to your clipboard.
2. Open a command shell and issue the below command, where `<address>` should be replaced with the value stored in your clipboard (e.g. `0x75Ad715443e1E2EBdaFA33ABB3B08443966019A6`). The faucet server will credit 100,000 OBX by default.
```bash
curl --location --request POST 'http://testnet-faucet.uksouth.azurecontainer.io/fund/obx' --header 'Content-Type: application/json' --data-raw '{ "address":"<your address>" }'
curl --location --request POST 'http://testnet-faucet.uksouth.azurecontainer.io/fund/eth' --header 'Content-Type: application/json' --data-raw '{ "address":"<your address>" }'
```
3. After a short period of time the curl command will return `{"status":"ok"}` confirming OBX have been credited to your wallet.
17 changes: 9 additions & 8 deletions integration/faucet/faucet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ func TestFaucet(t *testing.T) {
time.Sleep(2 * time.Second)

faucetConfig := &faucet.Config{
Port: startPort,
Host: "localhost",
HTTPPort: startPort + integration.DefaultHostRPCHTTPOffset,
PK: "0x" + contractDeployerPrivateKeyHex,
JWTSecret: "This_is_secret",
ChainID: big.NewInt(integration.ObscuroChainID),
ServerPort: integration.StartPortFaucetHTTPUnitTest,
Port: startPort,
Host: "localhost",
HTTPPort: startPort + integration.DefaultHostRPCHTTPOffset,
PK: "0x" + contractDeployerPrivateKeyHex,
JWTSecret: "This_is_secret",
ChainID: big.NewInt(integration.ObscuroChainID),
ServerPort: integration.StartPortFaucetHTTPUnitTest,
DefaultFundAmount: new(big.Int).Mul(big.NewInt(100), big.NewInt(1e18)),
}
faucetContainer, err := container.NewFaucetContainerFromConfig(faucetConfig)
assert.NoError(t, err)
Expand Down Expand Up @@ -98,7 +99,7 @@ func createObscuroNetwork(t *testing.T, startPort int) {
}

func fundWallet(port int, w wallet.Wallet) error {
url := fmt.Sprintf("http://localhost:%d/fund/obx", port)
url := fmt.Sprintf("http://localhost:%d/fund/eth", port)
method := "POST"

payload := strings.NewReader(fmt.Sprintf(`{"address":"%s"}`, w.Address()))
Expand Down
4 changes: 2 additions & 2 deletions integration/networktest/env/network_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func Testnet() networktest.Environment {
connector := NewTestnetConnector(
"http://erpc.testnet.obscu.ro:80",
[]string{"http://erpc.testnet.obscu.ro:80"}, // for now we'll just use sequencer as validator node... todo (@matt)
"http://testnet-faucet.uksouth.azurecontainer.io/fund/obx",
"http://testnet-faucet.uksouth.azurecontainer.io/fund/eth",
"ws://testnet-eth2network.uksouth.cloudapp.azure.com:9000",
)
return &testnetEnv{connector}
Expand All @@ -19,7 +19,7 @@ func DevTestnet() networktest.Environment {
connector := NewTestnetConnector(
"http://erpc.dev-testnet.obscu.ro:80",
[]string{"http://erpc.dev-testnet.obscu.ro:80"}, // for now we'll just use sequencer as validator node... todo (@matt)
"http://dev-testnet-faucet.uksouth.azurecontainer.io/fund/obx",
"http://dev-testnet-faucet.uksouth.azurecontainer.io/fund/eth",
"ws://dev-testnet-eth2network.uksouth.cloudapp.azure.com:9000",
)
return &testnetEnv{connector}
Expand Down
4 changes: 2 additions & 2 deletions tools/faucet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ on port `80` within the container, but maps port `8080` on the host machine to t


## Allocating OBX to an EOA on a local testnet
Allocating OBX to an externally owned account is done through a POST command to the `/fund/obx` endpoint, where the
Allocating OBX to an externally owned account is done through a POST command to the `/fund/eth` endpoint, where the
data in the POST command specifies the address e.g. for the account `0x0d2166b7b3A1522186E809e83d925d7b0B6db084`

```bash
curl --location --request POST 'http://127.0.0.1:8080/fund/obx' \
curl --location --request POST 'http://127.0.0.1:8080/fund/eth' \
--header 'Content-Type: application/json' \
--data-raw '{ "address":"0x0d2166b7b3A1522186E809e83d925d7b0B6db084" }'
```
Expand Down
28 changes: 21 additions & 7 deletions tools/faucet/cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ const (
serverPortName = "serverPort"
serverPortDefault = 80
serverPortUsage = "Port where the web server binds to"

defaultAmountName = "defaultAmount"
defaultAmountDefault = 100.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

float ?

defaultAmountUsage = "Default amount of token to fund (in ETH)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a pet annoyance, I'll leave it to you consideration. The flags receive ETH but the config struct receives WEI. Worth normalizing one way or the other imo.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like we should mostly use explicit big.Int for token values everywhere in code, seems to be mostly what geth does.

But the CLI is a human configured interface, imo it makes sense for it to use human readable values but not to leak that into other layers. If I'm checking the config for each env I don't want to have to count the zeroes ideally.

)

func parseCLIArgs() *faucet.Config {
Expand All @@ -41,15 +45,25 @@ func parseCLIArgs() *faucet.Config {
faucetPK := flag.String(faucetPKName, faucetPKDefault, faucetPKUsage)
jwtSecret := flag.String(jwtSecretName, jwtSecretDefault, jwtSecretUsage)
serverPort := flag.Int(serverPortName, serverPortDefault, serverPortUsage)
defaultAmount := flag.Float64(defaultAmountName, defaultAmountDefault, defaultAmountUsage)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any need for it to be a float ?

flag.Parse()

return &faucet.Config{
Port: *faucetPort,
Host: *nodeHost,
HTTPPort: *nodeHTTPPort,
PK: *faucetPK,
JWTSecret: *jwtSecret,
ServerPort: *serverPort,
ChainID: big.NewInt(443), // TODO make this configurable
Port: *faucetPort,
Host: *nodeHost,
HTTPPort: *nodeHTTPPort,
PK: *faucetPK,
JWTSecret: *jwtSecret,
ServerPort: *serverPort,
ChainID: big.NewInt(443), // TODO make this configurable
DefaultFundAmount: toWei(defaultAmount),
}
}

func toWei(amount *float64) *big.Int {
amtFloat := new(big.Float).SetFloat64(*amount)
weiFloat := new(big.Float).Mul(amtFloat, big.NewFloat(1e18))
// don't care about the accuracy here, float should have less than 18 decimal places
wei, _ := weiFloat.Int(nil)
return wei
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps gethcommon.ParseEther(ethValue) is a better alternative.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does sound better but I can't find it. I've changed 1e18 magic number to use geth's params.Ether but seems like in geth they mostly just do the multiplication.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could swear there was a ParseEther(string), but had a double look and nothing. params.Ether sounds good.

2 changes: 1 addition & 1 deletion tools/faucet/container/faucet_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewFaucetContainerFromConfig(cfg *faucet.Config) (*FaucetContainer, error)
return nil, err
}
bindAddress := fmt.Sprintf(":%d", cfg.ServerPort)
server := webserver.NewWebServer(f, bindAddress, []byte(cfg.JWTSecret))
server := webserver.NewWebServer(f, bindAddress, []byte(cfg.JWTSecret), cfg.DefaultFundAmount)

return NewFaucetContainer(f, server)
}
Expand Down
20 changes: 10 additions & 10 deletions tools/faucet/faucet/faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
"github.com/obscuronet/go-obscuro/go/obsclient"
"github.com/obscuronet/go-obscuro/go/rpc"
"github.com/obscuronet/go-obscuro/go/wallet"
)

const (
_timeout = 60 * time.Second
OBXNativeToken = "obx"
WrappedOBX = "wobx"
WrappedEth = "weth"
WrappedUSDC = "usdc"
_timeout = 60 * time.Second
NativeToken = "eth"
DeprecatedNativeToken = "obx" // leaving this in temporarily for tooling that is getting native funds using `/obx` URL
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to rip the band aid straight off imo

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed with Moray, we want to make it less painful to transition. No need to break the discord faucet and e2e tests unnecessarily even if it's an easy fix. I'll add a todo to remove it.

WrappedOBX = "wobx"
WrappedEth = "weth"
WrappedUSDC = "usdc"
)

type Faucet struct {
Expand All @@ -50,11 +50,11 @@ func NewFaucet(rpcURL string, chainID int64, pkString string) (*Faucet, error) {
}, nil
}

func (f *Faucet) Fund(address *common.Address, token string, amount int64) error {
func (f *Faucet) Fund(address *common.Address, token string, amount *big.Int) error {
var err error
var signedTx *types.Transaction

if token == OBXNativeToken {
if token == NativeToken || token == DeprecatedNativeToken {
signedTx, err = f.fundNativeToken(address, amount)
} else {
return fmt.Errorf("token not fundable atm")
Expand Down Expand Up @@ -105,7 +105,7 @@ func (f *Faucet) validateTx(tx *types.Transaction) error {
return fmt.Errorf("unable to fetch tx receipt after %s", _timeout)
}

func (f *Faucet) fundNativeToken(address *common.Address, amount int64) (*types.Transaction, error) {
func (f *Faucet) fundNativeToken(address *common.Address, amount *big.Int) (*types.Transaction, error) {
// only one funding at the time
f.fundMutex.Lock()
defer f.fundMutex.Unlock()
Expand All @@ -125,7 +125,7 @@ func (f *Faucet) fundNativeToken(address *common.Address, amount int64) (*types.
GasPrice: big.NewInt(225),
Gas: gas,
To: address,
Value: new(big.Int).Mul(big.NewInt(amount), big.NewInt(params.Ether)),
Value: amount,
}

signedTx, err := f.wallet.SignTransaction(tx)
Expand Down
15 changes: 8 additions & 7 deletions tools/faucet/faucet/faucet_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package faucet
import "math/big"

type Config struct {
Port int
Host string
HTTPPort int
PK string
JWTSecret string
ChainID *big.Int
ServerPort int
Port int
Host string
HTTPPort int
PK string
JWTSecret string
ChainID *big.Int
ServerPort int
DefaultFundAmount *big.Int // how much token to fund by default (in wei)
}
Loading
Loading