@@ -5,24 +5,32 @@ import { BaseCoin as CoinConfig } from '@bitgo/statics';
55import { Transaction as VetTransaction , Secp256k1 , TransactionClause } from '@vechain/sdk-core' ;
66import { Transaction } from './transaction' ;
77import { VetTransactionData } from '../iface' ;
8- import { ClaimRewardsData } from '../types' ;
9- import { CLAIM_BASE_REWARDS_METHOD_ID , CLAIM_STAKING_REWARDS_METHOD_ID } from '../constants' ;
8+ import { CLAIM_STAKING_REWARDS_METHOD_ID } from '../constants' ;
109import utils from '../utils' ;
1110
1211export class ClaimRewardsTransaction extends Transaction {
13- private _claimRewardsData : ClaimRewardsData ;
12+ private _stakingContractAddress : string ;
13+ private _tokenId : string ;
1414
1515 constructor ( _coinConfig : Readonly < CoinConfig > ) {
1616 super ( _coinConfig ) ;
1717 this . _type = TransactionType . StakingClaim ;
1818 }
1919
20- get claimRewardsData ( ) : ClaimRewardsData {
21- return this . _claimRewardsData ;
20+ get stakingContractAddress ( ) : string {
21+ return this . _stakingContractAddress ;
2222 }
2323
24- set claimRewardsData ( data : ClaimRewardsData ) {
25- this . _claimRewardsData = data ;
24+ set stakingContractAddress ( address : string ) {
25+ this . _stakingContractAddress = address ;
26+ }
27+
28+ get tokenId ( ) : string {
29+ return this . _tokenId ;
30+ }
31+
32+ set tokenId ( tokenId : string ) {
33+ this . _tokenId = tokenId ;
2634 }
2735
2836 /** @inheritdoc */
@@ -51,65 +59,37 @@ export class ClaimRewardsTransaction extends Transaction {
5159
5260 /** @inheritdoc */
5361 buildClauses ( ) : void {
54- if ( ! this . _claimRewardsData ) {
55- throw new InvalidTransactionError ( 'Missing claim rewards data ') ;
62+ if ( ! this . stakingContractAddress ) {
63+ throw new Error ( 'Staking contract address is not set ') ;
5664 }
5765
58- const clauses : TransactionClause [ ] = [ ] ;
66+ utils . validateStakingContractAddress ( this . stakingContractAddress , this . _coinConfig ) ;
5967
60- // Add clause for claiming base rewards if requested
61- const shouldClaimBaseRewards = this . claimRewardsData . claimBaseRewards !== false ; // Default true
62- if ( shouldClaimBaseRewards ) {
63- clauses . push ( this . buildClaimBaseRewardsClause ( ) ) ;
68+ if ( this . tokenId === undefined || this . tokenId === null ) {
69+ throw new Error ( 'Token ID is not set' ) ;
6470 }
6571
66- // Add clause for claiming staking rewards if requested
67- const shouldClaimStakingRewards = this . claimRewardsData . claimStakingRewards !== false ; // Default true
68- if ( shouldClaimStakingRewards ) {
69- clauses . push ( this . buildClaimStakingRewardsClause ( ) ) ;
70- }
72+ const data = this . encodeClaimRewardsMethod ( this . tokenId ) ;
73+ this . _transactionData = data ;
7174
72- if ( clauses . length === 0 ) {
73- throw new InvalidTransactionError ( 'At least one type of rewards must be claimed' ) ;
74- }
75-
76- this . clauses = clauses ;
75+ // Create the clause for claim rewards
76+ this . _clauses = [
77+ {
78+ to : this . stakingContractAddress ,
79+ value : '0x0' ,
80+ data : this . _transactionData ,
81+ } ,
82+ ] ;
7783
7884 // Set recipients as empty since claim rewards doesn't send value
7985 this . recipients = [ ] ;
8086 }
8187
82- /**
83- * Build clause for claiming base rewards (claimVetGeneratedVtho)
84- */
85- private buildClaimBaseRewardsClause ( ) : TransactionClause {
86- const methodData = this . encodeClaimRewardsMethod ( CLAIM_BASE_REWARDS_METHOD_ID , this . _claimRewardsData . tokenId ) ;
87-
88- return {
89- to : utils . getDefaultStakingAddress ( this . _coinConfig ) ,
90- value : '0x0' ,
91- data : methodData ,
92- } ;
93- }
94-
95- /**
96- * Build clause for claiming staking rewards (claimRewards)
97- */
98- private buildClaimStakingRewardsClause ( ) : TransactionClause {
99- const methodData = this . encodeClaimRewardsMethod ( CLAIM_STAKING_REWARDS_METHOD_ID , this . _claimRewardsData . tokenId ) ;
100-
101- return {
102- to : utils . getDefaultDelegationAddress ( this . _coinConfig ) ,
103- value : '0x0' ,
104- data : methodData ,
105- } ;
106- }
107-
10888 /**
10989 * Encode the claim rewards method call data
11090 */
111- private encodeClaimRewardsMethod ( methodId : string , tokenId : string ) : string {
112- const methodName = methodId === CLAIM_BASE_REWARDS_METHOD_ID ? 'claimVetGeneratedVtho' : 'claimRewards' ;
91+ private encodeClaimRewardsMethod ( tokenId : string ) : string {
92+ const methodName = 'claimRewards' ;
11393 const types = [ 'uint256' ] ;
11494 const params = [ tokenId ] ;
11595
@@ -133,7 +113,8 @@ export class ClaimRewardsTransaction extends Transaction {
133113 sender : this . sender ,
134114 feePayer : this . feePayerAddress ,
135115 recipients : this . recipients ,
136- claimRewardsData : this . _claimRewardsData ,
116+ tokenId : this . tokenId ,
117+ stakingContractAddress : this . stakingContractAddress ,
137118 } ;
138119 return json ;
139120 }
@@ -159,8 +140,14 @@ export class ClaimRewardsTransaction extends Transaction {
159140 this . dependsOn = body . dependsOn || null ;
160141 this . nonce = String ( body . nonce ) ;
161142
162- // Parse claim rewards data from clauses
163- this . parseClaimRewardsDataFromClauses ( body . clauses ) ;
143+ if ( body . clauses . length === 1 ) {
144+ const clause = body . clauses [ 0 ] ;
145+ if ( clause . data && clause . data . startsWith ( CLAIM_STAKING_REWARDS_METHOD_ID ) ) {
146+ // claimRewards should go to STARGATE_DELEGATION_ADDRESS
147+ this . tokenId = utils . decodeClaimRewardsData ( clause . data ) ;
148+ this . stakingContractAddress = clause . to || '0x0' ;
149+ }
150+ }
164151
165152 // Set recipients as empty for claim rewards
166153 this . recipients = [ ] ;
@@ -185,59 +172,4 @@ export class ClaimRewardsTransaction extends Transaction {
185172 throw new InvalidTransactionError ( `Failed to deserialize transaction: ${ e . message } ` ) ;
186173 }
187174 }
188-
189- /**
190- * Parse claim rewards data from transaction clauses
191- */
192- private parseClaimRewardsDataFromClauses ( clauses : TransactionClause [ ] ) : void {
193- if ( ! clauses || clauses . length === 0 ) {
194- throw new InvalidTransactionError ( 'No clauses found in transaction' ) ;
195- }
196-
197- let claimBaseRewards = false ;
198- let claimStakingRewards = false ;
199- let tokenId = '' ;
200- let delegationContractAddress = '' ;
201- let stargateNftAddress = '' ;
202-
203- for ( const clause of clauses ) {
204- if ( clause . data ) {
205- if ( clause . data . startsWith ( CLAIM_BASE_REWARDS_METHOD_ID ) ) {
206- // claimVetGeneratedVtho should go to STARGATE_NFT_ADDRESS
207- claimBaseRewards = true ;
208- if ( ! tokenId ) {
209- tokenId = utils . decodeClaimRewardsData ( clause . data ) ;
210- }
211- if ( ! stargateNftAddress && clause . to ) {
212- stargateNftAddress = clause . to ;
213- }
214- } else if ( clause . data . startsWith ( CLAIM_STAKING_REWARDS_METHOD_ID ) ) {
215- // claimRewards should go to STARGATE_DELEGATION_ADDRESS
216- claimStakingRewards = true ;
217- if ( ! tokenId ) {
218- tokenId = utils . decodeClaimRewardsData ( clause . data ) ;
219- }
220- if ( ! delegationContractAddress && clause . to ) {
221- delegationContractAddress = clause . to ;
222- }
223- }
224- }
225- }
226-
227- if ( ! claimBaseRewards && ! claimStakingRewards ) {
228- throw new InvalidTransactionError ( 'Transaction does not contain claim rewards clauses' ) ;
229- }
230-
231- this . _claimRewardsData = {
232- tokenId,
233- delegationContractAddress :
234- delegationContractAddress && ! utils . isDelegationContractAddress ( delegationContractAddress )
235- ? delegationContractAddress
236- : undefined ,
237- stargateNftAddress :
238- stargateNftAddress && ! utils . isNftContractAddress ( stargateNftAddress ) ? stargateNftAddress : undefined ,
239- claimBaseRewards,
240- claimStakingRewards,
241- } ;
242- }
243175}
0 commit comments