How to develop and deploy smart contracts on arbitrum

Chainlink 2022-04-04 19:18:01 阅读数:841

develop deploy smart contracts arbitrum

Link to the original text :

Arbitrum It is a layer 2 network of Ethereum , Developers can develop and deploy highly scalable smart contracts at low cost . adopt Arbitrum Upper Chainlink Data Feeds, Developers can be quick 、 Easily connect their smart contracts with off chain data , This includes building a large number of DeFi Application of highly reliable asset prices .

In this technical tutorial , We will explain what is Arbitrum, Learn how to be in Arbitrum Rinkeby Test online development , And give how to Arbitrum In smart contracts Chainlink Price Feeds Step description of . Although in this article, we will develop and deploy in the test network environment , But for the Arbitrum One For the main network , The steps are the same .

《 How to be in Arbitrum Develop and deploy smart contracts on 》 Details video :

What is? Arbitrum?

Arbitrum It's based on Optimistic rollup Ethereum layer 2 solution . In order to provide faster speed and lower cost on Ethereum , Some capacity expansion schemes have emerged , Include Layer 2 rollup、 state ( And payment ) passageway 、 Side chain 、Plasma and Validium etc. . The most important difference between these solutions is ,rollup And state channel inherit Ethereum blockchain (Layer 1) The security of , Developers can essentially work in Ethereum Layer 1 Develop on .

Layer 2 rollup Include Optimistic rollup and ZK-rollup Two kinds of . Both are " real Layer 2 Solution ", That means they can go at high speed 、 Execute a large number of transactions at a low cost , And then in Layer 1 Validate packaged transactions . about Optimistic rollup, We will “ Believe optimistically in ” These transactions really happened in Layer 2. these rollup yes “ hopeful ”, Because these packaged transactions are considered “ Innocent ”, Until proven by fraud “ guilty ”; That is, we will be optimistic that the transaction will be released to Layer 1 Is correct , Except in 7 The challenge was submitted during the challenge period of days .

Start using Arbitrum

In this tutorial , We will be in Arbitrum Rinkeby Test online development and deployment of a smart contract , This is a Rinkeby The second layer of the test network . The same procedure applies to Arbitrum One, It is the second floor of Ethereum main network . In order to use Arbitrum Rinkeby Test net , We need some Rinkeby Test network ETH. Can pass Chainlink Faucets Get your Rinkeby test LINK-- Just paste your wallet address , choice Rinkeby Ethereum, And get the test ETH.

then , We need to go from Rinkeby Save test ETH, In order to be in Arbitrum Rinkeby Test online payment fee . Navigate to Arbitrum Bridge , Connect wallet , Input Rinkeby ETH Number , And then click “ deposit ”. About need 10 Minutes to see the balance recorded in the layer 2 network —— You can have a coffee break .

Received on the second floor ETH after , You can go to Metamask Wallet configuration Arbitrum Rinkeby Test net . Navigate to Chainlist And find Arbitrum Rinkeby Network Details of , Or navigate to Etherscan Browser and find... In the footer of the website Add Arbitrum Network Or select Settings -> Networks -> Add Network in Metamask, Then manually enter the details .

Network Name: Arbitrum Rinkeby Testnet

Network URL:

Chain ID: 421611

Currency Symbol: ETH

Block Explorer URL:

Last , go back to Chainlink Faucets, choice Arbitrum Rinkeby, receive 10 A test network LINK.

The importance of accurate and reliable price data in smart contracts

In order to expand the possibility of smart contract on the second floor , Developers need to securely connect to off chain resources . With help from Chainlink Highly accurate and reliable price data of the Oracle , Developers can start at Arbitrum Develop and test all kinds of extensible DeFi Applications , These applications depend on ETH And other pass prices , For example, loan agreement 、 Decentralized exchange 、 Forecast the market, etc .

Although these DeFi Use cases require external data , However, blockchain and tier-2 solutions cannot access their own external data locally . When providing data to the blockchain to serve high-level services DeFi Use case , Data security and high quality must be guaranteed , To prevent price Oracle attacks .

Chainlink Price Feeds Provide aggregated data from a variety of high-quality data providers , And through a decentralized oracle Chainlink The network provides data on the chain , This reduces the risk of these attacks .Chainlink The decentralized Oracle mechanism ensures that the final price reflects a wide range of market coverage , This means that the final price is determined after summarizing a set of different prices in the whole market , Not just a small part , Other aspects such as transaction volume and liquidity are also considered . With the help of Chainlink Price Feeds, Developers can develop advanced software that does not affect security DeFi Applications .

Now we know Solidity The demand for accurate and reliable price data in smart contracts and Chainlink The important role of price prediction machine , We will pass on the Arbitrum Upper Solidity Smart contracts use Chainlink Price Feeds obtain ETH An example of the latest price .

stay Arbitrum Upper use Chainlink Price Feeds

First create a new one in your favorite code editor Solidity project . Use Hardhat And Typescript A complete example of can be found in Github Found on the . We will use Solidity and Chainlink Latest version .

import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

