开发商的复合体。产量耕作、借贷、借贷

148 阅读7分钟

5/5 - (1票)

本教程是我们的第一部分的延续。 DeFi 系列.在 这一部分,我们将讨论DeFi借贷协议Compound。

YouTube Video

🪙 完整的DeFi课程。点击链接访问我们免费的完整DeFi课程,它将向你展示分散式金融(DeFi)的内涵和外延。

Compound

Compound运营,它是一个受欢迎的各种ERC-20代币的借贷平台。根据DeFi脉冲,Compound的总价值锁定(TVL)为25.6亿美元。

图1:Compound的Defi脉冲

操作模式

你可以通过Comound存入可以作为贷款抵押品的物品。

基于你的抵押品,你只被允许借到一个特定的百分比(大约60-70%)。信用评级是没有意义的,由于以太坊账户是匿名的,在贷款违约的情况下,几乎不可能得到付款。

因此,每笔贷款都有一个额外的抵押资产,不属于正在进行的贷款的一部分。

如果借款人的抵押率低于预先确定的阈值水平,他们的头寸就会被清算以偿还贷款。

值得注意的是,对于复合型,代币在代币前使用字母'c'。例如,DAI将是cDAI,ETH将是cETH,以此类推。Compound中的出借人和借款人将获得COMP代币的奖励(就像MakerDAO中的MKR代币)。

图2:Compound的定义

Compound是一个算法的、自主的利率协议,它与无数的DeFi系统(建立在Compound基础上的其他应用程序)整合并支撑着这些系统,如Dharma、PoolTogether、Argent。

通过在以太坊上建立利率市场,Compound使用户能够获得他们贡献给借贷池的加密货币的利息。

自动连接借款人和贷款人,Compound智能合约根据借款与提供的资产比例来确定利率。

Compound是DeFi空间指数机会的一个完美例子:随着越来越多的设备采用Compound协议,更多的加密货币资产将能够产生收入,即使在休眠状态。

使用Combound的产量耕作

最早提供治理代币的DeFi解决方案之一是Compound。

DeFi "产量养殖 "的现象,只是一种旨在最大限度地提高DeFi平台激励结构的交易策略,是由其COMP币在2020年6月的首次亮相引入的。

🧑‍🌾 术语**"收益率农场主 "**是指那些向各种DeFi网络提供加密资产以换取治理代币的人。然后,这些治理代币被用于其他DeFi dApps,他们可能获得各种数字资产和金融工具,或赚取利息。

其他DeFi平台在看到产量耕作为系统带来新的资本以及治理代币的价值不断增加后,模仿了Compound的模式。

区块链治理的创新者,如DeFi解决方案和平台,表明高效治理不需要牺牲去中心化。

即使DeFi治理仍在发展,但治理代币和公平发布的普及表明,其主要做法将对区块链生态系统产生长久的影响。

复式借贷

理论够了,让我们写一些代码。在这一部分中,我们讨论使用JS代码从复合协议中借入Ethereum资产的快速入门。确保你已经安装了Node.jsLTS和npx

克隆 repo 和cd 到根目录。

$ git clone https://github.com/compound-developers/compound-borrow-examples.git
$ cd compound-borrow-examples/
$ npm install
$ npm install -g npx   # global install

infura.io/,创建一个Infura账户和一个新项目。注意你为Ethereum主网创建的项目的API INFURA KEY。

创建一个.env 文件,内容如下。

WEB3_INFURA_PROJECT_ID='INFURA API KEY'
export MAINNET_PROVIDER_URL="https://mainnet.infura.io/v3/$WEB3_INFURA_PROJECT_ID"
export DEV_ETH_MNEMONIC="clutch captain shoe salt awake harvest setup primary inmate ugly among become"

来源于.env 文件,用下面的命令在本地运行hardhat节点

$ source .env $ node ./scripts/run-localhost-fork.js

这将为我们的第一个硬帽本地账户(记忆体账户)注入测试的以太币和ERC20的种子。

你应该得到像下面这样的输出(地址可能不同)。

$./scripts/run-localhost-fork.jslhost-fork.js

Running a hardhat localhost fork of mainnet at http://localhost:8545

Impersonating address on localhost...  0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643
Impersonating address on localhost...  0x35a18000230da775cac24873d00ff85bccded550
Impersonating address on localhost...  0x39AA39c021dfbaE8faC545936693aC917d5E7563
Local test account successfully seeded with DAI
Local test account successfully seeded with UNI
Local test account successfully seeded with USDC
DAI amount in first localhost account wallet: 100.00020088272375
UNI amount in first localhost account wallet: 10
USDC amount in first localhost account wallet: 100

Ready to test locally! To exit, hold Ctrl+C.

正如你在上面的终端看到的,当你运行脚本时,ERC20代币DAI、UNI和USDC会自动记入硬帽本地账户。

打开另一个终端(使用打开新标签选项)。在这个终端上,运行下面的脚本,用ETH抵押借入DAI。

$ source .env
$ node examples-js/web3-js/borrow-erc20-with-eth-collateral.js
My Wallet's  ETH Balance: 10000
My Wallet's cETH Balance: 0
My Wallet's  DAI Balance: 100.00020088272375

Supplying ETH to the protocol as collateral (you will get cETH in return)...

