Skip to content

Commit 302a5d8

Browse files
authored
feat: include solo and pooled stackers in signer stacker endpoints (#1987)
* feat: add `pooled_stacker_count` and `solo_stacker_count` to pox signer info endpoints * feat: include solo and pooled stackers in signer stacker endpoint * chore: fix tests * fix: filter exact cycle ids in `stack-aggregation-commit` queries * ci: lint fix * fix: non-inclusive end_cycle_id filtering on stacking events
1 parent 34a8454 commit 302a5d8

File tree

11 files changed

+219
-31
lines changed

11 files changed

+219
-31
lines changed

docs/api/stacking/get-pox-cycle-signer-stackers.example.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
{
77
"pox_address": "15Z2sAvjgVDpcBh4vx9g2XKU8FVHYcXNaj",
88
"stacked_amount": "686251350000000000",
9-
"stacker_address": "STRYYQQ9M8KAF4NS7WNZQYY59X93XEKR31JP64CP"
9+
"stacker_address": "STRYYQQ9M8KAF4NS7WNZQYY59X93XEKR31JP64CP",
10+
"stacker_type": "solo"
1011
}
1112
]
1213
}

docs/api/stacking/get-pox-cycle-signers.example.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,29 @@
99
"stacked_amount": "686251350000000000",
1010
"stacked_amount_percent": 50,
1111
"weight": 5,
12-
"weight_percent": 55.55555555555556
12+
"weight_percent": 55.55555555555556,
13+
"solo_stacker_count": 16,
14+
"pooled_stacker_count": 3615
1315
},
1416
{
1517
"signing_key": "0x029874497a7952483aa23890e9d0898696f33864d3df90939930a1f45421fe3b09",
1618
"signer_address": "STF9B75ADQAVXQHNEQ6KGHXTG7JP305J2GRWF3A2",
1719
"stacked_amount": "457500900000000000",
1820
"stacked_amount_percent": 33.333333333333336,
1921
"weight": 3,
20-
"weight_percent": 33.33333333333333
22+
"weight_percent": 33.33333333333333,
23+
"solo_stacker_count": 0,
24+
"pooled_stacker_count": 1456
2125
},
2226
{
2327
"signing_key": "0x02dcde79b38787b72d8e5e0af81cffa802f0a3c8452d6b46e08859165f49a72736",
2428
"signer_address": "ST18MDW2PDTBSCR1ACXYRJP2JX70FWNM6YY2VX4SS",
2529
"stacked_amount": "228750450000000000",
2630
"stacked_amount_percent": 16.666666666666668,
2731
"weight": 1,
28-
"weight_percent": 11.11111111111111
32+
"weight_percent": 11.11111111111111,
33+
"solo_stacker_count": 637,
34+
"pooled_stacker_count": 0
2935
}
3036
]
3137
}

docs/entities/stacking/signer.example.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,7 @@
44
"stacked_amount": "686251350000000000",
55
"stacked_amount_percent": 50,
66
"weight": 5,
7-
"weight_percent": 55.55555555555556
7+
"weight_percent": 55.55555555555556,
8+
"solo_stacker_count": 16,
9+
"pooled_stacker_count": 3615
810
}

docs/entities/stacking/signer.schema.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
"weight",
99
"stacked_amount",
1010
"weight_percent",
11-
"stacked_amount_percent"
11+
"stacked_amount_percent",
12+
"solo_stacker_count",
13+
"pooled_stacker_count"
1214
],
1315
"properties": {
1416
"signing_key": {
@@ -29,6 +31,14 @@
2931
},
3032
"stacked_amount_percent": {
3133
"type": "number"
34+
},
35+
"solo_stacker_count": {
36+
"type": "integer",
37+
"description": "The number of solo stackers associated with this signer."
38+
},
39+
"pooled_stacker_count": {
40+
"type": "integer",
41+
"description": "The number of pooled stackers associated with this signer."
3242
}
3343
}
3444
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"pox_address": "15Z2sAvjgVDpcBh4vx9g2XKU8FVHYcXNaj",
33
"stacked_amount": "686251350000000000",
4-
"stacker_address": "STRYYQQ9M8KAF4NS7WNZQYY59X93XEKR31JP64CP"
4+
"stacker_address": "STRYYQQ9M8KAF4NS7WNZQYY59X93XEKR31JP64CP",
5+
"stacker_type": "solo"
56
}

