Skip to content

Commit

Permalink
New version v1.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
zapaz committed May 16, 2024
2 parents 003e38a + 1667ea3 commit 4e1c694
Show file tree
Hide file tree
Showing 104 changed files with 3,740 additions and 1,406 deletions.
5 changes: 0 additions & 5 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
[submodule "contracts/lib/forge-std"]
path = contracts/lib/forge-std
url = https://github.com/foundry-rs/forge-std
branch = v1
[submodule "contracts/lib/openzeppelin-contracts"]
path = contracts/lib/openzeppelin-contracts
url = https://github.com/openzeppelin/openzeppelin-contracts
branch = release-v4.8
[submodule "contracts/lib/openzeppelin-contracts-upgradeable"]
path = contracts/lib/openzeppelin-contracts-upgradeable
url = https://github.com/openzeppelin/openzeppelin-contracts-upgradeable
branch = release-v4.8
[submodule "contracts/lib/OpenNFTs"]
path = contracts/lib/OpenNFTs
url = https://github.com/kredeum/OpenNFTs
branch = main
[submodule "contracts/lib/forge-deploy-lite"]
path = contracts/lib/forge-deploy-lite
url = https://github.com/zapaz/forge-deploy-lite
branch = dev
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@
"avoid-sha3": "warn"
},
"window.zoomLevel": 0,
"workbench.colorTheme": "Night Owl Light",
"workbench.colorTheme": "Kiwi Light (rainglow)",
"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/"
}
5 changes: 3 additions & 2 deletions common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
"@kredeum/config": "workspace:^",
"@kredeum/contracts": "workspace:^",
"@kredeum/providers": "workspace:^",
"@kredeum/skale": "workspace:^",
"ethers": "^5.7.2",
"multiformats": "^13.1.0",
"viem": "^2.9.21"
"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);
13 changes: 7 additions & 6 deletions common/src/apis/api-covalent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import config from "@kredeum/config/dist/config.json";

import type { FetchResponse } from "../common/fetch";
import type { CollectionFilterType, CollectionType, NftType } from "../common/types";
import { ADDRESS_ZERO, getChecksumAddress } from "../common/config";
import { ADDRESS_ZERO, getChecksumAddress, isAddressNotZero } from "../common/config";
import { DEFAULT_NAME, DEFAULT_SYMBOL } from "../common/config";
import { fetchJson, FETCH_LIMIT } from "../common/fetch";
import { keyCollection, keyNft } from "../common/keys";
Expand All @@ -25,23 +25,23 @@ 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;
};

