@orca-so/common-sdk
Advanced tools
Comparing version 0.3.0-beta-3 to 0.3.0
@@ -23,2 +23,3 @@ import { Connection } from "@solana/web3.js"; | ||
getAccount<U extends T>(address: Address, parser: ParsableEntity<U>, opts?: FetchOptions | undefined, now?: number): Promise<U | null>; | ||
private getMaxAge; | ||
getAccounts<U extends T>(addresses: Address[], parser: ParsableEntity<U>, opts?: SimpleAccountFetchOptions | undefined, now?: number): Promise<ReadonlyMap<string, U | null>>; | ||
@@ -25,0 +26,0 @@ getAccountsAsArray<U extends T>(addresses: Address[], parser: ParsableEntity<U>, opts?: FetchOptions | undefined, now?: number): Promise<ReadonlyArray<U | null>>; |
@@ -6,3 +6,3 @@ "use strict"; | ||
const account_requests_1 = require("../account-requests"); | ||
// SimpleAccountCache is a simple implementation of AccountCache that stores the fetched | ||
// SimpleAccountFetcher is a simple implementation of AccountCache that stores the fetched | ||
// accounts in memory. If TTL is not provided, it will use TTL defined in the the retention policy | ||
@@ -21,3 +21,3 @@ // for the parser. If that is also not provided, the request will always prefer the cache value. | ||
const cached = this.cache.get(addressStr); | ||
const maxAge = opts?.maxAge ?? this.retentionPolicy.get(parser) ?? Number.POSITIVE_INFINITY; | ||
const maxAge = this.getMaxAge(this.retentionPolicy.get(parser), opts); | ||
const elapsed = !!cached ? now - (cached?.fetchedAt ?? 0) : Number.NEGATIVE_INFINITY; | ||
@@ -39,2 +39,8 @@ const expired = elapsed > maxAge; | ||
} | ||
getMaxAge(parserMaxAge, opts) { | ||
if (opts?.maxAge !== undefined) { | ||
return opts.maxAge; | ||
} | ||
return parserMaxAge === undefined ? Number.POSITIVE_INFINITY : parserMaxAge; | ||
} | ||
async getAccounts(addresses, parser, opts, now = Date.now()) { | ||
@@ -50,3 +56,2 @@ const addressStrs = address_util_1.AddressUtil.toStrings(addresses); | ||
}); | ||
// invariant(result.size === addresses.length, "not enough results fetched"); | ||
return result; | ||
@@ -78,7 +83,7 @@ } | ||
const addressStrs = address_util_1.AddressUtil.toStrings(addresses); | ||
const maxAge = opts?.maxAge ?? this.retentionPolicy.get(parser) ?? Number.POSITIVE_INFINITY; | ||
const maxAge = this.getMaxAge(this.retentionPolicy.get(parser), opts); | ||
// Filter out all unexpired accounts to get the accounts to fetch | ||
const undefinedAccounts = addressStrs.filter((addressStr) => { | ||
const cached = this.cache.get(addressStr); | ||
const elapsed = cached ? now - (cached?.fetchedAt ?? 0) : Number.NEGATIVE_INFINITY; | ||
const elapsed = !!cached ? now - (cached?.fetchedAt ?? 0) : Number.NEGATIVE_INFINITY; | ||
const expired = elapsed > maxAge; | ||
@@ -88,2 +93,5 @@ return !cached || expired; | ||
// Fetch all undefined accounts and place in cache | ||
// TODO: We currently do not support contextSlot consistency across the batched getMultipleAccounts call | ||
// If the addresses list contain accounts in the 1st gMA call as subsequent calls and the gMA returns on different contextSlots, | ||
// the returned results can be inconsistent and unexpected by the user. | ||
if (undefinedAccounts.length > 0) { | ||
@@ -90,0 +98,0 @@ const fetchedAccountsMap = await (0, account_requests_1.getMultipleAccountsInMap)(this.connection, undefinedAccounts); |
{ | ||
"name": "@orca-so/common-sdk", | ||
"version": "0.3.0-beta-3", | ||
"version": "0.3.0", | ||
"description": "Common Typescript components across Orca", | ||
@@ -5,0 +5,0 @@ "repository": "https://github.com/orca-so/orca-sdks", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
151274
1946