Skip to content

Commit

Permalink
Add withdrawal_fee_estimate to the js sdks (#338)
Browse files Browse the repository at this point in the history
* Add withdrawal_fee_estimate to the js sdks (#7876)

GitOrigin-RevId: a429b2377175ebbe29ff6dbcc6995a7d58953d1a

* Create rotten-bears-peel.md

---------

Co-authored-by: Jeremy Klein <[email protected]>
Co-authored-by: Corey Martin <[email protected]>
  • Loading branch information
3 people authored Dec 5, 2023
1 parent d694137 commit 4c93e7f
Show file tree
Hide file tree
Showing 15 changed files with 345 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/rotten-bears-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@lightsparkdev/lightspark-sdk": minor
"@lightsparkdev/wallet-sdk": minor
---

Add withdrawal_fee_estimate to the js sdks
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/ @lightsparkdev/js
* @lightsparkdev/js
44 changes: 44 additions & 0 deletions packages/lightspark-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import { SendPayment } from "./graphql/SendPayment.js";
import { SingleNodeDashboard as SingleNodeDashboardQuery } from "./graphql/SingleNodeDashboard.js";
import { TransactionSubscription } from "./graphql/TransactionSubscription.js";
import { TransactionsForNode } from "./graphql/TransactionsForNode.js";
import { WithdrawalFeeEstimate } from "./graphql/WithdrawalFeeEstimate.js";
import { RiskRating, TransactionStatus } from "./index.js";
import Account from "./objects/Account.js";
import { ApiTokenFromJson } from "./objects/ApiToken.js";
Expand Down Expand Up @@ -90,6 +91,8 @@ import type TransactionUpdate from "./objects/TransactionUpdate.js";
import { TransactionUpdateFromJson } from "./objects/TransactionUpdate.js";
import type UmaInvitation from "./objects/UmaInvitation.js";
import { UmaInvitationFromJson } from "./objects/UmaInvitation.js";
import type WithdrawalFeeEstimateOutput from "./objects/WithdrawalFeeEstimateOutput.js";
import { WithdrawalFeeEstimateOutputFromJson } from "./objects/WithdrawalFeeEstimateOutput.js";
import type WithdrawalMode from "./objects/WithdrawalMode.js";
import type WithdrawalRequest from "./objects/WithdrawalRequest.js";
import { WithdrawalRequestFromJson } from "./objects/WithdrawalRequest.js";
Expand Down Expand Up @@ -714,6 +717,47 @@ class LightsparkClient {
);
}

/**
* Returns an estimated amount for the L1 withdrawal fees for the specified node, amount, and
* strategy.
*
* @param nodeId The node from which you'd like to make the withdrawal.
* @param amountSats The amount you want to withdraw from this node in Satoshis. Use the special value -1 to withdrawal all funds from this node.
* @param withdrawalMode The strategy that should be used to withdraw the funds from this node.
* @returns An estimated amount for the L1 withdrawal fees for the specified node, amount, and strategy.
*/
public async getWithrawalFeeEstimate(
nodeId: string,
amountSats: number,
withdrawalMode: WithdrawalMode,
): Promise<CurrencyAmount> {
const response: WithdrawalFeeEstimateOutput | null =
await this.executeRawQuery({
queryPayload: WithdrawalFeeEstimate,
variables: {
node_id: nodeId,
amount_sats: amountSats,
withdrawal_mode: withdrawalMode,
},
constructObject: (response: {
withdrawal_fee_estimate: any; // eslint-disable-line @typescript-eslint/no-explicit-any
}) => {
return WithdrawalFeeEstimateOutputFromJson(
response.withdrawal_fee_estimate,
);
},
});

if (!response) {
throw new LightsparkException(
"WithdrawalFeeEstimateError",
"Null or invalid fee estimate response from server",
);
}

return response.feeEstimate;
}

/**
* Directly unlocks a node with a signing private key or alias.
*
Expand Down
21 changes: 21 additions & 0 deletions packages/lightspark-sdk/src/graphql/WithdrawalFeeEstimate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved

import { FRAGMENT as WithdrawalFeeEstimateOutputFragment } from "../objects/WithdrawalFeeEstimateOutput.js";

export const WithdrawalFeeEstimate = `
query WithdrawalFeeEstimate(
$node_id: ID!
$amount_sats: Long!
$withdrawal_mode: WithdrawalMode!
) {
withdrawal_fee_estimate(input: {
node_id: $node_id,
amount_sats: $amount_sats,
withdrawal_mode: $withdrawal_mode
}) {
...WithdrawalFeeEstimateOutputFragment
}
}
${WithdrawalFeeEstimateOutputFragment}
`;
40 changes: 40 additions & 0 deletions packages/lightspark-sdk/src/objects/WithdrawalFeeEstimateInput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved

import WithdrawalMode from "./WithdrawalMode.js";

interface WithdrawalFeeEstimateInput {
/** The node from which you'd like to make the withdrawal. **/
nodeId: string;

/**
* The amount you want to withdraw from this node in Satoshis. Use the special value -1 to
* withdrawal all funds from this node.
**/
amountSats: number;

/** The strategy that should be used to withdraw the funds from this node. **/
withdrawalMode: WithdrawalMode;
}