My Wallet's  ETH Balance: 9998.99960067
My Wallet's cETH Balance: 49.82984632
My Wallet's  DAI Balance: 100.00020088272375

Entering market (via Comptroller contract) for ETH (as collateral)...
Calculating your liquid assets in the protocol...
Fetching cETH collateral factor...
Fetching DAI price from the price feed...
Fetching borrow rate per block for DAI borrowing...

You have 1414.2063374441461 of LIQUID assets (worth of USD) pooled in the protocol.
You can borrow up to 82.5% of your TOTAL collateral supplied to the protocol as DAI.
1 DAI == 1.000379 USD
You can borrow up to 1413.6705563033072 DAI from the protocol.
NEVER borrow near the maximum amount because your account will be instantly liquidated.

Your borrowed amount INCREASES (1.0254012296e-8 * borrowed amount) DAI per block.
This is based on the current borrow rate.

Now attempting to borrow 50 DAI...
My Wallet's  ETH Balance: 9998.9985743775
My Wallet's cETH Balance: 49.82984632
My Wallet's  DAI Balance: 150.00020088272376

Fetching DAI borrow balance from cDAI contract...
Borrow balance is 50 DAI

This part is when you do something with those borrowed assets!

Now repaying the borrow...
Approving DAI to be transferred from your wallet to the cDAI contract...

Borrow repaid.

My Wallet's  ETH Balance: 9998.9980410225
My Wallet's cETH Balance: 49.82984632
My Wallet's  DAI Balance: 100.00020088272375

上述终端输出的亮点

  1. 你提供了近2个ETH作为抵押品,作为回报,你得到了cETH(49.82984)。DAI余额为100.0002。请看第一个我的钱包的余额。
  2. cETH抵押品系数 - 决定了用户可以借多少钱。在我们的案例中,它几乎达到了82.5%的最大值(~=1413.67DAI)。
  3. 脚本试图根据借款利率只借50DAI。借款利率是他们所借资产的当前平均利率,并在每个以太坊区块得到更新。借款成功后,DAI余额为150.0002。请看第二个我的钱包的余额。
  4. 一段时间后,在你使用了你所借的资产后,你会偿还DAI余额。请看第三个我的钱包的余额,DAI余额恢复到100.002。

复式借贷

与借贷类似,我们讨论快速开始使用JS代码从复合协议供应以太坊资产。

克隆 repo 和cd 到根目录。

git clone https://github.com/compound-developers/compound-supply-examples.git
cd compound-supply examples/
npm install

创建一个.env 文件,内容如下(与上述相同)。

WEB3_INFURA_PROJECT_ID='INFURA API KEY'
export MAINNET_PROVIDER_URL="https://mainnet.infura.io/v3/$WEB3_INFURA_PROJECT_ID"
export DEV_ETH_MNEMONIC="clutch captain shoe salt awake harvest setup primary inmate ugly among become"

来源于.env 文件,并在本地用以下命令运行hardhat节点

$ source .env
$ node ./scripts/run-localhost-fork.js

这将为我们的第一个hardhat本地账户(mnemonic账户)注入测试的以太币和ERC20的种子。

你应该得到如下输出(地址可能不同)。

$ node ./scripts/run-localhost-fork.js

Running a hardhat localhost fork of mainnet at http://localhost:8545

Impersonating address on localhost...  0x5d3a536E4D6DbD6114cc1Ead35777bAB948E3643
Impersonating address on localhost...  0x35a18000230da775cac24873d00ff85bccded550
Impersonating address on localhost...  0x39AA39c021dfbaE8faC545936693aC917d5E7563
Local test account successfully seeded with DAI
Local test account successfully seeded with UNI
Local test account successfully seeded with USDC
DAI amount in first localhost account wallet: 100.00020088272375
UNI amount in first localhost account wallet: 10
USDC amount in first localhost account wallet: 100

Ready to test locally! To exit, hold Ctrl+C.

打开另一个终端(使用打开新标签选项)。在这个终端上,运行下面的脚本,提供一些DAI。

$ node ./examples-js/web3-js/supply-erc20.js

My wallet's DAI Token Balance: 100.00020088272375
DAI contract "Approve" operation successful.
Supplying DAI to the Compound Protocol...

cDAI "Mint" operation successful.

DAI supplied to the Compound Protocol: 9.999999999995602

My wallet's cDAI Token Balance: 453.2961167
My wallet's DAI Token Balance: 90.00020088272375

Current exchange rate from cDAI to DAI: 0.02206063460855499

Redeeming the cDAI for DAI...
Exchanging all cDAI based on cToken amount...

My wallet's cDAI Token Balance: 0
My wallet's DAI Token Balance: 100.00020092079512

上述输出的要点

  1. DAI余额最初为100.0002。提供DAI后,我们得到cDAI的回报。DAI余额为90.0002,cDAI为453.296。
  2. 我们用cDAI赎回DAI,因此DAI余额为100.0002,cDAI变为0。

❤ 来源: github.com/compound-de…

摘要

在本教程中,我们窥探了广泛使用的借贷平台 - Compound。

我们看到Compound是如何运作的,以及它在Yield farming中的使用情况。

最后,我们还使用JavaScript代码来借入和借出复合协议的ERC20资产。

在接下来的DeFi系列文章中,我们将研究Aave协议,并使用Solidity代码来借入和借出化合物。