-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdepositDecimalWrapper.ts
159 lines (120 loc) · 6.02 KB
/
depositDecimalWrapper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import chai, {expect} from "chai";
import { deployContract, solidity} from "ethereum-waffle";
import { ethers, run, ethereum, network } from "@nomiclabs/buidler";
import { Signer, constants } from "ethers";
import DepositLogicDecimalWrapperArtifact from "../artifacts/DepositLogicDecimalWrapper.json";
import { MockTokenFactory } from "../typechain/MockTokenFactory";
import { MockToken } from "../typechain/MockToken";
import { MockDecimalWrapper } from "../typechain/MockDecimalWrapper";
import { LendingLogicAave } from "../typechain/LendingLogicAave";
import { MockAaveLendingPool } from "../typechain/MockAaveLendingPool";
import TimeTraveler from "../utils/TimeTraveler";
import { parseEther } from "ethers/lib/utils";
import { DepositLogicDecimalWrapper, LendingRegistry, LendingRegistryFactory, MockDecimalWrapperFactory } from "../typechain";
import { isTryStatement } from "typescript";
chai.use(solidity);
const mintAmount = parseEther("1000000");
const CONVERSION = parseEther("1");
// random key
const WRAPPER = "0x561ca898cce9f021c15a441ef41899706e923541cee724530075d1a114476aaa";
describe.only("DepositDecimalWrapper", function() {
this.timeout(300000000);
let signers: Signer[];
let account;
let timeTraveler: TimeTraveler;
let lendingLogic: DepositLogicDecimalWrapper;
let lendingRegistry: LendingRegistry;
let token: MockToken;
let decimalWrapper: MockDecimalWrapper;
before(async() => {
signers = await ethers.getSigners();
account = await signers[0].getAddress();
timeTraveler = new TimeTraveler(ethereum);
const tokenFactory = new MockTokenFactory(signers[0]);
const decimalWrapperFactory = new MockDecimalWrapperFactory(signers[0]);
token = await tokenFactory.deploy("token", "token");
decimalWrapper = await decimalWrapperFactory.deploy("WRAP", "WRAP", token.address, CONVERSION);
await token.mint(mintAmount, account);
lendingRegistry = await (new LendingRegistryFactory(signers[0])).deploy();
lendingLogic = await deployContract(signers[0], DepositLogicDecimalWrapperArtifact, [lendingRegistry.address, WRAPPER]) as unknown as DepositLogicDecimalWrapper;
// Add lendingLogic to lending Registry
await lendingRegistry.setProtocolToLogic(WRAPPER, lendingLogic.address);
await lendingRegistry.setUnderlyingToProtocolWrapped(token.address, WRAPPER, decimalWrapper.address);
await lendingRegistry.setWrappedToProtocol(decimalWrapper.address, WRAPPER);
await lendingRegistry.setWrappedToUnderlying(decimalWrapper.address, token.address);
await timeTraveler.snapshot();
});
beforeEach(async() => {
await timeTraveler.revertSnapshot();
});
it("Deploying with lendingRegistry set to the zero address should fail", async() => {
const promise = deployContract(signers[0], DepositLogicDecimalWrapperArtifact, [constants.AddressZero, WRAPPER]);
await expect(promise).to.be.revertedWith("INVALID_LENDING_REGISTRY");
});
it("lend()", async() => {
const calls = await lendingLogic.lend(token.address, mintAmount);
expect(calls.targets.length).to.eq(3);
expect(calls.data.length).to.eq(3);
await signers[0].sendTransaction({
to: calls.targets[0],
data: calls.data[0]
});
await signers[0].sendTransaction({
to: calls.targets[1],
data: calls.data[1]
});
await signers[0].sendTransaction({
to: calls.targets[2],
data: calls.data[2]
});
const tokenBalance = await token.balanceOf(account);
const decimalWrapperBalance = await decimalWrapper.balanceOf(account);
expect(tokenBalance).to.eq(0);
expect(decimalWrapperBalance).to.eq(mintAmount.mul(CONVERSION));
});
it("unlend()", async() => {
await token.approve(decimalWrapper.address, constants.MaxUint256);
await decimalWrapper.deposit(mintAmount);
expect(await decimalWrapper.balanceOf(account)).to.eq(mintAmount.mul(CONVERSION));
const calls = await lendingLogic.unlend(decimalWrapper.address, mintAmount.mul(CONVERSION));
expect(calls.targets.length).to.eq(1);
expect(calls.data.length).to.eq(1);
await signers[0].sendTransaction({
to: calls.targets[0],
data: calls.data[0]
})
const tokenBalance = await token.balanceOf(account);
const decimalWrapperBalance = await decimalWrapper.balanceOf(account);
expect(tokenBalance).to.eq(mintAmount);
expect(decimalWrapperBalance).to.eq(0);
});
it("getAPRFromUnderlying()", async() => {
const apr = await lendingLogic.getAPRFromUnderlying(token.address);
expect(apr).to.eq(0)
});
it("getAPRFromWrapped()", async() => {
const apr = await lendingLogic.getAPRFromWrapped(decimalWrapper.address);
expect(apr).to.eq(0) // one percent
});
it("exchangeRate()", async() => {
const exchangeRate = await lendingLogic.callStatic.exchangeRate(decimalWrapper.address);
// 1 wrapped = 1e18
expect(exchangeRate).to.eq(parseEther("1").div(CONVERSION));
})
it("exchangeRateView()", async() => {
const exchangeRate = await lendingLogic.exchangeRateView(decimalWrapper.address);
// 1 wrapped = 1e18
expect(exchangeRate).to.eq(parseEther("1").div(CONVERSION));
});
it("Deposit based on exchangeRate", async() => {
await token.approve(decimalWrapper.address, constants.MaxUint256);
const exchangeRate = await lendingLogic.exchangeRateView(decimalWrapper.address);
const targetAmount = parseEther("1");
const depositAmount = targetAmount.mul(exchangeRate.toString()).div(parseEther("1"));
console.log(targetAmount.toString());
console.log(depositAmount.toString());
await decimalWrapper.deposit(depositAmount);
const decimalWrapperBalance = await decimalWrapper.balanceOf(account);
expect(decimalWrapperBalance).to.eq(targetAmount);
});
});