Skip to content

Commit

Permalink
Merge branch '118-min-satoshi' into 'dev'
Browse files Browse the repository at this point in the history
Resolve "set minimum satoshi"

Closes #118

See merge request ergo/rosen-bridge/rosen-chains!142
  • Loading branch information
zargarzadehm committed Oct 2, 2024
2 parents 5aee3cb + e469aff commit 80782d2
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-fans-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rosen-chains/bitcoin': minor
---

remove legacy and script address payment restrictions in BitcoinChain
5 changes: 5 additions & 0 deletions .changeset/quiet-snakes-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rosen-chains/bitcoin': minor
---

set minimum satoshi to 546
20 changes: 14 additions & 6 deletions packages/chains/bitcoin/lib/BitcoinChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,14 @@ class BitcoinChain extends AbstractUtxoChain<BitcoinTx, BitcoinUtxo> {
if (order.assets.tokens.length) {
throw Error('Bitcoin does not support tokens in payment order');
}
if (order.address.slice(0, 4) !== 'bc1q') {
throw Error('Bitcoin does not support payment to non-segwit addresses');
if (
order.address.slice(0, 4) !== 'bc1q' &&
order.address[0] !== '1' &&
order.address[0] !== '3'
) {
throw Error(
'Bitcoin supports payments only to native-segwit, legacy or script addresses'
);
}
const orderBtc = this.unwrapBtc(order.assets.nativeToken).amount;

Expand Down Expand Up @@ -492,8 +498,7 @@ class BitcoinChain extends AbstractUtxoChain<BitcoinTx, BitcoinUtxo> {
* @returns the minimum amount
*/
getMinimumNativeToken = (): bigint => {
// there is no token in bitcoin
return 0n;
return 546n; // smallest non-dust value
};

/**
Expand Down Expand Up @@ -646,8 +651,11 @@ class BitcoinChain extends AbstractUtxoChain<BitcoinTx, BitcoinUtxo> {
*/
minimumMeaningfulSatoshi = (feeRatio: number): bigint => {
return BigInt(
Math.ceil(
(feeRatio * SEGWIT_INPUT_WEIGHT_UNIT) / 4 // estimate fee per weight and convert to virtual size
Math.max(
Math.ceil(
(feeRatio * SEGWIT_INPUT_WEIGHT_UNIT) / 4 // estimate fee per weight and convert to virtual size
),
Number(this.getMinimumNativeToken())
)
);
};
Expand Down
5 changes: 2 additions & 3 deletions packages/chains/bitcoin/tests/BitcoinChain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ describe('BitcoinChain', () => {
const expectedRequiredAssets = structuredClone(
testData.transaction2Order[0].assets
);
expectedRequiredAssets.nativeToken += BigInt(
Math.ceil(SEGWIT_INPUT_WEIGHT_UNIT / 4)
);
expectedRequiredAssets.nativeToken +=
bitcoinChain.getMinimumNativeToken();
expect(getCovBoxesSpy).toHaveBeenCalledWith(
testUtils.configs.addresses.lock,
expectedRequiredAssets,
Expand Down

0 comments on commit 80782d2

Please sign in to comment.