export const WithdrawalFeeEstimateInputFromJson = (
obj: any,
): WithdrawalFeeEstimateInput => {
return {
nodeId: obj["withdrawal_fee_estimate_input_node_id"],
amountSats: obj["withdrawal_fee_estimate_input_amount_sats"],
withdrawalMode:
WithdrawalMode[obj["withdrawal_fee_estimate_input_withdrawal_mode"]] ??
WithdrawalMode.FUTURE_VALUE,
} as WithdrawalFeeEstimateInput;
};
export const WithdrawalFeeEstimateInputToJson = (
obj: WithdrawalFeeEstimateInput,
): any => {
return {
withdrawal_fee_estimate_input_node_id: obj.nodeId,
withdrawal_fee_estimate_input_amount_sats: obj.amountSats,
withdrawal_fee_estimate_input_withdrawal_mode: obj.withdrawalMode,
};
};

export default WithdrawalFeeEstimateInput;
46 changes: 46 additions & 0 deletions packages/lightspark-sdk/src/objects/WithdrawalFeeEstimateOutput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved

import type CurrencyAmount from "./CurrencyAmount.js";
import {
CurrencyAmountFromJson,
CurrencyAmountToJson,
} from "./CurrencyAmount.js";

interface WithdrawalFeeEstimateOutput {
/** The estimated fee for the withdrawal. **/
feeEstimate: CurrencyAmount;
}

export const WithdrawalFeeEstimateOutputFromJson = (
obj: any,
): WithdrawalFeeEstimateOutput => {
return {
feeEstimate: CurrencyAmountFromJson(
obj["withdrawal_fee_estimate_output_fee_estimate"],
),
} as WithdrawalFeeEstimateOutput;
};
export const WithdrawalFeeEstimateOutputToJson = (
obj: WithdrawalFeeEstimateOutput,
): any => {
return {
withdrawal_fee_estimate_output_fee_estimate: CurrencyAmountToJson(
obj.feeEstimate,
),
};
};

export const FRAGMENT = `
fragment WithdrawalFeeEstimateOutputFragment on WithdrawalFeeEstimateOutput {
__typename
withdrawal_fee_estimate_output_fee_estimate: fee_estimate {
__typename
currency_amount_original_value: original_value
currency_amount_original_unit: original_unit
currency_amount_preferred_currency_unit: preferred_currency_unit
currency_amount_preferred_currency_value_rounded: preferred_currency_value_rounded
currency_amount_preferred_currency_value_approx: preferred_currency_value_approx
}
}`;

