Skip to content

Commit 8ce8c41

Browse files
committed
temp
1 parent 66265ec commit 8ce8c41

File tree

10 files changed

+13652
-2
lines changed

10 files changed

+13652
-2
lines changed

w5_1_code/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "w4_2_code",
2+
"name": "w5_1_code",
33
"version": "1.0.0",
44
"description": "",
55
"main": "index.js",

w5_2_code/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "w4_2_code",
2+
"name": "w5_2_code",
33
"version": "1.0.0",
44
"description": "",
55
"main": "index.js",

w6_1_code/.envrc.example

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export RINKEBY_ALCHEMY_URL=''
2+
export KOVAN_ALCHEMY_URL=''
3+
export PRIVATE_KEY=''
4+
export ETHERSCAN_API_KEY=''

w6_1_code/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## 测试
2+
- 先往 aave 合约存入 busd,用于支付借贷产生的利息,然后进行借贷
3+
4+
## 交易流水
5+
[0xfb7f2ae1d852fb0b1a4a931abe443dcfe15ec852a3c3575d10cc37d203c28fe1](https://kovan.etherscan.io/tx/0xfb7f2ae1d852fb0b1a4a931abe443dcfe15ec852a3c3575d10cc37d203c28fe1)
6+
7+
## 相关代码
8+
- [AaveFlashLoan](https://github.com/leoliew/blockchain-learn/blob/main/w5_2_code/contracts/AaveFlashLoan.sol)
9+
- [testFlashLoan](https://github.com/leoliew/blockchain-learn/blob/main/w5_2_code/scripts/testFlashLoan.js)
10+
11+
## 截图
12+
- 测试网执行
13+
14+
![image1](../images/w5_2_1.jpeg)
15+
16+
- 本地调试
17+
18+
![image2](../images/w5_2_2.png)
19+
20+
![image2](../images/w5_2_3.png)
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//SPDX-License-Identifier: Unlicense
2+
pragma solidity ^0.8.0;
3+
4+
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
5+
import "@openzeppelin/contracts/access/Ownable.sol";
6+
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
7+
8+
// ETH Call Opt
9+
contract CallOptionsToken is ERC20, Ownable {
10+
using SafeERC20 for IERC20;
11+
12+
uint public price;
13+
address public udscToken;
14+
uint public settlementTime; // 行权日期
15+
uint public constant during = 1 days; // 行权有效期
16+
17+
constructor(address usdc) ERC20("CallOptToken", "COPT") {
18+
udscToken = usdc;
19+
price = 10;
20+
settlementTime = 100 days;
21+
}
22+
23+
// 铸造期权 token
24+
function mint() external payable onlyOwner {
25+
_mint(msg.sender, msg.value);
26+
}
27+
28+
// 行权
29+
function settlement(uint amount) external {
30+
require(block.timestamp >= settlementTime && block.timestamp < settlementTime + during, "invalid time");
31+
_burn(msg.sender, amount);
32+
uint needUsdcAmount = price * amount;
33+
IERC20(udscToken).safeTransferFrom(msg.sender, address(this), needUsdcAmount);
34+
safeTransferETH(msg.sender, amount);
35+
}
36+
37+
38+
function safeTransferETH(address to, uint256 value) internal {
39+
(bool success,) = to.call{value : value}(new bytes(0));
40+
require(success, 'TransferHelper::safeTransferETH: ETH transfer failed');
41+
}
42+
43+
// 销毁合约
44+
function burnAll() external onlyOwner {
45+
require(block.timestamp >= settlementTime + during, "not end");
46+
uint usdcAmount = IERC20(udscToken).balanceOf(address(this));
47+
IERC20(udscToken).safeTransfer(msg.sender, usdcAmount);
48+
49+
selfdestruct(payable(msg.sender));
50+
// uint ethAmount = address(this).balance;
51+
// safeTransferETH(msg.sender, ethAmount);
52+
}
53+
54+
55+
}

w6_1_code/deploy/deploy.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// We require the Hardhat Runtime Environment explicitly here. This is optional
2+
// but useful for running the script in a standalone fashion through `node <script>`.
3+
//
4+
const { ethers } = require('hardhat')
5+
// When running the script with `npx hardhat run <script>` you'll find the Hardhat
6+
// Runtime Environment's members available in the global scope.
7+
module.exports = async (hardhat) => {
8+
9+
const { deployments } = hardhat
10+
const { deploy } = deployments
11+
12+
const [signer1] = await hardhat.ethers.getSigners()
13+
// kovan测试网地址
14+
const ADDRESS_PROVIDER = '0x88757f2f99175387ab4c6a4b3067c77a695b0349'
15+
let aaveFlashLoan = await deploy('AaveFlashLoan', {
16+
contract: 'AaveFlashLoan',
17+
args: [ADDRESS_PROVIDER],
18+
from: signer1.address,
19+
skipIfAlreadyDeployed: false
20+
})
21+
console.log('AaveFlashLoan:' + aaveFlashLoan.address)
22+
}

w6_1_code/hardhat.config.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require('@nomiclabs/hardhat-waffle')
2+
require('@nomiclabs/hardhat-ethers')
3+
require('@nomiclabs/hardhat-etherscan')
4+
require('hardhat-deploy')
5+
require('hardhat-deploy-ethers')
6+
7+
// You need to export an object to set up your config
8+
// Go to https://hardhat.org/config/ to learn more
9+
10+
const { RINKEBY_ALCHEMY_URL, KOVAN_ALCHEMY_URL, PRIVATE_KEY, ETHERSCAN_API_KEY } = process.env
11+
12+
/**
13+
* @type import('hardhat/config').HardhatUserConfig
14+
*/
15+
module.exports = {
16+
solidity: '0.8.4',
17+
18+
networks: {
19+
localhost: {
20+
url: 'http://127.0.0.1:8545',
21+
chainId: 31337
22+
},
23+
forking: {
24+
url: 'http://127.0.0.1:8545',
25+
chainId: 31337,
26+
accounts: [`0x${PRIVATE_KEY}`],
27+
blockNumber: 30834337 // kovan 测试网区块高度
28+
},
29+
kovan: {
30+
chainId: 42,
31+
url: KOVAN_ALCHEMY_URL,
32+
accounts: [`0x${PRIVATE_KEY}`]
33+
}
34+
},
35+
etherscan: {
36+
apiKey: {
37+
kovan: ETHERSCAN_API_KEY
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)