const covalentCollections = async (chainId: number, account: string): Promise<Map<string, CollectionType>> => {
// console.log(`covalentCollections ${keyCollection(chainId, account)}\n`);
// console.log(`covalentCollections ${chainId} ${account}\n`);

const collections: Map<string, CollectionType> = new Map();
const chainName = networks.getChainName(chainId);

if (!(chainId && chainName && account)) return collections;
if (!(chainId && chainName && isAddressNotZero(account))) return collections;

// const match =
// eslint-disable-next-line quotes
Expand All @@ -50,6 +50,7 @@ const covalentCollections = async (chainId: number, account: string): Promise<Ma
// `address/${account}/balances_v2/` + "?nft=true" + "&no-nft-fetch=false" + `&match=${encodeURIComponent(match)}`;

const path = `address/${account}/balances_nft/?no-spam=true&no-nft-asset-metadata=true&with-uncached=true`;
// console.log("covalentCollections ~ path:", path);

type CollectionCov = {
contract_name: string;
Expand Down Expand Up @@ -162,6 +163,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/apis/api-thegraph.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BigNumber } from "ethers";

import type { CollectionType, CollectionFilterType, NftType } from "../common/types";
import { ADDRESS_ZERO, getChecksumAddress } from "../common/config";
import { ADDRESS_ZERO, getChecksumAddress, isAddressNotZero } from "../common/config";

import { fetchGQL, FETCH_LIMIT } from "../common/fetch";
import { keyCollection, keyNft } from "../common/keys";
Expand Down Expand Up @@ -71,7 +71,7 @@ const thegraphCollections = async (chainId: number, account: string): Promise<Ma
const collections: Map<string, CollectionType> = new Map();
const network = networks.get(chainId);

if (account) {
if (isAddressNotZero(account)) {
const query = `
{
ownerPerTokenContracts(
Expand Down
10 changes: 3 additions & 7 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 @@ -35,7 +35,7 @@ const collectionList = async (
account?: string,
mintable?: boolean
): Promise<Map<string, CollectionType>> => {
// console.log(`collectionList ${keyCollections(chainId, account)}\n`);
// console.log(`collectionList ${chainId} ${account}\n`);

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

Expand All @@ -55,10 +55,6 @@ const collectionList = async (
collectionsApi = await covalentCollections(chainId, account);
// console.log("collectionList covalentCollections", collectionsApi);
}
// else if (infuraActive(chainId)) {
// collectionsApi = await infuraCollections(chainId, account);
// // console.log("collectionList infuraCollections", collectionsApi);
// }

const lengthBefore = collectionsApi.size;
// console.log("collectionsApi BEFORE", collectionsApi);
Expand All @@ -67,7 +63,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
17 changes: 10 additions & 7 deletions common/src/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,21 @@ const addresses = JSON.parse(JSON.stringify(addressesRaw));
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-return
const getAddresses = (chainId: number | string): AddressesType | undefined => addresses[String(chainId)];

const getAddressOpenNFTsTemplate = (chainId: number): string => getAddresses(chainId)?.OpenNFTsV4 || "";
const getAddressOpenAutoMarket = (chainId: number): string => getAddresses(chainId)?.OpenAutoMarket || "";

// GET OpenMulti address
const getOpenBound = (chainId: number): string => getAddresses(chainId)?.OpenBound || "";
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 Expand Up @@ -113,11 +114,11 @@ const textShort = (str: string, n = 16, p = n): string => {
return str.substring(0, n) + (l < n ? "" : "..." + (p > 0 ? str.substring(l - p, l) : ""));
};

const getShortAddress = (address: string, n = 8): string =>
const getShortAddress = (address: string, n = 4): string =>
address
? address.endsWith(".eth")
? textShort(address, 2 * n, 0)
: textShort(getChecksumAddress(address), n, n)
: textShort(getChecksumAddress(address), n + 2, n)
: "?";

// GENERIC helpers
Expand Down Expand Up @@ -362,6 +363,8 @@ export {
getShortAddress,
getChecksumAddress,
getAddresses,
getAddressOpenNFTsTemplate,
getAddressOpenAutoMarket,
getOpenBound,
hasOpenBound,
getDappUrl,
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
};
36 changes: 30 additions & 6 deletions common/src/common/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,25 @@ const networks = (() => {

// const _getMap = (): Map<number, NetworkType> | undefined => _networksMap;
const _getAll = (): NetworkType[] => [...(_networksMap?.values() || [])];
const _getAllActive = (): NetworkType[] => _getAll().filter((nw: NetworkType) => !(nw.active === false));
const getAllInactive = (): NetworkType[] => _getAll().filter((nw: NetworkType) => nw.active === false);
const getAllActive = (): NetworkType[] => _getAll().filter((nw: NetworkType) => !(nw.active === false));
const getAllSameType = (chainId: chainIdish): NetworkType[] =>
_getAllActive().filter((nw: NetworkType) => isMainnet(nw.chainId) === isMainnet(chainId));
getAllActive().filter((nw: NetworkType) => isMainnet(nw.chainId) === isMainnet(chainId));

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

const getAllTestnetIds = (): number[] =>
getAllActive()
.filter((nw: NetworkType) => isTestnet(nw.chainId))
.map((nw) => 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 All @@ -38,17 +54,19 @@ const networks = (() => {
const getRpcUrl = (chainId: chainIdish): string => get(chainId)?.rpcUrls[0] || "";

const has = (chainId: chainIdish): boolean => _networksMap?.has(Number(chainId));
const hasPaymaster = (chainId: chainIdish): boolean => get(chainId)?.paymaster || false;

const isActive = (chainId: chainIdish): boolean => get(chainId)?.active || false;
const isActive = (chainId: chainIdish): boolean => get(chainId)?.active || true;
const isEip1559 = (chainId: chainIdish): boolean => Boolean(get(chainId)?.eip1559);
const isMainnet = (chainId: chainIdish): boolean => getLinkedMainnet(chainId) == 0;
const isTestnet = (chainId: chainIdish): boolean => !isMainnet(chainId);
const isLayer1 = (chainId: chainIdish): boolean => getLinkedLayer1(chainId) === undefined;
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 @@ -58,9 +76,15 @@ const networks = (() => {

return {
has,
hasPaymaster,
get,

getAllActive,
getAllInactive,
getAllSameType,
getAllMainnetIds,
getAllTestnetIds,
getAllOpMainnetIds,

getBlur,
getBlurUrl,
Expand Down
11 changes: 9 additions & 2 deletions common/src/common/provider-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import type { WindowExternalProvider } from "./types";
import { getChecksumAddress, sleep } from "../common/config";
import { ethers } from "ethers";
import { networks } from "./networks";
import { receiveFunds } from "@kredeum/skale";

let _providerSetting = false;

// Cache providers per chainId
const _providersPerChainId: Map<number, Provider> = new Map();

const providerGetWindow = async (chainId = 0): Promise<Web3Provider | undefined> => {
const providerGetWindow = async (chainId: number = 0): Promise<Web3Provider | undefined> => {
const externalProvider = (window as WindowExternalProvider).ethereum || undefined;
if (!externalProvider) return undefined;

Expand All @@ -31,8 +32,14 @@ const providerGetWindow = async (chainId = 0): Promise<Web3Provider | undefined>

const providerGetSigner = async (chainId = 0, accountOrIndex: string | number = 0): Promise<Signer | undefined> => {
const provider = await providerGetWindow(chainId);
if (!provider) return;

return provider && provider.getSigner(accountOrIndex);
const signer = provider.getSigner(accountOrIndex);
const signerAddress = await signer.getAddress();

await receiveFunds(signerAddress, chainId);

return signer;
};

const providerGetAccount = async (): Promise<string> => {
Expand Down
Loading

0 comments on commit 4e1c694

Please sign in to comment.