Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: changed the api url for luxy nft call #218

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 122 additions & 66 deletions packages/sysweb3-utils/src/nfts.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { BigNumber } from 'bignumber.js';
import { ethers } from 'ethers';

import {BigNumber} from 'bignumber.js'

import NFT_BALANCE_CHECKER_ABI from './abi/nft_balance_checker_abi.json';
import { createContractUsingAbi } from './contracts';
import { BaseChainConfig, getScanApiByType, timeoutFetch, toLowerCaseEquals } from './nfts_config';
import NFT_BALANCE_CHECKER_ABI from './abi/nft_balance_checker_abi.json'

import {
BaseChainConfig,
getScanApiByType,
timeoutFetch,
toLowerCaseEquals,
} from './nfts_config';

const LOAD_NFT_MAX = 400;

export const isSupportOpensea = (chainId: number) => {
return chainId === 1 || chainId === 137;
}
return chainId === 1 || chainId === 137;
};

export const isSupportLuxy = (chainId: number) => {
return chainId === 57 || chainId === 570;
}
};

export const getOwnerCollectiblesApi = (
chainId: number,
Expand All @@ -39,7 +42,7 @@ export const getOwnerCollectiblesApi = (

export const getCollectiblesByType = async (
walletAddress: string,
chainId: number,
chainId: number
) => {
const api = await getScanApiByType(
chainId,
Expand Down Expand Up @@ -70,13 +73,13 @@ export const fetchLuxyNFTs = async (
try {
const network = chainId === 57 ? 'Syscoin' : 'Rollux';

const formattedWalletAddress = ethers.utils.getAddress(walletAddress)
const formattedWalletAddress = ethers.utils.getAddress(walletAddress);

const limit = 50;
let page = 0;
let pagingFinish = false;
let collectibles: any[] = [];
let url = `https://backend.luxy.io/nft/by-owner/${formattedWalletAddress}?network=["${network}"]&page=${page}&limit=${limit}`;
let url = `https://backend.luxy.io/nft/by-owner/${formattedWalletAddress}?network=%5B"${network}"%5D&page=${page}&limit=${limit}`;

do {
const response = await fetch(url);
Expand All @@ -90,7 +93,7 @@ export const fetchLuxyNFTs = async (
pagingFinish = true;
}

url = `https://backend.luxy.io/nft/by-owner/${formattedWalletAddress}?network=["${network}"]&page=${page}&limit=${limit}`;
url = `https://backend.luxy.io/nft/by-owner/${formattedWalletAddress}?network=%5B"${network}"%5D&page=${page}&limit=${limit}`;
page += 1;
} while (!pagingFinish);

Expand All @@ -113,7 +116,7 @@ export const fetchLuxyNFTs = async (
export const getCollectiblesByOpensea = async (
chainId: number,
walletAddress: string,
rpcUrl: string,
rpcUrl: string
) => {
const collectibles = await getOwnerCollectibles(chainId, walletAddress);
if (!collectibles) {
Expand All @@ -130,7 +133,7 @@ export const getCollectiblesByOpensea = async (
export const detectCollectibles = async (
requestedSelectedAddress: string,
chainId: number,
rpcUrl: string,
rpcUrl: string
): Promise<INftsStructure[] | undefined> => {
let collectibles: INftsStructure[] | undefined;

Expand All @@ -148,7 +151,7 @@ export const detectCollectibles = async (
);
}

return collectibles
return collectibles;
};

export const getOwnerCollectibles = async (
Expand Down Expand Up @@ -216,7 +219,7 @@ export const fixDataCollectibles = async (
collectibles: any,
chainId: number,
walletAddress: string,
rpcUrl: string,
rpcUrl: string
): Promise<INftsStructure[] | undefined> => {
const erc721Tokens: string[] = [];
const erc721Ids: string[] = [];
Expand All @@ -243,19 +246,27 @@ export const fixDataCollectibles = async (
erc721Tokens,
erc721Ids,
rpcUrl,
chainId,
chainId
);
} catch (e) {
console.log('PPYang getERC721OwnersInSingleCall e:', e);
}
if (owners && owners.length === erc721Tokens.length) {
erc721Tokens.forEach((address, index) => {
if (toLowerCaseEquals(owners[index], walletAddress)) {
allOwners.push({ balanceOf: new BigNumber(1), address, token_id: erc721Ids[index] });
allOwners.push({
balanceOf: new BigNumber(1),
address,
token_id: erc721Ids[index],
});
}
});
} else {
console.log('PPYang getERC721OwnersInSingleCall length is not match:', owners?.length, erc721Tokens.length);
console.log(
'PPYang getERC721OwnersInSingleCall length is not match:',
owners?.length,
erc721Tokens.length
);
return undefined;
}
}
Expand All @@ -267,31 +278,45 @@ export const fixDataCollectibles = async (
erc1155Tokens,
erc1155Ids,
rpcUrl,
chainId,
chainId
);
} catch (e) {
console.log('PPYang getERC1155BalancesInSingleCall e:', e);
}
if (owners && owners.length === erc1155Tokens.length) {
erc1155Tokens.forEach((address, index) => {
if (owners[index]?.gt(0)) {
allOwners.push({ balanceOf: owners[index], address, token_id: erc1155Ids[index] });
allOwners.push({
balanceOf: owners[index],
address,
token_id: erc1155Ids[index],
});
}
});
} else {
console.log('PPYang getERC1155BalancesInSingleCall length is not match:', owners?.length, erc1155Tokens.length);
console.log(
'PPYang getERC1155BalancesInSingleCall length is not match:',
owners?.length,
erc1155Tokens.length
);
return undefined;
}
}

const ownedCollectibles: INftsStructure[] = [];
collectibles.forEach((collectible: any) => {
const owner = allOwners.find(
(item) => item.address === collectible.asset_contract.address && item.token_id === collectible.token_id
);
(item) =>
item.address === collectible.asset_contract.address &&
item.token_id === collectible.token_id
);

if (owner) {
ownedCollectibles.push({ ...collectible, balanceOf: owner.balanceOf, chainId });
ownedCollectibles.push({
...collectible,
balanceOf: owner.balanceOf,
chainId,
});
}
});

Expand All @@ -302,7 +327,7 @@ export const getERC721OwnersInSingleCall = async (
walletAddress: string,
tokens: string[],
ids: string[],
rpcUrl:string,
rpcUrl: string,
currentChainId: number,
targetChainId: number | undefined = undefined
): Promise<string[]> => {
Expand All @@ -329,7 +354,7 @@ export const getERC721OwnersInSingleCall = async (
sliced_ids,
rpcUrl,
currentChainId,
targetChainId as number,
targetChainId as number
);
result && allBalances.push(...result);
}
Expand All @@ -356,30 +381,43 @@ export const getERC721OwnersInSingleCallInternal = async (
if (!ADDRESS) {
return [];
}
const provider = new ethers.providers.JsonRpcProvider(rpcUrl)
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);

const contract = createContractUsingAbi(NFT_BALANCE_CHECKER_ABI, ADDRESS, provider);
const contract = createContractUsingAbi(
NFT_BALANCE_CHECKER_ABI,
ADDRESS,
provider
);
return new Promise<string[]>((resolve) => {
contract.owners([walletAddress], tokens, ids)
.then((result: string[]) => {
contract
.owners([walletAddress], tokens, ids)
.then((result: string[]) => {
let allBalances = [];

allBalances = result;
resolve(allBalances);
})
.catch((error: Error) => {
if(error) {
resolve([])
return
}
})

});
})
.catch((error: Error) => {
if (error) {
resolve([]);
return;
}
});
});
};

export const getERC1155BalancesInSingleCall = async (walletAddress: string, tokens: string[], ids: string[], rpcUrl: string, currentChainId: number, targetChainId: number | undefined = undefined): Promise<BigNumber[]> => {
export const getERC1155BalancesInSingleCall = async (
walletAddress: string,
tokens: string[],
ids: string[],
rpcUrl: string,
currentChainId: number,
targetChainId: number | undefined = undefined
): Promise<BigNumber[]> => {
if (targetChainId && targetChainId !== currentChainId) {
console.log(`getERC1155BalancesInSingleCall:Cannot match target chainId, current ${currentChainId} target ${targetChainId}`);
console.log(
`getERC1155BalancesInSingleCall:Cannot match target chainId, current ${currentChainId} target ${targetChainId}`
);
return [];
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand All @@ -389,21 +427,35 @@ export const getERC1155BalancesInSingleCall = async (walletAddress: string, toke
return [];
}



const allBalances = [];
for (let i = 0; i <= tokens.length / 500; i++) {
const sliced_tokens = tokens.slice(i * 500, (i + 1) * 500);
const sliced_ids = ids.slice(i * 500, (i + 1) * 500);
const result = await getERC1155BalancesInSingleCallInternal(walletAddress, sliced_tokens, sliced_ids,rpcUrl, currentChainId, targetChainId);
const result = await getERC1155BalancesInSingleCallInternal(
walletAddress,
sliced_tokens,
sliced_ids,
rpcUrl,
currentChainId,
targetChainId
);
result && allBalances.push(...result);
}
return allBalances;
}
};

export const getERC1155BalancesInSingleCallInternal = async(walletAddress: string, tokens: string[], ids: string[], rpcUrl: string, currentChainId: number, targetChainId: number | undefined = undefined): Promise<BigNumber[]> => {
export const getERC1155BalancesInSingleCallInternal = async (
walletAddress: string,
tokens: string[],
ids: string[],
rpcUrl: string,
currentChainId: number,
targetChainId: number | undefined = undefined
): Promise<BigNumber[]> => {
if (targetChainId && targetChainId !== currentChainId) {
console.log(`getERC1155BalancesInSingleCall:Cannot match target chainId, current ${currentChainId} target ${targetChainId}`);
console.log(
`getERC1155BalancesInSingleCall:Cannot match target chainId, current ${currentChainId} target ${targetChainId}`
);
return [];
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand All @@ -412,24 +464,28 @@ export const getERC1155BalancesInSingleCallInternal = async(walletAddress: strin
if (!ADDRESS) {
return [];
}
const provider = new ethers.providers.JsonRpcProvider(rpcUrl)
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);

const contract = createContractUsingAbi(NFT_BALANCE_CHECKER_ABI, ADDRESS, provider);
const contract = createContractUsingAbi(
NFT_BALANCE_CHECKER_ABI,
ADDRESS,
provider
);

return new Promise<BigNumber[]>((resolve) => {
contract.balances([walletAddress], tokens, ids)
.then((result: BigNumber[]) => {
resolve(result)
})
.catch((error: Error) => {
if(error) {
resolve([])
return
}

})
contract
.balances([walletAddress], tokens, ids)
.then((result: BigNumber[]) => {
resolve(result);
})
.catch((error: Error) => {
if (error) {
resolve([]);
return;
}
});
});
}
};

export interface IApiNftsCreator {
user: { username: string };
Expand All @@ -444,10 +500,10 @@ export interface IApiNftsLastSale {
}

export interface IApiNftsCollection {
name: string,
slug: string,
image_url: string,
description: string
name: string;
slug: string;
image_url: string;
description: string;
}

export interface INftsStructure {
Expand Down
7 changes: 0 additions & 7 deletions packages/sysweb3-utils/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2409,13 +2409,6 @@ eth-object@^1.0.3, "eth-object@https://github.com/syscoin/eth-object.git":
dependencies:
eth-util-lite "^1.0.1"

"eth-object@git+https://github.com/syscoin/eth-object.git":
version "1.0.3"
uid b5ff300f57c136138b31d6e570c816a147e0f1c9
resolved "git+https://github.com/syscoin/eth-object.git#b5ff300f57c136138b31d6e570c816a147e0f1c9"
dependencies:
eth-util-lite "^1.0.1"

eth-proof@^2.1.6:
version "2.1.6"
resolved "https://registry.yarnpkg.com/eth-proof/-/eth-proof-2.1.6.tgz#6c8a468f82334d9c79347324e6eb237f1ecc965f"
Expand Down
Loading