Skip to content

Commit

Permalink
add zeroFee flag in wallet.sendOffchain method (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
louisinger authored Jan 30, 2025
1 parent 9c72e6f commit 53b82ee
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
18 changes: 12 additions & 6 deletions src/core/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ export class Wallet implements IWallet {
return this.arkProvider.getVirtualCoins(address.offchain);
}

async sendBitcoin(params: SendBitcoinParams): Promise<string> {
async sendBitcoin(
params: SendBitcoinParams,
zeroFee: boolean = true
): Promise<string> {
if (params.amount <= 0) {
throw new Error("Amount must be positive");
}
Expand All @@ -182,7 +185,7 @@ export class Wallet implements IWallet {

// If Ark is configured and amount is suitable, send via offchain
if (this.arkProvider && this.isOffchainSuitable(params)) {
return this.sendOffchain(params);
return this.sendOffchain(params, zeroFee);
}

// Otherwise, send via onchain
Expand Down Expand Up @@ -250,7 +253,10 @@ export class Wallet implements IWallet {
return txid;
}

async sendOffchain(params: SendBitcoinParams): Promise<string> {
async sendOffchain(
params: SendBitcoinParams,
zeroFee: boolean = true
): Promise<string> {
if (
!this.arkProvider ||
!this.offchainAddress ||
Expand All @@ -260,10 +266,10 @@ export class Wallet implements IWallet {
}

const virtualCoins = await this.getVirtualCoins();
const feeRate = params.feeRate || Wallet.FEE_RATE;

// Ensure fee is an integer by rounding up
const estimatedFee = Math.ceil(174 * feeRate);
const estimatedFee = zeroFee
? 0
: Math.ceil(174 * (params.feeRate || Wallet.FEE_RATE));
const totalNeeded = params.amount + estimatedFee;

const selected = await selectVirtualCoins(virtualCoins, totalNeeded);
Expand Down
5 changes: 3 additions & 2 deletions src/types/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ export interface Wallet {
getBalance(): Promise<WalletBalance>;
getCoins(): Promise<Coin[]>;
getVirtualCoins(): Promise<VirtualCoin[]>;
sendBitcoin(params: SendBitcoinParams): Promise<string>;
sendBitcoin(params: SendBitcoinParams, zeroFee?: boolean): Promise<string>;
sendOnchain(params: SendBitcoinParams): Promise<string>;
sendOffchain(params: SendBitcoinParams): Promise<string>;
// TODO: remove zeroFee with transaction v3
sendOffchain(params: SendBitcoinParams, zeroFee?: boolean): Promise<string>;
signMessage(message: string): Promise<string>;
verifyMessage(
message: string,
Expand Down
2 changes: 1 addition & 1 deletion test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe('Wallet SDK Integration Tests', () => {
await carolWallet.sendBitcoin({
address: daveOffchainAddress!,
amount: sendAmount,
})
}, false)

// Wait for the transaction to be processed
await new Promise(resolve => setTimeout(resolve, 500))
Expand Down

0 comments on commit 53b82ee

Please sign in to comment.