Skip to content

Commit

Permalink
fix: final
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsimao committed Oct 15, 2024
1 parent 6c40084 commit 41c4657
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 32 deletions.
1 change: 1 addition & 0 deletions sdk/src/gateway/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ export class GatewayApiClient {
gatewayQuote.satoshis,
params.fromUserPublicKey,
data.opReturnHash,
params.feeRate,
gatewayQuote.txProofDifficultyFactor
);
}
Expand Down
2 changes: 2 additions & 0 deletions sdk/src/gateway/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ export interface GatewayQuoteParams {
/** @description The percentage of fee charged by partners in Basis Points (BPS) units. This will override the default fee rate configured via platform. 1 BPS = 0.01%. The maximum value is 1000 (which equals 10%). The minimum value is 1 (which equals 0.01%). */
fee?: number;

feeRate?: number;

// NOTE: the following are new fields added by us
/** @description Amount of satoshis to swap for ETH */
gasRefill?: number;
Expand Down
22 changes: 0 additions & 22 deletions sdk/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,25 +187,3 @@ export function getMerkleProof(block: Block, txHash: string, forWitness?: boolea
root: merkleAndRoot.root.toString('hex'),
};
}

/**
* Estimate the tx inclusion fee for N P2WPKH inputs and 3 P2WPKH outputs.
*
* @param feeRate - The current rate for inclusion, satoshi per byte.
* @param numInputs - The number of inputs to estimate for.
* @returns The estimated fee for transaction inclusion.
*/
export function estimateTxFee(feeRate: number, numInputs: number = 1) {
const tx = new bitcoin.Transaction();
for (let i = 0; i < numInputs; i++) {
tx.addInput(Buffer.alloc(32, 0), 0, 0xfffffffd, Buffer.alloc(0));
}
// https://github.com/interlay/interbtc-clients/blob/6bd3e81d695b93180c5aeae4f33910ad4395ff1a/bitcoin/src/light/wallet.rs#L80
tx.ins.map((tx_input) => (tx_input.witness = [Buffer.alloc(33 + 32 + 7, 0), Buffer.alloc(33, 0)]));
tx.addOutput(Buffer.alloc(22, 0), 1000); // P2WPKH
tx.addOutput(Buffer.alloc(22, 0), 1000); // P2WPKH (change)
tx.addOutput(bitcoin.script.compile([bitcoin.opcodes.OP_RETURN, Buffer.alloc(20, 0)]), 0);
const vsize = tx.virtualSize();
const satoshis = feeRate * vsize;
return satoshis;
}
8 changes: 4 additions & 4 deletions sdk/src/wallet/utxo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Transaction, Script, selectUTXO, TEST_NETWORK, NETWORK, p2wpkh, p2sh }
import { hex, base64 } from '@scure/base';
import { AddressType, getAddressInfo, Network } from 'bitcoin-address-validation';
import { EsploraClient, UTXO } from '../esplora';
import { SelectionStrategy } from '@scure/btc-signer/lib/utxo';

export type BitcoinNetworkName = Exclude<Network, 'regtest'>;

Expand Down Expand Up @@ -70,7 +69,7 @@ export async function createBitcoinPsbt(
const addressInfo = getAddressInfo(fromAddress);

// TODO: possibly, allow other strategies to be passed to this function
const utxoSelectionStrategy: SelectionStrategy = 'default';
const utxoSelectionStrategy = 'default';

if (addressInfo.network === 'regtest') {
throw new Error('Bitcoin regtest not supported');
Expand Down Expand Up @@ -336,7 +335,7 @@ export async function estimateTxFee(
let outputs: Output[] = [];
// Select all UTXOs if no amount is specified
// Add outputs to the transaction after all UTXOs are selected to prevent tx creation failures
let utxoSelectionStrategy: SelectionStrategy = 'all';
let utxoSelectionStrategy = 'all';
if (amount) {
// Add the target outputs to the transaction
// Tx creation might fail if the requested amount is more than the available balance plus fees
Expand All @@ -349,7 +348,8 @@ export async function estimateTxFee(
// https://github.com/paulmillr/scure-btc-signer?tab=readme-ov-file#utxo-selection
// default = exactBiggest/accumBiggest creates tx with smallest fees, but it breaks
// big outputs to small ones, which in the end will create a lot of outputs close to dust.
const transaction = selectUTXO(possibleInputs, outputs, utxoSelectionStrategy, {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const transaction = selectUTXO(possibleInputs, outputs, utxoSelectionStrategy as any, {
changeAddress: fromAddress, // Refund surplus to the payment address
feePerByte: BigInt(Math.ceil(feeRate)), // round up to the nearest integer
bip69: true, // Sort inputs and outputs according to BIP69
Expand Down
8 changes: 2 additions & 6 deletions sdk/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Block } from 'bitcoinjs-lib';
import { assert, describe, it } from 'vitest';
import { MAINNET_ESPLORA_BASE_PATH } from '../src/esplora';
import { Block } from 'bitcoinjs-lib';
import { estimateTxFee, getMerkleProof } from '../src/utils';
import { getMerkleProof } from '../src/utils';

describe('Utils Tests', () => {
// NOTE: this is a bit flaky due to slow response times from electrs
Expand All @@ -19,8 +19,4 @@ describe('Utils Tests', () => {
root: '7cee5e99c8f0fc25fb115b7d7d00befca61f59a8544adaf3980f52132baf61ae',
});
});

it('should estimate fee', async () => {
assert.equal(estimateTxFee(1), 172);
});
});

0 comments on commit 41c4657

Please sign in to comment.