Skip to content

Commit

Permalink
ETH transfer fixes and sweep ETH addition (#481)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimalinux authored Jun 9, 2023
1 parent c3cce99 commit 9e3ef7b
Show file tree
Hide file tree
Showing 21 changed files with 727 additions and 162 deletions.
1 change: 1 addition & 0 deletions cliutil/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func SetLogLevels(level string) {
_ = logging.SetLogLevel("extethclient", level)
_ = logging.SetLogLevel("ethereum/watcher", level)
_ = logging.SetLogLevel("ethereum/block", level)
_ = logging.SetLogLevel("ethereum/extethclient", level)
_ = logging.SetLogLevel("monero", level)
_ = logging.SetLogLevel("net", level)
_ = logging.SetLogLevel("offers", level)
Expand Down
43 changes: 37 additions & 6 deletions cliutil/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"strings"

"github.com/cockroachdb/apd/v3"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
logging "github.com/ipfs/go-log"
Expand Down Expand Up @@ -136,24 +137,54 @@ func GetVersion() string {
return version.String()
}

// ReadUnsignedDecimalFlag reads a string flag and parses it into an *apd.Decimal.
// ReadUnsignedDecimalFlag reads a string flag and parses it into an
// *apd.Decimal, verifying that the value is >= 0.
func ReadUnsignedDecimalFlag(ctx *cli.Context, flagName string) (*apd.Decimal, error) {
s := ctx.String(flagName)
if s == "" {
return nil, fmt.Errorf("flag --%s cannot be empty", flagName)
}
bf, _, err := new(apd.Decimal).SetString(s)

d, _, err := new(apd.Decimal).SetString(s)
if err != nil {
return nil, fmt.Errorf("invalid value %q for flag --%s", s, flagName)
}
if bf.IsZero() {

if d.Negative {
return nil, fmt.Errorf("value of flag --%s cannot be negative", flagName)
}

return d, nil
}

// ReadPositiveUnsignedDecimalFlag reads a string flag and parses it into an
// *apd.Decimal, verifying that the value is strictly > 0.
func ReadPositiveUnsignedDecimalFlag(ctx *cli.Context, flagName string) (*apd.Decimal, error) {
d, err := ReadUnsignedDecimalFlag(ctx, flagName)
if err != nil {
return nil, err
}

if d.IsZero() {
return nil, fmt.Errorf("value of flag --%s cannot be zero", flagName)
}
if bf.Negative {
return nil, fmt.Errorf("value of flag --%s cannot be negative", flagName)

return d, nil
}

// ReadETHAddress reads a string flag and parses to an ethereum Address type
func ReadETHAddress(ctx *cli.Context, flagName string) (ethcommon.Address, error) {
s := ctx.String(flagName)
if s == "" {
return ethcommon.Address{}, fmt.Errorf("flag --%s cannot be empty", flagName)
}

ok := ethcommon.IsHexAddress(s)
if !ok {
return ethcommon.Address{}, fmt.Errorf("invalid ETH address: %q", s)
}

return bf, nil
return ethcommon.HexToAddress(s), nil
}

// ExpandBootnodes expands the boot nodes passed on the command line that
Expand Down
108 changes: 73 additions & 35 deletions cmd/swapcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const (
flagDetached = "detached"
flagTo = "to"
flagAmount = "amount"
flagEnv = "env"
flagGasLimit = "gas-limit"
)

func cliApp() *cli.App {
Expand Down Expand Up @@ -337,11 +337,6 @@ func cliApp() *cli.App {
Usage: "Amount of XMR to send",
Required: true,
},
&cli.StringFlag{
Name: flagEnv,
Usage: "Environment to use. Options are [mainnet, stagenet, dev]. Default = mainnet.",
Value: "mainnet",
},
swapdPortFlag,
},
},
Expand All @@ -352,20 +347,15 @@ func cliApp() *cli.App {
Flags: []cli.Flag{
&cli.StringFlag{
Name: flagTo,
Usage: "Address to send XMR to",
Usage: "Address to sweep the XMR to",
Required: true,
},
&cli.StringFlag{
Name: flagEnv,
Usage: "Environment to use. Options are [mainnet, stagenet, dev]. Default = mainnet.",
Value: "mainnet",
},
swapdPortFlag,
},
},
{
Name: "transfer-eth",
Usage: "Transfer ETH from the swap wallet to another address.",
Usage: "Transfer ETH from the swap wallet to an address.",
Action: runTransferETH,
Flags: []cli.Flag{
&cli.StringFlag{
Expand All @@ -378,6 +368,23 @@ func cliApp() *cli.App {
Usage: "Amount of ETH to send",
Required: true,
},
&cli.Uint64Flag{
Name: flagGasLimit,
Usage: "Set the gas limit (required if transferring to contract, otherwise ignored)",
},
swapdPortFlag,
},
},
{
Name: "sweep-eth",
Usage: "Sweep all ETH from the swap wallet to a non-contract address.",
Action: runSweepETH,
Flags: []cli.Flag{
&cli.StringFlag{
Name: flagTo,
Usage: "Address to sweep the ETH to",
Required: true,
},
swapdPortFlag,
},
},
Expand Down Expand Up @@ -667,12 +674,12 @@ func runQueryAll(ctx *cli.Context) error {
func runMake(ctx *cli.Context) error {
c := newClient(ctx)

min, err := cliutil.ReadUnsignedDecimalFlag(ctx, flagMinAmount)
min, err := cliutil.ReadPositiveUnsignedDecimalFlag(ctx, flagMinAmount)
if err != nil {
return err
}

max, err := cliutil.ReadUnsignedDecimalFlag(ctx, flagMaxAmount)
max, err := cliutil.ReadPositiveUnsignedDecimalFlag(ctx, flagMaxAmount)
if err != nil {
return err
}
Expand All @@ -683,7 +690,7 @@ func runMake(ctx *cli.Context) error {
ethAsset = types.EthAsset(ethcommon.HexToAddress(ethAssetStr))
}

exchangeRateDec, err := cliutil.ReadUnsignedDecimalFlag(ctx, flagExchangeRate)
exchangeRateDec, err := cliutil.ReadPositiveUnsignedDecimalFlag(ctx, flagExchangeRate)
if err != nil {
return err
}
Expand Down Expand Up @@ -775,7 +782,7 @@ func runTake(ctx *cli.Context) error {
return errInvalidFlagValue(flagOfferID, err)
}

providesAmount, err := cliutil.ReadUnsignedDecimalFlag(ctx, flagProvidesAmount)
providesAmount, err := cliutil.ReadPositiveUnsignedDecimalFlag(ctx, flagProvidesAmount)
if err != nil {
return err
}
Expand Down Expand Up @@ -1159,7 +1166,9 @@ func runGetSwapSecret(ctx *cli.Context) error {
}

func runTransferXMR(ctx *cli.Context) error {
env, err := common.NewEnv(ctx.String(flagEnv))
c := newClient(ctx)

env, err := queryEnv(c)
if err != nil {
return err
}
Expand All @@ -1169,12 +1178,11 @@ func runTransferXMR(ctx *cli.Context) error {
return err
}

amount, err := cliutil.ReadUnsignedDecimalFlag(ctx, flagAmount)
amount, err := cliutil.ReadPositiveUnsignedDecimalFlag(ctx, flagAmount)
if err != nil {
return err
}

c := newClient(ctx)
req := &rpc.TransferXMRRequest{
To: to,
Amount: amount,
Expand All @@ -1186,13 +1194,14 @@ func runTransferXMR(ctx *cli.Context) error {
return err
}

fmt.Printf("Transferred %s XMR to %s\n", amount, to)
fmt.Printf("Transaction ID: %s\n", resp.TxID)
fmt.Printf("Success, TX ID: %s\n", resp.TxID)
return nil
}

func runSweepXMR(ctx *cli.Context) error {
env, err := common.NewEnv(ctx.String(flagEnv))
c := newClient(ctx)

env, err := queryEnv(c)
if err != nil {
return err
}
Expand All @@ -1202,7 +1211,6 @@ func runSweepXMR(ctx *cli.Context) error {
return err
}

c := newClient(ctx)
request := &rpctypes.BalancesRequest{}
balances, err := c.Balances(request)
if err != nil {
Expand All @@ -1219,37 +1227,67 @@ func runSweepXMR(ctx *cli.Context) error {
return err
}

fmt.Printf("Transferred %s XMR to %s\n", balances.PiconeroBalance.AsMoneroString(), to)
fmt.Printf("Transaction IDs: %s\n", resp.TxIDs)
fmt.Printf("Success, TX ID(s): %s\n", resp.TxIDs)
return nil
}

func runTransferETH(ctx *cli.Context) error {
ok := ethcommon.IsHexAddress(ctx.String(flagTo))
if !ok {
return fmt.Errorf("invalid address: %s", ctx.String(flagTo))
to, err := cliutil.ReadETHAddress(ctx, flagTo)
if err != nil {
return err
}

to := ethcommon.HexToAddress(ctx.String(flagTo))
amount, err := cliutil.ReadUnsignedDecimalFlag(ctx, flagAmount)
if err != nil {
return err
}

var gasLimit *uint64
if ctx.IsSet(flagGasLimit) {
gasLimit = new(uint64)
*gasLimit = ctx.Uint64(flagGasLimit)
}

c := newClient(ctx)
req := &rpc.TransferETHRequest{
To: to,
Amount: amount,
To: to,
Amount: amount,
GasLimit: gasLimit,
}

fmt.Printf("Transferring %s ETH to %s\n", amount, to)
fmt.Printf("Transferring %s ETH to %s and waiting for confirmation\n", amount, to)
resp, err := c.TransferETH(req)
if err != nil {
return err
}

fmt.Printf("Transferred %s ETH to %s\n", amount, to)
fmt.Printf("Transaction ID: %s\n", resp.TxHash)
printSuccessWithETHTxHash(c, resp.TxHash)

return nil
}

func runSweepETH(ctx *cli.Context) error {
to, err := cliutil.ReadETHAddress(ctx, flagTo)
if err != nil {
return err
}

c := newClient(ctx)
request := &rpctypes.BalancesRequest{}
balances, err := c.Balances(request)
if err != nil {
return err
}

fmt.Printf("Sweeping %s ETH to %s and waiting block for confirmation\n", balances.WeiBalance.AsEtherString(), to)

resp, err := c.SweepETH(&rpc.SweepETHRequest{To: to})
if err != nil {
return err
}

printSuccessWithETHTxHash(c, resp.TxHash)

return nil
}

Expand Down
Loading

0 comments on commit 9e3ef7b

Please sign in to comment.