Код: Выделить всё
Connected to: https://starknet-sepolia.public.blastapi.io/rpc/v0_7
Error fetching USD/ETH price: Cannot use 'in' operator to search for 'blockIdentifier' in null
Код: Выделить всё
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 = [
'https://starknet-sepolia.public.blastapi.io/rpc/v0_7',
'https://free-rpc.nethermind.io/sepolia-juno/v0_7',
'https://starknet-mainnet.public.blastapi.io/rpc/v0_7',
'https://json-rpc.starknet-mainnet.public.lavanet.xyz',
'https://free-rpc.nethermind.io/mainnet-juno/v0_7',
];
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();
Любая критика или помощь будут оценены.
Подробнее здесь: https://stackoverflow.com/questions/793 ... blockident