Skip to content

Commit

Permalink
Merge pull request #565 from rsksmart/Stable-Test
Browse files Browse the repository at this point in the history
Flyover 2.0.2 - Stable-Test -> master
  • Loading branch information
Luisfc68 authored Nov 14, 2024
2 parents 0448dc3 + 76f79d9 commit e8b33ef
Show file tree
Hide file tree
Showing 58 changed files with 1,343 additions and 341 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ on:
push:
branches: [ QA-Test, Stable-Test, master ]
pull_request:
branches: [ QA-Test, Stable-Test, master ]

permissions:
contents: read
pull-requests: read
checks: write

jobs:
unit-tests:
name: Unit tests execution
runs-on: ubuntu-latest
permissions:
checks: write
contents: read
pull-requests: read

steps:
- name: Checkout
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
Expand All @@ -36,6 +39,11 @@ jobs:
code-lint:
name: Source code format validation
runs-on: ubuntu-latest
permissions:
checks: write
contents: read
pull-requests: read

steps:
- name: Checkout
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ on:
schedule:
- cron: "0 0 * * 1"

permissions:
contents: read

jobs:
analyze:
name: Analyze
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/dependency-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ jobs:
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
- name: 'Dependency Review'
uses: actions/dependency-review-action@72eb03d02c7872a771aacd928f3123ac62ad6d3a # v4.3.3
with:
comment-summary-in-pr: true
3 changes: 3 additions & 0 deletions .github/workflows/deploy-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
push:
branches:
- QA-Test

permissions:
contents: read

jobs:
execute-command:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ integration/geth_keystore
/docker-compose/lbc-deployer/liquidity-bridge-contract/
keystore/
build/
/utils/*
/**/.env.regtest
/**/gh_token.txt
!sample-config.env
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,7 @@ test: clean

clean:
rm -rf build $(TEMPORAL_COVER_FILE)

utils:
mkdir -p utils && cd utils
CGO_ENABLED=0 go build -v -o ./utils/update_provider_url ./cmd/utils/update_provider_url.go
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ mentioned before are the minimal dependencies, but in order to run a fully funct
- **PegIn**: process of converting BTC into RBTC. [Here](docs/diagrams/PegIn.mmd) is a diagram with a detailed view of the process.
- **PegOut**: process of converting RBTC into BTC. [Here](docs/diagrams/PegOut.mmd) is a diagram with a detailed view of the process.

## LPS Utilities
The [cmd/utils](cmd/utils) directory contains scripts with different utilities for the liquidity providers. You can either run them directly
with `go run` or build them with `make utils`. You can run the scripts with the `--help` flag to see the available options. The current utilities are:
- **update_provider_url**: updates the URL of a liquidity provider provided when the discovery function of the Liquidity Bridge Contract is executed.

### More information
If you're looking forward to integrate with Flyover Protocol then you can check the [Flyover SDK repository](https://github.com/rsksmart/unified-bridges-sdk/tree/main/packages/flyover-sdk).

