Skip to content

Commit

Permalink
feat: use r/demo/profile for gno user info (#1250)
Browse files Browse the repository at this point in the history
* 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 <norman@samourai.coop>

* fix: revert invalid change

Signed-off-by: Norman Meier <norman@samourai.coop>

---------

Signed-off-by: Norman Meier <norman@samourai.coop>
Co-authored-by: Norman Meier <norman@samourai.coop>
  • Loading branch information
hthieu1110 and n0izn0iz committed Aug 7, 2024
1 parent 0406117 commit 8a9726d
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 5 deletions.
7 changes: 5 additions & 2 deletions networks.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
44 changes: 41 additions & 3 deletions packages/hooks/useNSNameInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -32,7 +38,6 @@ export const useNSNameInfo = (
if (!network) {
return null;
}

switch (network?.kind) {
case NetworkKind.Cosmos: {
const nsClient = await getCosmosNameServiceQueryClient(networkId);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions packages/networks/gno-dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions packages/networks/gno-portal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
};
1 change: 1 addition & 0 deletions packages/networks/gno-test4/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
};
1 change: 1 addition & 0 deletions packages/networks/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ export type GnoNetworkInfo = NetworkInfoBase & {
daoInterfacesPkgPath?: string;
daoCorePkgPath?: string;
groupsPkgPath?: string;
profilePkgPath?: string;
faucetURL?: string;
};

Expand Down

0 comments on commit 8a9726d

Please sign in to comment.