|
| 1 | +import "module-alias/register"; |
| 2 | +import { BigNumber } from "@ethersproject/bignumber"; |
| 3 | + |
| 4 | +import { Address } from "@utils/types"; |
| 5 | +import { Account } from "@utils/test/types"; |
| 6 | +import { ADDRESS_ZERO, MAX_UINT_256, ZERO_BYTES } from "@utils/constants"; |
| 7 | +import { AaveV2WrapV2Adapter, SetToken, StandardTokenMock, WrapModuleV2 } from "@utils/contracts"; |
| 8 | +import DeployHelper from "@utils/deploys"; |
| 9 | +import { |
| 10 | + ether, |
| 11 | + preciseMul, |
| 12 | +} from "@utils/index"; |
| 13 | +import { |
| 14 | + getAccounts, |
| 15 | + getWaffleExpect, |
| 16 | + getSystemFixture, |
| 17 | + addSnapshotBeforeRestoreAfterEach, |
| 18 | + getAaveV2Fixture, |
| 19 | +} from "@utils/test/index"; |
| 20 | +import { AaveV2Fixture, SystemFixture } from "@utils/fixtures"; |
| 21 | +import { |
| 22 | + AaveV2AToken |
| 23 | +} from "@utils/contracts/aaveV2"; |
| 24 | + |
| 25 | +const expect = getWaffleExpect(); |
| 26 | + |
| 27 | +describe("AaveV2WrapModule", () => { |
| 28 | + |
| 29 | + let owner: Account; |
| 30 | + let deployer: DeployHelper; |
| 31 | + |
| 32 | + let setV2Setup: SystemFixture; |
| 33 | + let aaveV2Setup: AaveV2Fixture; |
| 34 | + |
| 35 | + let aaveV2WrapAdapter: AaveV2WrapV2Adapter; |
| 36 | + let wrapModule: WrapModuleV2; |
| 37 | + |
| 38 | + let underlyingToken: StandardTokenMock; |
| 39 | + let wrappedToken: AaveV2AToken; |
| 40 | + |
| 41 | + const aaveV2WrapAdapterIntegrationName: string = "AAVE_V2_WRAPPER"; |
| 42 | + |
| 43 | + before(async () => { |
| 44 | + [ owner ] = await getAccounts(); |
| 45 | + |
| 46 | + // System setup |
| 47 | + deployer = new DeployHelper(owner.wallet); |
| 48 | + setV2Setup = getSystemFixture(owner.address); |
| 49 | + await setV2Setup.initialize(); |
| 50 | + |
| 51 | + // Aave setup |
| 52 | + aaveV2Setup = getAaveV2Fixture(owner.address); |
| 53 | + await aaveV2Setup.initialize(setV2Setup.weth.address, setV2Setup.dai.address); |
| 54 | + |
| 55 | + underlyingToken = setV2Setup.dai; |
| 56 | + wrappedToken = aaveV2Setup.daiReserveTokens.aToken; |
| 57 | + |
| 58 | + // WrapModule setup |
| 59 | + wrapModule = await deployer.modules.deployWrapModuleV2(setV2Setup.controller.address, setV2Setup.weth.address); |
| 60 | + await setV2Setup.controller.addModule(wrapModule.address); |
| 61 | + |
| 62 | + // AaveV2WrapAdapter setup |
| 63 | + aaveV2WrapAdapter = await deployer.adapters.deployAaveV2WrapV2Adapter(aaveV2Setup.lendingPool.address); |
| 64 | + await setV2Setup.integrationRegistry.addIntegration(wrapModule.address, aaveV2WrapAdapterIntegrationName, aaveV2WrapAdapter.address); |
| 65 | + }); |
| 66 | + |
| 67 | + addSnapshotBeforeRestoreAfterEach(); |
| 68 | + |
| 69 | + context("when a SetToken has been deployed and issued", async () => { |
| 70 | + let setToken: SetToken; |
| 71 | + let setTokensIssued: BigNumber; |
| 72 | + |
| 73 | + before(async () => { |
| 74 | + setToken = await setV2Setup.createSetToken( |
| 75 | + [setV2Setup.dai.address], |
| 76 | + [ether(1)], |
| 77 | + [setV2Setup.issuanceModule.address, wrapModule.address] |
| 78 | + ); |
| 79 | + |
| 80 | + // Initialize modules |
| 81 | + await setV2Setup.issuanceModule.initialize(setToken.address, ADDRESS_ZERO); |
| 82 | + await wrapModule.initialize(setToken.address); |
| 83 | + |
| 84 | + // Issue some Sets |
| 85 | + setTokensIssued = ether(10); |
| 86 | + const underlyingRequired = setTokensIssued; |
| 87 | + await setV2Setup.dai.approve(setV2Setup.issuanceModule.address, underlyingRequired); |
| 88 | + await setV2Setup.issuanceModule.issue(setToken.address, setTokensIssued, owner.address); |
| 89 | + }); |
| 90 | + |
| 91 | + describe("#wrap", async () => { |
| 92 | + let subjectSetToken: Address; |
| 93 | + let subjectUnderlyingToken: Address; |
| 94 | + let subjectWrappedToken: Address; |
| 95 | + let subjectUnderlyingUnits: BigNumber; |
| 96 | + let subjectIntegrationName: string; |
| 97 | + let subjectWrapData: string; |
| 98 | + let subjectCaller: Account; |
| 99 | + |
| 100 | + beforeEach(async () => { |
| 101 | + subjectSetToken = setToken.address; |
| 102 | + subjectUnderlyingToken = underlyingToken.address; |
| 103 | + subjectWrappedToken = wrappedToken.address; |
| 104 | + subjectUnderlyingUnits = ether(1); |
| 105 | + subjectIntegrationName = aaveV2WrapAdapterIntegrationName; |
| 106 | + subjectWrapData = ZERO_BYTES; |
| 107 | + subjectCaller = owner; |
| 108 | + }); |
| 109 | + |
| 110 | + async function subject(): Promise<any> { |
| 111 | + return wrapModule.connect(subjectCaller.wallet).wrap( |
| 112 | + subjectSetToken, |
| 113 | + subjectUnderlyingToken, |
| 114 | + subjectWrappedToken, |
| 115 | + subjectUnderlyingUnits, |
| 116 | + subjectIntegrationName, |
| 117 | + subjectWrapData |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + it("should reduce the underlying quantity and mint the wrapped asset to the SetToken", async () => { |
| 122 | + const previousUnderlyingBalance = await underlyingToken.balanceOf(setToken.address); |
| 123 | + const previousWrappedBalance = await wrappedToken.balanceOf(setToken.address); |
| 124 | + |
| 125 | + await subject(); |
| 126 | + |
| 127 | + const underlyingBalance = await underlyingToken.balanceOf(setToken.address); |
| 128 | + const wrappedBalance = await wrappedToken.balanceOf(setToken.address); |
| 129 | + |
| 130 | + const expectedUnderlyingBalance = previousUnderlyingBalance.sub(setTokensIssued); |
| 131 | + expect(underlyingBalance).to.eq(expectedUnderlyingBalance); |
| 132 | + |
| 133 | + const expectedWrappedBalance = previousWrappedBalance.add(setTokensIssued); |
| 134 | + expect(wrappedBalance).to.eq(expectedWrappedBalance); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe("#unwrap", () => { |
| 139 | + let subjectSetToken: Address; |
| 140 | + let subjectUnderlyingToken: Address; |
| 141 | + let subjectWrappedToken: Address; |
| 142 | + let subjectWrappedTokenUnits: BigNumber; |
| 143 | + let subjectIntegrationName: string; |
| 144 | + let subjectUnwrapData: string; |
| 145 | + let subjectCaller: Account; |
| 146 | + |
| 147 | + let wrappedQuantity: BigNumber; |
| 148 | + |
| 149 | + beforeEach(async () => { |
| 150 | + subjectSetToken = setToken.address; |
| 151 | + subjectUnderlyingToken = underlyingToken.address; |
| 152 | + subjectWrappedToken = wrappedToken.address; |
| 153 | + subjectWrappedTokenUnits = ether(0.5); |
| 154 | + subjectIntegrationName = aaveV2WrapAdapterIntegrationName; |
| 155 | + subjectUnwrapData = ZERO_BYTES; |
| 156 | + subjectCaller = owner; |
| 157 | + |
| 158 | + wrappedQuantity = ether(1); |
| 159 | + |
| 160 | + await wrapModule.wrap( |
| 161 | + subjectSetToken, |
| 162 | + subjectUnderlyingToken, |
| 163 | + subjectWrappedToken, |
| 164 | + wrappedQuantity, |
| 165 | + subjectIntegrationName, |
| 166 | + ZERO_BYTES |
| 167 | + ); |
| 168 | + |
| 169 | + await underlyingToken.approve(aaveV2Setup.lendingPool.address, MAX_UINT_256); |
| 170 | + await aaveV2Setup.lendingPool.deposit(underlyingToken.address, ether(100000), owner.address, 0); |
| 171 | + }); |
| 172 | + |
| 173 | + async function subject(): Promise<any> { |
| 174 | + return wrapModule.connect(subjectCaller.wallet).unwrap( |
| 175 | + subjectSetToken, |
| 176 | + subjectUnderlyingToken, |
| 177 | + subjectWrappedToken, |
| 178 | + subjectWrappedTokenUnits, |
| 179 | + subjectIntegrationName, |
| 180 | + subjectUnwrapData, |
| 181 | + { |
| 182 | + gasLimit: 5000000, |
| 183 | + } |
| 184 | + ); |
| 185 | + } |
| 186 | + |
| 187 | + it("should burn the wrapped asset to the SetToken and increase the underlying quantity", async () => { |
| 188 | + const previousUnderlyingBalance = await underlyingToken.balanceOf(setToken.address); |
| 189 | + const previousWrappedBalance = await wrappedToken.balanceOf(setToken.address); |
| 190 | + |
| 191 | + await subject(); |
| 192 | + |
| 193 | + const underlyingBalance = await underlyingToken.balanceOf(setToken.address); |
| 194 | + const wrappedBalance = await wrappedToken.balanceOf(setToken.address); |
| 195 | + |
| 196 | + const delta = preciseMul(setTokensIssued, wrappedQuantity.sub(subjectWrappedTokenUnits)); |
| 197 | + |
| 198 | + const expectedUnderlyingBalance = previousUnderlyingBalance.add(delta); |
| 199 | + expect(underlyingBalance).to.eq(expectedUnderlyingBalance); |
| 200 | + |
| 201 | + const expectedWrappedBalance = previousWrappedBalance.sub(delta); |
| 202 | + expect(wrappedBalance).to.eq(expectedWrappedBalance); |
| 203 | + }); |
| 204 | + }); |
| 205 | + }); |
| 206 | +}); |
0 commit comments