Expand Down
1 change: 0 additions & 1 deletion cmd/application/lps/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ func (app *Application) prepareWatchers() ([]watcher.Watcher, error) {
watchers := []watcher.Watcher{
app.watcherRegistry.PeginDepositAddressWatcher,
app.watcherRegistry.PeginBridgeWatcher,
app.watcherRegistry.QuoteCleanerWatcher,
app.watcherRegistry.PegoutRskDepositWatcher,
app.watcherRegistry.PegoutBtcTransferWatcher,
app.watcherRegistry.LiquidityCheckWatcher,
Expand Down
34 changes: 34 additions & 0 deletions cmd/utils/defaults/defaults.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package defaults

import (
"errors"
"github.com/rsksmart/liquidity-provider-server/internal/configuration/environment"
)

func GetRsk(network string) (environment.RskEnv, error) {
switch network {
case "regtest":
return environment.RskEnv{
ChainId: 33,
LbcAddress: "0x8901a2bbf639bfd21a97004ba4d7ae2bd00b8da8",
BridgeAddress: "0x0000000000000000000000000000000001000006",
AccountNumber: 0,
}, nil
case "testnet":
return environment.RskEnv{
ChainId: 31,
LbcAddress: "0xc2a630c053d12d63d32b025082f6ba268db18300",
BridgeAddress: "0x0000000000000000000000000000000001000006",
AccountNumber: 0,
}, nil
case "mainnet":
return environment.RskEnv{
ChainId: 30,
LbcAddress: "0xaa9caf1e3967600578727f975f283446a3da6612",
BridgeAddress: "0x0000000000000000000000000000000001000006",
AccountNumber: 0,
}, nil
default:
return environment.RskEnv{}, errors.New("invalid network")
}
}
224 changes: 224 additions & 0 deletions cmd/utils/update_provider_url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
package main

import (
"context"
"errors"
"flag"
"fmt"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/go-playground/validator/v10"
"github.com/rsksmart/liquidity-provider-server/cmd/utils/defaults"
"github.com/rsksmart/liquidity-provider-server/internal/adapters/dataproviders/rootstock"
"github.com/rsksmart/liquidity-provider-server/internal/adapters/dataproviders/rootstock/bindings"
"github.com/rsksmart/liquidity-provider-server/internal/configuration/bootstrap"
"github.com/rsksmart/liquidity-provider-server/internal/configuration/bootstrap/wallet"
"github.com/rsksmart/liquidity-provider-server/internal/configuration/environment"
"github.com/rsksmart/liquidity-provider-server/internal/configuration/environment/secrets"
"golang.org/x/term"
"net/url"
"os"
"syscall"
)

type UpdateProviderScriptInput struct {
ProviderName string `validate:"required"`
ProviderUrl string `validate:"required,http_url"`
Network string `validate:"required,oneof=regtest testnet mainnet"`
RskEndpoint string `validate:"required,http_url"`
CustomLbcAddress string `validate:"omitempty,eth_addr"`
AwsLocalEndpoint string `validate:"http_url"`
SecretSource string `validate:"required,oneof=aws env"`
EncryptedJsonSecret string
EncryptedJsonPasswordSecret string
KeystoreFile string `validate:"omitempty,filepath"`
KeystorePassword string
}

type PasswordReader = func(int) ([]byte, error)

type UpdateProviderArgs struct {
Name string
url *url.URL
network string
}

func NewUpdateProviderArgs(name string, rawUrl string, network string) (UpdateProviderArgs, error) {
parsedUrl, err := url.Parse(rawUrl)
if err != nil {
return UpdateProviderArgs{}, err
}
return UpdateProviderArgs{Name: name, url: parsedUrl, network: network}, nil
}

func (args UpdateProviderArgs) Validate() error {
if args.Name == "" {
return errors.New("empty name")
}
if args.url.Scheme == "" || args.url.Host == "" {
return errors.New("invalid url")
}
if args.network != "regtest" && args.url.Scheme != "https" {
return errors.New("invalid url, not using https")
}
return nil
}

func (args UpdateProviderArgs) Url() string {
return fmt.Sprintf("%s://%s", args.url.Scheme, args.url.Host)
}

func main() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "This script is used to update the provider information displayed in the Liquidity Bridge Contract when the discovery function is executed.\n")
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
scriptInput := new(UpdateProviderScriptInput)
ReadUpdateProviderScriptInput(scriptInput)
env, err := ParseUpdateProviderScriptInput(scriptInput, term.ReadPassword)
if err != nil {
ExitWithError(2, "Error reading input", err)
}

ctx := context.Background()

rskClient, err := bootstrap.Rootstock(ctx, env.Rsk)
if err != nil {
ExitWithError(2, "Error connecting to RSK node", err)
}
rskWallet, err := GetWallet(ctx, env, rskClient)
if err != nil {
ExitWithError(2, "Error accessing to wallet", err)
}

updateArgs, err := NewUpdateProviderArgs(scriptInput.ProviderName, scriptInput.ProviderUrl, scriptInput.Network)
if err != nil {
ExitWithError(2, "Invalid provider information", err)
}
err = updateArgs.Validate()
if err != nil {
ExitWithError(2, "Invalid provider information", err)
}
err = ExecuteUpdateProvider(ctx, env, rskWallet, rskClient, updateArgs)
if err != nil {
ExitWithError(2, "Error on transaction execution", err)
}
}

