Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into siliev/fund-faucet
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanIliev545 committed Sep 29, 2023
2 parents 2a88acd + 341cc51 commit f219d66
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 96 deletions.
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
30 changes: 23 additions & 7 deletions tools/faucet/cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"flag"
"math/big"

"github.com/ethereum/go-ethereum/params"

"github.com/obscuronet/go-obscuro/tools/faucet/faucet"
)

Expand Down Expand Up @@ -32,6 +34,10 @@ const (
serverPortName = "serverPort"
serverPortDefault = 80
serverPortUsage = "Port where the web server binds to"

defaultAmountName = "defaultAmount"
defaultAmountDefault = 100.0
defaultAmountUsage = "Default amount of token to fund (in ETH)"
)

func parseCLIArgs() *faucet.Config {
Expand All @@ -41,15 +47,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)
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(params.Ether))
// don't care about the accuracy here, float should have less than 18 decimal places
wei, _ := weiFloat.Int(nil)
return wei
}
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
21 changes: 11 additions & 10 deletions tools/faucet/faucet/faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ 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 is left in temporarily for tooling that is getting native funds using `/obx` URL
DeprecatedNativeToken = "obx" // todo (@matt) remove this once we have fixed the /obx usages
WrappedOBX = "wobx"
WrappedEth = "weth"
WrappedUSDC = "usdc"
)

type Faucet struct {
Expand All @@ -50,11 +51,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 +106,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 +126,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

0 comments on commit f219d66

Please sign in to comment.