@@ -15,6 +15,13 @@ export interface MaxPricesForNodes {
1515 * Ensures the total cost does not exceed userMaxPrice.
1616 * Operates in the order of lowest priced node to highest.
1717 *
18+ * Example:
19+ * - Selected nodes have SIGN_SESSION_KEY prices of 10 and 20.
20+ * - `userMaxPrice` is 100.
21+ * - Base total = 10 + 20 = 30.
22+ * - Excess = 100 - 30 = 70.
23+ * - Each node receives 70 / 2 = 35 extra budget, yielding 45 and 55.
24+ *
1825 * @param nodePrices - An object where keys are node addresses and values are arrays of prices for different action types.
1926 * @param userMaxPrice - The maximum price the user is willing to pay to execute the request.
2027 * @param productId - The ID of the product to determine which price to consider.
@@ -47,14 +54,14 @@ export function getMaxPricesForNodeProduct({
4754 ? sortedNodes . slice ( 0 , numRequiredNodes )
4855 : sortedNodes ;
4956
57+ // Sum the unadjusted cost for the nodes we plan to use.
5058 let totalBaseCost = 0n ;
51-
52- // Calculate the base total cost without adjustments
5359 for ( const { prices } of nodesToConsider ) {
60+ // Example: base total accumulates 10 + 20 = 30 for the two cheapest nodes.
5461 totalBaseCost += prices [ productId ] ;
5562 }
5663
57- // Verify that we have a high enough userMaxPrice to fulfill the request
64+ // Refuse to proceed if the caller's budget cannot even cover the base cost.
5865 if ( totalBaseCost > userMaxPrice ) {
5966 throw new MaxPriceTooLow (
6067 {
@@ -72,13 +79,16 @@ export function getMaxPricesForNodeProduct({
7279 * our request to fail if the price on some of the nodes is higher than we think it was, as long as it's not
7380 * drastically different than we expect it to be
7481 */
82+ // Any remaining budget is spread across the participating nodes to
83+ // provide cushion for minor pricing fluctuations. Example: 100 - 30 = 70.
7584 const excessBalance = userMaxPrice - totalBaseCost ;
7685
7786 // Map matching the keys from `nodePrices`, but w/ the per-node maxPrice computed based on `userMaxPrice`
7887 const maxPricesPerNode : { url : string ; price : bigint } [ ] = [ ] ;
7988
8089 for ( const { url, prices } of nodesToConsider ) {
81- // For now, we'll distribute the remaining balance equally across nodes
90+ // Distribute the remaining budget evenly across nodes to form the max price.
91+ // Example: each node receives 70 / 2 = 35, becoming 10+35 and 20+35.
8292 maxPricesPerNode . push ( {
8393 url,
8494 price : excessBalance
0 commit comments