Explain how to use Chainlink oracles on the Matic network
onlineonlineSince then, the network has quickly become a popular second-layer solution for Ethereum developers due to its secure and scalable infrastructure and instant transactions. Matic's high-performance, low-fee infrastructure provides a viable platform for DeFi applications to facilitate adoption at scale. Also in the field of blockchain oracles, Chainlink has become the preferred oracle solution for DeFi protocols,Chainlink Price FeedsBillions of dollars in value have already been secured. In this technical article, we will show you how to use Chainlink oracles in your Matic DeFi application, and how to use Chainlink Price Feeds to give your application access to high-quality, tamper-proof data that is not vulnerable toOracle Vulnerabilities and Flash Loan Attacksfirst level title
The Need for Scalable and Secure DeFi Protocols
DeFi protocols have seen explosive growth this year, with DeFi’s Total Locked Volume (TVL) soaring from $680 million in January to nowover $14 billion. However, with this growth comes increased levels of on-chain transactions and unwanted attention from disruptors who seek to exploit these protocols for their own benefit.
Ethereum has limited scalability, low throughput, and increased number of on-chain transactions due to the growth of DeFihinderperformance of the blockchain. This creates a traffic effect that makes DeFi protocols suffer from poor user experience, high fees, and slow transaction times.
Slow transaction speed and high transaction fees are the two limiting factors preventing DeFi from entering the mainstream. Layer 2 networks such as Matic, with their increased scalability, high throughput, and low-cost transactions, can unlock the next phase of DeFi development. Matic sidechains are also composable and fully EVM compatible. One of the core values of DeFi is itsComposability without permission, so Matic offers a compelling option for developers looking to build a variety of useful DeFi applications that scale, offer low-cost transactions, ensure permissionless composability, and provide a seamless user experience .
However, these DeFi applications still requireAccess to high-quality and reliable external data. As we have seen many times this year, DeFi protocols can be attacked in various forms, such asPrice Oracle Attacks, that is, the perpetrators combine flash loans with low-quality oracle machines to manipulate the market, seek their own interests, and harm the interests of other users.
Chainlink Price FeedsDeFi applications are mitigated from being vulnerable to these attacks by providing aggregated data from different high-quality data providers, fed on-chain by decentralized oracles on the Chainlink network. Chainlink's decentralized architecture and wide range of high-quality data sources ensure that the final price can cover the broadest market, which essentially means that the price is determined after aggregating different prices from the entire market, not just a small Subset.
first level title
Using Chainlink Price Feeds on the Matic Network
Chainlink Price FeedsUsing multiple high-quality data inputs and combining them with the Chainlink oracle network, price data is fed into the reference contract and the results are then aggregated. DeFi protocols attach great importance to data quality and integrity. Chainlink Price Feeds provides the best practical production environment solutions for these protocols that do not want to be affected by various DeFi vulnerabilities due to data quality issues.
The following two tutorials are also available atRemixused as a complete smart contract.
The first step to using Chainlink Price Feeds and oracles on the Matic Network is to set up your MetaMask wallet toConnect to Matic Mumbai Testnetimage description
Get Testnet MATIC
MATIC is the native token of the Matic Network. This is similar to ether in Ethereum. To interact with the Matic network, a gas fee is required in MATIC tokens. To get Mumbai Testnet MATIC, go toMATIC Faucetimage description
Create a smart contract
Create a smart contract
The easiest way to start building smart contracts using Chainlink Price Feeds on Matic is from the standardPrice ConsumerThe contract begins. This is the underlying standardized contract for initiating requests to Chainlink Price Feeds. Open this contract in Remix and modify it as needed. In this example, we will use ETH/USD price data. The first step is to initialize a [Matic Price Feed Reference Contract。
Next, we need to get the latest price from the Price Feed Aggregator contract instantiated in the constructor above. Now we have a call from the Aggregator contractlastRoundDataDeploy and test smart contracts

Deploy and test smart contracts
Now we are ready to deploy and test our contract. Compile the contract in remix, then on the deployment tab, change the environment to"Injected Web3", and make sure the wallet address below is the address in your MetaMask wallet that contains your MATIC tokens, press the deploy button, and follow the steps. The end result is that your smart contract has been deployed to the Matic Mumbai testnet. The deployed contract address can be obtained through the transaction output in the remix console.
After deployment, we only need to execute"getLatestPrice "image description
first level title
Using Chainlink Oracles on the Matic Network
secondary title
Get Testnet LINK
You need to get some Testnet LINK to send requests to Chainlink oracles. To get Mumbai Testnet LINK, please joinMatic DiscordCreate a smart contract
Create a smart contract
The easiest way to start building smart contracts on Matic that use Chainlink oracles is to start with the standardChainlink APIConsumer contractstart. This is a basic standardized contract for initiating requests for external data via Chainlink oracles. So we'll open this contract in Remix and modify it according to our needs.
First, we will put the"volume "The parameter is renamed to"ethPrice",Bundle"requestVolumeData "function renamed to"requestEthereumPrice". Once done, we need to modify the constructor as follows.
The LINK token address of the Mumbai testnet is `0x70d1F773A9f81C852087B77F6Ae6d3032B02D2AB` so we pass it in"setChainlinkToken "in function
according toMatic DocumentationAccording to the regulations in Mumbai testnet, there is an oracle contract running at `0x1cf7D49BE7e0c6AC30dEd720623490B64F572E17`, so we set the oracle variable to this value
The API is obtained through an HTTP GET request, and the return value is an unsigned integer. There are various job specifications available for oracles running on the Mumbai testnet. according toMatic DocumentationThe list of jobs in , according to these requirements, is suitable for us `d8fcf41ee8984d3b8b0eae7b74eca7dd`, because we want to perform HTTP GET request, the returned data is an unsigned integer, so we will"jobId "variable is set to this value.
Will"fee"The variable is modified to 1LINK. This is the payment sent to oracle along with the external data request.

function."requestEthereumPrice"and"fulfill"function.
Request usage of Chainlink oraclesChainlink requests and responsesThe design pattern works, the on-chain function creates and makes the request, the oracle detects the request, and sends the response back to another function in the same contract. In our case,"requestEthereumPrice "function to create and make a request,"fulfill "Function to handle the response, which is defined by `this.fulfill.selector`.
The API of choice is crypto.com'sETH/EURexist
exist"requestEthereumPrice "In the function, modify it as follows:
Set the URL to the endpoint of the [ETH/EUR](ETH/EUR) API
will request the"path "variable is set to"EUR". This is the path to find and return the JSON response containing the current price of Ethereum based on the output of the API endpoint seen in the browser
Multiply the result by 100, because Solidity can't handle decimals in numbers
exist

exist"fulfill "In the function, you can see that the oracle will send the response back to the `_price` parameter, and then store it in the `ethereumPrice` variable in the contract.

Deploy and test smart contracts
Deploy and test smart contracts
Compile the contract with remix, then deploy the contract to the Mumbai Testnet like we did with the price giveaway contract.
image description
Transfer LINK to the contract
When the contract is deployed and has LINK, we can press"requestEthereumPrice "image description
Test the deployed contract
After the request is initiated, the Chainlink oracle will execute, and then the fullfil function in our contract will be called by the Chainlink node oracle contract. This will take a few seconds to complete, and we can then execute the"ethereumPrice "image description
Summarize
Summarize
DeFi applicationsDeFi applications, such as decentralized exchanges (DEXs), liquidity pools, lending protocols, decentralized insurance, and automated market makers (AMMs).
and joindevelopment documentationand joinDiscordherehereconnect.







