使用Python开发DeFi项目
原文鏈接:https://blog.chain.link/develop-python-defi-project/
原文標題:Develop a DeFi Project Using Python
原文作者:Patrick Collins
In this tutorial, we’ll walk through how to develop a DeFi project in Python. Decentralized finance (DeFi) is one of the most significant advancements of the blockchain and smart contract world and is often referred to as “the new fintech”. Right now, smart contract development is dominated by JavaScript, partially because JavaScript is the most commonly used language on the planet, and it’s easier to build full-stack applications with the “JavaScript everywhere” mentality that comes with Node.js. However, this isn’t the case with the fintech world of quantitative analysts, stock traders, and hedge funds. A majority of these fintech technology companies use Python for a variety of reasons:
在本文教程中,我們將詳細介紹如何使用Python開發DeFi項目。去中心化金融(DeFi)是區塊鏈和智能合約世界里最重要的進步之一,且常常被稱作“新金融科技”。目前,智能合約的開發由JavaScript語言主導,部分原因在于JavaScript是地球上最常用的語言,而且因為Node.js帶來的“JavaScript無處不在”的思想,使得構建全棧應用程序變得更加容易。但是,對金融科技界中的定量分析師,股票交易員和對沖基金來說,情況則有所不同。大多數金融科技公司中出于各種原因而選擇使用Python:
-
Great developer experience
-
Powerful AI and machine learning
-
Bundled fintech packages
-
Comprehensive analytic tools
-
Reliability in production settings
-
出色的開發者體驗
-
強大的AI和機器學習支持
-
配套的金融科技軟件工具包
-
全面的分析工具
-
生產環境中的可靠性
There is a reason why so many data scientists, academics, and fintech institutions use Python. Some projects in the DeFi space, like yearn.finance, share this sentiment and have built their entire DeFi platforms with Python and Solidity. It’s because of libraries and frameworks like web3.py and Brownie that we are able to see these projects come to life. Brownie is a Truffle-like framework (they are both quite “sweet”) that maintains your deployments, scripts, testing, and allows you to make an end-to-end backend.
為什么有眾多數據科學家,學者和金融科技機構使用Python是有一定原因的。DeFi領域中的某些項目,如yearn.finance,也認可這種想法,并已經使用Python和Solidity構建了他們的整個DeFi平臺。因為有了web3.py和Brownie之類的庫和框架,我們才能夠有機會看到這些項目的誕生。Brownie是一個類似Truffle的框架~~(它們都非常“甜”,注:Brownie-布朗尼蛋糕;Truffle-松露)~~,能夠支持項目部署,編寫腳本,測試等,且能夠進行端到端的后端開發。
Web3.py and Brownie also come with “mixes” where you can pre-open a project with some boilerplate code. This is what we will work with today, the Chainlink Brownie mix.
Web3.py和Brownie也能“混合”使用,你可以預先打開一個項目并在其中使用一些樣板代碼。這就是我們今天將要使用的Chainlink Brownie mix。
Now why do we want to use Chainlink + Python? Just as Python is the de facto programming language for fintech, Chainlink is the de facto oracle solution for feeding off-chain data into DeFi smart contracts, currently securing more than $7B in value for top DeFi protocols. The combination of these two technologies offers a powerful framework for securely decentralizing the fintech landscape.
現在我們為什么要使用Chainlink + Python?正如Python是金融科技領域中的首選編程語言,Chainlink事實上也是將鏈下數據帶到DeFi智能合約中的首選預言機解決方案,當前正守護著超過70億美金的頂級DeFi協議的價值。這兩種技術的結合為安全去中心化金融科技領域提供了強大的框架支撐。
Here’s a quick look at some of what Chainlink enabled in 2020.
下面是Chainlink在2020年啟用的一些功能快速概覽。
Chainlink Network stats 2020
2020年Chainlink網絡統計數據
To get started building your DeFi app with Brownie and Python, you first need to install Python. It is not recommended to have Python below version 3.4 at this point, so please upgrade if you have an older version than 3.4. You can see your version of Python and verify it’s correctly installed by running:
要開始使用Brownie和Python構建DeFi應用,首先需要安裝Python。目前建議Python的版本不低于3.4,因此,如果你使用的版本低于3.4,請先進行升級。你可以通過運行以下命令查看你的Python版本并驗證它是否已正確安裝:
Or, if with python3:
或者,如果用的是Python3:
You’ll also need to have Ganache installed. Ganache is a one-click-blockchain written in Python that allows you to easily spin up a local blockchain. Though you do have to download it with npm and node.js, this will be the only piece of JavaScript that you have to interact with.
你還需要安裝Ganache。Ganache是用Python編寫的一鍵式區塊鏈,它可讓你很容易啟動一個本地區塊鏈。但你必須通過npm和node.js來下載它,這將是唯一需要與之交互的JavaScript相關的內容。
First, you’ll need node.js and npm installed. Node.js comes installed with npm. Once you download it, you can check to see if it was done properly by running:
首先,需要安裝node.js和npm。Node.js會隨npm一起安裝。下載后,可以通過運行以下命令來檢查它是否正確安裝:
Then, you can install Ganache via the command line.
然后你就可以通過命令行安裝Ganache。
Once you have those installed, we will install eth-brownie the same way we install all Python repos, with pip!
安裝完這些后,我們就像安裝其他所有的Python安裝包一樣,將以pip的方式安裝eth-brownie。
Or if with pip3:
或者,如果使用的是pip3:
You’ll know you’ve done it right if you run brownie in your terminal, and you get an output similar to:
如果你能在終端執行brownie命令,就表示安裝成功,并且得到的輸出類似于:
Also, you’ll need a MetaMask or other Ethereum wallet. If you’ve never used an ETH wallet before, you can watch this video to help you get set up. Please note this also shows getting testnet ETH for Ropsten, which Chainlink doesn’t support anymore. If you follow along, please use Kovan.
另外,你還需要一個MetaMask或其他以太坊錢包。如果你以前從未使用過ETH錢包,則可以觀看下面的視頻輔助設置。需要注意的是,這個視頻也展示了在Ropsten網絡上獲取ETH測試代幣,但Chainlink已經不再支持了。如果你想按照視頻操作,請使用Kovan測試網絡。
https://www.youtube.com/watch?v=4ZgFijd02Jo
And last, be sure to have some testnet LINK and Kovan ETH in your ETH wallet. You can find some in the LINK faucet and ETH Kovan faucet.
最后,請確保你的ETH錢包中有一些測試網LINK代幣和Kovan測試網ETH代幣。你可以在LINK水龍頭和ETH Kovan水龍頭中獲取一些。
Starting a Chainlink Project
開啟一個Chainlink項目
To get started with Brownie, we can use what’s called a Brownie mix to give us boilerplate code. In this example, we will be deploying a simple Chainlink Price Feed as a way to learn about the Brownie framework. Let’s bake the chainlink-mix.
要開始使用Brownie,我們可以使用Brownie mix為我們提供樣板代碼。在本文的示例中,我們將部署一個簡單的Chainlink Price Feed,來了解Brownie框架。現在我們就開始創作chainlink-mix項目吧。
This will place us into a new project with some default code already built for us. If we run ls we can see what the layout of the file looks like:
我們會進入到一個包含一些默認代碼的新項目。如果運行ls命令,我們可以看到文件的布局如下:
- build : This is where the project keeps track of your deployed smart contracts and compiled contracts
- build:這是項目用來跟蹤已部署的智能合約和已編譯合約的文件夾
- contracts : The source code of your contracts, typically written in Solidity or Vyper
- contracts:合約的源代碼,通常以Solidity或Vyper語言編寫
- interfaces : A layout of interfaces you’ll need to work with deployed contracts. Every interaction with a contract needs an ABI and an address. Interfaces are great ways to get a contract’s ABI
- interfaces:和已部署合約交互所需要的接口文件夾。每次與合約的交互都需要一個ABI和合約地址。接口是獲取合約的ABI的好方法
- scripts : Scripts we create to automate processes of working with our contracts
- scripts:我們創建的用于自動執行合約流程的腳本
- tests : Tests
- tests:測試文件
- brownie-config.yaml : This is where we have all the information for Brownie to understand how to work with our smart contract. What blockchain do we want to deploy to? Are there any special parameters we want to set? All these are set in the config file.
- brownie-config.yaml:這個文件為Brownie提供了理解如何對待我們的智能合約所需要的所有信息。我們想部署到什么區塊鏈網絡?我們要設置任何特殊參數嗎?所有這些內容都在此配置文件中設置。
requirements.txt , README.md , LICENSE , and .gitignore can be ignored for now. You’ll find out what they are for as you practice.
requirements.txt , README.md , LICENSE , 以及 .gitignore 等文件現在可以忽略。你將在練習過程中了解它們的用途。
Setting Up Environment Variables
設置環境變量
Even though we just installed Ganache to do local testing, we also want to be able to connect to the ETH mainnet and testnets so we can deploy them on real testnets. To do this, we need to set our WEB3_INFURA_PROJECT_ID.You can get an Infura ID for free from the Infura site. You can use other web3 providers or your own node as well, but you’ll have to do a little more configuration for that.
In your brownie-config.yaml file, you can set a network with a host key, and define the URL that you’d like to connect to if you’d rather not use MetaMask.
雖然我們剛剛安裝好了Ganache進行本地測試,我們仍希望能夠連接到以太坊主網和測試網,這樣我們就可以將合約部署在真實的測試網上。為此,我們需要設置WEB3_INFURA_PROJECT_ID字段。你可以從Infura網站免費獲得一個Infura ID,也可以使用其他web3 providers或者自己的節點,但是你需要做更多配置。
在brownie-config.yaml文件中,你可以設定帶有host字段的網絡配置,如果不想使用MetaMask,還可以定義要連接的URL。
Now that you have your web3 ID, we need to have our private key as an environment variable, so that we can use our account with our wallet. If you’re using MetaMask, look for export key. With MetaMask, you may have to add 0x to the start of your private key. It is recommended to use a different account than your main account when testing and exporting your key, just in case.
現在有了web3 ID,我們需要將私鑰作為環境變量來使用,這樣我們可以在錢包中使用我們的帳戶。如果你使用的是MetaMask,請找到 export key選項(以導出私鑰)。使用MetaMask的話,你可能必須在私鑰的開頭添加0x。為了以防萬一,建議在測試和導出密鑰時使用與主帳戶不同的帳戶。
Now, to make them environment variables just run the following in your terminal:
現在,要使它們成為環境變量,只需在終端中運行以下命令:
If you run printenv and see your environment variables there, you’ll know you’ve done it right.
終端運行printenv命令,并在輸出中看到上面配置的環境變量,就表示你做對了。
You can learn more about setting environment variables here.
可以在此處了解更多有關設置環境變量的信息。
Deploy Your Smart Contract
部署你的智能合約
Now that we have everything set up, we can even go ahead and deploy a smart contract to the Kovan testnet!
現在我們已經完成了所有設置,我們將進一步把智能合約部署到Kovan測試網上!
In our scripts folder, we have a script called deploy_price_consumer_v3.py. This will deploy our smart contract that reads the price of Ethereum in USD.
在scripts文件夾中,我們有一個名為deploy_price_consumer_v3.py的腳本。這將部署我們的智能合約,該合約會讀取以美元計價的以太坊的價格數據。
If you want to see an easier walkthrough of what this contract does and how to deploy it, feel free to check out the Chainlink tutorial on deploying a price feed contract.
如果你想看到一個更加易懂的闡述該合約的功能以及如何部署它的教程,隨時查看Chainlink的部署喂價合約的教程。
Just use brownie run to use the deploy script:
只需使用brownie run命令即可使用此部署腳本:
You’ll see something like this:
你會看到以下輸出內容:
If this worked properly, we can go to Kovan Etherscan and find the contract we deployed. The link above shows the contract deployed in this example.
如果腳本工作正常,我們可以到Kovan區塊瀏覽器上查看我們部署的合約。上面的鏈接顯示了此示例中部署的合約。
Read Your Smart Contract
讀取你的智能合約
Now that we have deployed a smart contract, we can read the price of ETH from the contract we just deployed. We will be using another script that we have:
現在我們已經部署好了智能合約,我們可以從剛剛部署的合約中讀取以太坊的價格。我們將使用另一個腳本:
And we will get an output like:
我們將得到如下輸出:
Where 122322000000 is the current price of ETH in USD! Solidity doesn’t understand decimals, and we know that Chainlink Price Feeds have 8 decimals, so the price is 1 , 223.22. 其 中 122322000000 是 E T H 當 前 的 美 元 價 格 ! S o l i d i t y 無 法 理 解 小 數 , 而 我 們 知 道 C h a i n l i n k 喂 價 具 有 8 位 小 數 , 因 此 價 格 為 1,223.22. 其中122322000000是ETH當前的美元價格!Solidity無法理解小數,而我們知道Chainlink喂價具有8位小數,因此價格為 1,223.22.其中122322000000是ETH當前的美元價格!Solidity無法理解小數,而我們知道Chainlink喂價具有8位小數,因此價格為 1,223.22。
And you’ve just deployed your first smart contract using Python with Brownie!
至此你就完成了使用Python和Brownie部署你的第一個智能合約!
Testing Your Smart Contract
測試你的智能合約
This is also a great example of how to test smart contracts. We use mocks objects to test even locally!
這也是一個很好的關于如何測試智能合約的例子。我們甚至可以使用模擬對象在本地進行測試!
Simply run:
只需運行:
And your tests will run on a local Ganache instance!
你的測試將在本地Ganache實例上運行!
You can also test on testnets with something like:
你還可以使用以下內容在測試網上進行測試:
The tests have some functionality that know if you’re working on a testnet or not. If you are working locally, it deploys dummy or “mocks” of the oracle code, so that we can test from them.
這些測試具有一些可以知曉你是否正在使用測試網的功能。如果你在本地工作,它將部署預言機代碼的虛擬對象或“模擬對象”,因此我們可以通過它們進行測試。
Going Further
更進一步
Now that you know how to deploy smart contracts with Python, you can begin building on top of this framework to do more interesting things. Python has powerful packages like Numpy, Scikit, Pandas, and TensorFlow to do quantitative work, machine learning, and more. Being able to finally combine these technologies together is a recipe for success in the new age of fintech: decentralized finance.
現在你知道了如何使用Python來部署智能合約,就可以開始基于此框架進行構建以做更多有趣的事情。Python具有諸如Numpy,Scikit,Pandas和TensorFlow等強大的軟件包,可以用于定量工作,機器學習等等。最終將這些技術結合在一起是在金融科技新時代成功的秘訣:去中心化金融。
Chainlink is a flexible framework for bringing external financial data and systems on-chain and integrates seamlessly with data-focused packages like Numpy and Pandas. If you’re a developer and want to quickly get your application connected to Chainlink, visit the developer documentation and join the technical discussion in Discord. If you build something great with Python, Chainlink, and Brownie, be sure to tag us with @chainlink so we can check out all the cool work you do!
Chainlink是一個靈活的框架,用于將外部金融數據和系統引入到鏈上,并無縫集成這些與以數據為中心的軟件包,如Numpy和Pandas。如果你是開發者,并且希望快速將你的應用連接到Chainlink,請訪問開發者文檔并加入Discord中的技術討論。如果你使用Python,Chainlink和Brownie構建了很棒的東西,請確保使用@chainlink來標記我們,便于我們知悉所做的所有的出色的工作!
Website | Twitter | Reddit | YouTube | Telegram | Events | GitHub | Price Feeds | DeFi
總結
以上是生活随笔為你收集整理的使用Python开发DeFi项目的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OkHttp官方教程解析-彻底入门OkH
- 下一篇: ProcessingJoy —— 简单的