@@ -2,9 +2,7 @@ import { RgbppCkbVirtualTx, BtcJumpCkbVirtualTxParams, BtcJumpCkbVirtualTxResult
2
2
import { TypeAssetNotSupportedError } from '../error' ;
3
3
import {
4
4
append0x ,
5
- calculateCellOccupiedCapacity ,
6
5
calculateRgbppCellCapacity ,
7
- calculateTransactionFee ,
8
6
deduplicateList ,
9
7
fetchTypeIdCellDeps ,
10
8
isLockArgsSizeExceeded ,
@@ -22,11 +20,12 @@ import {
22
20
isRgbppCapacitySufficientForChange ,
23
21
isStandardUDTTypeSupported ,
24
22
isOfflineMode ,
23
+ adjustVirtualTxForTxFee ,
25
24
} from '../utils' ;
26
25
import { Hex , IndexerCell } from '../types' ;
27
26
import { RGBPP_WITNESS_PLACEHOLDER , getSecp256k1CellDep } from '../constants' ;
28
27
import { blockchain } from '@ckb-lumos/base' ;
29
- import { addressToScript , getTransactionSize } from '@nervosnetwork/ckb-sdk-utils' ;
28
+ import { addressToScript } from '@nervosnetwork/ckb-sdk-utils' ;
30
29
31
30
/**
32
31
* Generate the virtual ckb transaction for the jumping tx from BTC to CKB
@@ -99,31 +98,28 @@ export const genBtcJumpCkbVirtualTx = async ({
99
98
100
99
let needPaymasterCell = false ;
101
100
const needRgbppChange = sumAmount > transferAmount ;
102
- // To simplify, when the xUDT does not need change, all the capacity of the inputs will be given to the receiver
103
- const candidateCapacity = needRgbppChange ? BigInt ( rgbppTargetCells [ 0 ] . output . capacity ) : sumInputsCapacity ;
104
-
105
- const receiverLock = genBtcTimeLockScript ( toLock , isMainnet , btcTestnetType , btcConfirmationBlocks ) ;
106
- const receiverData = append0x ( u128ToLe ( transferAmount ) ) ;
107
101
108
- const minRequiredCapacity = calculateCellOccupiedCapacity ( {
109
- output : { lock : receiverLock , type : xudtType , capacity : '0x0' } ,
110
- outputData : receiverData ,
111
- } as IndexerCell ) ;
102
+ // To simplify, when the xUDT does not need change, all the capacity of the inputs will be given to the receiver
103
+ const candidateReceiverOutputCapacity = needRgbppChange
104
+ ? BigInt ( rgbppTargetCells [ 0 ] . output . capacity )
105
+ : sumInputsCapacity ;
106
+ const receiverOutputCapacity =
107
+ candidateReceiverOutputCapacity >= rgbppCellCapacity ? candidateReceiverOutputCapacity : rgbppCellCapacity ;
112
108
113
- const receiverOutputCapacity = candidateCapacity > minRequiredCapacity ? candidateCapacity : minRequiredCapacity ;
114
109
// The BTC time cell does not need to be bound to the BTC UTXO
115
110
const outputs : CKBComponents . CellOutput [ ] = [
116
111
{
117
- lock : receiverLock ,
112
+ lock : genBtcTimeLockScript ( toLock , isMainnet , btcTestnetType , btcConfirmationBlocks ) ,
118
113
type : xudtType ,
119
114
capacity : append0x ( receiverOutputCapacity . toString ( 16 ) ) ,
120
115
} ,
121
116
] ;
122
- const outputsData = [ receiverData ] ;
117
+ const outputsData = [ append0x ( u128ToLe ( transferAmount ) ) ] ;
118
+
119
+ const isCapacitySufficient = isRgbppCapacitySufficientForChange ( sumInputsCapacity , receiverOutputCapacity ) ;
120
+ needPaymasterCell = ! isCapacitySufficient ;
123
121
124
122
if ( needRgbppChange ) {
125
- const isCapacitySufficient = isRgbppCapacitySufficientForChange ( sumInputsCapacity , receiverOutputCapacity ) ;
126
- needPaymasterCell = ! isCapacitySufficient ;
127
123
// When the capacity of inputs is enough for the outputs, the sender needs to recover the excess capacity.
128
124
const udtChangeCapacity = isCapacitySufficient ? sumInputsCapacity - receiverOutputCapacity : rgbppCellCapacity ;
129
125
outputs . push ( {
@@ -188,11 +184,15 @@ export const genBtcJumpCkbVirtualTx = async ({
188
184
} ;
189
185
190
186
if ( ! needPaymasterCell ) {
191
- const txSize =
192
- getTransactionSize ( ckbRawTx ) + ( witnessLockPlaceholderSize ?? estimateWitnessSize ( deduplicatedLockArgsList ) ) ;
193
- const estimatedTxFee = calculateTransactionFee ( txSize , ckbFeeRate ) ;
194
- const changeCapacity = BigInt ( outputs [ outputs . length - 1 ] . capacity ) - estimatedTxFee ;
195
- ckbRawTx . outputs [ ckbRawTx . outputs . length - 1 ] . capacity = append0x ( changeCapacity . toString ( 16 ) ) ;
187
+ const txFeeAdjustedResult = adjustVirtualTxForTxFee (
188
+ ckbRawTx ,
189
+ isMainnet ,
190
+ witnessLockPlaceholderSize ?? estimateWitnessSize ( deduplicatedLockArgsList ) ,
191
+ ckbFeeRate ,
192
+ ) ;
193
+ needPaymasterCell = txFeeAdjustedResult . needPaymasterCell ;
194
+ ckbRawTx . outputs = txFeeAdjustedResult . outputs ;
195
+ ckbRawTx . cellDeps = txFeeAdjustedResult . cellDeps ;
196
196
}
197
197
198
198
const virtualTx : RgbppCkbVirtualTx = {
0 commit comments