admin管理员组

文章数量:1123600

I am making use of starknet.js, and i am making a contract call to Pragmas contract. however running myscript returns the following error:

Connected to: 
Error fetching USD/ETH price: Cannot use 'in' operator to search for 'blockIdentifier' in null

The script in question is as follows:

import { RpcProvider, Contract } from 'starknet';
import fs from 'fs';
import path from 'path';

const contractABIPath = path.resolve('./contractabi.json');
const contractABI = JSON.parse(fs.readFileSync(contractABIPath, 'utf-8'));

console.log(contractABI);

async function fetchUSDToETHPrice() {
  const providers = [
    '',
    '',
    '',
    '',
    '',
  ];

  let provider;
  for (const nodeUrl of providers) {
    try {
      provider = new RpcProvider({ nodeUrl });
      console.log(`Connected to: ${nodeUrl}`);
      break;
    } catch (error) {
      console.warn(`Failed to connect to ${nodeUrl}: ${error.message}`);
    }
  }

  if (!provider) {
    throw new Error('Failed to connect to any RPC provider.');
  }

  const contractAddress =
    '0x02a85bd616f912537c50a49a4076db02c00b29b2cdc8a197ce92ed1837fa875b';

  const contract = new Contract(contractABI, contractAddress, provider);

  const baseCurrencyId = '4346947'; // Ensure this ID is correct as per contract's logic
  const quoteCurrencyId = '4543560'; // Ensure this ID is correct as per contract's logic
  const aggregationMode = 'Median'; // Make sure this is a valid option in the contract
  const typeofData = 'price';
  const expirationTimestamp = null; // If no expiration, pass null or use a valid timestamp

  try {
    // Ensure method exists and is callable
    if (!contract.get_data_with_USD_hop) {
      throw new Error(
        'Method get_data_with_USD_hop not found in the contract.'
      );
    }

    const response = await contract.get_data_with_USD_hop(
      baseCurrencyId,
      quoteCurrencyId,
      aggregationMode,
      typeofData,
      expirationTimestamp
    );

    if (!response || response.price == null) {
      throw new Error('Invalid response: price data is missing.');
    }

    const humanReadablePrice = response.price / 1e18;
    console.log(`Price of USD/ETH: ${humanReadablePrice}`);
  } catch (error) {
    console.error('Error fetching USD/ETH price:', error.message);
  }
}

fetchUSDToETHPrice();

I have looked at other questions on SOF but I haven't had any luck in finding out what is causing the error? My assumption is that i am handling the json incorrectly but that cant be the case because it prints out the js object just fine.

Any criticisms or help would be appreciated.

本文标签: