Skip to content

Commit

Permalink
Merge branch 'dev' into mainnet
Browse files Browse the repository at this point in the history
  • Loading branch information
serefyarar committed Jun 26, 2024
2 parents 1b2e882 + f533946 commit b6de992
Show file tree
Hide file tree
Showing 66 changed files with 2,434 additions and 1,431 deletions.
12 changes: 11 additions & 1 deletion api/src/controllers/discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,12 @@ export const questions = async (req, res, next) => {
const questionCache = await redis.get(`questions:${sourcesHash}`);

if (questionCache) {
return res.status(200).json(JSON.parse(questionCache));
let resp = JSON.parse(questionCache);
if (resp.questions) {
resp.questions = resp.questions.slice(0, 4);
}

return res.status(200).json(resp);
}

try {
Expand All @@ -208,6 +213,11 @@ export const questions = async (req, res, next) => {
redis.set(`questions:${sourcesHash}`, JSON.stringify(response.data), {
EX: 86400,
});

if (response.data.questions) {
response.data.questions = response.data.questions.slice(0, 4);
}

res.status(200).json(response.data);
} catch (error) {
return res.status(400).json({ error: error.message });
Expand Down
235 changes: 124 additions & 111 deletions api/src/controllers/meta.js
Original file line number Diff line number Diff line change
@@ -1,141 +1,154 @@
import dotenv from 'dotenv'
if(process.env.NODE_ENV !== 'production'){
dotenv.config()
import dotenv from "dotenv";
if (process.env.NODE_ENV !== "production") {
dotenv.config();
}

import { chains } from '../types/chains.js';
import { chains } from "../types/chains.js";

import { ethers } from 'ethers'
import { ethers } from "ethers";

import Moralis from 'moralis';
import Moralis from "moralis";

const ethProvider = new ethers.providers.InfuraProvider("mainnet", {
projectId: `75984fd56f8c4508a6f8e0cdb21c3edb`,
projectSecret: `uQJdKF6wMPvmF+213HwEHS9mU7ODPD5zm1yB/r/YkJK9JQNwSWZAGg`,
});

const ethProvider = new ethers.providers.InfuraProvider("mainnet")
import RedisClient from "../clients/redis.js";
const redis = RedisClient.getInstance();

export const getAvatar = async (ensName) => {
try {
const resolver = await ethProvider.getResolver(ensName);
const avatar = await resolver?.getText("avatar");
if (!avatar || avatar.length === 0) return null;
return avatar;
} catch (error) {
console.log(`get Avatar error`, error);
return null;
}
}
try {
const resolver = await ethProvider.getResolver(ensName);
const avatar = await resolver?.getText("avatar");
if (!avatar || avatar.length === 0) return null;
return avatar;
} catch (error) {
console.log(`get Avatar error`, error);
return null;
}
};

export const getENSProfileByWallet = async (wallet) => {
const walletData = await ethProvider.lookupAddress(wallet);
if(walletData){
let profile = {
ensName: walletData,
};
const avatar = await getAvatar(walletData)
if(avatar){
profile.image = avatar;
}
return profile;
const cache = await redis.hGet(`ens`, wallet);
if (cache) {
return JSON.parse(cache);
}
const walletData = await ethProvider.lookupAddress(wallet);
if (walletData) {
let profile = {
ensName: walletData,
};
const avatar = await getAvatar(walletData);
if (avatar) {
profile.image = avatar;
}
await redis.hSet(`ens`, wallet, JSON.stringify(profile));
return profile;
}
};
export const getWalletByENS = async (ens) => {
const walletData = await ethProvider.resolveName(ens);
return walletData;
const walletData = await ethProvider.resolveName(ens);
return walletData;
};
export const getCollectionMetadataApi = async (chainName, tokenAddress) => {
const chain = chains[chainName];


try {
const hexChain = '0x' + chain.chainId.toString(16);;
const response = await Moralis.EvmApi.nft.getNFTContractMetadata({
"chain": hexChain,
"address": tokenAddress
});

if(response && response.raw && response.raw.token_address) {
return {
symbol: response.raw.symbol,
name: response.raw.name,
token: response.raw.token_address,
tokenType: response.raw.contract_type
}
}
} catch (e) {
console.error(e);
const chain = chains[chainName];

try {
const hexChain = "0x" + chain.chainId.toString(16);
const response = await Moralis.EvmApi.nft.getNFTContractMetadata({
chain: hexChain,
address: tokenAddress,
});

if (response && response.raw && response.raw.token_address) {
return {
symbol: response.raw.symbol,
name: response.raw.name,
token: response.raw.token_address,
tokenType: response.raw.contract_type,
};
}



} catch (e) {
console.error(e);
}
};

export const getNftMetadataApi = async (chainName, tokenAddress, tokenId, resyncMetadata = false) => {
try {
const chain = chains[chainName];
const hexChain = '0x' + chain.chainId.toString(16);

const response = await Moralis.EvmApi.nft.getNFTMetadata({
"chain": hexChain,
"address": tokenAddress,
"normalizeMetadata": true,
"tokenId": tokenId
});

return {
metadata: {
name: response.raw.name,
image: response.raw.normalized_metadata.image
}
};

} catch (error) {
console.log(error)
//throw new Error('Failed to fetch NFT metadata');
}
export const getNftMetadataApi = async (
chainName,
tokenAddress,
tokenId,
resyncMetadata = false,
) => {
try {
const chain = chains[chainName];
const hexChain = "0x" + chain.chainId.toString(16);

const response = await Moralis.EvmApi.nft.getNFTMetadata({
chain: hexChain,
address: tokenAddress,
normalizeMetadata: true,
tokenId: tokenId,
});

return {
metadata: {
name: response.raw.name,
image: response.raw.normalized_metadata.image,
},
};
} catch (error) {
console.log(error);
//throw new Error('Failed to fetch NFT metadata');
}
};


export const getCollectionMetadataHandler = async (req, res) => {
const { chainName, tokenAddress } = req.params;
try {
const metadata = await getCollectionMetadataApi(chainName, tokenAddress);
if(metadata){
res.json(metadata);
}else{
res.status(404).json({ error: 'Contract not found' });
}

} catch (error) {
res.status(500).json({ error: error.message });
const { chainName, tokenAddress } = req.params;
try {
const metadata = await getCollectionMetadataApi(chainName, tokenAddress);
if (metadata) {
res.json(metadata);
} else {
res.status(404).json({ error: "Contract not found" });
}
} catch (error) {
res.status(500).json({ error: error.message });
}
};

export const getNftMetadataHandler = async (req, res) => {
const { chainName, tokenAddress, tokenId } = req.params;
const resyncMetadata = req.query.resyncMetadata || false;
try {
const contract = await getCollectionMetadataApi(chainName, tokenAddress);
if(!contract){
return res.status(404).json({ error: 'Contract not found' });
}
const tokenData = await getNftMetadataApi(chainName, tokenAddress, tokenId, resyncMetadata);

if(tokenData){
contract.token = tokenData.token_id;
}
res.json(contract);
} catch (error) {
res.status(500).json({ error: error.message });
const { chainName, tokenAddress, tokenId } = req.params;
const resyncMetadata = req.query.resyncMetadata || false;
try {
const contract = await getCollectionMetadataApi(chainName, tokenAddress);
if (!contract) {
return res.status(404).json({ error: "Contract not found" });
}
const tokenData = await getNftMetadataApi(
chainName,
tokenAddress,
tokenId,
resyncMetadata,
);

if (tokenData) {
contract.token = tokenData.token_id;
}
res.json(contract);
} catch (error) {
res.status(500).json({ error: error.message });
}
};

export const getWalletByENSHandler = async (req, res) => {
const { ensName } = req.params;
try {
const metadata = await getWalletByENS(ensName);
res.json({
walletAddress: metadata
});
} catch (error) {
res.status(500).json({ error: error.message });
}
const { ensName } = req.params;
try {
const metadata = await getWalletByENS(ensName);
res.json({
walletAddress: metadata,
});
} catch (error) {
res.status(500).json({ error: error.message });
}
};
9 changes: 0 additions & 9 deletions api/src/services/conversation.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,6 @@ export class ConversationService {
if (!conversation) {
return;
}
if (conversation && conversation.messages) {
for (const message of conversation.messages) {
try {
await this.deleteMessage(id, message.id);
} catch (e) {
console.error(e);
}
}
}

return await this.updateConversation(id, {
deletedAt: getCurrentDateTime(),
Expand Down
37 changes: 26 additions & 11 deletions api/src/services/did.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getCurrentDateTime } from "../utils/helpers.js";
import RedisClient from "../clients/redis.js";
import { IndexService } from "./index.js";
import { DIDSession } from "did-session";
import { getENSProfileByWallet } from "../controllers/meta.js";

const redisClient = RedisClient.getInstance();

Expand Down Expand Up @@ -178,9 +179,11 @@ export class DIDService {

const indexService = new IndexService(this.definition);

return uniqueIndexes
.filter((i) => i.did.owned || i.did.starred)
.map((i) => indexService.transformIndex(i));
return await Promise.all(
uniqueIndexes
.filter((i) => i.did.owned || i.did.starred)
.map(async (i) => await indexService.transformIndex(i)),
);
} catch (error) {
// Log the error and rethrow it for external handling
console.error("Exception occurred in createDIDIndex:", error);
Expand Down Expand Up @@ -279,12 +282,9 @@ export class DIDService {
throw new Error("Invalid response data");
}

const profileObj = data.createProfile.document;

profileObj.id = profileObj.controllerDID.id;
delete profileObj.controllerDID;

return profileObj;
return await this.getProfile(
data.createProfile.document.controllerDID.id,
);
} catch (error) {
// Log the error and rethrow it for external handling
console.error("Exception occurred in createProfile:", error);
Expand Down Expand Up @@ -315,14 +315,29 @@ export class DIDService {
}

if (!data.node.profile) {
return null;
const wallet = did.split(":").slice(-1).pop();
const ensProfile = await getENSProfileByWallet(wallet);
if (ensProfile) {
return {
id: did,
name: ensProfile.ensName,
avatar: ensProfile.image,
bio: "",
};
}
return {
id: did,
};
}

const profileObj = data.node.profile;

profileObj.id = profileObj.controllerDID.id;
delete profileObj.controllerDID;

if (profileObj.avatar) {
profileObj.avatar = `https://ipfs.io/ipfs/${profileObj.avatar}`;
}

return profileObj;
} catch (error) {
// Log the error and rethrow it for external handling
Expand Down
Loading

0 comments on commit b6de992

Please sign in to comment.