@@ -16,11 +16,12 @@ import {
16
16
import { stxToMicroStx } from '../helpers' ;
17
17
import {
18
18
AnchorMode ,
19
+ StacksPrivateKey ,
19
20
bufferCV ,
20
21
makeContractCall ,
22
+ makeRandomPrivKey ,
21
23
makeSTXTokenTransfer ,
22
24
noneCV ,
23
- randomBytes ,
24
25
someCV ,
25
26
standardPrincipalCV ,
26
27
uintCV ,
@@ -35,6 +36,9 @@ import {
35
36
} from 'stacks-encoding-native-js' ;
36
37
import { AddressStxBalanceResponse } from '@stacks/stacks-blockchain-api-types' ;
37
38
import * as assert from 'assert' ;
39
+ import { hexToBytes } from '@stacks/common' ;
40
+ import { StackingClient } from '@stacks/stacking' ;
41
+ import { getPublicKeyFromPrivate } from '@stacks/encryption' ;
38
42
39
43
describe ( 'PoX-4 - Delegate aggregation increase operations' , ( ) => {
40
44
const seedKey = testnetKeys [ 4 ] . secretKey ;
@@ -51,12 +55,20 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
51
55
52
56
let poxCycleAddressIndex : bigint ;
53
57
58
+ let stackingClient : StackingClient ;
59
+ let signerPrivKey : StacksPrivateKey ;
60
+ let signerPubKey : string ;
61
+
54
62
beforeAll ( ( ) => {
55
63
seedAccount = accountFromKey ( seedKey ) ;
56
64
// delegatorKey = ECPair.makeRandom({ compressed: true }).privateKey!.toString('hex');
57
65
// delegateeKey = ECPair.makeRandom({ compressed: true }).privateKey!.toString('hex');
58
66
delegatorAccount = accountFromKey ( delegatorKey ) ;
59
67
delegateeAccount = accountFromKey ( delegateeKey ) ;
68
+
69
+ stackingClient = new StackingClient ( delegatorAccount . stxAddr , testEnv . stacksNetwork ) ;
70
+ signerPrivKey = makeRandomPrivKey ( ) ;
71
+ signerPubKey = getPublicKeyFromPrivate ( signerPrivKey . data ) ;
60
72
} ) ;
61
73
62
74
test ( 'Import testing accounts to bitcoind' , async ( ) => {
@@ -116,7 +128,8 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
116
128
} ) ;
117
129
118
130
test ( 'Get pox-info' , async ( ) => {
119
- // wait until the start of the next cycle so we have enough blocks within the cycle to perform the various txs
131
+ // wait until the start of the next cycle so we have enough blocks within the cycle to perform
132
+ // the various txs
120
133
poxInfo = await standByForNextPoxCycle ( ) ;
121
134
[ contractAddress , contractName ] = poxInfo . contract_id . split ( '.' ) ;
122
135
expect ( contractName ) . toBe ( 'pox-4' ) ;
@@ -175,6 +188,7 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
175
188
let amountDelegated : bigint ;
176
189
let amountStackedInitial : bigint ;
177
190
test ( 'Perform delegate-stack-stx operation' , async ( ) => {
191
+ await standByForPoxCycle ( ) ;
178
192
// get amount delegated
179
193
const getDelegationInfo1 = await readOnlyFnCall <
180
194
ClarityValueTuple < { 'amount-ustx' : ClarityValueUInt } >
@@ -190,7 +204,7 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
190
204
const poxInfo2 = await testEnv . client . getPox ( ) ;
191
205
const startBurnHt = poxInfo2 . current_burnchain_block_height as number ;
192
206
193
- amountStackedInitial = amountDelegated - 2000n ;
207
+ amountStackedInitial = amountDelegated - 20000n ;
194
208
195
209
const txFee = 10000n ;
196
210
const delegateStackStxTx = await makeContractCall ( {
@@ -203,7 +217,7 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
203
217
uintCV ( amountStackedInitial ) , // amount-ustx
204
218
delegateeAccount . poxAddrClar , // pox-addr
205
219
uintCV ( startBurnHt ) , // start-burn-ht
206
- uintCV ( 1 ) , // lock-period,
220
+ uintCV ( 6 ) , // lock-period,
207
221
] ,
208
222
network : testEnv . stacksNetwork ,
209
223
anchorMode : AnchorMode . OnChainOnly ,
@@ -236,7 +250,7 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
236
250
) ;
237
251
expect ( res . results [ 0 ] . data ) . toEqual (
238
252
expect . objectContaining ( {
239
- lock_period : '1 ' ,
253
+ lock_period : '6 ' ,
240
254
lock_amount : amountStackedInitial . toString ( ) ,
241
255
} )
242
256
) ;
@@ -250,8 +264,20 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
250
264
} ) ;
251
265
252
266
test ( 'Perform stack-aggregation-commit-indexed - delegator commit to stacking operation' , async ( ) => {
267
+ await standByForPoxCycle ( ) ;
253
268
const poxInfo2 = await testEnv . client . getPox ( ) ;
254
269
const rewardCycle = BigInt ( poxInfo2 . next_cycle . id ) ;
270
+ const signerSig = hexToBytes (
271
+ stackingClient . signPoxSignature ( {
272
+ topic : 'agg-commit' ,
273
+ poxAddress : delegateeAccount . btcAddr ,
274
+ rewardCycle : Number ( rewardCycle ) ,
275
+ period : 1 ,
276
+ signerPrivateKey : signerPrivKey ,
277
+ maxAmount : amountStackedInitial ,
278
+ authId : 0 ,
279
+ } )
280
+ ) ;
255
281
const stackAggrCommitTx = await makeContractCall ( {
256
282
senderKey : delegatorAccount . secretKey ,
257
283
contractAddress,
@@ -260,7 +286,10 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
260
286
functionArgs : [
261
287
delegateeAccount . poxAddrClar , // pox-addr
262
288
uintCV ( rewardCycle ) , // reward-cycle
263
- bufferCV ( randomBytes ( 33 ) ) , // signer-key
289
+ someCV ( bufferCV ( signerSig ) ) , // signer-sig
290
+ bufferCV ( hexToBytes ( signerPubKey ) ) , // signer-key
291
+ uintCV ( amountStackedInitial . toString ( ) ) , // max-amount
292
+ uintCV ( 0 ) , // auth-id
264
293
] ,
265
294
network : testEnv . stacksNetwork ,
266
295
anchorMode : AnchorMode . OnChainOnly ,
@@ -319,29 +348,6 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
319
348
Buffer . from ( delegateStackIncreaseTx . serialize ( ) )
320
349
) ;
321
350
322
- // then commit to increased amount with call to `stack-aggregation-increase`
323
- const poxInfo2 = await testEnv . client . getPox ( ) ;
324
- const rewardCycle = BigInt ( poxInfo2 . next_cycle . id ) ;
325
- const stackAggrIncreaseTx = await makeContractCall ( {
326
- senderKey : delegatorAccount . secretKey ,
327
- contractAddress,
328
- contractName,
329
- functionName : 'stack-aggregation-increase' ,
330
- functionArgs : [
331
- delegateeAccount . poxAddrClar , // pox-addr
332
- uintCV ( rewardCycle ) , // reward-cycle
333
- uintCV ( poxCycleAddressIndex ) , // reward-cycle-index
334
- ] ,
335
- network : testEnv . stacksNetwork ,
336
- anchorMode : AnchorMode . OnChainOnly ,
337
- fee : txFee ,
338
- validateWithAbi : false ,
339
- nonce : delegateStackIncreaseTx . auth . spendingCondition . nonce + 1n ,
340
- } ) ;
341
- const { txId : stackAggrIncreaseTxId } = await testEnv . client . sendTransaction (
342
- Buffer . from ( stackAggrIncreaseTx . serialize ( ) )
343
- ) ;
344
-
345
351
const delegateStackIncreaseDbTx = await standByForTxSuccess ( delegateStackIncreaseTxId ) ;
346
352
const delegateStackIncreaseResult = decodeClarityValue <
347
353
ClarityValueResponseOk <
@@ -392,6 +398,45 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
392
398
} )
393
399
) ;
394
400
401
+ // then commit to increased amount with call to `stack-aggregation-increase`
402
+ const poxInfo2 = await testEnv . client . getPox ( ) ;
403
+ const maxAmount = amountStackedInitial + stxToDelegateIncrease ;
404
+ const rewardCycle = BigInt ( poxInfo2 . next_cycle . id ) ;
405
+ const signerSig = hexToBytes (
406
+ stackingClient . signPoxSignature ( {
407
+ topic : 'agg-increase' ,
408
+ poxAddress : delegateeAccount . btcAddr ,
409
+ rewardCycle : Number ( rewardCycle ) ,
410
+ period : 1 ,
411
+ signerPrivateKey : signerPrivKey ,
412
+ maxAmount : maxAmount ,
413
+ authId : 1 ,
414
+ } )
415
+ ) ;
416
+ const stackAggrIncreaseTx = await makeContractCall ( {
417
+ senderKey : delegatorAccount . secretKey ,
418
+ contractAddress,
419
+ contractName,
420
+ functionName : 'stack-aggregation-increase' ,
421
+ functionArgs : [
422
+ delegateeAccount . poxAddrClar , // pox-addr
423
+ uintCV ( rewardCycle ) , // reward-cycle
424
+ uintCV ( poxCycleAddressIndex ) , // reward-cycle-index
425
+ someCV ( bufferCV ( signerSig ) ) , // signer-sig
426
+ bufferCV ( hexToBytes ( signerPubKey ) ) , // signer-key
427
+ uintCV ( maxAmount . toString ( ) ) , // max-amount
428
+ uintCV ( 1 ) , // auth-id
429
+ ] ,
430
+ network : testEnv . stacksNetwork ,
431
+ anchorMode : AnchorMode . OnChainOnly ,
432
+ fee : txFee ,
433
+ validateWithAbi : false ,
434
+ nonce : delegateStackIncreaseTx . auth . spendingCondition . nonce + 1n ,
435
+ } ) ;
436
+ const { txId : stackAggrIncreaseTxId } = await testEnv . client . sendTransaction (
437
+ Buffer . from ( stackAggrIncreaseTx . serialize ( ) )
438
+ ) ;
439
+
395
440
// validate API endpoint balance state for account
396
441
const apiBalance = await fetchGet < AddressStxBalanceResponse > (
397
442
`/extended/v1/address/${ delegateeAccount . stxAddr } /stx`
@@ -421,7 +466,7 @@ describe('PoX-4 - Delegate aggregation increase operations', () => {
421
466
} ) ;
422
467
423
468
test ( 'Wait for current pox cycle to complete' , async ( ) => {
424
- const poxStatus1 = await standByForPoxCycleEnd ( ) ;
469
+ const poxStatus1 = await standByForPoxCycle ( ) ;
425
470
const poxStatus2 = await standByForPoxCycle ( ) ;
426
471
console . log ( '___Wait for current pox cycle to complete___' , {
427
472
pox1 : { height : poxStatus1 . current_burnchain_block_height , ...poxStatus1 . next_cycle } ,
0 commit comments