Defi/LP/DAPP代幣合約流動性質押挖礦系統開發/Solidity合約示例

丸子qy發表於2023-05-15


  

  DAPP,是基於區塊鏈的底層開發平臺,被稱為去中心化應用、分散式應用程式,所有資料都儲存在分散式分類帳中。DApp自P2P網路開始以來就已經存在,其在不同計算機的P2P網路上執行,而不是在一臺計算機上執行。DAPP旨在以不受任何單個實體控制的方式在網路上執行。快速搭建v:wwqqyy420

  

  DAPP can also be understood as an upgraded version of the app.The app currently developed by the IOS and Android systems is taken out and thrown on the blockchain system.Combined with smart contracts,it becomes DAPP.DAPP firmly emphasizes decentralization.It is necessary to run on a distributed operating system and cannot work on traditional approaches such as Android and iOS.DAPP appears in many new distributed channels or networks such as Ethereum and EOS.It does not need to rely on any central server to achieve the purpose of decentralization.It can be run automatically and the code is open source.In addition,DAPP does not need to be downloaded and installed.It directly jumps from the platform to the application page to run.It can be opened and used anytime and anywhere.It is easy to operate.

  

  Dapp based on blockchain can solve the trust problem.Dapp widely uses the decentralized technology of smart contract,which can solve the trust problem between users and Dapp developers.It is to write the protocol code into the smart contract,and then automatically execute the contract content after certain conditions.The smart contract realizes the credibility and transparency of the contract at the code level to ensure decentralization.The asset custody of smart contracts ensures the safe transaction of decentralized and trusted assets.In the DAPP of the decentralized exchange,users completely control their own assets.Any transaction and transfer are controlled by users,and the exchange cannot touch your assets,which greatly reduces the trust risk with the exchange.

  

  去中心化,是網際網路發展過程中形成的社會關係形態和內容產生形態,是相對於“中心化”而言的新型網路內容生產過程。在一個分佈有眾多節點的系統中,每個節點都具有高度自治的特徵。節點之間彼此可以自由連線,形成新的連線單元。任何一個節點都可能成為階段性的中心,但不具備強制性的中心控制功能。節點與節點之間的影響,會透過網路而形成非線性因果關係。這種開放式、扁平化、平等性的系統現象或結構,我們稱之為去中心化。

  

  作為區塊鏈諸多特性中的重要的一個特點,其使用分散式儲存與算力, 使得整個網路節點的權利與義務相同,系統中資料本質為全網節點共同維護,從而區塊鏈不再依靠於中央處理節點,實現資料的分散式儲存、記錄與更新。

  

  dApp元件

  

  dApp的元件會有三個不同的型別:智慧合約,前端邏輯(UI)和資料儲存。

  

  智慧合約

  

  智慧合約儲存了dApp的業務邏輯和當前的狀態,這個是dApp和傳統網路應用的最大區別,也正是因為這一點讓dApp具備了以上提到過的優勢。

  

  前端/UI

  

  儘管後端邏輯需要開發者完成智慧合約程式碼,並把它部署在區塊鏈上,但是在前端,開發者還是使用標準的網路技術,比如HTML和javascript,因此開發者可以使用自己熟悉的工具,庫和框架。

  

  第一步:建立智慧合約:

  

  我們dApp中的智慧合約是一個簡單的例子,它可以檢視資料並且反應出區塊鏈上的變化。在這個例子中,我們會透過Chainlink ETH/USD喂價對檢視ETH/USD的價格,然後將結果儲存在智慧合約上。

  

  第一步是開啟Chainlink的檔案,然後導航到Using Data Feeds頁面。從這裡將原始碼複製進你的IDE中的一個新的檔案裡(比如Visual Code),或者你可以點選“Open In Remix”按鈕,然後使用線上IDE Remix。

  

  在這個例子中,我們會使用Visual Studio Code和Hardhat(一個EVM開發框架)。

  

  首先,為我們的dApp建立一個新的資料夾,並在這個資料夾中建立一個後端資料夾,用來儲存智慧合約程式碼:

  

  mkdir chainlink-dapp-example

  

  cd chainlink-dapp-example

  

  mkdir backend

  

  cd backend

  

  接下來,我透過VS Code開啟建立好的資料夾,然後安裝Hardhat:

  

  npm init-y

  

  npm install--save-dev hardhat

  

  npx hardhat

  

  (choose create javascript project,choose default parameters)

  

  當安裝完成之後,在“contracts”資料夾中刪掉Touch.sol,然後在這個資料夾中建立一個叫做PriceConsumerV3.sol的檔案。在這個檔案將儲存我們的合約,所以將Chainlink檔案中的程式碼複製到這個檔案中,然後儲存。

  

  在樣例程式碼中,你會看到demo合約已經有一個叫做getLatestPrice的功能來透過Rinkeby上的ETH/USD喂價對檢視Ethereum的當前價格。

  

  function getLatestPrice()public view returns(int){

  

  (

  

  /uint80 roundID/,

  

  int price,

  

  /uint startedAt/,

  

  /uint timeStamp/,

  

  /uint80 answeredInRound/

  

  )=priceFeed.latestRoundData();

  

  return price;

  

  建立一個新的變數和函式,在智慧合約上儲存這個值。

  

  int public storedPrice;

  

  然後,建立一個新的函式,它會被dApp的前端呼叫。這個函式會透過呼叫getLatestPrice函式檢視Ethereum的最新價格,然後將這個值儲存在storedPrice這個引數中:

  

  function storeLatestPrice()external{

  

  storedPrice=getLatestPrice();

  

  }

  

  你的新的合約應該和下面的一樣:

  

  //SPDX-License-Identifier:MIT

  

  pragma solidity^0.8.7;

  

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

  

  contract PriceConsumerV3{

  

  AggregatorV3Interface internal priceFeed;

  

  int public storedPrice;

  

  /**

  

  *Network:Rinkeby

  

  *Aggregator:ETH/USD

  

  *Address:0x8A753747A1Fa494EC906cE90E9f37563A8AF630e

  

  */

  

  constructor(){

  

  priceFeed=

  

  AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);

  

  }

  

  /**

  

  *Returns the latest price

  

  */

  

  function getLatestPrice()public view returns(int){

  

  (

  

  /uint80 roundID/,

  

  int price,

  

  /uint startedAt/,

  

  /uint timeStamp/,

  

  /uint80 answeredInRound/

  

  )=priceFeed.latestRoundData();

  

  return price;

  

  }

  

  function storeLatestPrice()external{

  

  storedPrice=getLatestPrice();


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70029396/viewspace-2952198/,如需轉載,請註明出處,否則將追究法律責任。

相關文章