Skip to content

Commit 90561f8

Browse files
committed
multi: add fee percentage to rpc
1 parent dd1a2de commit 90561f8

File tree

4 files changed

+246
-183
lines changed

4 files changed

+246
-183
lines changed

loopd/swapclient_server.go

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -587,19 +587,24 @@ func (s *swapClientServer) GetLiquidityParams(_ context.Context,
587587
MaxSwapAmount: uint64(cfg.ClientRestrictions.Maximum),
588588
}
589589

590-
feeCategories, ok := cfg.FeeLimit.(*liquidity.FeeCategoryLimit)
591-
if !ok {
592-
return nil, fmt.Errorf("unknown fee limit: %T", cfg.FeeLimit)
593-
}
590+
switch f := cfg.FeeLimit.(type) {
591+
case *liquidity.FeeCategoryLimit:
592+
satPerByte := f.SweepFeeRateLimit.FeePerKVByte() / 1000
593+
594+
rpcCfg.SweepFeeRateSatPerVbyte = uint64(satPerByte)
594595

595-
satPerByte := feeCategories.SweepFeeRateLimit.FeePerKVByte() / 1000
596+
rpcCfg.MaxMinerFeeSat = uint64(f.MaximumMinerFee)
597+
rpcCfg.MaxSwapFeePpm = f.MaximumSwapFeePPM
598+
rpcCfg.MaxRoutingFeePpm = f.MaximumRoutingFeePPM
599+
rpcCfg.MaxPrepayRoutingFeePpm = f.MaximumPrepayRoutingFeePPM
600+
rpcCfg.MaxPrepaySat = uint64(f.MaximumPrepay)
596601

597-
rpcCfg.SweepFeeRateSatPerVbyte = uint64(satPerByte)
598-
rpcCfg.MaxMinerFeeSat = uint64(feeCategories.MaximumMinerFee)
599-
rpcCfg.MaxSwapFeePpm = feeCategories.MaximumSwapFeePPM
600-
rpcCfg.MaxRoutingFeePpm = feeCategories.MaximumRoutingFeePPM
601-
rpcCfg.MaxPrepayRoutingFeePpm = feeCategories.MaximumPrepayRoutingFeePPM
602-
rpcCfg.MaxPrepaySat = uint64(feeCategories.MaximumPrepay)
602+
case *liquidity.FeePortion:
603+
rpcCfg.FeePpm = f.PartsPerMillion
604+
605+
default:
606+
return nil, fmt.Errorf("unknown fee limit: %T", cfg.FeeLimit)
607+
}
603608

604609
// Zero golang time is different to a zero unix time, so we only set
605610
// our start date if it is non-zero.
@@ -641,18 +646,10 @@ func (s *swapClientServer) SetLiquidityParams(ctx context.Context,
641646
in *looprpc.SetLiquidityParamsRequest) (*looprpc.SetLiquidityParamsResponse,
642647
error) {
643648

644-
satPerVbyte := chainfee.SatPerKVByte(
645-
in.Parameters.SweepFeeRateSatPerVbyte * 1000,
646-
)
647-
648-
feeLimit := liquidity.NewFeeCategoryLimit(
649-
in.Parameters.MaxSwapFeePpm,
650-
in.Parameters.MaxRoutingFeePpm,
651-
in.Parameters.MaxPrepayRoutingFeePpm,
652-
btcutil.Amount(in.Parameters.MaxMinerFeeSat),
653-
btcutil.Amount(in.Parameters.MaxPrepaySat),
654-
satPerVbyte.FeePerKWeight(),
655-
)
649+
feeLimit, err := rpcToFee(in.Parameters)
650+
if err != nil {
651+
return nil, err
652+
}
656653

657654
params := liquidity.Parameters{
658655
FeeLimit: feeLimit,
@@ -732,6 +729,45 @@ func (s *swapClientServer) SetLiquidityParams(ctx context.Context,
732729
return &looprpc.SetLiquidityParamsResponse{}, nil
733730
}
734731

732+
// rpcToFee converts the values provided over rpc to a fee limit interface,
733+
// failing if an inconsistent set of fields are set.
734+
func rpcToFee(req *looprpc.LiquidityParameters) (liquidity.FeeLimit,
735+
error) {
736+
737+
// Check which fee limit type we have values set for. If any fields
738+
// relevant to our individual categories are set, we count that type
739+
// as set.
740+
isFeePPM := req.FeePpm != 0
741+
isCategories := req.MaxSwapFeePpm != 0 || req.MaxRoutingFeePpm != 0 ||
742+
req.MaxPrepayRoutingFeePpm != 0 || req.MaxMinerFeeSat != 0 ||
743+
req.MaxPrepaySat != 0 || req.SweepFeeRateSatPerVbyte != 0
744+
745+
switch {
746+
case isFeePPM && isCategories:
747+
return nil, errors.New("set either fee ppm, or individual " +
748+
"fee categories")
749+
case isFeePPM:
750+
return liquidity.NewFeePortion(req.FeePpm), nil
751+
752+
case isCategories:
753+
satPerVbyte := chainfee.SatPerKVByte(
754+
req.SweepFeeRateSatPerVbyte * 1000,
755+
)
756+
757+
return liquidity.NewFeeCategoryLimit(
758+
req.MaxSwapFeePpm,
759+
req.MaxRoutingFeePpm,
760+
req.MaxPrepayRoutingFeePpm,
761+
btcutil.Amount(req.MaxMinerFeeSat),
762+
btcutil.Amount(req.MaxPrepaySat),
763+
satPerVbyte.FeePerKWeight(),
764+
), nil
765+
766+
default:
767+
return nil, errors.New("no fee categories set")
768+
}
769+
}
770+
735771
// rpcToRule switches on rpc rule type to convert to our rule interface.
736772
func rpcToRule(rule *looprpc.LiquidityRule) (*liquidity.ThresholdRule, error) {
737773
switch rule.Type {

0 commit comments

Comments
 (0)