Class: PendingSwapStore
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.
listresolves[]when storage fails. - Operations of one store instance are serialized, so concurrent
remember/forgetcalls 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);
listremoves them from storage. - An entry whose
uniqueKeyis not a valid apply-results key is terminal, not pending:confirmAppliedTradefails it withINVALID_UNIQUE_KEYevery time, so a resume loop would retry it forever.listprunes it like an expired one, andrememberrefuses to save it; both tellonPrunethe reason (PENDING_SWAP_PRUNE_REASONS). - New entries go to the end (or
insertAt: 'start');listreturns the wallet's entries in stored order (orlistOrder: 'newest-first'). storageKey,darkFlagField,insertAtandlistOrderadapt 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.
confirm applied trade unique key invalid on prune pending swap reasons insert at list order storage dark flag field
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
Parameters
| Parameter | Type |
|---|---|
storage | StorageAdapter | undefined |
options? | PendingSwapStoreOptions |
Returns
PendingSwapStore
pending swap store storage adapter options
Methods
forget()
forget(
uniqueKey):Promise<void>
Removes a pending swap once its outcome is known.
Parameters
| Parameter | Type |
|---|---|
uniqueKey | string |
Returns
Promise<void>
unique key
list()
list(
wallet):Promise<PendingSwap[]>
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
| Parameter | Type |
|---|---|
wallet | string |
Returns
Promise<PendingSwap[]>
pending swap list order on prune unique key invalid confirm applied trade
remember()
remember(
swap):Promise<void>
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
| Parameter | Type |
|---|---|
swap | PendingSwap |
Returns
Promise<void>
unique key on prune invalid pending swap
settle()
settle(
uniqueKey,error):Promise<"forget"|"forget_quietly"|"keep">
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 andonPrunehearschain_rejected.forget_quietly: the entry is removed andonPrunehearsunconfirmable.keep: nothing changes andonPruneis 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
| Parameter | Type |
|---|---|
uniqueKey | string |
error | unknown |
Returns
Promise<"forget" | "forget_quietly" | "keep">
unique key forget quietly on prune chain rejected