@orca-so/common-sdk
Advanced tools
Comparing version 0.3.0-beta-1 to 0.3.0-beta-2
@@ -6,6 +6,6 @@ import { Account, Mint } from "@solana/spl-token"; | ||
export type BasicSupportedTypes = Account | Mint; | ||
export type AccountFetchOpts = { | ||
ttl: number; | ||
}; | ||
export interface AccountCache<T> { | ||
/** | ||
* Interface for fetching and caching on-chain accounts | ||
*/ | ||
export interface AccountCache<T, AccountFetchOptions> { | ||
/** | ||
@@ -18,3 +18,3 @@ * Fetch an account from the cache or from the network | ||
*/ | ||
getAccount: (address: Address, parser: ParsableEntity<T>, opts?: AccountFetchOpts) => Promise<T | null>; | ||
getAccount: (address: Address, parser: ParsableEntity<T>, opts?: AccountFetchOptions) => Promise<T | null>; | ||
/** | ||
@@ -27,15 +27,11 @@ * Fetch multiple accounts from the cache or from the network | ||
*/ | ||
getAccounts: (address: Address[], parser: ParsableEntity<T>, opts?: AccountFetchOpts) => Promise<ReadonlyMap<string, T | null>>; | ||
getAccounts: (address: Address[], parser: ParsableEntity<T>, opts?: AccountFetchOptions) => Promise<ReadonlyMap<string, T | null>>; | ||
/** | ||
* Fetch multiple accounts from the cache or from the network and return as an array | ||
* @param address A list of account addresses to fetch from cache or network | ||
* @param parser The parser to used for theses accounts | ||
* @param opts Options when fetching the accounts | ||
* @returns an array of accounts. The ordering of the array is the same as the ordering of the input addresses. | ||
*/ | ||
getAccountsAsArray: (address: Address[], parser: ParsableEntity<T>, opts?: AccountFetchOpts) => Promise<ReadonlyArray<T | null>>; | ||
/** | ||
* Refresh all accounts in the cache | ||
* Fetch multiple accounts from the cache or from the network and return as an array | ||
* @param address A list of account addresses to fetch from cache or network | ||
* @param parser The parser to used for theses accounts | ||
* @param opts Options when fetching the accounts | ||
* @returns an array of accounts. The ordering of the array is the same as the ordering of the input addresses. | ||
*/ | ||
refreshAll: () => Promise<void>; | ||
getAccountsAsArray: (address: Address[], parser: ParsableEntity<T>, opts?: AccountFetchOptions) => Promise<ReadonlyArray<T | null>>; | ||
} |
import { Connection } from "@solana/web3.js"; | ||
import { AccountCache, AccountFetchOpts } from "."; | ||
import { AccountCache } from "."; | ||
import { Address } from "../../address-util"; | ||
@@ -11,3 +11,9 @@ import { ParsableEntity } from "../parsing"; | ||
export type RetentionPolicy<T> = ReadonlyMap<ParsableEntity<T>, number>; | ||
export declare class SimpleAccountCache<T> implements AccountCache<T> { | ||
/** | ||
* Options when fetching the accounts | ||
*/ | ||
export type SimpleAccountFetchOptions = { | ||
maxAge?: number; | ||
}; | ||
export declare class SimpleAccountCache<T, FetchOptions extends SimpleAccountFetchOptions> implements AccountCache<T, FetchOptions> { | ||
readonly connection: Connection; | ||
@@ -17,5 +23,5 @@ readonly retentionPolicy: RetentionPolicy<T>; | ||
constructor(connection: Connection, retentionPolicy: RetentionPolicy<T>); | ||
getAccount<U extends T>(address: Address, parser: ParsableEntity<U>, opts?: AccountFetchOpts | undefined, now?: number): Promise<U | null>; | ||
getAccounts<U extends T>(addresses: Address[], parser: ParsableEntity<U>, opts?: AccountFetchOpts | undefined, now?: number): Promise<ReadonlyMap<string, U | null>>; | ||
getAccountsAsArray<U extends T>(addresses: Address[], parser: ParsableEntity<U>, opts?: AccountFetchOpts | undefined, now?: number): Promise<ReadonlyArray<U | null>>; | ||
getAccount<U extends T>(address: Address, parser: ParsableEntity<U>, opts?: FetchOptions | undefined, now?: number): Promise<U | null>; | ||
getAccounts<U extends T>(addresses: Address[], parser: ParsableEntity<U>, opts?: SimpleAccountFetchOptions | undefined, now?: number): Promise<ReadonlyMap<string, U | null>>; | ||
getAccountsAsArray<U extends T>(addresses: Address[], parser: ParsableEntity<U>, opts?: FetchOptions | undefined, now?: number): Promise<ReadonlyArray<U | null>>; | ||
refreshAll(now?: number): Promise<void>; | ||
@@ -22,0 +28,0 @@ private populateCache; |
@@ -6,2 +6,5 @@ "use strict"; | ||
const account_requests_1 = require("../account-requests"); | ||
// SimpleAccountCache 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 | ||
// for the parser. If that is also not provided, the request will always prefer the cache value. | ||
class SimpleAccountCache { | ||
@@ -18,5 +21,5 @@ constructor(connection, retentionPolicy) { | ||
const cached = this.cache.get(addressStr); | ||
const ttl = opts?.ttl ?? this.retentionPolicy.get(parser) ?? Number.POSITIVE_INFINITY; | ||
const maxAge = opts?.maxAge ?? this.retentionPolicy.get(parser) ?? Number.POSITIVE_INFINITY; | ||
const elapsed = !!cached ? now - (cached?.fetchedAt ?? 0) : Number.NEGATIVE_INFINITY; | ||
const expired = elapsed > ttl; | ||
const expired = elapsed > maxAge; | ||
if (!!cached && !expired) { | ||
@@ -61,3 +64,2 @@ return cached.value; | ||
} | ||
; | ||
async refreshAll(now = Date.now()) { | ||
@@ -75,3 +77,3 @@ const addresses = Array.from(this.cache.keys()); | ||
const addressStrs = address_util_1.AddressUtil.toStrings(addresses); | ||
const ttl = opts?.ttl ?? this.retentionPolicy.get(parser) ?? Number.POSITIVE_INFINITY; | ||
const maxAge = opts?.maxAge ?? this.retentionPolicy.get(parser) ?? Number.POSITIVE_INFINITY; | ||
// Filter out all unexpired accounts to get the accounts to fetch | ||
@@ -81,3 +83,3 @@ const undefinedAccounts = addressStrs.filter((addressStr) => { | ||
const elapsed = cached ? now - (cached?.fetchedAt ?? 0) : Number.NEGATIVE_INFINITY; | ||
const expired = elapsed > ttl; | ||
const expired = elapsed > maxAge; | ||
return !cached || expired; | ||
@@ -84,0 +86,0 @@ }); |
@@ -0,10 +1,34 @@ | ||
import { Account, Mint } from "@solana/spl-token"; | ||
import { ParsableEntity } from ".."; | ||
import { Address } from "../../address-util"; | ||
export type AccountFetchOpts = { | ||
ttl: number; | ||
}; | ||
export interface AccountCache<SupportedTypes> { | ||
getAccount: <T extends SupportedTypes>(address: Address, parser: ParsableEntity<T>, opts?: AccountFetchOpts) => Promise<T | null>; | ||
getAccounts: <T extends SupportedTypes>(address: Address, parser: ParsableEntity<T>, opts?: AccountFetchOpts) => Promise<Map<string, T | null>>; | ||
refreshAll: () => void; | ||
export * from "./simple-fetcher-impl"; | ||
export type BasicSupportedTypes = Account | Mint; | ||
/** | ||
* Interface for fetching and caching on-chain accounts | ||
*/ | ||
export interface AccountCache<T, AccountFetchOptions> { | ||
/** | ||
* Fetch an account from the cache or from the network | ||
* @param address The account address to fetch from cache or network | ||
* @param parser The parser to used for theses accounts | ||
* @param opts Options when fetching the accounts | ||
* @returns | ||
*/ | ||
getAccount: (address: Address, parser: ParsableEntity<T>, opts?: AccountFetchOptions) => Promise<T | null>; | ||
/** | ||
* Fetch multiple accounts from the cache or from the network | ||
* @param address A list of account addresses to fetch from cache or network | ||
* @param parser The parser to used for theses accounts | ||
* @param opts Options when fetching the accounts | ||
* @returns a Map of addresses to accounts. The ordering of the Map iteration is the same as the ordering of the input addresses. | ||
*/ | ||
getAccounts: (address: Address[], parser: ParsableEntity<T>, opts?: AccountFetchOptions) => Promise<ReadonlyMap<string, T | null>>; | ||
/** | ||
* Fetch multiple accounts from the cache or from the network and return as an array | ||
* @param address A list of account addresses to fetch from cache or network | ||
* @param parser The parser to used for theses accounts | ||
* @param opts Options when fetching the accounts | ||
* @returns an array of accounts. The ordering of the array is the same as the ordering of the input addresses. | ||
*/ | ||
getAccountsAsArray: (address: Address[], parser: ParsableEntity<T>, opts?: AccountFetchOptions) => Promise<ReadonlyArray<T | null>>; | ||
} |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./simple-fetcher-impl"), exports); |
export * from "./account-requests"; | ||
export * from "./cache"; | ||
export * from "./fetcher"; | ||
export * from "./parsing"; |
@@ -18,3 +18,3 @@ "use strict"; | ||
__exportStar(require("./account-requests"), exports); | ||
__exportStar(require("./cache"), exports); | ||
__exportStar(require("./fetcher"), exports); | ||
__exportStar(require("./parsing"), exports); |
{ | ||
"name": "@orca-so/common-sdk", | ||
"version": "0.3.0-beta-1", | ||
"version": "0.3.0-beta-2", | ||
"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
150815
58
1937