Building smart contracts, smartly

0
220

Using reference contracts for decentralized data; mitigate risk with trusted nodes for your DeFi applications.

Photo by Clifford Photography on Unsplash

“Build smart contracts, smartly” — Patrick

Ok so I wasn’t the best in english class, but it’s a fun title no?

You’ll notice I wrote a similar article about building smart contracts, and getting started is here. In that article I messaged about how to get data from a single node, which is great if you’re looking for a specific type of data. There is at least one major issue with grabbing data from a single point, as you now have a single point of failure. We have seen recent cases in history where these single points, have actually caused issues. See here: 1. 2.

A much safer, cheaper, and more efficient method is to get your data from a decentralized oracle feed, or a reference data contract. Chainlink has made steps to make specific widely used pricing data, available on chain for you to use in your smart contract, and they couldn’t be simpler to use. The data is collected and aggregated from multiple sources, and there is a reputation system in place, so each node is incentivized to always send the most accurate data.

The easiest way to test it out, is with Remix (an online solidity IDE):

pragma solidity ^0.4.24;
import "github.com/smartcontractkit/chainlink/evm-contracts/src/v0.4/interfaces/AggregatorInterface.sol";
contract Demo {

AggregatorInterface internal ref;

constructor(address _aggregator) public {
ref = AggregatorInterface(_aggregator);
}
function getLatestPrice() public view returns (int256) {
return ref.latestAnswer();
}
}

There are a few steps to test out the aggregator contracts.

  1. Make sure you have MetaMask installed, with Rinkeby test ETH in your wallet, and you have the Rinkeby testnet selected.
  2. Copy and paste the above code into the Remix IDE
  3. Compile (version 0.4.24 for this code)
  4. Then deploy using one of the Rinkeby testnet aggregator addresses. In this demo we use the LINK-USD feed.
  5. Select the ```getLatestPrice``` button in the UI after the block confirms the application.

Now you can see the price of Chainlink on the Ethereum blockchain!

You can get the mainnet addresses here, along with what nodes host the data, who is using that data at the moment, and how the price is fluctuating. Other rinkeby addresses to test can be found here.

A visual representation of nodes sending data to a reference contract.

And it’s as simple as that

Now you can use pricing data in your DeFi app!

To pull data in your mainnet contract directly from a node, you just have to pay a little bit of LINK token, which is why Chainlink token is so valuable. It’s a crucial utility token for the ecosystem of on-chain data, ensuring the nodes are up at all times, providing the blockchain with data. These pulls from node operators is what powers these reference contracts.

Want to go a little deeper?

A wonderful course on building solidity smart contracts and UI’s can be found here. It covers everything end-to-end, @ste_grider.

All opinions in here are my own.

Follow me on twitter, medium, github, linkedin, for more content and insights.

#financerevolution #blockchain #ETH #chainlink #smartcontracts


Building smart contracts, smartly was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.