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

Real-Time NFT Price Validation #83

Open
Cherkaso8 opened this issue Dec 20, 2024 · 0 comments
Open

Real-Time NFT Price Validation #83

Cherkaso8 opened this issue Dec 20, 2024 · 0 comments

Comments

@Cherkaso8
Copy link

Hello,

I often use Zerion Wallet and noticed that NFT prices sometimes deviate from actual market prices. I couldn't find an API library for real-time price validation, and after some research, I believe this functionality could be useful to add. From what I understand, the Zerion API is hosted at https://api-staging.zerion.io (as per the URL in the code snippet you provided). This got me thinking: we could build a parallel system that checks the current prices of NFTs across various trusted marketplaces.

The issue is that the prices of NFTs provided by Zerion sometimes do not match the prices on popular NFT marketplaces. I propose a solution that will:

Fetch NFT prices from Zerion.
Compare those prices with real-time prices from several trusted marketplaces (OpenSea, Zora, SuperRare, MagicEden, NFTScan, etc.).
Display the price that is most commonly found across these marketplaces.

Zerion’s internal API is used to fetch initial NFT price data.
A parallel system will pull NFT prices from various trusted sources like OpenSea, Zora, SuperRare, MagicEden, NFTScan, and others.
The price discrepancies between Zerion and these sources will be identified.
The final price will be determined by comparing prices across the sources, choosing the price that appears most frequently among them.
The most accurate price will then be displayed in the Zerion Wallet interface.

So, my proposal is create something like that:

Crete new axios library

Then create functions to fetch prices from different marketplaces:

import axios from 'axios';

// OpenSea API Client
export const getOpenSeaPrice = async (tokenId) => {
  try {
    const response = await axios.get('https://api.opensea.io/api/v1/assets', {
      params: {
        token_ids: [tokenId],
        order_direction: 'desc',
        order_by: 'price',
        order_task: '1',
      },
    });
    const price = response.data.assets[0]?.sell_orders?.[0]?.current_price;
    return price ? parseFloat(price) : null;
  } catch (error) {
    console.error('OpenSea price error:', error);
    return null;
  }
};

// Zora API Client
export const getZoraPrice = async (tokenId) => {
  try {
    const response = await axios.get(`https://api.zora.co/v1/assets/${tokenId}`);
    return response.data?.price || null;
  } catch (error) {
    console.error('Zora price error:', error);
    return null;
  }
};

// SuperRare API Client
export const getSuperRarePrice = async (tokenId) => {
  try {
    const response = await axios.get(`https://api.superrare.com/api/v1/asset/${tokenId}`);
    return response.data?.price || null;
  } catch (error) {
    console.error('SuperRare price error:', error);
    return null;
  }
};

// MagicEden API Client
export const getMagicEdenPrice = async (tokenId) => {
  try {
    const response = await axios.get(`https://api.magiceden.io/v2/assets/${tokenId}`);
    return response.data?.price || null;
  } catch (error) {
    console.error('MagicEden price error:', error);
    return null;
  }
};

// NFTScan API Client
export const getNFTScanPrice = async (tokenId) => {
  try {
    const response = await axios.get(`https://api.nftscan.io/api/v1/nft/${tokenId}`);
    return response.data?.price || null;
  } catch (error) {
    console.error('NFTScan price error:', error);
    return null;
  }
};

// Add more API functions here for other platforms (e.g., Highlight, Element, ArtRun)

Create a function to fetch and compare prices from all the marketplaces:

import { getOpenSeaPrice, getZoraPrice, getSuperRarePrice, getMagicEdenPrice, getNFTScanPrice } from './apiClients';

// Function to fetch prices from all sources
const getPricesFromPlatforms = async (tokenId) => {
  const prices = await Promise.all([
    getOpenSeaPrice(tokenId),
    getZoraPrice(tokenId),
    getSuperRarePrice(tokenId),
    getMagicEdenPrice(tokenId),
    getNFTScanPrice(tokenId),
    // Add other API functions here if needed
  ]);

  return prices.filter(price => price !== null);
};

// Function to get the most frequent price
const getMostFrequentPrice = (prices) => {
  const priceCounts = prices.reduce((acc, price) => {
    acc[price] = (acc[price] || 0) + 1;
    return acc;
  }, {});

  return Object.entries(priceCounts).reduce((max, [price, count]) => 
    count > max.count ? { price, count } : max, { price: null, count: 0 }).price;
};

// Example usage
const getNFTPrice = async (tokenId) => {
  const prices = await getPricesFromPlatforms(tokenId);
  if (prices.length === 0) {
    return null; // No price data available
  }
  
  const mostFrequentPrice = getMostFrequentPrice(prices);
  return mostFrequentPrice;
};

// Test with a sample tokenId
const tokenId = 'exampleTokenId';
getNFTPrice(tokenId).then((price) => {
  console.log(`The most accurate price for NFT ${tokenId} is: ${price}`);
});

And integrate this price validation system with Zerion’s existing API to ensure that the price displayed is the most accurate one:

// zerionIntegration.js
import { client } from "../../";  // Assuming you're using Zerion's client library
import { getNFTPrice } from './priceComparison';  // Import the price comparison logic
import { address, url, apiToken } from "./config";  // Assuming these are defined elsewhere

client.configure({
  url,
  apiToken,
});

const currency = "usd";

client.addressPositions(
  {
    address,
    currency,
  },
  {
    onData: async (data) => {
      const assetsToDisplay = Object.values(data.positions.positions)
        .map(addressAsset => {
          // Assuming `addressAsset.tokenId` is available
          const tokenId = addressAsset.tokenId;
          return getNFTPrice(tokenId).then((marketplacePrice) => {
            return {
              ...addressAsset,
              price: marketplacePrice || addressAsset.price,  // Use marketplace price if available
            };
          });
        })
        .filter(asset => asset.value != null && asset.value > 1)
        .sort((a, b) => Number(b.value) - Number(a.value));

      // Wait for all the promises to resolve
      Promise.all(assetsToDisplay).then((assetsWithPrices) => {
        console.table(assetsWithPrices);  // Display updated asset prices
      });
    },
  }
);

By integrating this system into Zerion Wallet, users will get accurate and real-time NFT pricing data pulled from multiple trusted marketplaces. This will not only ensure the accuracy of NFT pricing displayed in Zerion but also improve user confidence in the platform.

Great wallet, great team.
Best regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant