# 闪电贷详解，你也可以成为科学家：零成本套利无聊猿$Ape

By [kk](https://paragraph.com/@ethisweb3) · 2022-04-28

---

本文展示的所有代码全部开源：

[https://github.com/KeFuCode/ApeFlashLoan](https://github.com/KeFuCode/ApeFlashLoan)

攻击步骤
====

攻击者主要执行三个步骤：

\*\*步骤1：\*\*攻击者通过闪电贷从NFTX金库获取无聊猿碎片--ERC20代币，并将其转换为NFT。

![NFTX金库](https://storage.googleapis.com/papyrus_images/256f4995f165d77c41b746c1fc4189c520196f5d59d69f4c4a8bf7ab3f5af92f.png)

NFTX金库

\*\*步骤2：\*\*攻击者使用获取的 NFT 获取空投代币$Ape。

![$Ape Claim](https://storage.googleapis.com/papyrus_images/9a20d8a9ed2d6cb127ef57c5556e023bc08366017f45fdf7ae9b878fc02a645a.png)

$Ape Claim

\*\*步骤3：\*\*将 NFT 质押到NFTX金库，获取质押的 ERC20 代币，闪电贷还款，并将获利资金转移到钱包地址。

获利分析
----

### 交易流程

[https://etherscan.io/tx/0xeb8c3bebed11e2e4fcd30cbfc2fb3c55c4ca166003c7f7d319e78eaab9747098](https://etherscan.io/tx/0xeb8c3bebed11e2e4fcd30cbfc2fb3c55c4ca166003c7f7d319e78eaab9747098)

1.  闪电贷借5.2个vToken
    
2.  通过借到的5.2个vToken赎回5个BAYC，这里费用为randomRedeemFee为0.04，所以5\*0.04=0.2个vToken交给FeeDistributor进行分配。到这时，0x77有6个BAYC。
    
3.  用6个BAYC领取$Ape空投
    
4.  将6个BAYC铸造为vToken，这里因为有mintFee 0.1，所以0.6个vToken再次被分配。最终得到5.4个vToken。
    
5.  将5.2个vToken归还闪电贷，0.2个vToken在sushiswap上卖出得到14个eth。
    

### 利益计算

*   投入：1BAYC + 100ETH
    
*   产出：14ETH + 60000$Ape(213ETH)
    
*   利润：227ETH - 100ETH = 127ETH
    

闪电贷核心合约
-------

### 1.闪电贷借出vToken

flashLoan是进行闪电贷的核心代码，注意到下图第三行调用了onFlashLoan，所有调用闪电贷进行的套利行为，都发生在onFlashLoan中。

![NFTXVaultUpgradeable.sol](https://storage.googleapis.com/papyrus_images/678b05cb571a12e8c0331ab6606a7aec6fbb8cbc14c14f90d27da137e014fa44.png)

NFTXVaultUpgradeable.sol

在你调用NFTX金库进行闪电贷的合约中，最关键的也就是将onFlashLoan重写。

![NFTXVaultUpgradeable.sol](https://storage.googleapis.com/papyrus_images/aa745754f8d45598df08d2e4b62e94c92f09d915ffb29b2d97d701f8d20b4444.png)

NFTXVaultUpgradeable.sol

### 2.使用vToken兑换NFT

这一步主要调用redeem，将闪电贷获得的bayc碎片从NFX金库中，全部换成bayc。

![NFTXVaultFactoryUpgradeable.sol](https://storage.googleapis.com/papyrus_images/35666e0ef2703386a0393f65a7855e79a543c6d72d3b72d269ee6b792d0cc4d7.png)

NFTXVaultFactoryUpgradeable.sol

可以看到redeem调用了redeemTo函数，兑换bayc的逻辑都在redeemTo里。

![NFTXVaultFactoryUpgradeable.sol](https://storage.googleapis.com/papyrus_images/178a72b2f69c6379152dd3d951a0a683ad3d448df1363572050201cba424f23e.png)

NFTXVaultFactoryUpgradeable.sol

### 3.使用NFT领取空投

合约内获取bayc之后，调用$ApeCoin领域空投的智能合约。

![AirdropGraphsToken.sol](https://storage.googleapis.com/papyrus_images/c49682ccc597ce0fc32d03451318a97e6801b16b57e516fc369bd74a0b07278f.png)

AirdropGraphsToken.sol

### 4.将NFT铸造为vToken

使用合约内的bayc领取空投之后，调用NFTX金库的mint函数，将合约内的bayc重新铸造为bayc碎片token，并还给NFTX金库，完成闪电贷流程。

![NFTXVaultUpgradeable.sol](https://storage.googleapis.com/papyrus_images/a8ebb766bd884cb54f299973c717f04e805de82a727078882893fbf2a483ce5d.png)

NFTXVaultUpgradeable.sol

套利过程再现
------

### 套利合约

[https://github.com/KeFuCode/ApeFlashLoan/blob/main/contracts/Hunter.sol](https://github.com/KeFuCode/ApeFlashLoan/blob/main/contracts/Hunter.sol)

### 运行合约脚本

[https://github.com/KeFuCode/ApeFlashLoan/blob/main/scripts/run.js](https://github.com/KeFuCode/ApeFlashLoan/blob/main/scripts/run.js)

---

*Originally published on [kk](https://paragraph.com/@ethisweb3/ape)*
