π₯οΈBuilding A Frontend
We've got the contract setup and use Chainlink Functions to pull in the latest news article. We can see the titles, links, and dates published in Remix. That's awesome but not super user-friendly. Let's add a simplistic front end to present this data.
π Initiate Liftoff
All of the code for this project can be found on GitHub
We'll be using Astro to build out the frontend. Let's create a skeleton project.
npm create astro@latest

Head to cd ./distributed-news and run npm run dev. You should now be able to access Astro at http://localhost:4321, if you visit that site, Astro should greet you. The basics are in place.
Read From The Blockchain
To read our data from the smart contract we deployed, we'll need to create 2 files. To do that, we must install a few NPM packages first. If your server is running, stop it with Ctrl-C, and run
to add them to our project. Now, we can move to creating our files.
First, we'll get the ABI (Application Binary Interface) from Remix. This will allow ethers.js to know how to interact with our contract. To find the ABI, head back to Remix.
Under the compiler tab, look for the copy ABI button at the bottom.

Once you've copied the code, head to your code editor, create a new file, distributed-news/src/lib/newsArchiveABI.json, and paste the ABI.
Next, we'll need to create the script to pull the data off the blockchain. Create a new file, distributed-news/src/lib/newsArchive.js and add the following code.
Environment Variables
You may have noticed that we are using dotenv to load values for PROVIDER_URL and CONTRACT_ADDRESS, you'll need to create distributed-news/.env to house those values. Be sure to replace the value for CONTRACT_ADDRESS with the contract address you deployed.
Displaying The Results
One last step! We are almost there. We need to update distributed-news/src/pages/index.astro to display the articles we've saved.
First, let's update the frontmatter.
Once that's updated we can use the variable articles to display them in the browser.
We'll setup a conditional rendering block to check for any errors and if there are none, display the articles.
With these two changes you should see the results at http://localhost:4321
Your front end should look something like this.

What it lacks in style, it makes up for in functionality.
Add Some Style
You might be cringing at the lack of style, lets fix that and make things look just a little bit nicer.
Below the </html> tag add in the following.
Great, now, you've got some style!
Congratulations!
Awesome! You've successfully deployed a smart contract using Chainlink Functions and connected it to a front end!
Last updated