Skip to content
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
84 changes: 84 additions & 0 deletions packages/extension/src/libs/nft-handlers/conflux.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { NFTCollection, NFTType, NFTItem } from '@/types/nft';
import { NodeType } from '@/types/provider';
import cacheFetch from '../cache-fetch';
import { NetworkNames } from '@enkryptcom/types';
import {
ConfluxResponse,
ConfluxNFTItem,
NFTBalanceItem,
ListResponse,
NFTItemWithDesc,
} from './types/conflux';

const CONFLUX_ENDPOINT = 'https://evmapi.confluxscan.org';
const CACHE_TTL = 60 * 1000;

// will fetch most 100 collections
// every collection will have most 100 items
export default async (
network: NodeType,
address: string,
): Promise<NFTCollection[]> => {
if (network.name !== NetworkNames.Conflux)
throw new Error('Conflux: network not supported');
let allItems: NFTCollection[] = [];

let nftBalances: ConfluxResponse = await cacheFetch(
{
url: `${CONFLUX_ENDPOINT}/nft/balances?owner=${address}&skip=0&limit=100`,
},
CACHE_TTL,
);
if (nftBalances.status !== '1' || nftBalances.result.total === 0) {
return [];
}

for (let collection of nftBalances.result
.list as unknown as NFTBalanceItem[]) {
let contract = collection.contract;

let items = await getCollectionItems(contract, address);
if (!items.length) {
continue;
}

let nftCollection: NFTCollection = {
name: collection.name,
image: collection.iconUrl || '',
description: items[0].description,
contract,
items,
};
allItems.push(nftCollection);
}

return allItems;
};

async function getCollectionItems(
contract: string,
address: string,
): Promise<NFTItemWithDesc[]> {
let itemResponse: ConfluxResponse = await cacheFetch(
{
url: `${CONFLUX_ENDPOINT}/nft/tokens?contract=${contract}&owner=${address}&skip=0&limit=100&withBrief=${true}&withMetadata=${true}&suppressMetadataError=${true}`,
},
CACHE_TTL,
);

if (itemResponse.status !== '1') {
return [];
}
let items: ConfluxNFTItem[] = itemResponse.result
.list as unknown as ConfluxNFTItem[];

return items.map(item => ({
name: item.name,
id: item.tokenId,
contract: item.contract,
image: item.image,
description: item.description,
url: `https://evm.confluxscan.org/nft/${item.contract}/${item.tokenId}`,
type: item.type === 'ERC721' ? NFTType.ERC721 : NFTType.ERC1155,
}));
}
39 changes: 39 additions & 0 deletions packages/extension/src/libs/nft-handlers/types/conflux.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { NFTItem } from '@/types/nft';

export interface ConfluxNFTItem {
owner: string;
contract: string;
tokenId: string;
amount: string;
type: string;
name: string;
image: string;
description: string;
rawData: object;
}

export interface NFTBalanceItem {
owner: string;
contract: string;
balance: string;
name: string;
symbol: string;
type: string;
website: string | null;
iconUrl: string | null;
}

export interface ListResponse {
total: number;
list: object[];
}

export interface ConfluxResponse {
status: string;
message: string;
result: ListResponse;
}

export interface NFTItemWithDesc extends NFTItem {
description: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const NetworkEndpoints: Record<string, string> = {
[NetworkNames.Scroll]: 'https://api.scrollscan.com/',
[NetworkNames.Fraxtal]: 'https://api.fraxscan.com/',
[NetworkNames.Coti]: 'https://mainnet.cotiscan.io/',
[NetworkNames.Conflux]: 'https://evmapi.confluxscan.org/',
};

export { NetworkEndpoints };
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,11 @@ const supportedNetworks: Record<SupportedNetworkNames, SupportedNetwork> = {
[NetworkNames.Unichain]: {
tbName: 'uni',
cgPlatform: CoingeckoPlatform.Unichain,
}
},
[NetworkNames.Conflux]: {
tbName: 'cfx',
cgPlatform: CoingeckoPlatform.Conflux,
},
};

const getTokens = (
Expand Down Expand Up @@ -274,15 +278,15 @@ export default (

const marketInfo = supportedNetworks[networkName].cgPlatform
? await marketData.getMarketInfoByContracts(
Object.keys(balances).filter(
contract => contract !== NATIVE_TOKEN_ADDRESS,
),
supportedNetworks[networkName].cgPlatform as CoingeckoPlatform,
)
Object.keys(balances).filter(
contract => contract !== NATIVE_TOKEN_ADDRESS,
),
supportedNetworks[networkName].cgPlatform as CoingeckoPlatform,
)
: tokens.reduce(
(obj, cur) => ({ ...obj, [cur.contract]: null }),
{} as Record<string, CoinGeckoTokenMarket | null>,
);
(obj, cur) => ({ ...obj, [cur.contract]: null }),
{} as Record<string, CoinGeckoTokenMarket | null>,
);
if (network.coingeckoID) {
const nativeMarket = await marketData.getMarketData([
network.coingeckoID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@ export type SupportedNetworkNames =
| NetworkNames.Ink
| NetworkNames.Story
| NetworkNames.Base
| NetworkNames.ImmutableZkevm;
| NetworkNames.ImmutableZkevm
| NetworkNames.Conflux;
29 changes: 29 additions & 0 deletions packages/extension/src/providers/ethereum/networks/conflux.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { EvmNetwork, EvmNetworkOptions } from '../types/evm-network';
import { NetworkNames } from '@enkryptcom/types';
import wrapActivityHandler from '@/libs/activity-state/wrap-activity-handler';
import { EtherscanActivity } from '../libs/activity-handlers';
import icon from './icons/conflux.png';
import assetsInfoHandler from '@/providers/ethereum/libs/assets-handlers/assetinfo-mew';
import NFTHandler from '@/libs/nft-handlers/conflux';

const confluxOptions: EvmNetworkOptions = {
name: NetworkNames.Conflux,
name_long: 'Conflux eSpace',
homePage: 'https://confluxnetwork.org/',
blockExplorerTX: 'https://evm.confluxscan.org/tx/[[txHash]]',
blockExplorerAddr: 'https://evm.confluxscan.org/address/[[address]]',
chainID: '0x406',
isTestNetwork: false,
currencyName: 'CFX',
currencyNameLong: 'CFX',
node: 'https://evm.confluxrpc.com',
icon,
NFTHandler,
coingeckoID: 'conflux',
assetsInfoHandler,
activityHandler: wrapActivityHandler(EtherscanActivity),
};

const conflux = new EvmNetwork(confluxOptions);

export default conflux;
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion packages/extension/src/providers/ethereum/networks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import abstractNode from './abstract';
import inkNode from './inkonchain';
import taraxa from './taraxa';
import coti from './coti';
import conflux from './conflux';

export default {
sepolia: sepoliaNode,
Expand Down Expand Up @@ -154,5 +155,6 @@ export default {
unichain: unichainNode,
abstract: abstractNode,
ink: inkNode,
coti: coti
coti: coti,
conflux: conflux,
};
2 changes: 2 additions & 0 deletions packages/types/src/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export enum NetworkNames {
Bera = "Bera",
Taraxa = "Taraxa",
Coti = "Coti",
Conflux = "Conflux",
}

export enum CoingeckoPlatform {
Expand Down Expand Up @@ -171,4 +172,5 @@ export enum CoingeckoPlatform {
Story = "story",
Ink = "ink",
Taraxa = "taraxa",
Conflux = "conflux",
}