Skip to content

Commit

Permalink
Add a function to set fees for request_withdrawal
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenlu committed May 30, 2024
1 parent 441e3b6 commit 0c0b2b3
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 4 deletions.
65 changes: 65 additions & 0 deletions objects/on_chain_fee_target.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved
package objects

import (
"encoding/json"
)

type OnChainFeeTarget int

const (
OnChainFeeTargetUndefined OnChainFeeTarget = iota

// OnChainFeeTargetHigh Transaction expected to be confirmed within 2 blocks.
OnChainFeeTargetHigh
// OnChainFeeTargetMedium Transaction expected to be confirmed within 6 blocks.
OnChainFeeTargetMedium
// OnChainFeeTargetLow Transaction expected to be confirmed within 18 blocks.
OnChainFeeTargetLow
// OnChainFeeTargetBackground Transaction expected to be confirmed within 50 blocks.
OnChainFeeTargetBackground
)

func (a *OnChainFeeTarget) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
switch s {
default:
*a = OnChainFeeTargetUndefined
case "HIGH":
*a = OnChainFeeTargetHigh
case "MEDIUM":
*a = OnChainFeeTargetMedium
case "LOW":
*a = OnChainFeeTargetLow
case "BACKGROUND":
*a = OnChainFeeTargetBackground

}
return nil
}

func (a OnChainFeeTarget) StringValue() string {
var s string
switch a {
default:
s = "undefined"
case OnChainFeeTargetHigh:
s = "HIGH"
case OnChainFeeTargetMedium:
s = "MEDIUM"
case OnChainFeeTargetLow:
s = "LOW"
case OnChainFeeTargetBackground:
s = "BACKGROUND"

}
return s
}

func (a OnChainFeeTarget) MarshalJSON() ([]byte, error) {
s := a.StringValue()
return json.Marshal(s)
}
6 changes: 6 additions & 0 deletions objects/request_withdrawal_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ type RequestWithdrawalInput struct {

// IdempotencyKey The idempotency key of the request. The same result will be returned for the same idempotency key.
IdempotencyKey *string `json:"request_withdrawal_input_idempotency_key"`

// FeeTarget The target of the fee that should be used when crafting the L1 transaction. You should only set `fee_target` or `sats_per_vbyte`. If neither of them is set, default value of MEDIUM will be used as `fee_target`.
FeeTarget *OnChainFeeTarget `json:"request_withdrawal_input_fee_target"`

// SatsPerVbyte A manual fee rate set in sat/vbyte that should be used when crafting the L1 transaction. You should only set `fee_target` or `sats_per_vbyte`
SatsPerVbyte *int64 `json:"request_withdrawal_input_sats_per_vbyte"`
}
2 changes: 1 addition & 1 deletion objects/transaction_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type TransactionStatus int
const (
TransactionStatusUndefined TransactionStatus = iota

// TransactionStatusSuccess Transaction succeeded..
// TransactionStatusSuccess Transaction succeeded.
TransactionStatusSuccess
// TransactionStatusFailed Transaction failed.
TransactionStatusFailed
Expand Down
4 changes: 4 additions & 0 deletions scripts/request_withdrawal.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ mutation RequestWithdrawal(
$bitcoin_address: String!
$withdrawal_mode: WithdrawalMode!
$idempotency_key: String
$fee_target: OnChainFeeTarget
$sats_per_vbyte: Int
) {
request_withdrawal(input: {
node_id: $node_id
amount_sats: $amount_sats
bitcoin_address: $bitcoin_address
withdrawal_mode: $withdrawal_mode
idempotency_key: $idempotency_key
fee_target: $fee_target
sats_per_vbyte: $sats_per_vbyte
}) {
request {
...WithdrawalRequestFragment
Expand Down
21 changes: 18 additions & 3 deletions services/lightspark_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,13 @@ func (client *LightsparkClient) PayUmaInvoice(nodeId string, encodedInvoice stri
func (client *LightsparkClient) RequestWithdrawal(nodeId string, amountSats int64,
bitcoinAddress string, withdrawalMode objects.WithdrawalMode,
) (*objects.WithdrawalRequest, error) {
return client.RequestWithdrawalWithIdempotencyKey(nodeId, amountSats, bitcoinAddress, withdrawalMode, nil)
return client.RequestWithdrawalWithIdempotencyKeyAndFee(nodeId, amountSats, bitcoinAddress, withdrawalMode, nil, nil, nil)
}

func (client *LightsparkClient) RequestWithdrawalWithIdempotencyKey(nodeId string, amountSats int64,
bitcoinAddress string, withdrawalMode objects.WithdrawalMode, idempotencyKey *string,
) (*objects.WithdrawalRequest, error) {
return client.RequestWithdrawalWithIdempotencyKeyAndFee(nodeId, amountSats, bitcoinAddress, withdrawalMode, idempotencyKey, nil, nil)
}

// RequestWithdrawalWithIdempotencyKey withdraws funds from the account and sends it to the requested
Expand All @@ -680,8 +686,9 @@ func (client *LightsparkClient) RequestWithdrawal(nodeId string, amountSats int6
// idempotencyKey: A unique key that identifies this withdrawal. If a withdrawal with the same idempotency key has
// already been made, the same WithdrawalRequest will be returned. This must be unique for each payment. If
// you do not have a specific idempotency requirement, you can pass nil or just use `RequestWithdrawal`.
func (client *LightsparkClient) RequestWithdrawalWithIdempotencyKey(nodeId string, amountSats int64,
bitcoinAddress string, withdrawalMode objects.WithdrawalMode, idempotencyKey *string,
func (client *LightsparkClient) RequestWithdrawalWithIdempotencyKeyAndFee(nodeId string, amountSats int64,
bitcoinAddress string, withdrawalMode objects.WithdrawalMode, idempotencyKey *string,
feeTarget *objects.OnChainFeeTarget, satsPerVbyte *int,
) (*objects.WithdrawalRequest, error) {
variables := map[string]interface{}{
"node_id": nodeId,
Expand All @@ -694,6 +701,14 @@ func (client *LightsparkClient) RequestWithdrawalWithIdempotencyKey(nodeId strin
variables["idempotency_key"] = *idempotencyKey
}

if feeTarget != nil && satsPerVbyte != nil {
return nil, errors.New("feeTarget and satsPerVbyte cannot be set at the same time")
} else if feeTarget != nil {
variables["fee_target"] = *feeTarget
} else if satsPerVbyte != nil {
variables["sats_per_vbyte"] = *satsPerVbyte
}

signingKey, err := client.getNodeSigningKey(nodeId)
if err != nil {
return nil, err
Expand Down

0 comments on commit 0c0b2b3

Please sign in to comment.