From 0a9725068e31557fd393227ccdebc64de5ac432f Mon Sep 17 00:00:00 2001 From: Hamish Peebles Date: Thu, 12 Oct 2023 16:41:13 +0100 Subject: [PATCH] Pull nervous system details from the Registry (#4571) --- .../src/services/openchatAgent.ts | 11 +++++-- .../src/services/registry/candid/idl.js | 8 +++++ .../src/services/registry/candid/types.d.ts | 8 +++++ .../src/services/registry/mappers.ts | 9 +++++- .../openchat-agent/src/utils/registryCache.ts | 2 +- frontend/openchat-client/src/utils/chat.ts | 1 + .../src/domain/registry/index.ts | 30 +++++++++++++------ 7 files changed, 56 insertions(+), 13 deletions(-) diff --git a/frontend/openchat-agent/src/services/openchatAgent.ts b/frontend/openchat-agent/src/services/openchatAgent.ts index a1ee1a3fd1..9eee28263e 100644 --- a/frontend/openchat-agent/src/services/openchatAgent.ts +++ b/frontend/openchat-agent/src/services/openchatAgent.ts @@ -25,7 +25,7 @@ import { LedgerClient } from "./ledger/ledger.client"; import { GroupIndexClient } from "./groupIndex/groupIndex.client"; import { MarketMakerClient } from "./marketMaker/marketMaker.client"; import { RegistryClient } from "./registry/registry.client"; -import { toRecord } from "../utils/list"; +import { distinctBy, toRecord } from "../utils/list"; import { measure } from "./common/profiling"; import { buildBlobUrl, @@ -2484,7 +2484,14 @@ export class OpenChatAgent extends EventTarget { if (updates.kind === "success" && updates.tokenDetails !== undefined) { const updated = { lastUpdated: updates.lastUpdated, - tokenDetails: updates.tokenDetails, + tokenDetails: distinctBy( + [...updates.tokenDetails, ...(current?.tokenDetails ?? [])], + (t) => t.ledgerCanisterId, + ), + nervousSystemDetails: distinctBy( + [...updates.nervousSystemDetails, ...(current?.nervousSystemDetails ?? [])], + (ns) => ns.governanceCanisterId, + ), }; setCachedRegistry(updated); return updated; diff --git a/frontend/openchat-agent/src/services/registry/candid/idl.js b/frontend/openchat-agent/src/services/registry/candid/idl.js index 764857639a..e366a073bd 100644 --- a/frontend/openchat-agent/src/services/registry/candid/idl.js +++ b/frontend/openchat-agent/src/services/registry/candid/idl.js @@ -22,10 +22,18 @@ export const idlFactory = ({ IDL }) => { 'symbol' : IDL.Text, 'transaction_url_format' : IDL.Text, }); + const NervousSystemSummary = IDL.Record({ + 'submitting_proposals_enabled' : IDL.Bool, + 'is_nns' : IDL.Bool, + 'governance_canister_id' : CanisterId, + 'proposal_rejection_fee' : IDL.Nat64, + 'ledger_canister_id' : CanisterId, + }); const UpdatesResponse = IDL.Variant({ 'Success' : IDL.Record({ 'last_updated' : TimestampMillis, 'token_details' : IDL.Opt(IDL.Vec(TokenDetails)), + 'nervous_system_details' : IDL.Vec(NervousSystemSummary), }), 'SuccessNoUpdates' : IDL.Null, }); diff --git a/frontend/openchat-agent/src/services/registry/candid/types.d.ts b/frontend/openchat-agent/src/services/registry/candid/types.d.ts index 5e4c670a90..95bcec3aed 100644 --- a/frontend/openchat-agent/src/services/registry/candid/types.d.ts +++ b/frontend/openchat-agent/src/services/registry/candid/types.d.ts @@ -863,6 +863,13 @@ export interface MessagesSuccessResult { export type Milliseconds = bigint; export type MultiUserChat = { 'Group' : ChatId } | { 'Channel' : [CommunityId, ChannelId] }; +export interface NervousSystemSummary { + 'submitting_proposals_enabled' : boolean, + 'is_nns' : boolean, + 'governance_canister_id' : CanisterId, + 'proposal_rejection_fee' : bigint, + 'ledger_canister_id' : CanisterId, +} export interface NnsCompletedCryptoTransaction { 'to' : NnsCryptoAccount, 'fee' : Tokens, @@ -1206,6 +1213,7 @@ export type UpdatesResponse = { 'Success' : { 'last_updated' : TimestampMillis, 'token_details' : [] | [Array], + 'nervous_system_details' : Array, } } | { 'SuccessNoUpdates' : null }; diff --git a/frontend/openchat-agent/src/services/registry/mappers.ts b/frontend/openchat-agent/src/services/registry/mappers.ts index d23ebf2db6..f1b3759dd5 100644 --- a/frontend/openchat-agent/src/services/registry/mappers.ts +++ b/frontend/openchat-agent/src/services/registry/mappers.ts @@ -8,7 +8,14 @@ export function updatesResponse(candid: ApiUpdatesResponse): RegistryUpdatesResp return { kind: "success", lastUpdated: candid.Success.last_updated, - tokenDetails: optional(candid.Success.token_details, (t) => t.map(tokenDetails)), + tokenDetails: optional(candid.Success.token_details, (t) => t.map(tokenDetails)) ?? [], + nervousSystemDetails: candid.Success.nervous_system_details.map((ns) => ({ + governanceCanisterId: ns.governance_canister_id.toString(), + ledgerCanisterId: ns.ledger_canister_id.toString(), + isNns: ns.is_nns, + proposalRejectionFee: ns.proposal_rejection_fee, + submittingProposalsEnabled: ns.submitting_proposals_enabled, + })), }; } if ("SuccessNoUpdates" in candid) { diff --git a/frontend/openchat-agent/src/utils/registryCache.ts b/frontend/openchat-agent/src/utils/registryCache.ts index 4bb47933ac..1dfcb05ffb 100644 --- a/frontend/openchat-agent/src/utils/registryCache.ts +++ b/frontend/openchat-agent/src/utils/registryCache.ts @@ -1,7 +1,7 @@ import { openDB, type DBSchema, type IDBPDatabase } from "idb"; import type { RegistryValue } from "openchat-shared"; -const CACHE_VERSION = 1; +const CACHE_VERSION = 2; const KEY = "registry"; let db: RegistryDatabase | undefined; diff --git a/frontend/openchat-client/src/utils/chat.ts b/frontend/openchat-client/src/utils/chat.ts index 1071e773e0..806d2a8420 100644 --- a/frontend/openchat-client/src/utils/chat.ts +++ b/frontend/openchat-client/src/utils/chat.ts @@ -1085,6 +1085,7 @@ export function mergeSendMessageResponse( token: msg.content.transfer.token, endDate: msg.content.endDate, caption: msg.content.caption, + diamondOnly: msg.content.diamondOnly, } as PrizeContent; break; } diff --git a/frontend/openchat-shared/src/domain/registry/index.ts b/frontend/openchat-shared/src/domain/registry/index.ts index 506fcc4d28..2a03113aab 100644 --- a/frontend/openchat-shared/src/domain/registry/index.ts +++ b/frontend/openchat-shared/src/domain/registry/index.ts @@ -1,19 +1,23 @@ -export type RegistryUpdatesResponse = RegistryUpdatesResponseSuccess | RegistryUpdatesResponseSuccessNoUpdates; +export type RegistryUpdatesResponse = + | RegistryUpdatesResponseSuccess + | RegistryUpdatesResponseSuccessNoUpdates; export type RegistryValue = { lastUpdated: bigint; - tokenDetails: TokenDetails[], -} + tokenDetails: TokenDetails[]; + nervousSystemDetails: NervousSystemSummary[]; +}; export type RegistryUpdatesResponseSuccess = { kind: "success"; lastUpdated: bigint; - tokenDetails?: TokenDetails[]; -} + tokenDetails: TokenDetails[]; + nervousSystemDetails: NervousSystemSummary[]; +}; export type RegistryUpdatesResponseSuccessNoUpdates = { kind: "success_no_updates"; -} +}; export type TokenDetails = { ledgerCanisterId: string; @@ -23,12 +27,20 @@ export type TokenDetails = { fee: bigint; logo: string; nervousSystem?: { - root: string - governance: string + root: string; + governance: string; }; infoUrl: string; howToBuyUrl: string; transactionUrlFormat: string; added: bigint; lastUpdated: bigint; -} +}; + +export type NervousSystemSummary = { + governanceCanisterId: string; + ledgerCanisterId: string; + isNns: boolean; + proposalRejectionFee: bigint; + submittingProposalsEnabled: boolean; +};