|
| 1 | +import { MockContract, smock } from "@defi-wonderland/smock" |
| 2 | +import chai, { expect } from "chai" |
| 3 | +import { Wallet } from "ethers" |
| 4 | +import { parseEther } from "ethers/lib/utils" |
| 5 | +import { ethers, waffle } from "hardhat" |
| 6 | +import { |
| 7 | + ChainlinkPriceFeedV2, |
| 8 | + ChainlinkPriceFeedV2__factory, |
| 9 | + PriceFeedUpdater, |
| 10 | + TestAggregatorV3__factory, |
| 11 | +} from "../typechain" |
| 12 | + |
| 13 | +chai.use(smock.matchers) |
| 14 | + |
| 15 | +interface PriceFeedUpdaterFixture { |
| 16 | + ethPriceFeed: MockContract<ChainlinkPriceFeedV2> |
| 17 | + btcPriceFeed: MockContract<ChainlinkPriceFeedV2> |
| 18 | + priceFeedUpdater: PriceFeedUpdater |
| 19 | + admin: Wallet |
| 20 | + alice: Wallet |
| 21 | +} |
| 22 | + |
| 23 | +describe("PriceFeedUpdater Spec", () => { |
| 24 | + const loadFixture: ReturnType<typeof waffle.createFixtureLoader> = waffle.createFixtureLoader() |
| 25 | + let fixture: PriceFeedUpdaterFixture |
| 26 | + |
| 27 | + beforeEach(async () => { |
| 28 | + fixture = await loadFixture(createFixture) |
| 29 | + }) |
| 30 | + afterEach(async () => { |
| 31 | + fixture.btcPriceFeed.update.reset() |
| 32 | + fixture.ethPriceFeed.update.reset() |
| 33 | + }) |
| 34 | + |
| 35 | + async function executeFallback(priceFeedUpdater: PriceFeedUpdater) { |
| 36 | + const { alice } = fixture |
| 37 | + await alice.sendTransaction({ |
| 38 | + to: priceFeedUpdater.address, |
| 39 | + value: 0, |
| 40 | + gasLimit: 150000, // Give gas limit to force run transaction without dry run |
| 41 | + }) |
| 42 | + } |
| 43 | + |
| 44 | + async function createFixture(): Promise<PriceFeedUpdaterFixture> { |
| 45 | + const [admin, alice] = waffle.provider.getWallets() |
| 46 | + |
| 47 | + const aggregatorFactory = await smock.mock<TestAggregatorV3__factory>("TestAggregatorV3") |
| 48 | + const aggregator = await aggregatorFactory.deploy() |
| 49 | + |
| 50 | + const chainlinkPriceFeedV2Factory = await smock.mock<ChainlinkPriceFeedV2__factory>("ChainlinkPriceFeedV2") |
| 51 | + const ethPriceFeed = await chainlinkPriceFeedV2Factory.deploy(aggregator.address, 900) |
| 52 | + const btcPriceFeed = await chainlinkPriceFeedV2Factory.deploy(aggregator.address, 900) |
| 53 | + |
| 54 | + await ethPriceFeed.deployed() |
| 55 | + await btcPriceFeed.deployed() |
| 56 | + |
| 57 | + const priceFeedUpdaterFactory = await ethers.getContractFactory("PriceFeedUpdater") |
| 58 | + const priceFeedUpdater = (await priceFeedUpdaterFactory.deploy([ |
| 59 | + ethPriceFeed.address, |
| 60 | + btcPriceFeed.address, |
| 61 | + ])) as PriceFeedUpdater |
| 62 | + |
| 63 | + return { ethPriceFeed, btcPriceFeed, priceFeedUpdater, admin, alice } |
| 64 | + } |
| 65 | + it("the result of getPriceFeeds should be same as priceFeeds given when deployment", async () => { |
| 66 | + const { ethPriceFeed, btcPriceFeed, priceFeedUpdater } = fixture |
| 67 | + const priceFeeds = await priceFeedUpdater.getPriceFeeds() |
| 68 | + expect(priceFeeds).deep.equals([ethPriceFeed.address, btcPriceFeed.address]) |
| 69 | + }) |
| 70 | + |
| 71 | + it("force error, when someone sent eth to contract", async () => { |
| 72 | + const { alice, priceFeedUpdater } = fixture |
| 73 | + const tx = alice.sendTransaction({ |
| 74 | + to: priceFeedUpdater.address, |
| 75 | + value: parseEther("0.1"), |
| 76 | + gasLimit: 150000, // Give gas limit to force run transaction without dry run |
| 77 | + }) |
| 78 | + await expect(tx).to.be.reverted |
| 79 | + }) |
| 80 | + |
| 81 | + describe("When priceFeedUpdater fallback execute", () => { |
| 82 | + it("should success if all priceFeed are updated successfully", async () => { |
| 83 | + const { ethPriceFeed, btcPriceFeed, priceFeedUpdater } = fixture |
| 84 | + |
| 85 | + await executeFallback(priceFeedUpdater) |
| 86 | + |
| 87 | + expect(ethPriceFeed.update).to.have.been.calledOnce |
| 88 | + expect(btcPriceFeed.update).to.have.been.calledOnce |
| 89 | + }) |
| 90 | + it("should still success if any one of priceFeed is updated fail", async () => { |
| 91 | + const { ethPriceFeed, btcPriceFeed, priceFeedUpdater } = fixture |
| 92 | + |
| 93 | + ethPriceFeed.update.reverts() |
| 94 | + await executeFallback(priceFeedUpdater) |
| 95 | + |
| 96 | + expect(ethPriceFeed.update).to.have.been.calledOnce |
| 97 | + expect(btcPriceFeed.update).to.have.been.calledOnce |
| 98 | + }) |
| 99 | + }) |
| 100 | +}) |
0 commit comments