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

Refactor refund_user_pegout script #580

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
60 changes: 8 additions & 52 deletions cmd/utils/refund_user_pegout/refund_user_pegout.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"context"
"flag"
"fmt"
"syscall"

"github.com/go-playground/validator/v10"
"github.com/rsksmart/liquidity-provider-server/cmd/utils/defaults"
"github.com/rsksmart/liquidity-provider-server/cmd/utils/scripts"
"github.com/rsksmart/liquidity-provider-server/internal/configuration/bootstrap"
"github.com/rsksmart/liquidity-provider-server/internal/configuration/environment"
Expand All @@ -16,21 +14,18 @@ import (
)

type RefundUserPegOutScriptInput struct {
QuoteHashBytes string `validate:"required,hexadecimal"`
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
scripts.BaseInput // Embedding BaseInput
QuoteHashBytes string `validate:"required,hexadecimal"`
}

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

func main() {
scripts.SetUsageMessage(
"This script is used to execute a refund for a PegOut transaction in the Liquidity Bridge Contract." +
" It is intended for use when the final user does not receive their funds." +
" To perform this refund, you must provide the hash of the Bitcoin transaction associated with the PegOut.",
AndresQuijano marked this conversation as resolved.
Show resolved Hide resolved
)
scriptInput := new(RefundUserPegOutScriptInput)
ReadRefundUserPegOutScriptInput(scriptInput)
env, err := ParseRefundUserPegOutScriptInput(flag.Parse, scriptInput, term.ReadPassword)
Expand Down Expand Up @@ -66,53 +61,14 @@ func ReadRefundUserPegOutScriptInput(scriptInput *RefundUserPegOutScriptInput) {
}

func ParseRefundUserPegOutScriptInput(parse scripts.ParseFunc, scriptInput *RefundUserPegOutScriptInput, pwdReader PasswordReader) (environment.Environment, error) {
var env environment.Environment
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
return scriptInput.BaseInput.ToEnv(pwdReader)
}

func ExecuteRefundUserPegOut(lbc blockchain.LiquidityBridgeContract, quoteHash string) (string, error) {
Expand Down
22 changes: 14 additions & 8 deletions cmd/utils/refund_user_pegout/refund_user_pegout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/term"

"github.com/rsksmart/liquidity-provider-server/cmd/utils/scripts"
)

const (
Expand Down Expand Up @@ -53,10 +55,12 @@ func TestParseRefundUserPegOutScriptInput(t *testing.T) {

t.Run("should validate required fields", func(t *testing.T) {
scriptInput := &RefundUserPegOutScriptInput{
Network: "",
BaseInput: scripts.BaseInput{
Network: "",
RskEndpoint: "",
SecretSource: "",
},
QuoteHashBytes: "",
RskEndpoint: "",
SecretSource: "",
}

_, err := ParseRefundUserPegOutScriptInput(parse, scriptInput, term.ReadPassword)
Expand All @@ -66,11 +70,13 @@ func TestParseRefundUserPegOutScriptInput(t *testing.T) {

t.Run("should parse valid input", func(t *testing.T) {
scriptInput := &RefundUserPegOutScriptInput{
Network: testNetwork,
QuoteHashBytes: testQuoteHash,
RskEndpoint: testRskEndpoint,
SecretSource: "aws",
AwsLocalEndpoint: testAwsLocalEndpoint,
BaseInput: scripts.BaseInput{
Network: testNetwork,
RskEndpoint: testRskEndpoint,
SecretSource: "aws",
AwsLocalEndpoint: testAwsLocalEndpoint,
},
QuoteHashBytes: testQuoteHash,
}

env, err := ParseRefundUserPegOutScriptInput(parse, scriptInput, func(fd int) ([]byte, error) {
Expand Down
Loading