export default WithdrawalFeeEstimateOutput;
2 changes: 2 additions & 0 deletions packages/lightspark-sdk/src/objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ export { default as WalletToTransactionsConnection } from "./WalletToTransaction
export { default as WalletToWithdrawalRequestsConnection } from "./WalletToWithdrawalRequestsConnection.js";
export { default as WebhookEventType } from "./WebhookEventType.js";
export { default as Withdrawal, getWithdrawalQuery } from "./Withdrawal.js";
export { default as WithdrawalFeeEstimateInput } from "./WithdrawalFeeEstimateInput.js";
export { default as WithdrawalFeeEstimateOutput } from "./WithdrawalFeeEstimateOutput.js";
export { default as WithdrawalMode } from "./WithdrawalMode.js";
export { default as WithdrawalRequest } from "./WithdrawalRequest.js";
export { default as WithdrawalRequestStatus } from "./WithdrawalRequestStatus.js";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,15 @@ describe(p2SuiteName, () => {
TESTS_TIMEOUT,
);

test("Should get a withdrawal fee estimate", async () => {
const fee = await lightsparkClient.getWithrawalFeeEstimate(
getRegtestNodeId(),
100,
WithdrawalMode.WALLET_THEN_CHANNELS,
);
expect(fee).not.toBeNull();
});

test(
"Should execute a raw graphql query",
async () => {
Expand Down
43 changes: 43 additions & 0 deletions packages/wallet-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import RequestWithdrawalMutation from "./graqhql/RequestWithdrawal.js";
import SendPaymentMutation from "./graqhql/SendPayment.js";
import TerminateWallet from "./graqhql/TerminateWallet.js";
import WalletDashboardQuery from "./graqhql/WalletDashboard.js";
import { WithdrawalFeeEstimate } from "./graqhql/WithdrawalFeeEstimate.js";
import { logger } from "./logger.js";
import { BalancesFromJson } from "./objects/Balances.js";
import type CurrencyAmount from "./objects/CurrencyAmount.js";
Expand Down Expand Up @@ -66,6 +67,9 @@ import type WalletDashboard from "./objects/WalletDashboard.js";
import WalletStatus from "./objects/WalletStatus.js";
import { WalletToPaymentRequestsConnectionFromJson } from "./objects/WalletToPaymentRequestsConnection.js";
import { WalletToTransactionsConnectionFromJson } from "./objects/WalletToTransactionsConnection.js";
import type WithdrawalFeeEstimateOutput from "./objects/WithdrawalFeeEstimateOutput.js";
import { WithdrawalFeeEstimateOutputFromJson } from "./objects/WithdrawalFeeEstimateOutput.js";
import type WithdrawalMode from "./objects/WithdrawalMode.js";
import type WithdrawalRequest from "./objects/WithdrawalRequest.js";
import { WithdrawalRequestFromJson } from "./objects/WithdrawalRequest.js";

Expand Down Expand Up @@ -766,6 +770,45 @@ class LightsparkClient {
);
}

/**
* Returns an estimated amount for the L1 withdrawal fees for the specified node, amount, and
* strategy.
*
* @param amountSats The amount you want to withdraw from this node in Satoshis. Use the special value -1 to
* withdrawal all funds from this wallet.
* @param withdrawalMode The strategy that should be used to withdraw the funds from this node.
* @returns An estimated amount for the L1 withdrawal fees for the specified node, amount, and strategy.
*/
public async getWithrawalFeeEstimate(
amountSats: number,
withdrawalMode: WithdrawalMode,
): Promise<CurrencyAmount> {
const response: WithdrawalFeeEstimateOutput | null =
await this.executeRawQuery({
queryPayload: WithdrawalFeeEstimate,
variables: {
amount_sats: amountSats,
withdrawal_mode: withdrawalMode,
},
constructObject: (response: {
withdrawal_fee_estimate: any; // eslint-disable-line @typescript-eslint/no-explicit-any
}) => {
return WithdrawalFeeEstimateOutputFromJson(
response.withdrawal_fee_estimate,
);
},
});

if (!response) {
throw new LightsparkException(
"WithdrawalFeeEstimateError",
"Null or invalid fee estimate response from server",
);
}

return response.feeEstimate;
}

/**
* Unlocks the wallet for use with the SDK for the current application
* session. This function must be called before any other functions that
Expand Down
19 changes: 19 additions & 0 deletions packages/wallet-sdk/src/graqhql/WithdrawalFeeEstimate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved

import { FRAGMENT as WithdrawalFeeEstimateOutputFragment } from "../objects/WithdrawalFeeEstimateOutput.js";

export const WithdrawalFeeEstimate = `
query WithdrawalFeeEstimate(
$amount_sats: Long!
$withdrawal_mode: WithdrawalMode!
) {
withdrawal_fee_estimate(input: {
amount_sats: $amount_sats,
withdrawal_mode: $withdrawal_mode
}) {
...WithdrawalFeeEstimateOutputFragment
}
}
${WithdrawalFeeEstimateOutputFragment}
`;
35 changes: 35 additions & 0 deletions packages/wallet-sdk/src/objects/WithdrawalFeeEstimateInput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved

import WithdrawalMode from "./WithdrawalMode.js";

interface WithdrawalFeeEstimateInput {
/**
* The amount you want to withdraw from this node in Satoshis. Use the special value -1 to
* withdrawal all funds from this node.
**/
amountSats: number;

/** The strategy that should be used to withdraw the funds from this node. **/
withdrawalMode: WithdrawalMode;
}

export const WithdrawalFeeEstimateInputFromJson = (
obj: any,
): WithdrawalFeeEstimateInput => {
return {
amountSats: obj["withdrawal_fee_estimate_input_amount_sats"],
withdrawalMode:
WithdrawalMode[obj["withdrawal_fee_estimate_input_withdrawal_mode"]] ??
WithdrawalMode.FUTURE_VALUE,
} as WithdrawalFeeEstimateInput;
};
export const WithdrawalFeeEstimateInputToJson = (
obj: WithdrawalFeeEstimateInput,
): any => {
return {
withdrawal_fee_estimate_input_amount_sats: obj.amountSats,
withdrawal_fee_estimate_input_withdrawal_mode: obj.withdrawalMode,
};
};

export default WithdrawalFeeEstimateInput;
46 changes: 46 additions & 0 deletions packages/wallet-sdk/src/objects/WithdrawalFeeEstimateOutput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved

import type CurrencyAmount from "./CurrencyAmount.js";
import {
CurrencyAmountFromJson,
CurrencyAmountToJson,
} from "./CurrencyAmount.js";

interface WithdrawalFeeEstimateOutput {
/** The estimated fee for the withdrawal. **/
feeEstimate: CurrencyAmount;
}

export const WithdrawalFeeEstimateOutputFromJson = (
obj: any,
): WithdrawalFeeEstimateOutput => {
return {
feeEstimate: CurrencyAmountFromJson(
obj["withdrawal_fee_estimate_output_fee_estimate"],
),
} as WithdrawalFeeEstimateOutput;
};
export const WithdrawalFeeEstimateOutputToJson = (
obj: WithdrawalFeeEstimateOutput,
): any => {
return {
withdrawal_fee_estimate_output_fee_estimate: CurrencyAmountToJson(
obj.feeEstimate,
),
};
};

export const FRAGMENT = `
fragment WithdrawalFeeEstimateOutputFragment on WithdrawalFeeEstimateOutput {
__typename
withdrawal_fee_estimate_output_fee_estimate: fee_estimate {
__typename
currency_amount_original_value: original_value
currency_amount_original_unit: original_unit
currency_amount_preferred_currency_unit: preferred_currency_unit
currency_amount_preferred_currency_value_rounded: preferred_currency_value_rounded
currency_amount_preferred_currency_value_approx: preferred_currency_value_approx
}
}`;

export default WithdrawalFeeEstimateOutput;
16 changes: 16 additions & 0 deletions packages/wallet-sdk/src/objects/WithdrawalMode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright ©, 2023-present, Lightspark Group, Inc. - All Rights Reserved

/** This is an enum of the potential modes that your Bitcoin withdrawal can take. **/
export enum WithdrawalMode {
/**
* This is an enum value that represents values that could be added in the future.
* Clients should support unknown values as more of them could be added without notice.
*/
FUTURE_VALUE = "FUTURE_VALUE",

WALLET_ONLY = "WALLET_ONLY",

WALLET_THEN_CHANNELS = "WALLET_THEN_CHANNELS",
}

export default WithdrawalMode;
Loading

0 comments on commit 4c93e7f

Please sign in to comment.