func ReadUpdateProviderScriptInput(scriptInput *UpdateProviderScriptInput) {
flag.StringVar(&scriptInput.Network, "network", "", "The network to execute the script. Must be one of the following: regtest, testnet, mainnet")
flag.StringVar(&scriptInput.ProviderName, "provider-name", "", "The liquidity provider name to display")
flag.StringVar(&scriptInput.ProviderUrl, "provider-url", "", "The URL of the liquidity provider to be accessible by the users")

flag.StringVar(&scriptInput.AwsLocalEndpoint, "aws-endpoint", "http://localhost:4566", "AWS endpoint for localstack")
flag.StringVar(&scriptInput.SecretSource, "secret-src", "", "The source of the secrets to execute the transaction. Must be one of the following: env, aws")
flag.StringVar(&scriptInput.RskEndpoint, "rsk-endpoint", "", "The URL of the RSK RPC server. E.g. http://localhost:4444")
flag.StringVar(&scriptInput.CustomLbcAddress, "lbc-address", "", "Custom address of the liquidity bridge contract. If not provided will use the network default.")

flag.StringVar(&scriptInput.KeystoreFile, "keystore-file", "", "Path to the keystore file. Only required if the secret source is env")
flag.StringVar(&scriptInput.EncryptedJsonSecret, "keystore-secret", "", "Name of the secret storing the keystore. Only required if the secret source is aws")
flag.StringVar(&scriptInput.EncryptedJsonPasswordSecret, "password-secret", "", "Name of the secret storing the keystore password. Only required if the secret source is aws")
}

func ParseUpdateProviderScriptInput(scriptInput *UpdateProviderScriptInput, pwdReader PasswordReader) (environment.Environment, error) {
var env environment.Environment
flag.Parse()
validate := validator.New(validator.WithRequiredStructEnabled())
err := validate.Struct(scriptInput)
if err != nil {
return environment.Environment{}, fmt.Errorf("invalid input: %w", err)
}

if scriptInput.SecretSource == "env" {
var password []byte
fmt.Println("Insert keystore password:")
if password, err = pwdReader(syscall.Stdin); err != nil {
return environment.Environment{}, fmt.Errorf("error reading password: %w", err)
}
scriptInput.KeystorePassword = string(password)
}

rskEnvDefaults, err := defaults.GetRsk(scriptInput.Network)
if err != nil {
return environment.Environment{}, fmt.Errorf("invalid input: %w", err)
}

var lbcAddress string
if scriptInput.CustomLbcAddress != "" {
lbcAddress = scriptInput.CustomLbcAddress
} else {
lbcAddress = rskEnvDefaults.LbcAddress
}

env.LpsStage = scriptInput.Network
env.AwsLocalEndpoint = scriptInput.AwsLocalEndpoint
env.SecretSource = scriptInput.SecretSource
env.WalletManagement = "native"
env.Rsk = environment.RskEnv{
Endpoint: scriptInput.RskEndpoint,
ChainId: rskEnvDefaults.ChainId,
LbcAddress: lbcAddress,
BridgeAddress: rskEnvDefaults.BridgeAddress,
AccountNumber: rskEnvDefaults.AccountNumber,
EncryptedJsonSecret: scriptInput.EncryptedJsonSecret,
EncryptedJsonPasswordSecret: scriptInput.EncryptedJsonPasswordSecret,
KeystoreFile: scriptInput.KeystoreFile,
KeystorePassword: scriptInput.KeystorePassword,
}
env.Btc = environment.BtcEnv{Network: scriptInput.Network}
return env, nil
}

func GetWallet(
ctx context.Context,
env environment.Environment,
rskClient *rootstock.RskClient,
) (rootstock.RskSignerWallet, error) {
secretLoader, err := secrets.GetSecretLoader(ctx, env)
if err != nil {
return nil, err
}
walletFactory, err := wallet.NewFactory(env, wallet.FactoryCreationArgs{
Ctx: ctx, Env: env, SecretLoader: secretLoader, RskClient: rskClient,
})
if err != nil {
return nil, err
}
return walletFactory.RskWallet()
}

func ExecuteUpdateProvider(
ctx context.Context,
env environment.Environment,
rskWallet rootstock.RskSignerWallet,
rskClient *rootstock.RskClient,
args UpdateProviderArgs,
) error {
lbc, err := bindings.NewLiquidityBridgeContract(common.HexToAddress(env.Rsk.LbcAddress), rskClient.Rpc())
if err != nil {
return err
}
opts := &bind.TransactOpts{From: rskWallet.Address(), Signer: rskWallet.Sign}
tx, err := lbc.UpdateProvider(opts, args.Name, args.Url())
if err != nil {
return err
}

receipt, err := bind.WaitMined(ctx, rskClient.Rpc(), tx)
if err != nil {
return err
}

if receipt.Status == 1 {
fmt.Println("Provider information updated successfully. Transaction hash: ", receipt.TxHash.Hex())
return nil
} else {
return fmt.Errorf("transaction %s failed", receipt.TxHash.Hex())
}
}

func ExitWithError(code int, message string, err error) {
fmt.Println(fmt.Sprintf("%s: %s", message, err.Error()))
os.Exit(code)
}
Loading

0 comments on commit e8b33ef

Please sign in to comment.