@@ -781,12 +781,6 @@ export class Sol extends BaseCoin {
781781 let totalFee = new BigNumber ( 0 ) ;
782782 let totalFeeForTokenRecovery = new BigNumber ( 0 ) ;
783783
784- if ( params . durableNonce ) {
785- const durableNonceInfo = await this . getAccountInfo ( params . durableNonce . publicKey ) ;
786- blockhash = durableNonceInfo . blockhash ;
787- authority = durableNonceInfo . authority ;
788- }
789-
790784 // check for possible token recovery, recover the token provide by user
791785 if ( params . tokenContractAddress ) {
792786 const tokenAccounts = await this . getTokenAccountsByOwner ( bs58EncodedPublicKey ) ;
@@ -869,6 +863,10 @@ export class Sol extends BaseCoin {
869863 }
870864
871865 if ( params . durableNonce ) {
866+ const durableNonceInfo = await this . getAccountInfo ( params . durableNonce . publicKey ) ;
867+ blockhash = durableNonceInfo . blockhash ;
868+ authority = durableNonceInfo . authority ;
869+
872870 txBuilder . nonce ( blockhash , {
873871 walletNonceAddress : params . durableNonce . publicKey ,
874872 authWalletAddress : authority ,
@@ -879,13 +877,43 @@ export class Sol extends BaseCoin {
879877 const unsignedTransactionWithoutFee = ( await txBuilder . build ( ) ) as Transaction ;
880878 const serializedMessage = unsignedTransactionWithoutFee . solTransaction . serializeMessage ( ) . toString ( 'base64' ) ;
881879
882- const feePerSignature = await this . getFeeForMessage ( serializedMessage ) ;
883- const baseFee = params . durableNonce ? feePerSignature * 2 : feePerSignature ;
880+ const baseFee = await this . getFeeForMessage ( serializedMessage ) ;
881+ const feePerSignature = params . durableNonce ? baseFee / 2 : baseFee ;
884882 totalFee = totalFee . plus ( new BigNumber ( baseFee ) ) ;
885883 totalFeeForTokenRecovery = totalFeeForTokenRecovery . plus ( new BigNumber ( baseFee ) ) ;
886884 if ( totalFee . gt ( balance ) ) {
887885 throw Error ( 'Did not find address with funds to recover' ) ;
888886 }
887+
888+ if ( params . tokenContractAddress ) {
889+ // Check if there is sufficient native solana to recover tokens
890+ if ( new BigNumber ( balance ) . lt ( totalFeeForTokenRecovery ) ) {
891+ throw Error (
892+ 'Not enough funds to pay for recover tokens fees, have: ' +
893+ balance +
894+ ' need: ' +
895+ totalFeeForTokenRecovery . toString ( )
896+ ) ;
897+ }
898+ txBuilder . fee ( { amount : feePerSignature } ) ;
899+ } else {
900+ const netAmount = new BigNumber ( balance ) . minus ( totalFee ) ;
901+ txBuilder = factory
902+ . getTransferBuilder ( )
903+ . nonce ( blockhash )
904+ . sender ( bs58EncodedPublicKey )
905+ . send ( { address : params . recoveryDestination , amount : netAmount . toString ( ) } )
906+ . feePayer ( bs58EncodedPublicKey )
907+ . fee ( { amount : feePerSignature } ) ;
908+
909+ if ( params . durableNonce ) {
910+ txBuilder . nonce ( blockhash , {
911+ walletNonceAddress : params . durableNonce . publicKey ,
912+ authWalletAddress : authority ,
913+ } ) ;
914+ }
915+ }
916+
889917 if ( ! isUnsignedSweep ) {
890918 // Sign the txn
891919 if ( ! params . userKey ) {
@@ -900,25 +928,6 @@ export class Sol extends BaseCoin {
900928 throw new Error ( 'missing wallet passphrase' ) ;
901929 }
902930
903- if ( params . tokenContractAddress ) {
904- totalFeeForTokenRecovery = totalFeeForTokenRecovery . plus ( new BigNumber ( baseFee ) ) ;
905- // Check if there is sufficient native solana to recover tokens
906- if ( new BigNumber ( balance ) . lt ( totalFeeForTokenRecovery ) ) {
907- throw Error (
908- 'Not enough funds to pay for recover tokens fees, have: ' +
909- balance +
910- ' need: ' +
911- totalFeeForTokenRecovery . toString ( )
912- ) ;
913- }
914- txBuilder . fee ( { amount : feePerSignature } ) ;
915- } else {
916- totalFee = new BigNumber ( baseFee ) ;
917- const netAmount = new BigNumber ( balance ) . minus ( totalFee ) ;
918- txBuilder
919- . send ( { address : params . recoveryDestination , amount : netAmount . toString ( ) } )
920- . fee ( { amount : feePerSignature } ) ;
921- }
922931 // build the transaction with fee
923932 const unsignedTransaction = ( await txBuilder . build ( ) ) as Transaction ;
924933
0 commit comments