docs/entities/stacking/stacker.schema.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"required": [
66
"stacker_address",
77
"stacked_amount",
8-
"pox_address"
8+
"pox_address",
9+
"stacker_type"
910
],
1011
"properties": {
1112
"stacker_address": {
@@ -16,6 +17,10 @@
1617
},
1718
"pox_address": {
1819
"type": "string"
20+
},
21+
"stacker_type": {
22+
"type": "string",
23+
"enum": ["solo", "pooled"]
1924
}
2025
}
2126
}

docs/generated.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3372,6 +3372,7 @@ export interface PoxStacker {
33723372
stacker_address: string;
33733373
stacked_amount: string;
33743374
pox_address: string;
3375+
stacker_type: "solo" | "pooled";
33753376
}
33763377
/**
33773378
* GET request that returns signers for a PoX cycle
@@ -3401,6 +3402,14 @@ export interface PoxSigner {
34013402
stacked_amount: string;
34023403
weight_percent: number;
34033404
stacked_amount_percent: number;
3405+
/**
3406+
* The number of solo stackers associated with this signer.
3407+
*/
3408+
solo_stacker_count: number;
3409+
/**
3410+
* The number of pooled stackers associated with this signer.
3411+
*/
3412+
pooled_stacker_count: number;
34043413
}
34053414
/**
34063415
* GET request that returns PoX cycles

src/api/routes/v2/helpers.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ export function parseDbPoxSigner(signer: DbPoxCycleSigner, isMainnet: boolean):
188188
stacked_amount: signer.stacked_amount,
189189
weight_percent: signer.weight_percent,
190190
stacked_amount_percent: signer.stacked_amount_percent,
191+
pooled_stacker_count: signer.pooled_stacker_count,
192+
solo_stacker_count: signer.solo_stacker_count,
191193
};
192194
return result;
193195
}
@@ -197,6 +199,7 @@ export function parseDbPoxSignerStacker(stacker: DbPoxCycleSignerStacker): PoxSt
197199
stacker_address: stacker.stacker,
198200
stacked_amount: stacker.locked,
199201
pox_address: stacker.pox_addr,
202+
stacker_type: stacker.stacker_type,
200203
};
201204
// Special handling for pool operator stackers
202205
if (

src/datastore/common.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,8 +1086,8 @@ export interface DbPoxCycleSigner {
10861086
stacked_amount: string;
10871087
weight_percent: number;
10881088
stacked_amount_percent: number;
1089-
// TODO: Figure this out
1090-
// total_stackers: number;
1089+
pooled_stacker_count: number;
1090+
solo_stacker_count: number;
10911091
}
10921092

10931093
export interface DbPoxCycleSignerStacker {
@@ -1096,6 +1096,7 @@ export interface DbPoxCycleSignerStacker {
10961096
pox_addr: string;
10971097
name: string;
10981098
amount_ustx: string;
1099+
stacker_type: 'solo' | 'pooled';
10991100
}
11001101

11011102
interface ReOrgEntities {

src/datastore/pg-store-v2.ts

Lines changed: 152 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -629,12 +629,52 @@ export class PgStoreV2 extends BasePgStoreModule {
629629
if (cycleCheck.count === 0)
630630
throw new InvalidRequestError(`PoX cycle not found`, InvalidRequestErrorType.invalid_param);
631631
const results = await sql<(DbPoxCycleSigner & { total: number })[]>`
632-
SELECT
633-
signing_key, weight, stacked_amount, weight_percent, stacked_amount_percent,
634-
COUNT(*) OVER()::int AS total
635-
FROM pox_sets
636-
WHERE canonical = TRUE AND cycle_number = ${args.cycle_number}
637-
ORDER BY weight DESC, stacked_amount DESC, signing_key
632+
WITH signer_keys AS (
633+
SELECT DISTINCT ON (stacker) stacker, signer_key
634+
FROM pox4_events
635+
WHERE canonical = true AND microblock_canonical = true
636+
AND name in ('stack-aggregation-commit-indexed', 'stack-aggregation-commit')
637+
AND start_cycle_id = ${args.cycle_number}
638+
AND end_cycle_id = ${args.cycle_number + 1}
639+
ORDER BY stacker, block_height DESC, tx_index DESC, event_index DESC
640+
), delegated_stackers AS (
641+
SELECT DISTINCT ON (main.stacker)
642+
main.stacker,
643+
sk.signer_key
644+
FROM pox4_events main
645+
LEFT JOIN signer_keys sk ON main.delegator = sk.stacker
646+
WHERE main.canonical = true
647+
AND main.microblock_canonical = true
648+
AND main.name IN ('delegate-stack-stx', 'delegate-stack-increase', 'delegate-stack-extend')
649+
AND main.start_cycle_id <= ${args.cycle_number}
650+
AND main.end_cycle_id > ${args.cycle_number}
651+
ORDER BY main.stacker, main.block_height DESC, main.microblock_sequence DESC, main.tx_index DESC, main.event_index DESC
652+
), solo_stackers AS (
653+
SELECT DISTINCT ON (stacker)
654+
stacker,
655+
signer_key
656+
FROM pox4_events
657+
WHERE canonical = true AND microblock_canonical = true
658+
AND name in ('stack-stx', 'stacks-increase', 'stack-extend')
659+
AND start_cycle_id <= ${args.cycle_number}
660+
AND end_cycle_id > ${args.cycle_number}
661+
ORDER BY stacker, block_height DESC, microblock_sequence DESC, tx_index DESC, event_index DESC
662+
)
663+
SELECT
664+
ps.signing_key,
665+
ps.weight,
666+
ps.stacked_amount,
667+
ps.weight_percent,
668+
ps.stacked_amount_percent,
669+
COUNT(DISTINCT ds.stacker)::int AS pooled_stacker_count,
670+
COUNT(DISTINCT ss.stacker)::int AS solo_stacker_count,
671+
COUNT(*) OVER()::int AS total
672+
FROM pox_sets ps
673+
LEFT JOIN delegated_stackers ds ON ps.signing_key = ds.signer_key
674+
LEFT JOIN solo_stackers ss ON ps.signing_key = ss.signer_key
675+
WHERE ps.canonical = TRUE AND ps.cycle_number = ${args.cycle_number}
676+
GROUP BY ps.signing_key, ps.weight, ps.stacked_amount, ps.weight_percent, ps.stacked_amount_percent
677+
ORDER BY ps.weight DESC, ps.stacked_amount DESC, ps.signing_key
638678
OFFSET ${offset}
639679
LIMIT ${limit}
640680
`;
@@ -654,10 +694,52 @@ export class PgStoreV2 extends BasePgStoreModule {
654694
if (cycleCheck.count === 0)
655695
throw new InvalidRequestError(`PoX cycle not found`, InvalidRequestErrorType.invalid_param);
656696
const results = await sql<DbPoxCycleSigner[]>`
657-
SELECT
658-
signing_key, weight, stacked_amount, weight_percent, stacked_amount_percent
659-
FROM pox_sets
660-
WHERE canonical = TRUE AND cycle_number = ${args.cycle_number} AND signing_key = ${args.signer_key}
697+
WITH signer_keys AS (
698+
SELECT DISTINCT ON (stacker) stacker, signer_key
699+
FROM pox4_events
700+
WHERE canonical = true AND microblock_canonical = true
701+
AND name in ('stack-aggregation-commit-indexed', 'stack-aggregation-commit')
702+
AND start_cycle_id = ${args.cycle_number}
703+
AND end_cycle_id = ${args.cycle_number + 1}
704+
ORDER BY stacker, block_height DESC, tx_index DESC, event_index DESC
705+
), delegated_stackers AS (
706+
SELECT DISTINCT ON (main.stacker)
707+
main.stacker,
708+
sk.signer_key
709+
FROM pox4_events main
710+
LEFT JOIN signer_keys sk ON main.delegator = sk.stacker
711+
WHERE main.canonical = true
712+
AND main.microblock_canonical = true
713+
AND main.name IN ('delegate-stack-stx', 'delegate-stack-increase', 'delegate-stack-extend')
714+
AND main.start_cycle_id <= ${args.cycle_number}
715+
AND main.end_cycle_id > ${args.cycle_number}
716+
ORDER BY main.stacker, main.block_height DESC, main.microblock_sequence DESC, main.tx_index DESC, main.event_index DESC
717+
), solo_stackers AS (
718+
SELECT DISTINCT ON (stacker)
719+
stacker,
720+
signer_key
721+
FROM pox4_events
722+
WHERE canonical = true AND microblock_canonical = true
723+
AND name in ('stack-stx', 'stacks-increase', 'stack-extend')
724+
AND start_cycle_id <= ${args.cycle_number}
725+
AND end_cycle_id > ${args.cycle_number}
726+
ORDER BY stacker, block_height DESC, microblock_sequence DESC, tx_index DESC, event_index DESC
727+
)
728+
SELECT
729+
ps.signing_key,
730+
ps.weight,
731+
ps.stacked_amount,
732+
ps.weight_percent,
733+
ps.stacked_amount_percent,
734+
COUNT(DISTINCT ds.stacker)::int AS pooled_stacker_count,
735+
COUNT(DISTINCT ss.stacker)::int AS solo_stacker_count
736+
FROM pox_sets ps
737+
LEFT JOIN delegated_stackers ds ON ps.signing_key = ds.signer_key
738+
LEFT JOIN solo_stackers ss ON ps.signing_key = ss.signer_key
739+
WHERE ps.canonical = TRUE
740+
AND ps.cycle_number = ${args.cycle_number}
741+
AND ps.signing_key = ${args.signer_key}
742+
GROUP BY ps.signing_key, ps.weight, ps.stacked_amount, ps.weight_percent, ps.stacked_amount_percent
661743
LIMIT 1
662744
`;
663745
if (results.count > 0) return results[0];
@@ -687,19 +769,68 @@ export class PgStoreV2 extends BasePgStoreModule {
687769
InvalidRequestErrorType.invalid_param
688770
);
689771
const results = await sql<(DbPoxCycleSignerStacker & { total: number })[]>`
690-
WITH stackers AS (
691-
SELECT DISTINCT ON (stacker) stacker, locked, pox_addr, amount_ustx, name
692-
FROM pox4_events
693-
WHERE canonical = true
694-
AND microblock_canonical = true
695-
AND start_cycle_id <= ${args.cycle_number}
696-
AND (end_cycle_id >= ${args.cycle_number} OR end_cycle_id IS NULL)
697-
AND signer_key = ${args.signer_key}
698-
ORDER BY stacker, block_height DESC, tx_index DESC, event_index DESC
772+
WITH signer_keys AS (
773+
SELECT DISTINCT ON (stacker) stacker, signer_key
774+
FROM pox4_events
775+
WHERE canonical = true AND microblock_canonical = true
776+
AND name in ('stack-aggregation-commit-indexed', 'stack-aggregation-commit')
777+
AND start_cycle_id = ${args.cycle_number}
778+
AND end_cycle_id = ${args.cycle_number + 1}
779+
ORDER BY stacker, block_height DESC, tx_index DESC, event_index DESC
780+
), delegated_stackers AS (
781+
SELECT DISTINCT ON (main.stacker)
782+
main.stacker,
783+
sk.signer_key,
784+
main.locked,
785+
main.pox_addr,
786+
main.name,
787+
main.amount_ustx,
788+
'pooled' as stacker_type
789+
FROM pox4_events main
790+
LEFT JOIN signer_keys sk ON main.delegator = sk.stacker
791+
WHERE main.canonical = true
792+
AND main.microblock_canonical = true
793+
AND main.name IN ('delegate-stack-stx', 'delegate-stack-increase', 'delegate-stack-extend')
794+
AND main.start_cycle_id <= ${args.cycle_number}
795+
AND main.end_cycle_id > ${args.cycle_number}
796+
ORDER BY main.stacker, main.block_height DESC, main.microblock_sequence DESC, main.tx_index DESC, main.event_index DESC
797+
), solo_stackers AS (
798+
SELECT DISTINCT ON (stacker)
799+
stacker,
800+
signer_key,
801+
locked,
802+
pox_addr,
803+
name,
804+
amount_ustx,
805+
'solo' as stacker_type
806+
FROM pox4_events
807+
WHERE canonical = true AND microblock_canonical = true
808+
AND name in ('stack-stx', 'stacks-increase', 'stack-extend')
809+
AND start_cycle_id <= ${args.cycle_number}
810+
AND end_cycle_id > ${args.cycle_number}
811+
ORDER BY stacker, block_height DESC, microblock_sequence DESC, tx_index DESC, event_index DESC
812+
), combined_stackers AS (
813+
SELECT * FROM delegated_stackers
814+
UNION ALL
815+
SELECT * FROM solo_stackers
699816
)
700-
SELECT *, COUNT(*) OVER()::int AS total FROM stackers
701-
OFFSET ${offset}
817+
SELECT
818+
ps.signing_key,
819+
cs.stacker,
820+
cs.locked,
821+
cs.pox_addr,
822+
cs.name,
823+
cs.amount_ustx,
824+
cs.stacker_type,
825+
COUNT(*) OVER()::int AS total
826+
FROM pox_sets ps
827+
LEFT JOIN combined_stackers cs ON ps.signing_key = cs.signer_key
828+
WHERE ps.canonical = TRUE
829+
AND ps.cycle_number = ${args.cycle_number}
830+
AND ps.signing_key = ${args.signer_key}
831+
ORDER BY locked DESC
702832
LIMIT ${limit}
833+
OFFSET ${offset}
703834
`;
704835
return {
705836
limit,

0 commit comments

Comments
 (0)