Reputation: 21
I've been following Richard Gottleber's tutorial on creating a decentralized news service using Chainlink and ethers.js, deployed via the Astro framework. I finished the tutorial, reading from the contract I deployed, but now I'm trying to go one step further and adapt this slightly to fit my hackathon project. The next step is to do a write transaction, triggering the sendRequest function to fetch another news article. All attempts have failed, so I'm switching to Scaffold-Eth for a while.
Project Setup:
Goal:
Expand on the tutorial by adding functionality to fetch more articles using a sendRequest
function, which should trigger updates to displayed articles and then incorporate Chainlink's RNG for added randomness and D&D flavor.
Current Behavior: Data displays correctly initially as per the tutorial. However, attempts to add dynamic functionality (fetching more articles on button click) are not working.
Issues Encountered:
onclick
attributes in HTML to adding event listeners via JavaScript to ensure proper loading order and scope resolution.sendRequest
from a separate module into newsArchive.js
), faced issues with file not found errors.Error Messages:
404 Not Found
on sendRequest.js
or newArchive.js
when I moved the write call code there.GET http://localhost:4321/lib/sendRequest.js net::ERR_ABORTED 404 (Not Found)
(index):4 Uncaught ReferenceError: handleButtonClick is not defined at HTMLButtonElement.onclick ((index):4:1611)
Uncaught TypeError: Failed to resolve module specifier "ethers". Relative references must start with either "/", "./", or "../".
Code Sample (simplified version highlighting problematic parts) full code:
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdn.jsdelivr.net/npm/ethers/dist/ethers.min.js"></script>
</head>
<body>
<nav>
<button id="fetchButton">Fetch More, Chainlink</button>
</nav>
<script type="module">
import { sendRequest } from '/newsArchive.js';
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('fetchButton').addEventListener('click', async function() {
console.log('Fetching more articles...');
try {
const newArticles = await sendRequest();
console.log('New articles fetched:', newArticles);
} catch (error) {
console.log('error fetching new articles', error);
}
});
});
</script>
</body>
</html>
Questions:
Any insights, particularly those familiar with Astro and blockchain development, would be greatly appreciated. I'm currently planning to just shift my code into Scaffold-ETH and continue that way.
JavaScript Module Management
newsArchive.js
were located in the src/lib
directory. To resolve module path issues, I tried moving these files to the public
directory of the Astro project, expecting them to be more accessible to the client-side scripts.<script>
tag's type attribute from module
to text/javascript
and back, attempting to address the issues related to ES module imports not being recognized in the browser.Event Handling
onclick
attributes within HTML tags for interaction handling. Moved to adding event listeners via JavaScript to ensure they are attached after the DOM is fully loaded and the necessary JavaScript is initialized. This was supposed to fix issues with functions not being recognized at runtime.DOMContentLoaded
event to ensure that they are not initialized before the entire page is loaded, thereby preventing any timing issues.Error Handling and Debugging
File Path and Import Issues
Security Concerns
Despite these efforts, I am still facing:
Upvotes: 1
Views: 367
Reputation: 11
Assuming I understand correctly, you're having issues triggering sendRequest(), fetching articles, and ethers being defined, so I have the following feedback based off what I understand to be your issues.
Please feel free to clarify any issues I may have missed
Ensure the environment variables for PROVIDER_URL
and CONTRACT_ADDRESS
are set correctly.
PROVIDER_URL
="https://sepolia.drpc.org" is what I used for troubleshooting.Ensure the subscriptionId
on the deployed consumer aligns with a subscription you created, which may be found here for the subscriptionId you have in contracts/functions.sol
, which may be found here for subscriptionId #2456.
Ensure you are connected to the wallet address that owns the deployed consumer.
Make use of an error report within the console when an onclick request fails to act as anticipated and respond here with the output you see in your console.
Also, what does submitting ones preferences do in this case? I noticed the button for setting preferences, but unsure of the desired outcome and thus implementation.
Note: I was able to get the articles to show for the consumer I deployed, however I understand this does not resolve your issue, but should let you know you're moving in the right direction.
Shows Articles: https://github.com/BunsDev/ceptor-tech-warmup-functions/tree/help
Upvotes: 1