@@ -34,9 +34,17 @@ function makeTxCommand (): Command {
34
34
. argument ( '<amount>' , 'amount of tokens to stake expressed in TON denom e.g 0.1' )
35
35
. action ( getDelegatePoolTx )
36
36
37
+ const validateIndex = ( value : string ) : '1' | '2' => {
38
+ if ( value !== '1' && value !== '2' ) {
39
+ throw new Error ( 'validator index must be 1 or 2' )
40
+ }
41
+ return value
42
+ }
43
+
37
44
tx . command ( 'unstake-pool' )
38
45
. description ( 'generate a unstake funds to TON pool contract transaction' )
39
46
. argument ( '<amount>' , 'amount of tokens to unstake expressed in TON denom e.g 0.1' )
47
+ . requiredOption ( '-I, --validator-index <value>' , 'validator index to unstake from (1 or 2)' , validateIndex )
40
48
. action ( getUnstakePoolTx )
41
49
42
50
tx . command ( 'delegate-nominator-pool' )
@@ -95,7 +103,7 @@ async function init (
95
103
96
104
async function runTx (
97
105
msgType : string ,
98
- _options : any ,
106
+ options : { validatorIndex : '1' | '2' } | undefined ,
99
107
cmd : Command < [ string ] > | Command < [ string , string ] > | Command < [ string , string , string ] > | Command < [ ] > ,
100
108
arg : string [ ]
101
109
) : Promise < void > {
@@ -132,10 +140,20 @@ async function runTx (
132
140
tonStaker = new TonPoolStaker ( { ...networkConfig } )
133
141
await tonStaker . init ( )
134
142
143
+ if ( ! config . validatorAddress2 ) {
144
+ cmd . error ( 'second validator address is required for TON Pool' , { exitCode : 1 , code : `${ msgType } .tx.abort` } )
145
+ }
146
+ const validatorAddressPair : [ string , string ] = [ config . validatorAddress , config . validatorAddress2 ]
147
+
148
+ const validatorToDelegate = await tonStaker . getPoolAddressForStake ( { validatorAddressPair } )
149
+ const validatorIndex = validatorToDelegate === config . validatorAddress ? 1 : 2
150
+
151
+ console . log ( 'Delegating to validator #' + validatorIndex + ': ' + validatorToDelegate )
152
+
135
153
unsignedTx = (
136
154
await tonStaker . buildStakeTx ( {
137
155
delegatorAddress : config . delegatorAddress ,
138
- validatorAddress : config . validatorAddress ,
156
+ validatorAddressPair ,
139
157
amount : arg [ 0 ] // amount
140
158
} )
141
159
) . tx
@@ -145,10 +163,25 @@ async function runTx (
145
163
tonStaker = new TonPoolStaker ( { ...networkConfig } )
146
164
await tonStaker . init ( )
147
165
166
+ if ( ! config . validatorAddress2 ) {
167
+ cmd . error ( 'second validator address is required for TON Pool' , { exitCode : 1 , code : `${ msgType } .tx.abort` } )
168
+ }
169
+
170
+ if ( ! options ?. validatorIndex ) {
171
+ cmd . error ( 'validator index is required for TON Pool' , { exitCode : 1 , code : `${ msgType } .tx.abort` } )
172
+ }
173
+ const validatorAddressPair : [ string , string ] = [ config . validatorAddress , config . validatorAddress2 ]
174
+
175
+ const validatorIndex = parseInt ( options . validatorIndex )
176
+
177
+ const validatorAddress = validatorAddressPair [ validatorIndex - 1 ]
178
+
179
+ console . log ( 'Unstaking from validator #' + validatorIndex + ': ' + validatorAddress )
180
+
148
181
unsignedTx = (
149
182
await tonStaker . buildUnstakeTx ( {
150
183
delegatorAddress : config . delegatorAddress ,
151
- validatorAddress : config . validatorAddress ,
184
+ validatorAddress,
152
185
amount : arg [ 0 ] // amount
153
186
} )
154
187
) . tx
@@ -233,7 +266,7 @@ async function runTx (
233
266
}
234
267
}
235
268
} catch ( e : any ) {
236
- cmd . error ( e , { exitCode : 1 , code : msgType + '.tx.sign ' } )
269
+ cmd . error ( e , { exitCode : 1 , code : msgType + '.tx.abort ' } )
237
270
}
238
271
239
272
if ( unsignedTx === undefined ) {
@@ -284,7 +317,11 @@ async function getDelegatePoolTx (amount: string, options: any, cmd: Command<[st
284
317
await runTx ( 'delegate-pool' , options , cmd , [ amount ] )
285
318
}
286
319
287
- async function getUnstakePoolTx ( amount : string , options : any , cmd : Command < [ string ] > ) : Promise < void > {
320
+ async function getUnstakePoolTx (
321
+ amount : string ,
322
+ options : { validatorIndex : '1' | '2' } ,
323
+ cmd : Command < [ string ] >
324
+ ) : Promise < void > {
288
325
await runTx ( 'unstake-pool' , options , cmd , [ amount ] )
289
326
}
290
327
0 commit comments