Skip to main content

Class: PendingSwapStore

gSwap SDK 2.0.0-rc.4


gSwap SDK 2.0.0-rc.4 / PendingSwapStore

Class: PendingSwapStore

Defined in: packages/gswap-sdk/dist/index.d.ts:3771

Remembers queued swaps so their outcome can be confirmed after the UI, popup or service worker that submitted them is gone (confirmAppliedTrade with the stored uniqueKey).

Guarantees:

  • Persistence is best effort and NEVER throws: a failing or missing storage must not turn an accepted request into a client-side resubmission. list resolves [] when storage fails.
  • Operations of one store instance are serialized, so concurrent remember / forget calls cannot lose each other's write (read-modify-write over an async adapter). Two store instances over the same storage (for example popup and service worker) are not coordinated.
  • Entries that do not match the expected shape are ignored and dropped on the next write.
  • Entries expire after seven days (an entry exactly seven days old is expired); list removes them from storage.
  • An entry whose uniqueKey is not a valid apply-results key is terminal, not pending: confirmAppliedTrade fails it with INVALID_UNIQUE_KEY every time, so a resume loop would retry it forever. list prunes it like an expired one, and remember refuses to save it; both tell onPrune the reason (PENDING_SWAP_PRUNE_REASONS).
  • New entries go to the end (or insertAt: 'start'); list returns the wallet's entries in stored order (or listOrder: 'newest-first').
  • storageKey, darkFlagField, insertAt and listOrder adapt the stored layout to an existing storage. JSON key order inside an entry is not preserved.

Passing no storage yields a store that remembers nothing.

Example​

const store = new PendingSwapStore({
get: async (key) => (await chrome.storage.local.get(key))[key],
set: (key, value) => chrome.storage.local.set({ [key]: value }),
});
await store.remember({ uniqueKey, requestTransactionId, wallet, token0Symbol: 'GALA',
token1Symbol: 'GUSDC', isDarkSwap: false, createdAt: Date.now() });
for (const swap of await store.list(wallet)) await confirmAppliedTrade(client, swap);

Constructors​

Constructor​

new PendingSwapStore(storage, options?): PendingSwapStore

Defined in: packages/gswap-sdk/dist/index.d.ts:3780

Parameters​

storage​

StorageAdapter | undefined

options?​

PendingSwapStoreOptions

Returns​

PendingSwapStore

Methods​

forget()​

forget(uniqueKey): Promise<void>

Defined in: packages/gswap-sdk/dist/index.d.ts:3788

Removes a pending swap once its outcome is known.

Parameters​

uniqueKey​

string

Returns​

Promise<void>


list()​

list(wallet): Promise<PendingSwap[]>

Defined in: packages/gswap-sdk/dist/index.d.ts:3809

The wallet's pending swaps that can still be confirmed (see listOrder). Entries that never can be are pruned from storage, for every wallet, and reported to onPrune: expired ones (expired) and ones whose uniqueKey is not a valid apply-results key (invalid_unique_key: confirmAppliedTrade would fail INVALID_UNIQUE_KEY on it forever).

Parameters​

wallet​

string

Returns​

Promise<PendingSwap[]>


remember()​

remember(swap): Promise<void>

Defined in: packages/gswap-sdk/dist/index.d.ts:3786

Saves (or replaces, by uniqueKey) a pending swap. A swap whose uniqueKey is not a valid apply-results key is not saved (it could never be confirmed); it is reported to onPrune as invalid_unique_key.

Parameters​

swap​

PendingSwap

Returns​

Promise<void>


settle()​

settle(uniqueKey, error): Promise<"forget" | "forget_quietly" | "keep">

Defined in: packages/gswap-sdk/dist/index.d.ts:3802

Applies pendingSwapFateFor to a swap whose confirmation failed with error, and returns the fate so the app can word it (tell the user on forget, say nothing on forget_quietly, keep polling on keep):

  • forget: the entry is removed and onPrune hears chain_rejected.
  • forget_quietly: the entry is removed and onPrune hears unconfirmable.
  • keep: nothing changes and onPrune is not called.

Like every store operation it is serialized and best effort: if storage fails the entry stays (a later settle or list handles it) and the fate is still returned. A key that is not stored (already forgotten) is not an error and onPrune is not called for it.

Parameters​

uniqueKey​

string

error​

unknown

Returns​

Promise<"forget" | "forget_quietly" | "keep">