|
| 1 | +import { TransactionResponse } from "@ethersproject/providers"; |
| 2 | +import { expect } from "chai"; |
| 3 | +import { Contract } from "ethers"; |
| 4 | +import { parseEther } from "ethers/lib/utils"; |
| 5 | +import { ethers } from "hardhat"; |
| 6 | +import { NETWORK_ADDRESSES } from "src/networkAddresses"; |
| 7 | +import { expectEvents, initMainnetUser } from "src/utils"; |
| 8 | +import { forking, testVip } from "src/vip-framework"; |
| 9 | + |
| 10 | +import vip573 from "../../vips/vip-573/bscmainnet"; |
| 11 | +import COMPTROLLER_ABI from "./abi/Comptroller.json"; |
| 12 | +import DIAMOND_ABI from "./abi/Diamond.json"; |
| 13 | + |
| 14 | +const { bscmainnet } = NETWORK_ADDRESSES; |
| 15 | + |
| 16 | +const OLD_MARKET_FACET = "0x7ec871BA4248CC443a994f2febeDFB96DAe444F1"; |
| 17 | + |
| 18 | +const NEW_MARKET_FACET = "0x87FdF72FA2fB29Cb43f03aCa261A8DC2C613a860"; |
| 19 | + |
| 20 | +// Function selector for enterMarketBehalf(address,address) |
| 21 | +const ENTER_MARKET_BEHALF_SELECTOR = "0xd585c3c6"; |
| 22 | + |
| 23 | +// Test accounts |
| 24 | +const TEST_USER = "0x14A1c22EF6d2eF6cE33c0b018d8A34D02021e5c8"; |
| 25 | +const TEST_DELEGATE = "0x9cc6f5f16498fceef4d00a350bd8f8921d304dc9"; |
| 26 | +const VUSDT = "0xfD5840Cd36d94D7229439859C0112a4185BC0255"; |
| 27 | + |
| 28 | +forking(69628476, async () => { |
| 29 | + const provider = ethers.provider; |
| 30 | + let unitroller: Contract; |
| 31 | + |
| 32 | + let marketFacetFunctionSelectors: string[]; |
| 33 | + |
| 34 | + before(async () => { |
| 35 | + unitroller = new ethers.Contract(bscmainnet.UNITROLLER, COMPTROLLER_ABI, provider); |
| 36 | + |
| 37 | + // Get current MarketFacet function selectors |
| 38 | + marketFacetFunctionSelectors = await unitroller.facetFunctionSelectors(OLD_MARKET_FACET); |
| 39 | + }); |
| 40 | + |
| 41 | + describe("Pre-VIP behavior", async () => { |
| 42 | + it("MarketFacet should have old implementation", async () => { |
| 43 | + expect(await unitroller.facetFunctionSelectors(OLD_MARKET_FACET)).to.deep.equal(marketFacetFunctionSelectors); |
| 44 | + expect(await unitroller.facetFunctionSelectors(NEW_MARKET_FACET)).to.deep.equal([]); |
| 45 | + }); |
| 46 | + |
| 47 | + it("enterMarketBehalf function should not exist", async () => { |
| 48 | + expect(marketFacetFunctionSelectors).to.not.include(ENTER_MARKET_BEHALF_SELECTOR); |
| 49 | + }); |
| 50 | + |
| 51 | + it("unitroller should contain old MarketFacet address", async () => { |
| 52 | + expect(await unitroller.facetAddresses()).to.include(OLD_MARKET_FACET); |
| 53 | + expect(await unitroller.facetAddresses()).to.not.include(NEW_MARKET_FACET); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + testVip("vip-573", await vip573(), { |
| 58 | + callbackAfterExecution: async (txResponse: TransactionResponse) => { |
| 59 | + await expectEvents(txResponse, [DIAMOND_ABI], ["DiamondCut"], [1]); |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + describe("Post-VIP behavior", async () => { |
| 64 | + it("MarketFacet function selectors should be updated for new facet address", async () => { |
| 65 | + const newMarketFacetFunctionSelectors = [ENTER_MARKET_BEHALF_SELECTOR]; |
| 66 | + |
| 67 | + const expectSelectors = [...marketFacetFunctionSelectors, ...newMarketFacetFunctionSelectors].sort(); |
| 68 | + const updatedSelectors = [...(await unitroller.facetFunctionSelectors(NEW_MARKET_FACET))].sort(); |
| 69 | + |
| 70 | + expect(updatedSelectors).to.deep.equal(expectSelectors); |
| 71 | + expect(await unitroller.facetFunctionSelectors(OLD_MARKET_FACET)).to.deep.equal([]); |
| 72 | + }); |
| 73 | + |
| 74 | + it("unitroller should contain new MarketFacet address", async () => { |
| 75 | + expect(await unitroller.facetAddresses()).to.include(NEW_MARKET_FACET); |
| 76 | + expect(await unitroller.facetAddresses()).to.not.include(OLD_MARKET_FACET); |
| 77 | + }); |
| 78 | + |
| 79 | + it("enterMarketBehalf function should exist in new facet", async () => { |
| 80 | + const newFacetSelectors = await unitroller.facetFunctionSelectors(NEW_MARKET_FACET); |
| 81 | + expect(newFacetSelectors).to.include(ENTER_MARKET_BEHALF_SELECTOR); |
| 82 | + }); |
| 83 | + it("should allow approved delegate to enter market on behalf of user", async () => { |
| 84 | + const user = await initMainnetUser(TEST_USER, parseEther("10000")); |
| 85 | + |
| 86 | + // First, exit the market if already in it |
| 87 | + try { |
| 88 | + await unitroller.connect(user).exitMarket(VUSDT); |
| 89 | + } catch (e) { |
| 90 | + // Market might not be entered, continue |
| 91 | + } |
| 92 | + |
| 93 | + // Approve delegate |
| 94 | + await unitroller.connect(user).updateDelegate(TEST_DELEGATE, true); |
| 95 | + |
| 96 | + // Switch to delegate |
| 97 | + const delegate = await initMainnetUser(TEST_DELEGATE, parseEther("10000")); |
| 98 | + |
| 99 | + // Check membership before |
| 100 | + const membershipBefore = await unitroller.checkMembership(TEST_USER, VUSDT); |
| 101 | + expect(membershipBefore).to.be.false; |
| 102 | + |
| 103 | + await expect(await unitroller.connect(delegate).enterMarketBehalf(TEST_USER, VUSDT)) |
| 104 | + .to.emit(unitroller, "MarketEntered") |
| 105 | + .withArgs(VUSDT, TEST_USER); |
| 106 | + |
| 107 | + // Check membership after |
| 108 | + const membershipAfter = await unitroller.checkMembership(TEST_USER, VUSDT); |
| 109 | + expect(membershipAfter).to.be.true; |
| 110 | + }); |
| 111 | + |
| 112 | + it("should revert when unapproved delegate tries to enter market", async () => { |
| 113 | + const userSigner = await initMainnetUser(TEST_DELEGATE, parseEther("10000")); |
| 114 | + const comptroller = unitroller.connect(userSigner); |
| 115 | + |
| 116 | + try { |
| 117 | + await comptroller.exitMarket(VUSDT); |
| 118 | + } catch (e) { |
| 119 | + // Market might not be entered, continue |
| 120 | + } |
| 121 | + |
| 122 | + const delegateSigner = await initMainnetUser(TEST_USER, parseEther("10000")); |
| 123 | + const comptrollerDelegate = unitroller.connect(delegateSigner); |
| 124 | + |
| 125 | + // Should revert with NotAnApprovedDelegate |
| 126 | + await expect(comptrollerDelegate.enterMarketBehalf(TEST_DELEGATE, VUSDT)).to.be.revertedWithCustomError( |
| 127 | + comptrollerDelegate, |
| 128 | + "NotAnApprovedDelegate", |
| 129 | + ); |
| 130 | + }); |
| 131 | + }); |
| 132 | +}); |
0 commit comments