/** * The script is used by Chainlink nodes to execute the external adapter logic. * * It will call the pricing API and return the price of a house. * * API requirements: * - API key */ const { ethers } = await import("npm:ethers@6.10.0"); if (!secrets.apiKey) { throw new Error( "FRED API KEY is required to access the API data. Please provide one." ); } const API_URL = `https://api.stlouisfed.org/fred/series/observations?series_id=DPRIME&api_key=${secrets.apiKey}&file_type=json&sort_order=desc&limit=1`; console.log(`Making request to: ${API_URL}`); console.log(`API Key present: ${!!secrets.apiKey}`); const primeRateRequest = await Functions.makeHttpRequest({ url: API_URL, }); console.log(`Response status: ${primeRateRequest.status}`); console.log(`Response data: ${JSON.stringify(primeRateRequest.data)}`); if (!primeRateRequest.data) { throw new Error(`API request failed with status ${primeRateRequest.status}. Response: ${JSON.stringify(primeRateRequest)}`); } if (!primeRateRequest.data?.observations || primeRateRequest.data.observations.length === 0) { throw new Error(`API response missing observations field. Full response: ${JSON.stringify(primeRateRequest.data)}`); } // The response from the API is expected to be in JSON format const primeRate = primeRateRequest.data.observations[0].value; // returns e.g 7.5 return Functions.encodeUint256(ethers.parseUnits(primeRate.toString(), 25));