Skip to content

Commit b532c39

Browse files
committed
fmt
1 parent d97f177 commit b532c39

File tree

4 files changed

+124
-110
lines changed

4 files changed

+124
-110
lines changed

packages/contracts-sdk/src/lib/contracts-sdk.ts

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -620,18 +620,18 @@ export class LitContracts {
620620
};
621621

622622
/**
623-
* Retrieves the PriceFeed contract instance based on the provided network, context, and RPC URL.
624-
* If a context is provided, it determines if a contract resolver is used for bootstrapping contracts.
625-
* If a resolver address is present in the context, it retrieves the PriceFeed contract from the contract resolver instance.
626-
* Otherwise, it retrieves the PriceFeed contract using the contract address and ABI.
627-
* Throws an error if required contract data is missing or if the PriceFeed contract cannot be obtained.
628-
*
629-
* @param network - The network key.
630-
* @param context - The contract context or contract resolver context.
631-
* @param rpcUrl - The RPC URL.
632-
* @returns The PriceFeed contract instance.
633-
* @throws Error if required contract data is missing or if the PriceFeed contract cannot be obtained.
634-
*/
623+
* Retrieves the PriceFeed contract instance based on the provided network, context, and RPC URL.
624+
* If a context is provided, it determines if a contract resolver is used for bootstrapping contracts.
625+
* If a resolver address is present in the context, it retrieves the PriceFeed contract from the contract resolver instance.
626+
* Otherwise, it retrieves the PriceFeed contract using the contract address and ABI.
627+
* Throws an error if required contract data is missing or if the PriceFeed contract cannot be obtained.
628+
*
629+
* @param network - The network key.
630+
* @param context - The contract context or contract resolver context.
631+
* @param rpcUrl - The RPC URL.
632+
* @returns The PriceFeed contract instance.
633+
* @throws Error if required contract data is missing or if the PriceFeed contract cannot be obtained.
634+
*/
635635
public static async getPriceFeedContract(
636636
network: LIT_NETWORKS_KEYS,
637637
context?: LitContractContext | LitContractResolverContext,
@@ -1083,7 +1083,7 @@ export class LitContracts {
10831083
* "https://192.168.1.1:443",
10841084
* "http://192.168.1.2:80"
10851085
* ]
1086-
*/
1086+
*/
10871087
public static generateValidatorURLs({
10881088
activeValidatorStructs,
10891089
nodeProtocol,
@@ -1132,7 +1132,7 @@ export class LitContracts {
11321132
networkContext,
11331133
rpcUrl,
11341134
nodeProtocol,
1135-
sortByPrice
1135+
sortByPrice,
11361136
}: {
11371137
litNetwork: LIT_NETWORKS_KEYS;
11381138
networkContext?: LitContractContext | LitContractResolverContext;
@@ -1146,7 +1146,6 @@ export class LitContracts {
11461146
bootstrapUrls: string[];
11471147
priceByNetwork: Record<string, number>;
11481148
}> => {
1149-
11501149
// if it's true, we will sort the networks by price feed from lowest to highest
11511150
// if it's false, we will not sort the networks
11521151
let _sortByPrice = sortByPrice || true;
@@ -1202,9 +1201,10 @@ export class LitContracts {
12021201
const unsortedNetworks = LitContracts.generateValidatorURLs({
12031202
activeValidatorStructs,
12041203
litNetwork,
1205-
})
1204+
});
12061205

1207-
// networks are all the nodes we know, but we also want to sort it by price feed
1206+
// networks are all the nodes we know from the `getActiveUnkickedValidatorStructsAndCounts` function, but we also want to sort it by price feed
1207+
// which we need to call the price feed contract
12081208
const priceFeedInfo = await LitContracts.getPriceFeedInfo({
12091209
litNetwork,
12101210
networkContext,
@@ -1220,13 +1220,13 @@ export class LitContracts {
12201220
const PRICE_BY_NETWORK = priceFeedInfo.networkPrices.mapByAddress;
12211221

12221222
// sorted networks by prices (lowest to highest)
1223-
// [
1223+
// [
12241224
// 'http://xxx:7470', <-- lowest price
12251225
// 'http://zzz:7472', <-- middle price
12261226
// 'http://yyy:7471' <-- highest price
12271227
// ]
1228-
const sortedNetworks = unsortedNetworks.sort((a, b) =>
1229-
PRICE_BY_NETWORK[a] - PRICE_BY_NETWORK[b]
1228+
const sortedNetworks = unsortedNetworks.sort(
1229+
(a, b) => PRICE_BY_NETWORK[a] - PRICE_BY_NETWORK[b]
12301230
);
12311231

12321232
const bootstrapUrls = _sortByPrice ? sortedNetworks : unsortedNetworks;
@@ -1246,64 +1246,72 @@ export class LitContracts {
12461246
rpcUrl,
12471247
productIds, // Array of product IDs
12481248
}: {
1249-
litNetwork: LIT_NETWORKS_KEYS,
1249+
litNetwork: LIT_NETWORKS_KEYS;
12501250
networkContext?: LitContractContext | LitContractResolverContext;
12511251
rpcUrl?: string;
12521252
nodeProtocol?: typeof HTTP | typeof HTTPS | null;
12531253
productIds?: number[];
12541254
}) => {
1255-
12561255
if (!productIds || productIds.length === 0) {
12571256
log('No product IDs provided. Defaulting to 0');
12581257

12591258
// You should use all [0,1,2] because we fetch the price first to connect to the cheapest node. And after that the user calls the actual function
1260-
productIds = [0, 1, 2]
1259+
productIds = [0, 1, 2];
12611260
}
12621261

12631262
const priceFeedContract = await LitContracts.getPriceFeedContract(
12641263
litNetwork,
12651264
networkContext,
1266-
rpcUrl,
1267-
)
1265+
rpcUrl
1266+
);
12681267

1269-
const nodesForRequest = await priceFeedContract['getNodesForRequest'](productIds);
1268+
const nodesForRequest = await priceFeedContract['getNodesForRequest'](
1269+
productIds
1270+
);
12701271

12711272
const epochId = nodesForRequest[0].toNumber();
12721273
const minNodeCount = nodesForRequest[1].toNumber();
12731274
const nodesAndPrices = nodesForRequest[2];
12741275

1275-
const activeValidatorStructs: ValidatorStruct[] = nodesAndPrices.map((item: any) => {
1276-
return {
1277-
ip: item.validator.ip,
1278-
ipv6: item.validator.ipv6,
1279-
port: item.validator.port,
1280-
nodeAddress: item.validator.nodeAddress,
1281-
reward: item.validator.reward,
1282-
seconderPubkey: item.validator.seconderPubkey,
1283-
receiverPubkey: item.validator.receiverPubkey,
1276+
const activeValidatorStructs: ValidatorStruct[] = nodesAndPrices.map(
1277+
(item: any) => {
1278+
return {
1279+
ip: item.validator.ip,
1280+
ipv6: item.validator.ipv6,
1281+
port: item.validator.port,
1282+
nodeAddress: item.validator.nodeAddress,
1283+
reward: item.validator.reward,
1284+
seconderPubkey: item.validator.seconderPubkey,
1285+
receiverPubkey: item.validator.receiverPubkey,
1286+
};
12841287
}
1285-
});
1288+
);
12861289

12871290
const networks = LitContracts.generateValidatorURLs({
12881291
activeValidatorStructs,
12891292
litNetwork,
1290-
})
1293+
});
12911294

1292-
console.log("networks:", networks);
1295+
console.log('networks:', networks);
12931296

12941297
const prices = nodesAndPrices.flatMap((item: any) => {
12951298
// Flatten the nested prices array and convert BigNumber to number
1296-
return item.prices.map((price: ethers.BigNumber) => parseFloat(price.toString()));
1299+
return item.prices.map((price: ethers.BigNumber) =>
1300+
parseFloat(price.toString())
1301+
);
12971302
});
12981303

1299-
console.log("Prices as numbers:", prices);
1304+
console.log('Prices as numbers:', prices);
13001305

1301-
const networkPriceMap: Record<string, number> = networks.reduce((acc: any, network, index) => {
1302-
acc[network] = prices[index];
1303-
return acc;
1304-
}, {});
1306+
const networkPriceMap: Record<string, number> = networks.reduce(
1307+
(acc: any, network, index) => {
1308+
acc[network] = prices[index];
1309+
return acc;
1310+
},
1311+
{}
1312+
);
13051313

1306-
console.log("Network to Price Map:", networkPriceMap);
1314+
console.log('Network to Price Map:', networkPriceMap);
13071315

13081316
const networkPriceObjArr = networks.map((network, index) => {
13091317
return {
@@ -1317,10 +1325,10 @@ export class LitContracts {
13171325
minNodeCount,
13181326
networkPrices: {
13191327
arr: networkPriceObjArr,
1320-
mapByAddress: networkPriceMap
1328+
mapByAddress: networkPriceMap,
13211329
},
1322-
}
1323-
}
1330+
};
1331+
};
13241332

13251333
private static async _resolveContractContext(
13261334
network: LIT_NETWORK_VALUES

packages/core/src/lib/lit-core.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export type LitNodeClientConfigWithDefaults = Required<
116116
nodeProtocol?: typeof HTTP | typeof HTTPS | null;
117117
} & {
118118
priceByNetwork: Record<string, number>; // eg. <nodeAddress, price>
119-
}
119+
};
120120

121121
// On epoch change, we wait this many seconds for the nodes to update to the new epoch before using the new epoch #
122122
const EPOCH_PROPAGATION_DELAY = 45_000;
@@ -260,14 +260,19 @@ export class LitCore {
260260
bootstrapUrls: string[];
261261
priceByNetwork: Record<string, number>;
262262
}> {
263-
const { stakingContract, epochInfo, minNodeCount, bootstrapUrls, priceByNetwork } =
264-
await LitContracts.getConnectionInfo({
265-
litNetwork: this.config.litNetwork,
266-
networkContext: this.config.contractContext,
267-
rpcUrl: this.config.rpcUrl,
268-
nodeProtocol: this.config.nodeProtocol,
269-
sortByPrice: true
270-
});
263+
const {
264+
stakingContract,
265+
epochInfo,
266+
minNodeCount,
267+
bootstrapUrls,
268+
priceByNetwork,
269+
} = await LitContracts.getConnectionInfo({
270+
litNetwork: this.config.litNetwork,
271+
networkContext: this.config.contractContext,
272+
rpcUrl: this.config.rpcUrl,
273+
nodeProtocol: this.config.nodeProtocol,
274+
sortByPrice: true,
275+
});
271276

272277
// Validate minNodeCount
273278
if (!minNodeCount) {
@@ -297,7 +302,7 @@ export class LitCore {
297302
epochInfo,
298303
minNodeCount,
299304
bootstrapUrls,
300-
priceByNetwork
305+
priceByNetwork,
301306
};
302307
}
303308

@@ -706,9 +711,11 @@ export class LitCore {
706711
await Promise.race([
707712
new Promise((_resolve, reject) => {
708713
timeoutHandle = setTimeout(() => {
709-
const msg = `Error: Could not handshake with nodes after timeout of ${this.config.connectTimeout
710-
}ms. Could only connect to ${Object.keys(serverKeys).length} of ${this.config.bootstrapUrls.length
711-
} nodes. Please check your network connection and try again. Note that you can control this timeout with the connectTimeout config option which takes milliseconds.`;
714+
const msg = `Error: Could not handshake with nodes after timeout of ${
715+
this.config.connectTimeout
716+
}ms. Could only connect to ${Object.keys(serverKeys).length} of ${
717+
this.config.bootstrapUrls.length
718+
} nodes. Please check your network connection and try again. Note that you can control this timeout with the connectTimeout config option which takes milliseconds.`;
712719

713720
try {
714721
throw new InitError({}, msg);
@@ -1036,8 +1043,8 @@ export class LitCore {
10361043
this._epochCache.currentNumber &&
10371044
this._epochCache.startTime &&
10381045
Math.floor(Date.now() / 1000) <
1039-
this._epochCache.startTime +
1040-
Math.floor(EPOCH_PROPAGATION_DELAY / 1000) &&
1046+
this._epochCache.startTime +
1047+
Math.floor(EPOCH_PROPAGATION_DELAY / 1000) &&
10411048
this._epochCache.currentNumber >= 3 // FIXME: Why this check?
10421049
) {
10431050
return this._epochCache.currentNumber - 1;
@@ -1068,7 +1075,7 @@ export class LitCore {
10681075
data,
10691076
requestId,
10701077
}: // eslint-disable-next-line @typescript-eslint/no-explicit-any
1071-
SendNodeCommand): Promise<any> => {
1078+
SendNodeCommand): Promise<any> => {
10721079
// FIXME: Replace <any> usage with explicit, strongly typed handlers
10731080
data = { ...data, epoch: this.currentEpochNumber };
10741081

packages/lit-node-client-nodejs/src/lib/lit-node-client-nodejs.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ import { LitContracts } from '@lit-protocol/contracts-sdk';
137137

138138
export class LitNodeClientNodeJs
139139
extends LitCore
140-
implements LitClientSessionManager, ILitNodeClient {
140+
implements LitClientSessionManager, ILitNodeClient
141+
{
141142
defaultAuthCallback?: (authSigParams: AuthCallbackParams) => Promise<AuthSig>;
142143

143144
// ========== Constructor ==========
@@ -1164,8 +1165,8 @@ export class LitNodeClientNodeJs
11641165
// -- optional params
11651166
...(params.authMethods &&
11661167
params.authMethods.length > 0 && {
1167-
authMethods: params.authMethods,
1168-
}),
1168+
authMethods: params.authMethods,
1169+
}),
11691170

11701171
nodeSet,
11711172
};
@@ -1898,8 +1899,8 @@ export class LitNodeClientNodeJs
18981899
const sessionCapabilityObject = params.sessionCapabilityObject
18991900
? params.sessionCapabilityObject
19001901
: await this.generateSessionCapabilityObjectWithWildcards(
1901-
params.resourceAbilityRequests.map((r) => r.resource)
1902-
);
1902+
params.resourceAbilityRequests.map((r) => r.resource)
1903+
);
19031904
const expiration = params.expiration || LitNodeClientNodeJs.getExpiration();
19041905

19051906
// -- (TRY) to get the wallet signature
@@ -1981,19 +1982,18 @@ export class LitNodeClientNodeJs
19811982

19821983
const capabilities = params.capacityDelegationAuthSig
19831984
? [
1984-
...(params.capabilityAuthSigs ?? []),
1985-
params.capacityDelegationAuthSig,
1986-
authSig,
1987-
]
1985+
...(params.capabilityAuthSigs ?? []),
1986+
params.capacityDelegationAuthSig,
1987+
authSig,
1988+
]
19881989
: [...(params.capabilityAuthSigs ?? []), authSig];
19891990

1990-
19911991
// Get new price feed info from the contract if user wants to
19921992

19931993
let priceByNetwork = this.config.priceByNetwork;
19941994

19951995
if (params.getNewPrices) {
1996-
log(`Getting new prices from the contract`)
1996+
log(`Getting new prices from the contract`);
19971997
const priceFeedInfo = await LitContracts.getPriceFeedInfo({
19981998
litNetwork: this.config.litNetwork,
19991999
networkContext: this.config.contractContext,
@@ -2002,7 +2002,6 @@ export class LitNodeClientNodeJs
20022002
priceByNetwork = priceFeedInfo.networkPrices.mapByAddress;
20032003
}
20042004

2005-
20062005
// This is the template that will be combined with the node address as a single object, then signed by the session key
20072006
// so that the node can verify the session signature
20082007
const sessionSigningTemplate = {

0 commit comments

Comments
 (0)