liquidity: fail suggest swaps when no rules are set

In an effort to surface more information about why autoloop is not
executing, we add an error when suggest swaps is called with no rules.
In other cases we can surface a reason enum with each rule that is set,
but in the case where we have no rules, there are no results to
accompany with reasons.
This commit is contained in:
carla 2021-02-08 09:38:23 +02:00
parent 67f417188d
commit 7ba1821696
No known key found for this signature in database
GPG key ID: 4CA7FE54A6213C91
5 changed files with 44 additions and 7 deletions

View file

@ -2,12 +2,15 @@ package main
import (
"context"
"errors"
"fmt"
"strconv"
"github.com/lightninglabs/loop/liquidity"
"github.com/lightninglabs/loop/looprpc"
"github.com/urfave/cli"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var getLiquidityParamsCommand = cli.Command{
@ -411,11 +414,22 @@ func suggestSwap(ctx *cli.Context) error {
resp, err := client.SuggestSwaps(
context.Background(), &looprpc.SuggestSwapsRequest{},
)
if err != nil {
if err == nil {
printJSON(resp)
return nil
}
// If we got an error because no rules are set, we want to display a
// friendly message.
rpcErr, ok := status.FromError(err)
if !ok {
return err
}
printJSON(resp)
if rpcErr.Code() != codes.FailedPrecondition {
return err
}
return nil
return errors.New("no rules set for autolooper, please set rules " +
"using the setrule command")
}