-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Populate icrc store with imported tokens #5378
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import type { ImportedTokens } from "$lib/canisters/nns-dapp/nns-dapp.types"; | |
import { MAX_IMPORTED_TOKENS } from "$lib/constants/imported-tokens.constants"; | ||
import { FORCE_CALL_STRATEGY } from "$lib/constants/mockable.constants"; | ||
import { getAuthenticatedIdentity } from "$lib/services/auth.services"; | ||
import { icrcCanistersStore } from "$lib/stores/icrc-canisters.store"; | ||
import { importedTokensStore } from "$lib/stores/imported-tokens.store"; | ||
import { toastsError, toastsSuccess } from "$lib/stores/toasts.store"; | ||
import type { ImportedTokenData } from "$lib/types/imported-tokens"; | ||
|
@@ -19,6 +20,7 @@ import { | |
toImportedTokenData, | ||
} from "$lib/utils/imported-tokens.utils"; | ||
import { isNullish } from "@dfinity/utils"; | ||
import { get } from "svelte/store"; | ||
import { queryAndUpdate } from "./utils.services"; | ||
|
||
/** Load imported tokens from the `nns-dapp` backend and update the `importedTokensStore` store. | ||
|
@@ -32,11 +34,31 @@ export const loadImportedTokens = async ({ | |
return queryAndUpdate<ImportedTokens, unknown>({ | ||
request: (options) => getImportedTokens(options), | ||
strategy: FORCE_CALL_STRATEGY, | ||
onLoad: ({ response: { imported_tokens: importedTokens }, certified }) => | ||
onLoad: ({ | ||
response: { imported_tokens: rawImportedTokens }, | ||
certified, | ||
}) => { | ||
const importedTokens = rawImportedTokens.map(toImportedTokenData); | ||
importedTokensStore.set({ | ||
importedTokens: importedTokens.map(toImportedTokenData), | ||
importedTokens, | ||
certified, | ||
}), | ||
}); | ||
|
||
if (!certified && notForceCallStrategy()) { | ||
return; | ||
} | ||
|
||
// Populate icrcCanistersStore with the imported tokens. | ||
for (const { ledgerCanisterId, indexCanisterId } of importedTokens) { | ||
// If the imported token is not already in the store, add it. | ||
if (isNullish(get(icrcCanistersStore)[ledgerCanisterId.toText()])) { | ||
icrcCanistersStore.setCanisters({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't populated 2 different stores with the same data. Instead we should have a derived store which combines the relevant stores. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, or we rename icrcCanisterStore to something else like, |
||
ledgerCanisterId, | ||
indexCanisterId, | ||
}); | ||
} | ||
} | ||
}, | ||
onError: ({ error: err, certified }) => { | ||
console.error(err); | ||
|
||
|
@@ -97,8 +119,6 @@ export const addImportedToken = async ({ | |
tokenToAdd: ImportedTokenData; | ||
importedTokens: ImportedTokenData[]; | ||
}): Promise<{ success: boolean }> => { | ||
// TODO: validate importedToken (not sns, not ck, is unique, etc.) | ||
|
||
const tokens = [...importedTokens, tokenToAdd]; | ||
const { err } = await saveImportedToken({ tokens }); | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -7,7 +7,7 @@ import { writable } from "svelte/store"; | |||
|
||||
export interface IcrcCanisters { | ||||
ledgerCanisterId: Principal; | ||||
indexCanisterId: Principal; | ||||
indexCanisterId: Principal | undefined; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This means the work-around can now be removed, right?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only after the feature in not behind the flag. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How come? With this change, the type of |
||||
} | ||||
|
||||
export type IcrcCanistersStoreData = Record< | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is
importedTokensStore
populated regardless ofcertified
buticrcCanistersStore
only ifcertified
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a
certified
property in theimportedTokensStore
, so that we always know the version. Since we skip adding a token when it’s already in the store, I thought it made sense to use only the certified version. However, it’s not a strong opinion. Especially now, when only query request is used.