Skip to content

Commit

Permalink
New beta version (#388)
Browse files Browse the repository at this point in the history
* fix typo

* skale test

* refacto stats layout

* add ankr rpcs

* add network stat page

* add skale calypso deployment

* refacto

* fix wp mint color bug

* add addresses.txt

* force ipfs on mainnets

* viem fix avatar bug with ethers5

* add view-all action

* store stats to localStorage

* optimize localStorage walktrhough and add refresh button

* back to simple network stats

* suppress console.log's

* version 1.6.0

* various small fixes

* fix Mint test
  • Loading branch information
zapaz authored May 15, 2024
1 parent c8262f5 commit 8b985ef
Show file tree
Hide file tree
Showing 79 changed files with 1,693 additions and 752 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@
"workbench.layoutControl.enabled": true,
"workbench.statusBar.visible": true,
"favorites.sortOrder": "DESC",
"workbench.preferredLightColorTheme": "Kimbie Dark",
"workbench.preferredLightColorTheme": "Bluloco Light Italic",
"liveServer.settings.root": "/web/dapp/"
}
4 changes: 2 additions & 2 deletions common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
"@kredeum/skale": "workspace:^",
"ethers": "^5.7.2",
"multiformats": "^13.1.0",
"viem": "^2.9.26"
"viem": "^2.9.32"
},
"devDependencies": {
"@ipld/dag-pb": "^4.1.0",
"@types/findup-sync": "^4.0.5",
"@types/node": "^20.12.7",
"@types/node": "^20.12.8",
"findup-sync": "^5.0.0",
"tslib": "^2.6.2",
"typescript": "^5.4.5"
Expand Down
14 changes: 14 additions & 0 deletions common/scripts/ens/ethers5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { providers } from "ethers";

const main = async () => {
const rpcUrl = "https://rpc.ankr.com/eth";
const provider = new providers.JsonRpcProvider(rpcUrl);

const ensName = await provider.resolveName("zapaz.eth");
console.log("ensName:", ensName);

const ensAvatar = await provider.getAvatar("zapaz.eth");
console.log("ensAvatar:", ensAvatar);
};

main().catch(console.error);
24 changes: 24 additions & 0 deletions common/scripts/ens/viem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createPublicClient, http } from "viem";
import { normalize } from "viem/ens";
import { mainnet } from "viem/chains";

export const publicClient = createPublicClient({
chain: mainnet,
transport: http()
});

const address = "0x981ab0D817710d8FFFC5693383C00D985A3BDa38";

const main = async () => {
const ensName = await publicClient.getEnsName({ address });
console.log("ensName :", ensName);

if (!ensName) return;

const ensAvatar = await publicClient.getEnsAvatar({
name: normalize(ensName)
});
console.log("ensAvatar:", ensAvatar);
};

main().catch(console.error);
6 changes: 3 additions & 3 deletions common/src/apis/api-covalent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ const _covalentFetch = async (chainId: number, path: string): Promise<unknown> =
Accept: "application/json"
}
};
console.info("_covalentFetch <==", urlPath, "\n", config);
// console.log("_covalentFetch <==", urlPath, "\n", config);

const answerCov: FetchResponse = await fetchJson(urlPath, config);

if (answerCov.error) console.error("_covalentFetch ERROR", answerCov.error);

console.info("_covalentFetch ==>", answerCov?.data);
// console.log("_covalentFetch ==>", answerCov?.data);
return answerCov?.data;
};

Expand Down Expand Up @@ -162,6 +162,6 @@ const covalentNftList = async (
return nfts;
};

const covalentActive = (chainId: number): boolean => Boolean(networks.get(chainId)?.covalent?.active);
const covalentActive = (chainId: number): boolean => !(networks.get(chainId)?.covalent?.active === false);

export { covalentCollections, covalentNftList, covalentActive, _covalentFetch };
4 changes: 2 additions & 2 deletions common/src/collection/collection-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { collectionMerge } from "../collection/collection-get";
import { alchemyActive, alchemyCollections } from "../apis/api-alchemy";
import { covalentActive, covalentCollections } from "../apis/api-covalent";
import { thegraphActive, thegraphCollections } from "../apis/api-thegraph";
import { resolverFilterCollections, resolverGetCollections } from "../resolver/resolver-get-collection";
import { resolverFilterCollections, resolverGetCollectionsInfos } from "../resolver/resolver-get-collection";
import { networks } from "../common/networks";
// import { infuraActive, infuraCollections } from "../apis/api-infura";

