@@ -33,10 +33,9 @@ class TinymanSTAlgoClient extends TinymanBaseClient {
3333 const suggestedParams = await this . getSuggestedParams ( ) ;
3434 const userStateBoxName = this . getUserStateBoxName ( userAddress ) ;
3535
36- let newBox : Record < string , Struct > = { } ;
37-
3836 const txns = [
3937 ...( await this . getApplyRateChangeTxnIfNeeded ( ) ) ,
38+ ...( await this . getUserBoxPaymentTxnIfNeeded ( userAddress , suggestedParams ) ) ,
4039 ...( await this . getOptinTxnIfNeeded ( userAddress , STALGO_ASSET_ID [ this . network ] ) ) ,
4140 algosdk . makeAssetTransferTxnWithSuggestedParamsFromObject ( {
4241 from : userAddress ,
@@ -59,25 +58,12 @@ class TinymanSTAlgoClient extends TinymanBaseClient {
5958 } )
6059 ] ;
6160
62- if ( ! ( await this . boxExists ( userStateBoxName ) ) ) {
63- newBox = {
64- [ fromByteArray ( userStateBoxName ) ] : USER_STATE
65- } ;
66-
67- const boxPaymentTxn = algosdk . makePaymentTxnWithSuggestedParamsFromObject ( {
68- from : userAddress ,
69- to : this . applicationAddress ,
70- suggestedParams,
71- amount : this . calculateMinBalance ( { boxes : newBox } )
72- } ) ;
73-
74- txns . splice ( 1 , 0 , boxPaymentTxn ) ;
75- }
76-
77- return this . setupTxnFeeAndAssignGroupId ( {
78- txns,
79- additionalFeeCount : 2
80- } ) ;
61+ return Promise . all (
62+ this . setupTxnFeeAndAssignGroupId ( {
63+ txns,
64+ additionalFeeCount : 2
65+ } )
66+ ) ;
8167 }
8268
8369 async decreaseStake ( amount : number , userAddress : string ) {
@@ -97,7 +83,7 @@ class TinymanSTAlgoClient extends TinymanBaseClient {
9783 } )
9884 ] ;
9985
100- return this . setupTxnFeeAndAssignGroupId ( { txns, additionalFeeCount : 2 } ) ;
86+ return Promise . all ( this . setupTxnFeeAndAssignGroupId ( { txns, additionalFeeCount : 2 } ) ) ;
10187 }
10288
10389 async claimRewards ( userAddress : string ) {
@@ -121,7 +107,7 @@ class TinymanSTAlgoClient extends TinymanBaseClient {
121107 } )
122108 ] ;
123109
124- return this . setupTxnFeeAndAssignGroupId ( { txns, additionalFeeCount : 3 } ) ;
110+ return Promise . all ( this . setupTxnFeeAndAssignGroupId ( { txns, additionalFeeCount : 3 } ) ) ;
125111 }
126112
127113 async calculateIncreaseStakeFee ( accountAddress : string ) {
@@ -133,7 +119,7 @@ class TinymanSTAlgoClient extends TinymanBaseClient {
133119 const doesUserBoxExist = await this . boxExists (
134120 this . getUserStateBoxName ( accountAddress )
135121 ) ;
136- const initialTxnCount = 3 ;
122+ const initialTxnCount = 4 ;
137123 const totalTxnCount =
138124 initialTxnCount +
139125 Number ( shouldApplyRateChange ) +
@@ -168,6 +154,38 @@ class TinymanSTAlgoClient extends TinymanBaseClient {
168154 return [ ] ;
169155 }
170156
157+ private async getUserBoxPaymentTxnIfNeeded (
158+ userAddress : string ,
159+ suggestedParams : algosdk . SuggestedParams
160+ ) {
161+ const userStateBoxName = this . getUserStateBoxName ( userAddress ) ;
162+
163+ if ( ! ( await this . boxExists ( userStateBoxName ) ) ) {
164+ return this . getUserBoxPaymentTxn ( userStateBoxName , userAddress , suggestedParams ) ;
165+ }
166+
167+ return [ ] ;
168+ }
169+
170+ private getUserBoxPaymentTxn (
171+ userStateBoxName : Uint8Array ,
172+ userAddress : string ,
173+ suggestedParams : algosdk . SuggestedParams
174+ ) {
175+ const newBox : Record < string , Struct > = {
176+ [ fromByteArray ( userStateBoxName ) ] : USER_STATE
177+ } ;
178+
179+ const boxPaymentTxn = algosdk . makePaymentTxnWithSuggestedParamsFromObject ( {
180+ from : userAddress ,
181+ to : this . applicationAddress ,
182+ suggestedParams,
183+ amount : this . calculateMinBalance ( { boxes : newBox } )
184+ } ) ;
185+
186+ return [ boxPaymentTxn ] ;
187+ }
188+
171189 private async shouldApplyRateChange ( ) {
172190 const now = Math . floor ( Date . now ( ) / SECOND_IN_MS ) ;
173191
0 commit comments