Skip to content

Commit

Permalink
Optimizations
Browse files Browse the repository at this point in the history
* PropType the whole codebase.
* Optimize Indexer
  • Loading branch information
anudit committed Sep 12, 2021
1 parent 3bc4d41 commit 38138fa
Show file tree
Hide file tree
Showing 21 changed files with 180 additions and 102 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"react"
],
"rules": {
"react/prop-types": 0
// "react/prop-types": 0
},
"globals": {
"process": true
Expand Down
18 changes: 13 additions & 5 deletions components/Card.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useColorModeValue, Box, Flex, Text, Divider } from "@chakra-ui/react";
import { ArrowForwardIcon } from "@chakra-ui/icons";
import PropTypes from 'prop-types';

const Card = (props) => {
const Card = ({icon, title, children, internalLink}) => {

return (
<Flex
Expand All @@ -28,7 +29,7 @@ const Card = (props) => {
p={2}
mr={4}
>
{props.icon}
{icon}
</Box>

<Text
Expand All @@ -41,17 +42,17 @@ const Card = (props) => {
pt={4}
mt={{ base: 2, md: 0 }}
>
{props.title}
{title}
</Text>

<Divider my={4} borderWidth="1px" borderColor={useColorModeValue("gray.200", "#3e3e3e")}/>

{props.children}
{children}

<Text
size="lg"
my={3}
as="a" href={`https://docs.theconvo.space/${props.internalLink}`}
as="a" href={`https://docs.theconvo.space/${internalLink}`}
aria-label="View Docs"
className="glow"
>
Expand All @@ -62,4 +63,11 @@ const Card = (props) => {
);
};

Card.propTypes = {
icon: PropTypes.element,
title: PropTypes.string,
internalLink: PropTypes.string,
children: PropTypes.element
}

export default Card;
5 changes: 5 additions & 0 deletions components/CodeBlock.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react"
import { chakra, Code, useColorMode } from "@chakra-ui/react"
import PropTypes from 'prop-types';

export const MakeOwnCodeBlock = (props) => {

Expand All @@ -15,3 +16,7 @@ export const MakeOwnCodeBlock = (props) => {
)

}

MakeOwnCodeBlock.propTypes = {
value: PropTypes.string
}
2 changes: 1 addition & 1 deletion components/CustomAvatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const CustomAvatar = (props) => {

CustomAvatar.propTypes = {
address: PropTypes.string,
ensName: PropTypes.string,
ensName: PropTypes.node,
badgesize: PropTypes.string,
size: PropTypes.string,
mr: PropTypes.number
Expand Down
2 changes: 1 addition & 1 deletion components/CustomButtons.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const CustomButton = (props) => {
};

CustomButton.propTypes = {
children: PropTypes.element
children: PropTypes.array
}

export default CustomButton;
2 changes: 1 addition & 1 deletion components/DashboardShell.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const PageShell = ({title, children}) => {
};
PageShell.propTypes = {
title:PropTypes.string,
children:PropTypes.element
children:PropTypes.array
}

const DashboardShell = ({title, active, children}) => {
Expand Down
2 changes: 1 addition & 1 deletion components/PageShell.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ PageShell.propTypes = {
title:PropTypes.string,
metaImageLink:PropTypes.string,
align:PropTypes.string,
children:PropTypes.element
children:PropTypes.array
}

export default PageShell;
23 changes: 9 additions & 14 deletions components/ThreadView.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,15 @@ export const ThreadView = ({link, threads, exploreAll}) => {
const { isOpen, onOpen, onClose } = useDisclosure()
const searchText = useRef()
const newThreadTitleRef = useRef()
const newThreadUrlRef = useRef()
const toast = useToast()
const [title, setTitle] = useState("");
const [modalUrl, setModalUrl] = useState("");

const [url, setUrl] = useState(null);

const [searchQuery, setSearchQuery] = useState("");

function updateSearchQuery(event){
setSearchQuery(event.target.value.toLowerCase());
setSearchQuery(event.target.value);
}

useEffect(() => {
Expand All @@ -38,8 +36,8 @@ export const ThreadView = ({link, threads, exploreAll}) => {
}, [router.query]);

useEffect(() => {
console.log('Got', link);
if (link.slice(0, 7) == "http://" || link.slice(0, 8) == "https://"){
console.log('Got', link, Boolean(link) === true );
if (Boolean(link) === true && (link.slice(0, 7) === "http://" || link.slice(0, 8) === "https://")){
try {
const urlObj = new URL(link);
let tempurl = urlObj['origin'] + urlObj['pathname'];
Expand All @@ -52,15 +50,17 @@ export const ThreadView = ({link, threads, exploreAll}) => {
console.log(error);
}
}
else {
setUrl(false)
}
}, [link]);

async function handleModal() {
setTitle(searchText.current.value);
setModalUrl(url);
onOpen();
}

async function creatNewThread () {
async function creatNewThread() {

if (signerAddress !== "") {

Expand All @@ -81,7 +81,7 @@ export const ThreadView = ({link, threads, exploreAll}) => {
'createdOn': Date.now().toString(),
'creator': signerAddress,
'title': title,
'url': url,
'url': 'https://theconvo.space/',
};
let threadId = await createThread(data);
let newData = {
Expand Down Expand Up @@ -169,11 +169,6 @@ export const ThreadView = ({link, threads, exploreAll}) => {
<FormLabel>What&apos;s the thread about?</FormLabel>
<Input placeholder="Thread Title" ref={newThreadTitleRef} max={300} isRequired={true} defaultValue={title}/>
</FormControl>
<br/>
<FormControl>
<FormLabel>URL?</FormLabel>
<Input placeholder="Link about the topic" ref={newThreadUrlRef} max={300} isRequired={false} defaultValue={modalUrl}/>
</FormControl>
</ModalBody>

<ModalFooter>
Expand Down Expand Up @@ -207,7 +202,7 @@ export const ThreadView = ({link, threads, exploreAll}) => {
<Stack spacing={1}>
{
threads && threads.filter((thread) => {
return thread.title.toLowerCase().search(searchQuery) >= 0 || thread.creator.toLowerCase().search(searchQuery) >= 0 || thread.url.toLowerCase().search(searchQuery) >= 0
return thread.title.toLowerCase().search(searchQuery.toLowerCase()) >= 0 || thread.creator.toLowerCase().search(searchQuery.toLowerCase()) >= 0 || thread.url.toLowerCase().search(searchQuery.toLowerCase()) >= 0
}).map((thread) => (
<ThreadCard threadData={thread} key={thread._id}/>
))
Expand Down
2 changes: 0 additions & 2 deletions cron/backupFilecoin.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,5 @@ getData().then((data)=>{
console.log("✅ Backed up Data to NFT.Storage");
await pinToPinata(ipfsHash);
console.log("✅ Replicated Backup to Pinata");
await pinToInfura(ipfsHash);
console.log("✅ Replicated Backup to Infura");
});
})
79 changes: 40 additions & 39 deletions cron/cacheTrustScore.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ async function getSybil(address) {
const query = new Where('_id').eq(getAddress(address));
let resp = await threadClient.find(threadId, 'cachedSybil', query);
return resp;

}

async function checkPoH(address, provider) {
Expand Down Expand Up @@ -423,7 +422,7 @@ async function getRaribleData(address = "") {
}
};

let response = await fetch("https://api-mainnet.rarible.com/marketplace/api/v4/items", {
let response = await fetch("https://api-mainnet.rarible.com/marketplace/api/v2/items", {
"headers": {
"accept": "*/*",
"content-type": "application/json",
Expand All @@ -432,7 +431,6 @@ async function getRaribleData(address = "") {
"body": JSON.stringify(body),
})
let artworks = await response.json();

let totalCountSold = artworks.length;

let totalAmountSold = 0;
Expand All @@ -445,6 +443,7 @@ async function getRaribleData(address = "") {
totalCountSold-=1;
}
}

return {
totalCountSold,
totalAmountSold
Expand Down Expand Up @@ -536,43 +535,39 @@ async function getEthPrice() {

}

function avg(array) {
var total = 0;
var count = 0;

array.forEach(function(item) {
total += item;
count++;
});

return total / count;
}

async function calculateScore(address) {

let matic_price_data = await fetch(`https://api.nomics.com/v1/currencies/ticker?key=d6c838c7a5c87880a3228bb913edb32a0e4f2167&ids=MATIC&interval=1d&convert=USD&per-page=100&page=1%27`).then(async (data)=>{return data.json()}) ;
GLOBAL_MATIC_PRICE = parseFloat(matic_price_data[0].price);

let tp = new ethers.providers.AlchemyProvider("mainnet","hHgRhUVdMTMcG3_gezsZSGAi_HoK43cA");

let startDate;
if (DEBUG === true){ startDate = new Date(); }

let promiseArray = [
checkPoH(address, tp),
fetcher(`https://app.brightid.org/node/v5/verifications/Convo/${address.toLowerCase()}`, "GET", {}),
fetcher(`https://api.poap.xyz/actions/scan/${address}`, "GET", {}),
tp.lookupAddress(address),
fetcher(`https://api.idena.io/api/Address/${address}`, "GET", {}),
];

if (DEBUG === true){var startDate = new Date(); }
let results1 = await Promise.allSettled(promiseArray);
if (DEBUG === true){
var endDate = new Date();
console.log('results1', (endDate.getTime() - startDate.getTime()) / 1000, 's')
}
let promiseArray2 = [
fetcher(`https://api.cryptoscamdb.org/v1/check/${address}`, "GET", {}),
checkUnstoppableDomains(address),
getSybil(address),
fetcher(`https://backend.deepdao.io/user/${address.toLowerCase()}`, "GET", {}),
fetcher(`https://0pdqa8vvt6.execute-api.us-east-1.amazonaws.com/app/task_progress?address=${address}`, "GET", {}),
];

if (DEBUG === true){ startDate = new Date(); }
let results2 = await Promise.allSettled(promiseArray2);
if (DEBUG === true){
endDate = new Date();
console.log('results2', (endDate.getTime() - startDate.getTime()) / 1000, 's')
}

let promiseArray3 = [
getEthPrice(),
getFoundationData(address), // * ethPrice
getSuperrareData(address),
Expand All @@ -584,17 +579,13 @@ async function calculateScore(address) {
getZoraData(address) // * ethPrice
];

if (DEBUG === true){ startDate = new Date(); }
let results3 = await Promise.allSettled(promiseArray3);
let results = await Promise.allSettled(promiseArray);

if (DEBUG === true){
endDate = new Date();
console.log('results3', (endDate.getTime() - startDate.getTime()) / 1000, 's')
let endDate = new Date();
console.log('results', (endDate.getTime() - startDate.getTime()) / 1000, 's')
}

let results = results1.concat(results2);
results = results.concat(results3);
// console.log(results);

let score = 0;
let retData = {
'success': true,
Expand Down Expand Up @@ -655,7 +646,7 @@ async function calculateScore(address) {
score += results[2].value.length;
}
if(Boolean(results[3].value) === true){ // ens
score += 12;
score += 2;
}
if(Boolean(results[4].value?.result) === true){ // idena
score += 1;
Expand All @@ -664,7 +655,7 @@ async function calculateScore(address) {
score -= 20;
}
if(Boolean(results[6].value) === true){ // unstoppable domains
score += 1;
score += 2;
}
if(results[7].value?.length > 0){ // uniswap sybil
score += 10;
Expand Down Expand Up @@ -703,17 +694,28 @@ const cacheTrustScores = async () => {
const threadClient = await getClient();
const threadId = ThreadID.fromString(TEXTILE_THREADID);

let times = [];

for (let index = 0; index < addresses.length; index++) {
let startDate;
if (DEBUG === true){ startDate = new Date(); }

let data = await getTrustScore(addresses[index]);
await threadClient.save(threadId, 'cachedTrustScores', [{
'_id': getAddress(addresses[index]),
...data
}]);

let endDate = new Date();
let td = (endDate.getTime() - startDate.getTime()) / 1000;
times.push(td);

if (index%10 == 0){
console.log(`🟢 Cached Chunk#${parseInt(index/10)}`);
}
}
console.log(`⚠️ erroredAddresses ${erroredAddresses}`);
console.log(`ℹ️ Avg Time: ${avg(times)}`);

}

Expand Down Expand Up @@ -745,17 +747,17 @@ const validateSchema = async () =>{
}

const cacheTrustScoresManual = async (addresses = []) => {
DEBUG=false;
DEBUG=true;
const threadClient = await getClient();
const threadId = ThreadID.fromString(TEXTILE_THREADID);

for (let index = 0; index < addresses.length; index++) {
let data = await getTrustScore(addresses[index]);
// console.log(data);
await threadClient.save(threadId, 'cachedTrustScores', [{
'_id': getAddress(addresses[index]),
...data
}]);
console.log(data);
// await threadClient.save(threadId, 'cachedTrustScores', [{
// '_id': getAddress(addresses[index]),
// ...data
// }]);
console.log(`🟢 Cached ${index}`);
}
}
Expand Down Expand Up @@ -789,5 +791,4 @@ cacheTrustScores().then(()=>{

// validateSchema();
// updateSchema();

// cacheTrustScoresManual(["0xa28992A6744e36f398DFe1b9407474e1D7A3066b", "0x707aC3937A9B31C225D8C240F5917Be97cab9F20", "0x8df737904ab678B99717EF553b4eFdA6E3f94589"]);
Loading

1 comment on commit 38138fa

@vercel
Copy link

@vercel vercel bot commented on 38138fa Sep 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.