Expand Down Expand Up @@ -67,7 +67,7 @@ const collectionList = async (
const removed = lengthBefore - collectionsApi.size;
if (removed > 0) console.info("collectionList collectionsApi removed", removed);

collectionsResolver = await resolverGetCollections(chainId, account);
collectionsResolver = await resolverGetCollectionsInfos(chainId, account);
// console.log("collectionList collectionsResolver", collectionsResolver);

// MERGE collectionsApi and collectionsResolver
Expand Down
8 changes: 3 additions & 5 deletions common/src/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,13 @@ const getOpenBound = (chainId: number): string => getAddresses(chainId)?.OpenBou
const hasOpenBound = (chainId: number): boolean => isAddress(getOpenBound(chainId));

// GET Dapp Url
const getDappUrl = (
chainId: number,
ref: NftType | { address?: string; tokenID?: string } = {},
base = "."
): string => {
const getDappUrl = (chainId: number, ref: RefPageType = {}, base = "."): string => {
let dappUrl = `${base}/#/${chainId}`;
if (isAddress(ref.address)) {
dappUrl += `/${ref.address}`;
if (isNumeric(ref.tokenID)) dappUrl += `/${ref.tokenID}`;
if (isAddress(ref.account)) dappUrl += `@${ref.account}`;
if (ref.action) dappUrl += `|${ref.action}`;
}
return dappUrl;
};
Expand Down
3 changes: 3 additions & 0 deletions common/src/common/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const keyCollectionContract = (chainId: number, address: string, getSigner: bool
const keyNft = (chainId: number, address: string, tokenID: string, account?: string): string =>
`nft://${String(chainId)}/${address}/${tokenID}${account ? "@" + account : ""}`;

const keyStats = (chainId: number): string => `stats://${String(chainId)}`;

const keyNftInverse = (key: string): NftType => {
const [key1] = key.split("@");
const [chId, addr, tokenID] = key1.replace("nft://", "").split("/");
Expand All @@ -38,6 +40,7 @@ export {
keyCollectionInverse,
keyCollectionDefault,
keyCollectionContract,
keyStats,
keyNft,
keyNftInverse
};
9 changes: 5 additions & 4 deletions common/src/common/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,25 @@ const localStorageClear = (): void => _localStorage?.clear();

const localStorageKey = (index: number): string => _localStorage?.key(index) || "";

const localStorageLength = (): number => _localStorage?.length || 0;

const localStorageRemove = (key: string): void => _localStorage?.removeItem(key);

const localStorageInit = (): void => {
// console.log("localStorageInit");
if (!localStorageGet("KEY")) localStorageSet("KEY", "KEY_DEFAULT");
};

const localStorageKeys = (startsWith: string): string[] =>
Object.keys(localStorage).filter((key) => key.startsWith(startsWith));

export {
localNamespace,
localStorageKeys,
localConfigNamespace,
localStorageDefined,
localStorageSet,
localStorageGet,
localStorageKey,
localStorageInit,
localStorageRemove,
localStorageClear,
localStorageLength
localStorageClear
};
28 changes: 18 additions & 10 deletions common/src/common/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,20 @@ const networks = (() => {
const getAllSameType = (chainId: chainIdish): NetworkType[] =>
getAllActive().filter((nw: NetworkType) => isMainnet(nw.chainId) === isMainnet(chainId));

const getAllMainnets = (): NetworkType[] => getAllActive().filter((nw: NetworkType) => isMainnet(nw.chainId));
const getAllMainnetIds = (): number[] =>
getAllActive()
.filter((nw: NetworkType) => isMainnet(nw.chainId))
.map((nw) => nw.chainId);

const getAllTestnets = (): NetworkType[] => getAllActive().filter((nw: NetworkType) => isTestnet(nw.chainId));
const getAllTestnetIds = (): number[] =>
getAllActive()
.filter((nw: NetworkType) => isTestnet(nw.chainId))
.map((nw) => nw.chainId);

const getAllOpMainnets = (): NetworkType[] =>
getAllActive().filter((nw: NetworkType) => isOpStack(nw.chainId) && isMainnet(nw.chainId));
const getAllOpMainnetIds = (): number[] =>
getAllActive()
.filter((nw: NetworkType) => (isOpStack(nw.chainId) && isMainnet(nw.chainId)) || nw.chainId == 1)
.map((nw) => nw.chainId);

const get = (chainId: chainIdish): NetworkType | undefined => _networksMap?.get(Number(chainId));

Expand Down Expand Up @@ -56,9 +64,9 @@ const networks = (() => {
const isLayer2 = (chainId: chainIdish): boolean => !isLayer1(chainId);
const isOpStack = (chainId: chainIdish): boolean => get(chainId)?.opstack || false;

const getChainName = (chainId: chainIdish): string | undefined => get(chainId)?.chainName;
const getChainLabel = (chainId: chainIdish): string | undefined =>
get(chainId)?.chainLabel || strUpFirst(strSanitize(getChainName(chainId)));
const getChainName = (chainId: chainIdish): string => get(chainId)?.chainName || "";
const getChainLabel = (chainId: chainIdish): string =>
get(chainId)?.chainLabel || strUpFirst(strSanitize(getChainName(chainId))) || "";
const getMainnetName = (chainId: chainIdish) =>
isTestnet(chainId) ? getChainName(getLinkedMainnet(chainId)) : getChainName(chainId);

Expand All @@ -74,9 +82,9 @@ const networks = (() => {
getAllActive,
getAllInactive,
getAllSameType,
getAllMainnets,
getAllTestnets,
getAllOpMainnets,
getAllMainnetIds,
getAllTestnetIds,
getAllOpMainnetIds,

getBlur,
getBlurUrl,
Expand Down
6 changes: 4 additions & 2 deletions common/src/nft/nft-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,18 @@ const nftListTokenIds = async (
} else if (covalentActive(chainId)) {
nftsOwner = await covalentNftList(chainId, collection, filter);
} else {
console.error("No NFTs found:-(");
console.error(`No NFT API found:-( on ${chainId}`);
}

({ nfts: nftsKredeum } = await resolverGetNfts(chainId, collection, filter));
}
// console.log("nftListTokenIds", nfts);

// MERGE nftsOwner and nftsKredeum
nfts = nftsMerge(nftsOwner, nftsKredeum);

if (nfts.size === 0) console.error("No NFT found:-(");

// console.log("nftListTokenIds", nfts);
return nfts;
};

Expand Down
4 changes: 2 additions & 2 deletions common/src/nft/nft-uri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ipfsConfigValid, ipfsPin, ipfsTokenUri } from "../storage/ipfs";
///////////////////////////////////////////////////////////////////////////////////
// GET image uri
const nftPin = async (chainId: number, media: string): Promise<string> => {
if (ipfsConfigValid()) return ipfsPin(media);
if (ipfsConfigValid(chainId)) return ipfsPin(media);
if (swarmConfigValid(chainId)) return swarmPin(media);
return "";
};
Expand All @@ -26,7 +26,7 @@ const nftTokenUri = async (
pdfUri = ""
): Promise<string> => {
let nftTokenUriFunc;
if (ipfsConfigValid()) nftTokenUriFunc = ipfsTokenUri;
if (ipfsConfigValid(chainId)) nftTokenUriFunc = ipfsTokenUri;
if (swarmConfigValid(chainId)) nftTokenUriFunc = swarmTokenUri;

return nftTokenUriFunc
Expand Down
54 changes: 36 additions & 18 deletions common/src/resolver/resolver-get-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,51 +58,69 @@ const resolverAreCollections = async (chainId: number, collections: Array<string
return checks;
};

const resolverGetCollections = async (
// const resolverGetCollections = async (chainId: number): Promise<CollectionType[]> => {
// console.log("resolverGetCollections >", chainId);
// const collections: CollectionType[] = [];

// const nftsResolver = await resolverGetContract(chainId);
// const colls = await nftsResolver.getCo();

// console.log("resolverGetCollections <", collections);
// return collections;
// };

const resolverGetCollectionsAddresses = async (chainId: number): Promise<string[]> => {
// console.log("resolverGetCollectionsAddresses", chainId);

const nftsResolver = await resolverGetContract(chainId);

const addresses = await nftsResolver.getAddresses();
// console.log("resolverGetCollectionsInfos openNFTsStructOutput", collectionInfos);

// console.log("resolverGetCollectionsAddresses", chainId, addresses);
return addresses;
};

const resolverGetCollectionsInfos = async (
chainId: number,
account = ADDRESS_ZERO
): Promise<Map<string, CollectionType>> => {
// console.log(`resolverGetCollections ${keyCollections(chainId, account)}\n`, chainId, account);
// console.log("resolverGetCollectionsInfos >", chainId, account);

const collections: Map<string, CollectionType> = new Map();

const nftsResolver = await resolverGetContract(chainId);

const collectionInfos = await nftsResolver.getOpenNFTsCollectionsInfos(account);
// console.log("resolverGetCollections openNFTsStructOutput", collectionInfos);
const collectionsInfos = await nftsResolver.getOpenNFTsCollectionsInfos(account);
// console.log("resolverGetCollectionsInfos openNFTsStructOutput", collectionsInfos);

if (collectionInfos[0].length !== collectionInfos[1].length) {
console.error("ERROR resolverGetCollections", collectionInfos);
if (collectionsInfos[0].length !== collectionsInfos[1].length) {
console.error("ERROR resolverGetCollectionsInfos", collectionsInfos);
}

for (let index = 0; index < collectionInfos[0].length; index++) {
for (let index = 0; index < collectionsInfos[0].length; index++) {
const collection = resolverConvOpenNFTsCollectionInfos(
chainId,
[collectionInfos[0][index], collectionInfos[1][index]],
[collectionsInfos[0][index], collectionsInfos[1][index]],
account
);
// console.log("collection", collection);
collections.set(keyCollection(chainId, collection.address), collection);
}

// console.log(`resolverGetCollections ${chainId} ${account}\n`, collections);
// console.log("resolverGetCollectionsInfos <", collections);
return collections;
};

const countCollectionsCache = new Map<number, number>();
const resolverCountCollections = async (chainId: number): Promise<number | undefined> => {
if (!countCollectionsCache.has(chainId)) {
const nftsResolver = await resolverGetContract(chainId);
const countCollections = Number(await nftsResolver.countAddresses());
countCollectionsCache.set(chainId, countCollections);
}

return countCollectionsCache.get(chainId);
const nftsResolver = await resolverGetContract(chainId);
return Number(await nftsResolver.countAddresses());
};

export {
resolverCountCollections,
resolverGetCollections,
resolverGetCollectionsInfos,
resolverGetCollectionsAddresses,
resolverGetCollection,
resolverAreCollections,
resolverFilterCollections,
Expand Down
2 changes: 1 addition & 1 deletion common/src/storage/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const ipfsGatewayLink = (ipfs: string): string => urlToLink(ipfsGatewayUrl(ipfs)
/////////////////////////////////////////////////////////////////////////////////////////////////////

// TEST if ipfs config or params are valid
const ipfsConfigValid = (): boolean => storageDefault() == "ipfs" && ipfsParamsValid();
const ipfsConfigValid = (chainId: number): boolean => storageDefault(chainId) == "ipfs" && ipfsParamsValid();
const ipfsParamsValid = (): boolean => storageParamsValid(storageParamsGet("ipfs"));

// GET ipfs gateway
Expand Down
4 changes: 3 additions & 1 deletion common/src/storage/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ipfsGateway, ipfsGatewayUrl, ipfsLinkToUrlHttp, ipfsGetLink } from "./i
import { swarmGateway, swarmGatewayUrl, swarmLinkToUrlHttp, swarmGetLink } from "./swarm";
import config from "@kredeum/config/dist/config.json";
import { localConfigNamespace, localStorageGet, localStorageSet } from "../common/local";
import { networks } from "../common/networks";

// IN MEMORY Storage Config
let _storageConfig: StorageConfigType;
Expand Down Expand Up @@ -43,7 +44,8 @@ const storageConfigGet = (): StorageConfigType => {
};

// GET storage default
const storageDefault = (): string => storageConfigGet().default;
// only IPFS on Mainnets
const storageDefault = (chainId: number): string => (networks.isMainnet(chainId) ? "ipfs" : storageConfigGet().default);

// GET storage params
const storageParamsGet = (type: StorageType): StorageParamsType | undefined => {
Expand Down
2 changes: 1 addition & 1 deletion common/src/storage/swarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const swarmGateway = (): string => storageParamsGet("swarm")?.gateway.replace(/\
const SWARM_ZERO_APIKEY = "0000000000000000000000000000000000000000000000000000000000000000";

// TEST if swarm config or params are valid
const swarmConfigValid = (chainId: number): boolean => storageDefault() == "swarm" && swarmParamsValid(chainId);
const swarmConfigValid = (chainId: number): boolean => storageDefault(chainId) == "swarm" && swarmParamsValid(chainId);
const swarmParamsValid = (chainId: number): boolean => {
const swarmParams = storageParamsGet("swarm");

Expand Down
Loading

0 comments on commit 8b985ef

Please sign in to comment.