Skip to content

Commit 711ce23

Browse files
authored
price setting script: divide expected prices by number of required nodes aka min threshold (#71)
* divide expected prices by number of required nodes aka min threshold * clean up script and redundant vars
1 parent 1adc418 commit 711ce23

File tree

1 file changed

+64
-16
lines changed

1 file changed

+64
-16
lines changed

blockchain/contracts/scripts/generatePriceSettingTransactions.ts

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ const PRICE_MAP: PriceMap = {
7272
// ENUMS AND CONSTANTS
7373
// ============================================================================
7474

75+
/**
76+
* Most PriceFeed products are priced *per node*, and users typically must talk to at least
77+
* a threshold number of nodes. We treat the USD values in PRICE_MAP as the *expected total*
78+
* cost to the user, and divide by this expected threshold when setting on-chain per-node prices.
79+
*
80+
* PKP minting is global (not per-node) and is NOT divided by this value.
81+
*/
82+
const EXPECTED_THRESHOLD_NODES = 5;
83+
7584
enum ProductId {
7685
PkpSign = 0,
7786
EncSign = 1,
@@ -327,6 +336,22 @@ async function main() {
327336
return priceWei;
328337
};
329338

339+
// Helper: convert "expected total user cost" (USD) into a per-node USD price for on-chain setting.
340+
const totalUsdToPerNodeUsd = (totalUsd: number): number => {
341+
if (!Number.isFinite(totalUsd) || totalUsd < 0) {
342+
throw new Error(`Invalid USD price: ${totalUsd}`);
343+
}
344+
if (
345+
!Number.isInteger(EXPECTED_THRESHOLD_NODES) ||
346+
EXPECTED_THRESHOLD_NODES <= 0
347+
) {
348+
throw new Error(
349+
`EXPECTED_THRESHOLD_NODES must be a positive integer; got ${EXPECTED_THRESHOLD_NODES}`
350+
);
351+
}
352+
return totalUsd / EXPECTED_THRESHOLD_NODES;
353+
};
354+
330355
// Get contract instances
331356
const pkpNft = await ethers.getContractAt('PKPNFTDiamond', pkpNftAddress);
332357
const priceFeed = await ethers.getContractAt(
@@ -341,8 +366,9 @@ async function main() {
341366
data: string;
342367
operation: number;
343368
description: string;
344-
priceUSD: number;
345-
priceLITKEY: string;
369+
priceUSD: number; // Per-node USD price
370+
priceUSDTotal?: number; // Total USD price across all nodes
371+
priceLITKEY: string; // Per-node LITKEY price
346372
}> = [];
347373

348374
// ============================================================================
@@ -389,28 +415,41 @@ async function main() {
389415
PRICE_MAP.basePrices.signSessionKey,
390416
];
391417

392-
const basePriceWeis = basePrices.map((usdPrice) =>
393-
adjustPriceForEnv(usdToLitKeyWei(usdPrice, litKeyPriceUSD))
418+
const basePricesPerNodeUsd = basePrices.map((usdTotal) =>
419+
totalUsdToPerNodeUsd(usdTotal)
420+
);
421+
const basePriceWeis = basePricesPerNodeUsd.map((usdPerNode) =>
422+
adjustPriceForEnv(usdToLitKeyWei(usdPerNode, litKeyPriceUSD))
394423
);
395424

396425
for (let i = 0; i < productIds.length; i++) {
397426
const productId = productIds[i];
398-
const usdPrice = basePrices[i];
427+
const usdPriceTotal = basePrices[i];
428+
const usdPricePerNode = basePricesPerNodeUsd[i];
399429
const priceWei = basePriceWeis[i];
400430
const priceTokens = parseFloat(ethers.formatUnits(priceWei, 18));
401431
const maxPriceWei = priceWei * 10n; // Max price is 10x base price
402432
const maxPriceTokens = parseFloat(ethers.formatUnits(maxPriceWei, 18));
403-
const maxUsdPrice = usdPrice * 10;
433+
const maxUsdPricePerNode = usdPricePerNode * 10;
434+
const maxUsdPriceTotal = usdPriceTotal * 10;
404435

405436
console.log(
406-
`${PRODUCT_NAMES[productId]} Base: ${priceTokens.toFixed(
437+
`${PRODUCT_NAMES[productId]} Base (per-node): ${priceTokens.toFixed(
407438
6
408-
)} LITKEY ($${usdPrice.toFixed(4)} USD)`
439+
)} LITKEY ($${usdPricePerNode.toFixed(
440+
4
441+
)} USD per node; $${usdPriceTotal.toFixed(
442+
4
443+
)} total @ ${EXPECTED_THRESHOLD_NODES} nodes)`
409444
);
410445
console.log(
411-
`${PRODUCT_NAMES[productId]} Max: ${maxPriceTokens.toFixed(
446+
`${PRODUCT_NAMES[productId]} Max (per-node): ${maxPriceTokens.toFixed(
412447
6
413-
)} LITKEY ($${maxUsdPrice.toFixed(4)} USD)`
448+
)} LITKEY ($${maxUsdPricePerNode.toFixed(
449+
4
450+
)} USD per node; $${maxUsdPriceTotal.toFixed(
451+
4
452+
)} total @ ${EXPECTED_THRESHOLD_NODES} nodes)`
414453
);
415454

416455
// Set base price
@@ -422,7 +461,8 @@ async function main() {
422461
transactions.push({
423462
...createSafeTransaction(priceFeedAddress, setBasePriceData),
424463
description: `Set Base Network Price - ${PRODUCT_NAMES[productId]}`,
425-
priceUSD: usdPrice,
464+
priceUSD: usdPricePerNode,
465+
priceUSDTotal: usdPriceTotal,
426466
priceLITKEY: priceTokens.toFixed(6),
427467
});
428468

@@ -435,7 +475,8 @@ async function main() {
435475
transactions.push({
436476
...createSafeTransaction(priceFeedAddress, setMaxPriceData),
437477
description: `Set Max Network Price - ${PRODUCT_NAMES[productId]}`,
438-
priceUSD: maxUsdPrice,
478+
priceUSD: maxUsdPricePerNode,
479+
priceUSDTotal: maxUsdPriceTotal,
439480
priceLITKEY: maxPriceTokens.toFixed(6),
440481
});
441482
}
@@ -520,8 +561,10 @@ async function main() {
520561
];
521562

522563
for (const config of litActionComponentMap) {
564+
const usdPriceTotal = config.usdPrice;
565+
const usdPricePerNode = totalUsdToPerNodeUsd(usdPriceTotal);
523566
const priceWei = adjustPriceForEnv(
524-
usdToLitKeyWei(config.usdPrice, litKeyPriceUSD)
567+
usdToLitKeyWei(usdPricePerNode, litKeyPriceUSD)
525568
);
526569
const priceTokens = parseFloat(ethers.formatUnits(priceWei, 18));
527570
const measurementName =
@@ -531,9 +574,13 @@ async function main() {
531574
? '/MB'
532575
: '/count';
533576
console.log(
534-
`${config.name} ${measurementName}: ${priceTokens.toFixed(
577+
`${config.name} ${measurementName} (per-node): ${priceTokens.toFixed(
578+
6
579+
)} LITKEY ($${usdPricePerNode.toFixed(
580+
6
581+
)} USD per node; $${usdPriceTotal.toFixed(
535582
6
536-
)} LITKEY ($${config.usdPrice.toFixed(4)} USD)`
583+
)} total @ ${EXPECTED_THRESHOLD_NODES} nodes)`
537584
);
538585

539586
const setLitActionPriceData = priceFeed.interface.encodeFunctionData(
@@ -548,7 +595,8 @@ async function main() {
548595
transactions.push({
549596
...createSafeTransaction(priceFeedAddress, setLitActionPriceData),
550597
description: `Set Lit Action Price - ${config.name} ${measurementName}`,
551-
priceUSD: config.usdPrice,
598+
priceUSD: usdPricePerNode,
599+
priceUSDTotal: usdPriceTotal,
552600
priceLITKEY: priceTokens.toFixed(6),
553601
});
554602
}

0 commit comments

Comments
 (0)