@@ -13,14 +13,9 @@ import {
1313 pvm ,
1414} from '@flarenetwork/flarejs' ;
1515import utils from './utils' ;
16- import { DecodedUtxoObj , SECP256K1_Transfer_Output , FlareTransactionType , Tx , Context } from './iface' ;
17- import { FlrpFeeState } from '@bitgo/public-types' ;
16+ import { DecodedUtxoObj , SECP256K1_Transfer_Output , FlareTransactionType , Tx } from './iface' ;
1817
1918export class ExportInPTxBuilder extends AtomicTransactionBuilder {
20- private _amount : bigint ;
21- private _feeState : FlrpFeeState ;
22- private _context : Context ;
23-
2419 constructor ( _coinConfig : Readonly < CoinConfig > ) {
2520 super ( _coinConfig ) ;
2621 // For Export FROM P-chain:
@@ -37,41 +32,6 @@ export class ExportInPTxBuilder extends AtomicTransactionBuilder {
3732 return TransactionType . Export ;
3833 }
3934
40- /**
41- * Amount is a bigint that specifies the quantity of the asset that this output owns. Must be positive.
42- * @param {bigint | string } amount The withdrawal amount
43- */
44- amount ( value : bigint | string ) : this {
45- const valueBigInt = typeof value === 'string' ? BigInt ( value ) : value ;
46- this . validateAmount ( valueBigInt ) ;
47- this . _amount = valueBigInt ;
48- return this ;
49- }
50-
51- feeState ( state : FlrpFeeState ) : this {
52- this . _feeState = state ;
53- return this ;
54- }
55-
56- context ( context : Context ) : this {
57- this . _context = context ;
58- return this ;
59- }
60-
61- /**
62- * Sets the UTXOs for the transaction from hex strings
63- * @param utxoHexStrings - Array of UTXO hex strings from getUTXOs API
64- */
65- utxos ( utxoHexStrings : string [ ] ) : this {
66- if ( ! utxoHexStrings || utxoHexStrings . length === 0 ) {
67- throw new BuildTransactionError ( 'UTXOs array cannot be empty' ) ;
68- }
69- this . transaction . _utxoHexStrings = utxoHexStrings ;
70- const nativeUtxos = utils . parseUtxoHexArray ( utxoHexStrings ) ;
71- this . transaction . _utxos = utils . utxosToDecoded ( nativeUtxos , this . transaction . _network ) ;
72- return this ;
73- }
74-
7535 initBuilder ( tx : Tx , rawBytes ?: Buffer , parsedCredentials ?: Credential [ ] ) : this {
7636 const exportTx = tx as pvmSerial . ExportTx ;
7737
@@ -95,15 +55,15 @@ export class ExportInPTxBuilder extends AtomicTransactionBuilder {
9555 this . transaction . _threshold = outputOwners . threshold . value ( ) ;
9656 this . transaction . _fromAddresses = outputOwners . addrs . map ( ( addr ) => Buffer . from ( addr . toBytes ( ) ) ) ;
9757 this . _externalChainId = Buffer . from ( exportTx . destination . toBytes ( ) ) ;
98- this . _amount = outputTransfer . amount ( ) ;
58+ this . transaction . _amount = outputTransfer . amount ( ) ;
9959 this . transaction . _utxos = this . recoverUtxos ( [ ...exportTx . baseTx . inputs ] ) ;
10060
10161 const totalInputAmount = exportTx . baseTx . inputs . reduce ( ( sum , input ) => sum + input . amount ( ) , BigInt ( 0 ) ) ;
10262 const changeOutputAmount = exportTx . baseTx . outputs . reduce ( ( sum , out ) => {
10363 const transferOut = out . output as TransferOutput ;
10464 return sum + transferOut . amount ( ) ;
10565 } , BigInt ( 0 ) ) ;
106- const fee = totalInputAmount - changeOutputAmount - this . _amount ;
66+ const fee = totalInputAmount - changeOutputAmount - this . transaction . _amount ;
10767 this . transaction . _fee . fee = fee . toString ( ) ;
10868
10969 const credentials = parsedCredentials || [ ] ;
@@ -157,25 +117,35 @@ export class ExportInPTxBuilder extends AtomicTransactionBuilder {
157117 protected async buildFlareTransaction ( ) : Promise < void > {
158118 if ( this . transaction . hasCredentials ) return ;
159119
160- const feeState = this . _feeState ;
120+ const feeState = this . transaction . _feeState ;
161121 if ( ! feeState ) {
162122 throw new BuildTransactionError ( 'Fee state is required' ) ;
163123 }
164- if ( ! this . _context ) {
124+ if ( ! this . transaction . _context ) {
165125 throw new BuildTransactionError ( 'context is required' ) ;
166126 }
167- if ( this . _amount === undefined ) {
127+ if ( this . transaction . _amount === undefined ) {
168128 throw new BuildTransactionError ( 'amount is required' ) ;
169129 }
170130
171131 const nativeUtxos = utils . parseUtxoHexArray ( this . transaction . _utxoHexStrings ) ;
172132
133+ const totalUtxoAmount = nativeUtxos . reduce ( ( sum , utxo ) => {
134+ const output = utxo . output as TransferOutput ;
135+ return sum + output . amount ( ) ;
136+ } , BigInt ( 0 ) ) ;
137+
138+ if ( totalUtxoAmount < this . transaction . _amount ) {
139+ throw new BuildTransactionError (
140+ `Insufficient UTXO balance: have ${ totalUtxoAmount . toString ( ) } nFLR, need at least ${ this . transaction . _amount . toString ( ) } nFLR (plus fee)`
141+ ) ;
142+ }
143+
173144 const assetId = utils . flareIdString ( this . transaction . _assetId ) . toString ( ) ;
174- `` ;
175145 const fromAddresses = this . transaction . _fromAddresses . map ( ( addr ) => Buffer . from ( addr ) ) ;
176146 const transferableOutput = TransferableOutput . fromNative (
177147 assetId ,
178- this . _amount ,
148+ this . transaction . _amount ,
179149 fromAddresses ,
180150 this . transaction . _locktime ,
181151 this . transaction . _threshold
@@ -189,7 +159,7 @@ export class ExportInPTxBuilder extends AtomicTransactionBuilder {
189159 outputs : [ transferableOutput ] ,
190160 utxos : nativeUtxos ,
191161 } ,
192- this . _context
162+ this . transaction . _context
193163 ) ;
194164
195165 this . transaction . setTransaction ( exportTx ) ;
0 commit comments