Now we're going to write a function to retrieve Chainlink Network Price Feeds data . Navigate to Chainlink In the official document Data Feeds part . We're going to "getPrice" Function as an example , And make some changes .

function getThePrice(address _priceFeedAddress) public view returns (int) {AggregatorV3Interface priceFeed = AggregatorV3Interface(_priceFeedAddress);(uint80 roundID,int price,uint startedAt,uint updatedAt,uint80 answeredInRound) = priceFeed.latestRoundData();return price;}

You can see that we put "PriceFeedAddress" Pass... As a parameter , Make this smart contract more scalable . You can see it here Arbitrum Rinkeby Test all online Price Feed A complete list of addresses .

for example , If we want to know BTC The dollar price , We can "0x0c9973e7a27d00e656B9f153348dA46CaD70d03d" As "_priceFeedAddress" Parameters passed to our function .

It's almost done ! know L2 Health signs of sequencer

Arbitrum Transactions in are confirmed at a high speed . This is because there is a so-called sequencer . The sequencer is an off chain component , It can sort users' transactions at high speed and provide users with receipts . However , If the sequencer is not available , Users must submit their transactions through Ethereum , So that they can Arbitrum In dealing with . This is bad for the user experience , many dApps Still unable to handle the need to work with Ethereum and Arbitrum Interaction .

If you don't want to worry about this in your application , have access to Chainlink Oracle to ensure that the sequencer is available to users . Here are the steps to do this .

First , We need to be in our Solidity Add the following import statement to the code :

import "@chainlink/contracts/src/v0.8/interfaces/FlagsInterface.sol";

according to Chainlink Documents ,L2 The sequencer health sign consists of three roles :

  • Chainlink Cluster( A set of verifier nodes )-- It's at every heartbeat "T"(Chainlink feed Minimum frequency of configured updates ) perform OCR Job.
  • Report the actual status of the sequencer OCR feed-- This can be used for Layer 1 Check by external users OR agreement ( Such as Arbitrum) The state of .
  • Validator -- from OCR feed Trigger , If the current answer is different from the previous one , Then perform the action of raising or lowering the sign .

Now? , We need to extend our contract with the following lines :

// Identifier of the Sequencer offline flag on the Flags contractaddress constant private FLAG_ARBITRUM_SEQ_OFFLINE = address(bytes20(bytes32(uint256(keccak256("chainlink.flags.arbitrum-seq-offline")) - 1)));FlagsInterface internal chainlinkFlags;constructor() {chainlinkFlags = FlagsInterface(0x491B1dDA0A8fa069bbC1125133A975BF4e85a91b);}

"0x491B1dDA0A8fa069bbC1125133A975BF4e85a91b" yes Arbitrum Rinkeby Flags The address of the contract . Check other addresses , succeed in inviting sb. Chainlink View in document .

A rising sign indicates ,feed stay "T" No update in time , Its data can be considered outdated . let me put it another way , The sequencer is offline , Your contract should not perform any key operations . When the sequencer resumes , On the second floor Chainlink Data Feeds After the update , You can continue to use your contract as usual . We added this extra check .

function getThePrice(address _priceFeedAddress) public view returns (int) {bool isRaised = chainlinkFlags.getFlag(FLAG_ARBITRUM_SEQ_OFFLINE);if (isRaised) {// If flag is raised we shouldn't perform any critical operationsrevert("Chainlink feeds are not being updated");}AggregatorV3Interface priceFeed = AggregatorV3Interface(_priceFeedAddress);(uint80 roundID,int price,uint startedAt,uint updatedAt,uint80 answeredInRound) = priceFeed.latestRoundData();return price;}

Deploy and test smart contracts

Now we are ready to deploy and test our contract . stay Remix The contract is compiled in , Then in the deployment tab , Change the environment to "Injected Web3". Make sure the wallet is connected to Arbitrum Rinkeby Test net , And the wallet address below is MetaMask The address of the wallet , Include previously obtained ETH. then , Press the deploy button , Follow the steps .

The end result is a successful transaction , Smart contracts deployed to Arbitrum Rinkeby Test net .

To test the contract , We just need to call our "getThePrice" function , And will Arbitrum Rinkeby Test an online Chainlink Price Feed Address as "_priceFeedAddress" Parameters are passed . remember , You can Chainlink See all available in the document Price Feed Contract address .


Chainlink Popular in China ETH/USD Price data , as well as LINK/USD、AAVE/USD and BTC/USD All price data can be found in Arbitrum Upper use . these Chainlink Price Feeds Based on decentralized Oracle infrastructure , It is composed of many node operators and high-quality data sources that have been subject to security review , Achieved a high degree of accuracy 、 Usable and tamper proof Data Feeds, these Data Feeds Itself can resist loopholes such as price manipulation attacks caused by flash loans .

This technical tutorial has been shown in Arbitrum How easy it is to write and deploy hybrid smart contracts on . With this knowledge , You can use it Arbitrum Low cost and high speed and Chainlink Price Feeds Unlocked advanced application scenarios , Start developing your own smart contract .

This article was first published in :