My First Dapp using Chanlink Price Feed

0
22

I’ve been learning solidity for three months, and finally, I built my very first Dapp (Link).

Home

Motivation

A couple of weeks ago, I got so inspired by the winners of Chainlink Virtual Hackathon 2021 spring. They all built very creative Dapps built around Chiainlink oracle systems. They literally showed how much potential smart contracts and Chainlink oracle have.

That’s when I got interested in Chainlink Price Feeds and made me feel like using it.

What is Chainlink Price Feed

It let you access a lot of kinds of the latest and historical price data within your smart contracts, so it’s useful when you want to know the current market prices of any assets.

Chainlink Price Feeds are the quickest way to connect your smart contracts to the real-world market prices of assets. They enable smart contracts to retrieve the latest price of an asset in a single call.

Introduction to Price Feeds | Chainlink Documentation

Dapp Idea

Sometimes people want to send the same worth of Ether as a specific amount of fiat currency for like payment.

Let’s say, you want to send $500 worth of Ether, then you realize you can’t set the amount of Ether which is worth of a specific amount of US dollar in Metamask.

It shows what’s your Ether worth in US dollar, but like you cannot set $500 worth of Ether.

That’s where my Dapp comes in. You can send the amount of Ether based on fiat currency.

How It Works

demo

It’s simple.

  1. Select a fiat currency, recipient, and amount of the fiat currency.
  2. Click “Send Ether” and “Confirm”.

It calculates the amount of Ether as soon as you enter amount using Chainlink Price Feeds.

The good thing is it does not cost anything to get the latest price since the contract is a view function.

Smart Contract

Here is the smart contract code I use for this Dapp. This works on Kovan testnet. If you want it work on mainnnet or rinkeby, then all you have to do is change the addresses on AggregatorV3Interface (Line 22~26). Here is the link for each address on Ethereum network.

Problem

The problem is that it violate the DRY principle. The last 5 functions are doing the same thing with different variables.

Ideally, I could combine the last 5 functions together and add a new argument that specify a currency pair.

Possible Solution

If you use feed registry, you get any pair of price data and don’t have to remember each address. However, it’s still in alpha, so you cannot use in production. In fact, currently some pairs don’t really work.

Feed Registry | Chainlink Documentation

Here is the sample code using feed registry.

Hope this helps. Thank you for reading 😁

DEMO / TwitterGitHub

References

Also, Read

Join Coinmonks Telegram Channel and learn about crypto trading and investing

Also, Read


My First Dapp using Chanlink Price Feed was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.