Skip to content

Commit

Permalink
Fix: Adds an override for the name and token.symbol values for abando…
Browse files Browse the repository at this point in the history
…ned SNS (#5915)

# Motivation

As discussed
[here](https://dfinity.slack.com/archives/C03H6QEPW5D/p1733263773416409)
and
[here](https://dfinity.slack.com/archives/C039M7YS6F6/p1733302975333649),
we have a situation with the SNS `CYCLES-TRANSFER-STATION`. They have
decided to change all their metadata, resulting in the name and token
being displayed as `---`.

This first PR introduces a method to override the values we receive from
the SNS-Aggregator, allowing the rest of the application to use them.

# Changes

- Checks a dictionary of "broken" SNSs for matches and overrides `name`
and `token.symbol` for any that are found.

# Tests

- Unit test for the aggregator store derived data to verify that when
`rootCanisterId` is present in the dictionary, the values for `name` and
`token.symbol` are replaced.

# Todos

- [ ] Add entry to changelog (if necessary).
  • Loading branch information
yhabib authored Dec 4, 2024
1 parent 9d353f8 commit e4d3667
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-Nns-Dapp-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ proposal is successful, the changes it released will be moved from this file to

#### Fixed

- Adds an override for the name and token.symbol values for abandoned SNS

#### Security

#### Not Published
Expand Down
58 changes: 52 additions & 6 deletions frontend/src/lib/stores/sns-aggregator.store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import type { CachedSnsDto } from "$lib/types/sns-aggregator";
import type {
CachedSnsDto,
CachedSnsTokenMetadataDto,
} from "$lib/types/sns-aggregator";
import { SnsSwapLifecycle } from "@dfinity/sns";
import { nonNullish } from "@dfinity/utils";
import { derived, writable, type Readable } from "svelte/store";
Expand Down Expand Up @@ -35,10 +38,53 @@ export const snsAggregatorIncludingAbortedProjectsStore =
export const snsAggregatorStore: SnsAggregatorStore = derived(
snsAggregatorIncludingAbortedProjectsStore,
(store) => ({
data: store.data?.filter(
(sns) =>
nonNullish(sns.lifecycle) &&
sns.lifecycle.lifecycle !== SnsSwapLifecycle.Aborted
),
data: store.data
?.filter(
(sns) =>
nonNullish(sns.lifecycle) &&
sns.lifecycle.lifecycle !== SnsSwapLifecycle.Aborted
)
.map(fixBrokenSnsMetadataBasedOnId),
})
);

// TODO: Find a better way to fix broken SNS metadata.
const brokenSnsOverrides: Record<
string,
{ name: string; tokenSymbol: string }
> = {
// Overrided for CYCLES_TRANSFER_STATION as discussed in https://dfinity.slack.com/archives/C039M7YS6F6/p1733302975333649
"ibahq-taaaa-aaaaq-aadna-cai": {
name: "CYCLES_TRANSFER_STATION",
tokenSymbol: "CTS",
},
};

const fixBrokenSnsMetadataBasedOnId = (sns: CachedSnsDto): CachedSnsDto => {
const override = brokenSnsOverrides[sns.list_sns_canisters.root];
if (!nonNullish(override)) return sns;
const newMeta = {
...sns.meta,
name: `${sns.meta.name} (formerly ${override.name})`,
};

const newIcrc1Metadata = sns.icrc1_metadata.map<
[string, CachedSnsTokenMetadataDto[0][1]]
>(([name, value]) => {
if (name === "icrc1:symbol" && "Text" in value) {
return [
name,
{
Text: `${value.Text} (${override.tokenSymbol})`,
},
];
}
return [name, value];
});

return {
...sns,
meta: { ...newMeta },
icrc1_metadata: [...newIcrc1Metadata],
};
};
55 changes: 55 additions & 0 deletions frontend/src/tests/lib/stores/sns-aggregator.store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,59 @@ describe("sns-aggregator store", () => {
expect(get(snsAggregatorStore).data).toEqual(nonAbortedData);
});
});

describe("brokenSnsOverrides", () => {
const withBrokenSns = ({
sns,
rootCanisterId,
}: {
sns: CachedSnsDto;
rootCanisterId: string;
}) => ({
...sns,
list_sns_canisters: {
...sns.list_sns_canisters,
root: rootCanisterId,
},
});

it("should override information for SNS with rootCanisterId ibahq-taaaa-aaaaq-aadna-cai", () => {
const mockedSns = aggregatorMockSnsesDataDto[0];
const brokenSns = withBrokenSns({
sns: {
...mockedSns,
meta: {
...mockedSns.meta,
name: "---",
},
icrc1_metadata: [...mockedSns.icrc1_metadata].map(([name, value]) => {
if (name === "icrc1:symbol" && "Text" in value) {
return [
name,
{
Text: "---",
},
];
}
return [name, value];
}),
},
rootCanisterId: "ibahq-taaaa-aaaaq-aadna-cai",
});

const data = [brokenSns];
snsAggregatorIncludingAbortedProjectsStore.setData(data);
expect(
get(snsAggregatorIncludingAbortedProjectsStore).data[0].meta.name
).toBe("---");
expect(
get(snsAggregatorIncludingAbortedProjectsStore).data[0]
.icrc1_metadata[3][1]
).toEqual({ Text: "---" });

const result = get(snsAggregatorStore).data[0];
expect(result.meta.name).toBe("--- (formerly CYCLES_TRANSFER_STATION)");
expect(result.icrc1_metadata[3][1]).toEqual({ Text: "--- (CTS)" });
});
});
});

0 comments on commit e4d3667

Please sign in to comment.