@@ -16,6 +16,8 @@ import {
1616 VoteWitnessContractParameter ,
1717 FreezeContractDecoded ,
1818 VoteContractDecoded ,
19+ UnfreezeBalanceContractParameter ,
20+ WithdrawExpireUnfreezeContractParameter ,
1921} from './iface' ;
2022import { ContractType , PermissionType , TronResource } from './enum' ;
2123import { AbiCoder , hexConcat } from 'ethers/lib/utils' ;
@@ -177,7 +179,9 @@ export function decodeTransaction(hexString: string): RawData {
177179 | AccountPermissionUpdateContract [ ]
178180 | TriggerSmartContract [ ]
179181 | FreezeBalanceContractParameter [ ]
180- | VoteWitnessContractParameter [ ] ;
182+ | VoteWitnessContractParameter [ ]
183+ | UnfreezeBalanceContractParameter [ ]
184+ | WithdrawExpireUnfreezeContractParameter [ ] ;
181185
182186 let contractType : ContractType ;
183187
@@ -203,6 +207,14 @@ export function decodeTransaction(hexString: string): RawData {
203207 contractType = ContractType . VoteWitness ;
204208 contract = decodeVoteWitnessContract ( rawTransaction . contracts [ 0 ] . parameter . value ) ;
205209 break ;
210+ case 'type.googleapis.com/protocol.WithdrawExpireUnfreezeContract' :
211+ contract = decodeWithdrawExpireUnfreezeContract ( rawTransaction . contracts [ 0 ] . parameter . value ) ;
212+ contractType = ContractType . WithdrawExpireUnfreeze ;
213+ break ;
214+ case 'type.googleapis.com/protocol.UnfreezeBalanceV2Contract' :
215+ contract = decodeUnfreezeBalanceV2Contract ( rawTransaction . contracts [ 0 ] . parameter . value ) ;
216+ contractType = ContractType . UnfreezeBalanceV2 ;
217+ break ;
206218 default :
207219 throw new UtilsError ( 'Unsupported contract type' ) ;
208220 }
@@ -488,6 +500,99 @@ export function decodeVoteWitnessContract(base64: string): VoteWitnessContractPa
488500 } ,
489501 } ,
490502 ] ;
503+ }
504+
505+ /**
506+ * Deserialize the segment of the txHex corresponding with unfreeze balance contract
507+ *
508+ * @param {string } base64 - The base64 encoded contract data
509+ * @returns {UnfreezeBalanceContractParameter[] } - Array containing the decoded unfreeze contract
510+ */
511+ export function decodeUnfreezeBalanceV2Contract ( base64 : string ) : UnfreezeBalanceContractParameter [ ] {
512+ interface UnfreezeContractDecoded {
513+ ownerAddress ?: string ;
514+ resource ?: number ;
515+ unfrozenBalance ?: string | number ;
516+ }
517+
518+ let unfreezeContract : UnfreezeContractDecoded ;
519+ try {
520+ unfreezeContract = protocol . UnfreezeBalanceContract . decode ( Buffer . from ( base64 , 'base64' ) ) . toJSON ( ) ;
521+ } catch ( e ) {
522+ throw new UtilsError ( 'There was an error decoding the unfreeze contract in the transaction.' ) ;
523+ }
524+
525+ if ( ! unfreezeContract . ownerAddress ) {
526+ throw new UtilsError ( 'Owner address does not exist in this unfreeze contract.' ) ;
527+ }
528+
529+ if ( unfreezeContract . resource === undefined ) {
530+ throw new UtilsError ( 'Resource type does not exist in this unfreeze contract.' ) ;
531+ }
532+
533+ if ( unfreezeContract . unfrozenBalance === undefined ) {
534+ throw new UtilsError ( 'Unfreeze balance does not exist in this unfreeze contract.' ) ;
535+ }
536+
537+
538+ // deserialize attributes
539+ const owner_address = getBase58AddressFromByteArray (
540+ getByteArrayFromHexAddress ( Buffer . from ( unfreezeContract . ownerAddress , 'base64' ) . toString ( 'hex' ) )
541+ ) ;
542+
543+ // Convert ResourceCode enum value to string resource name
544+ const resourceValue = unfreezeContract . resource ;
545+ const resourceEnum = resourceValue === protocol . ResourceCode . BANDWIDTH ? TronResource . BANDWIDTH : TronResource . ENERGY ;
546+
547+ return [
548+ {
549+ parameter : {
550+ value : {
551+ resource : resourceEnum ,
552+ unfreeze_balance : Number ( unfreezeContract . unfrozenBalance ) ,
553+ owner_address,
554+ } ,
555+ } ,
556+ } ,
557+ ] ;
558+ }
559+
560+ /**
561+ * Deserialize the segment of the txHex corresponding with withdraw expire unfreeze contract
562+ *
563+ * @param {string } base64 - The base64 encoded contract data
564+ * @returns {WithdrawExpireUnfreezeContractParameter[] } - Array containing the decoded withdraw contract
565+ */
566+ export function decodeWithdrawExpireUnfreezeContract ( base64 : string ) : WithdrawExpireUnfreezeContractParameter [ ] {
567+ interface WithdrawContractDecoded {
568+ ownerAddress ?: string ;
569+ }
570+
571+ let withdrawContract : WithdrawContractDecoded ;
572+ try {
573+ withdrawContract = protocol . WithdrawBalanceContract . decode ( Buffer . from ( base64 , 'base64' ) ) . toJSON ( ) ;
574+ } catch ( e ) {
575+ throw new UtilsError ( 'There was an error decoding the withdraw contract in the transaction.' ) ;
576+ }
577+
578+ if ( ! withdrawContract . ownerAddress ) {
579+ throw new UtilsError ( 'Owner address does not exist in this withdraw contract.' ) ;
580+ }
581+
582+ // deserialize attributes
583+ const owner_address = getBase58AddressFromByteArray (
584+ getByteArrayFromHexAddress ( Buffer . from ( withdrawContract . ownerAddress , 'base64' ) . toString ( 'hex' ) )
585+ ) ;
586+
587+ return [
588+ {
589+ parameter : {
590+ value : {
591+ owner_address,
592+ } ,
593+ } ,
594+ } ,
595+ ] ;
491596}
492597
493598/**
0 commit comments