From 8a9726d8d09f486d155e06bb82b8236edf9af265 Mon Sep 17 00:00:00 2001 From: hthieu1110 Date: Thu, 8 Aug 2024 05:28:03 +0700 Subject: [PATCH] feat: use `r/demo/profile` for gno user info (#1250) * use gno profile for user upp * fix: refactor to be compatible with existing structure * chore: remove unused code + adjust code * chore: add missing commit * chore: adjust code * chore: remove unneeded changes * feat: test4 upp support Signed-off-by: Norman Meier * fix: revert invalid change Signed-off-by: Norman Meier --------- Signed-off-by: Norman Meier Co-authored-by: Norman Meier --- networks.json | 7 +++-- packages/hooks/useNSNameInfo.ts | 44 +++++++++++++++++++++++++-- packages/networks/gno-dev/index.ts | 1 + packages/networks/gno-portal/index.ts | 1 + packages/networks/gno-test4/index.ts | 1 + packages/networks/types.ts | 1 + 6 files changed, 50 insertions(+), 5 deletions(-) diff --git a/networks.json b/networks.json index 12a222e74e..ce66418722 100644 --- a/networks.json +++ b/networks.json @@ -4516,6 +4516,7 @@ "groupsPkgPath": "gno.land/r/teritori/groups", "votingGroupPkgPath": "gno.land/p/teritori/dao_voting_group", "daoProposalSinglePkgPath": "gno.land/p/teritori/dao_proposal_single", + "profilePkgPath": "gno.land/r/demo/profile", "daoInterfacesPkgPath": "gno.land/p/teritori/dao_interfaces", "daoCorePkgPath": "gno.land/p/teritori/dao_core", "nameServiceDefaultImage": "ipfs://bafkreignptjimiu7wuux6mk6uh4hb4odb6ff62ny4bvdokrhes7g67huse", @@ -4563,7 +4564,8 @@ "votingGroupPkgPath": "gno.land/p/teritori/dao_voting_group", "daoProposalSinglePkgPath": "gno.land/p/teritori/dao_proposal_single", "daoInterfacesPkgPath": "gno.land/p/teritori/dao_interfaces", - "daoCorePkgPath": "gno.land/p/teritori/dao_core" + "daoCorePkgPath": "gno.land/p/teritori/dao_core", + "profilePkgPath": "gno.land/r/demo/profile" }, { "id": "gno-teritori", @@ -4687,7 +4689,8 @@ "nameServiceDefaultImage": "ipfs://bafkreignptjimiu7wuux6mk6uh4hb4odb6ff62ny4bvdokrhes7g67huse", "socialFeedsPkgPath": "gno.land/r/teritori/social_feeds", "socialFeedsDAOPkgPath": "gno.land/r/teritori/social_feeds_dao", - "daoInterfacesPkgPath": "gno.land/p/teritori/dao_interfaces" + "daoInterfacesPkgPath": "gno.land/p/teritori/dao_interfaces", + "profilePkgPath": "gno.land/r/teritori/profile" }, { "id": "cosmos-registry:gravitybridge", diff --git a/packages/hooks/useNSNameInfo.ts b/packages/hooks/useNSNameInfo.ts index 4e53905d07..cc742ac9cb 100644 --- a/packages/hooks/useNSNameInfo.ts +++ b/packages/hooks/useNSNameInfo.ts @@ -16,6 +16,12 @@ export const nsNameInfoQueryKey = ( tokenId: string | null | undefined, ) => ["nsNameInfo", networkId, tokenId]; +type UserProfile = { + displayName: string; + bio: string; + avatar: string; +}; + export const useNSNameInfo = ( networkId: string | undefined, tokenId: string | null | undefined, @@ -32,7 +38,6 @@ export const useNSNameInfo = ( if (!network) { return null; } - switch (network?.kind) { case NetworkKind.Cosmos: { const nsClient = await getCosmosNameServiceQueryClient(networkId); @@ -64,12 +69,18 @@ export const useNSNameInfo = ( if (!address) { return null; } - // TODO: use profile realm + const profile = await gnoGetUserProfile(network, address); + const res: NftInfoResponse = { - extension: {}, + extension: { + public_bio: profile.bio, + public_name: profile.displayName, + image: profile.avatar, + }, }; return res; } + if (!tokenId.startsWith("gno.land/")) { return null; } @@ -101,6 +112,33 @@ export const useNSNameInfo = ( return { nsInfo, notFound: nsInfo === null, ...other }; }; +const gnoGetUserProfile = async (network: GnoNetworkInfo, address: string) => { + const provider = new GnoJSONRPCProvider(network?.endpoint); + const { profilePkgPath } = network; + if (!profilePkgPath) { + throw Error("profilePkgPath is not given"); + } + + const profileFields = ["DisplayName", "Bio", "Avatar"]; + const promises = profileFields.map((field) => + provider.evaluateExpression( + profilePkgPath, + `GetStringField("${address}","${field}","")`, + ), + ); + + const dataRaw = await Promise.all(promises); + const extractedData = dataRaw.map(extractGnoString); + + const profile: UserProfile = { + displayName: extractedData[0], + bio: extractedData[1], + avatar: extractedData[2], + }; + + return profile; +}; + const gnoGetAddressByUsername = async ( network: GnoNetworkInfo, name: string, diff --git a/packages/networks/gno-dev/index.ts b/packages/networks/gno-dev/index.ts index 3af1938c6e..85e1ae8e08 100644 --- a/packages/networks/gno-dev/index.ts +++ b/packages/networks/gno-dev/index.ts @@ -39,6 +39,7 @@ export const gnoDevNetwork: GnoNetworkInfo = { groupsPkgPath: "gno.land/r/teritori/groups", votingGroupPkgPath: "gno.land/p/teritori/dao_voting_group", daoProposalSinglePkgPath: "gno.land/p/teritori/dao_proposal_single", + profilePkgPath: "gno.land/r/demo/profile", daoInterfacesPkgPath: "gno.land/p/teritori/dao_interfaces", daoCorePkgPath: "gno.land/p/teritori/dao_core", diff --git a/packages/networks/gno-portal/index.ts b/packages/networks/gno-portal/index.ts index ac76c7d796..f357b66621 100644 --- a/packages/networks/gno-portal/index.ts +++ b/packages/networks/gno-portal/index.ts @@ -35,4 +35,5 @@ export const gnoPortalNetwork: GnoNetworkInfo = { daoProposalSinglePkgPath: "gno.land/p/teritori/dao_proposal_single", daoInterfacesPkgPath: "gno.land/p/teritori/dao_interfaces", daoCorePkgPath: "gno.land/p/teritori/dao_core", + profilePkgPath: "gno.land/r/demo/profile", }; diff --git a/packages/networks/gno-test4/index.ts b/packages/networks/gno-test4/index.ts index 70f51252ba..b5233667bf 100644 --- a/packages/networks/gno-test4/index.ts +++ b/packages/networks/gno-test4/index.ts @@ -25,4 +25,5 @@ export const gnoTest4Network: GnoNetworkInfo = { socialFeedsPkgPath: "gno.land/r/teritori/social_feeds", socialFeedsDAOPkgPath: "gno.land/r/teritori/social_feeds_dao", daoInterfacesPkgPath: "gno.land/p/teritori/dao_interfaces", + profilePkgPath: "gno.land/r/teritori/profile", }; diff --git a/packages/networks/types.ts b/packages/networks/types.ts index 626a7357df..1fcb3d2fd3 100644 --- a/packages/networks/types.ts +++ b/packages/networks/types.ts @@ -117,6 +117,7 @@ export type GnoNetworkInfo = NetworkInfoBase & { daoInterfacesPkgPath?: string; daoCorePkgPath?: string; groupsPkgPath?: string; + profilePkgPath?: string; faucetURL?: string; };