|
| 1 | +import hre from "hardhat"; |
| 2 | +import {expect} from "chai"; |
| 3 | +import {getContractAt, deploy, toBytes32, setupTests} from "./helpers"; |
| 4 | +import { |
| 5 | + TestUSDC, TestWETH, PaxosOracle, Processor, Netter, TransparentUpgradeableProxy, |
| 6 | +} from "../typechain-types"; |
| 7 | +import {loadFixture} from "@nomicfoundation/hardhat-toolbox/network-helpers"; |
| 8 | +import {addressToBytes32, ZERO_ADDRESS} from "../scripts/common"; |
| 9 | + |
| 10 | +describe("Netter", function () { |
| 11 | + setupTests(); |
| 12 | + |
| 13 | + const USDC = 10n ** 6n; |
| 14 | + const WETH = 10n ** 18n; |
| 15 | + |
| 16 | + const deployAll = async () => { |
| 17 | + const [deployer, admin, caller, user, receiverA, receiverB] = await hre.ethers.getSigners(); |
| 18 | + |
| 19 | + const CALLER_ROLE = toBytes32("CALLER_ROLE"); |
| 20 | + |
| 21 | + const usdcRef = (await deploy("TestUSDC", deployer)) as TestUSDC; |
| 22 | + const assetA = (await deploy("TestUSDC", deployer)) as TestUSDC; // 6 decimals |
| 23 | + const assetB = (await deploy("TestWETH", deployer)) as TestWETH; // 18 decimals |
| 24 | + |
| 25 | + const oracle = (await deploy("PaxosOracle", deployer, {}, admin, usdcRef, [ |
| 26 | + {assetId: addressToBytes32(assetA.target), decimals: 6}, |
| 27 | + {assetId: addressToBytes32(assetB.target), decimals: 18}, |
| 28 | + ])) as PaxosOracle; |
| 29 | + |
| 30 | + const deployProcessor = async (asset: TestUSDC | TestWETH, receiver: typeof receiverA) => { |
| 31 | + const impl = (await deploy("Processor", deployer, {}, asset, receiver, oracle)) as Processor; |
| 32 | + const initData = (await impl.initialize.populateTransaction(admin, admin, admin)).data; |
| 33 | + const proxy = (await deploy( |
| 34 | + "TransparentUpgradeableProxy", deployer, {}, impl, admin, initData, |
| 35 | + )) as TransparentUpgradeableProxy; |
| 36 | + return (await getContractAt("Processor", proxy, deployer)) as Processor; |
| 37 | + }; |
| 38 | + |
| 39 | + const processorA = await deployProcessor(assetA, receiverA); |
| 40 | + const processorB = await deployProcessor(assetB, receiverB); |
| 41 | + |
| 42 | + const netter = (await deploy("Netter", deployer, {}, oracle, admin, caller)) as Netter; |
| 43 | + |
| 44 | + await processorA.connect(admin).grantRole(CALLER_ROLE, netter); |
| 45 | + await processorB.connect(admin).grantRole(CALLER_ROLE, netter); |
| 46 | + |
| 47 | + return { |
| 48 | + deployer, admin, caller, user, receiverA, receiverB, |
| 49 | + assetA, assetB, oracle, processorA, processorB, netter, |
| 50 | + }; |
| 51 | + }; |
| 52 | + |
| 53 | + describe("constructor", function () { |
| 54 | + it("reverts ZeroAddress when oracle is zero", async function () { |
| 55 | + const {admin, caller, netter} = await loadFixture(deployAll); |
| 56 | + await expect(deploy("Netter", admin, {}, ZERO_ADDRESS, admin, caller)) |
| 57 | + .to.be.revertedWithCustomError(netter, "ZeroAddress"); |
| 58 | + }); |
| 59 | + |
| 60 | + it("reverts ZeroAddress when admin is zero", async function () { |
| 61 | + const {oracle, caller, netter} = await loadFixture(deployAll); |
| 62 | + await expect(deploy("Netter", caller, {}, oracle, ZERO_ADDRESS, caller)) |
| 63 | + .to.be.revertedWithCustomError(netter, "ZeroAddress"); |
| 64 | + }); |
| 65 | + |
| 66 | + it("stores ORACLE and grants DEFAULT_ADMIN_ROLE and CALLER_ROLE", async function () { |
| 67 | + const {netter, oracle, admin, caller} = await loadFixture(deployAll); |
| 68 | + const DEFAULT_ADMIN_ROLE = await netter.DEFAULT_ADMIN_ROLE(); |
| 69 | + const CALLER_ROLE = await netter.CALLER_ROLE(); |
| 70 | + |
| 71 | + expect(await netter.ORACLE()).to.equal(oracle.target); |
| 72 | + expect(await netter.hasRole(DEFAULT_ADMIN_ROLE, admin)).to.be.true; |
| 73 | + expect(await netter.hasRole(CALLER_ROLE, caller)).to.be.true; |
| 74 | + expect(await netter.hasRole(CALLER_ROLE, admin)).to.be.false; |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe("net", function () { |
| 79 | + it("reverts AccessControlUnauthorizedAccount when caller lacks CALLER_ROLE", async function () { |
| 80 | + const {netter, user, processorA, processorB} = await loadFixture(deployAll); |
| 81 | + await expect(netter.connect(user).net(processorA, processorB, 1n, 1n)) |
| 82 | + .to.be.revertedWithCustomError(netter, "AccessControlUnauthorizedAccount"); |
| 83 | + }); |
| 84 | + |
| 85 | + it("reverts ValuesNotEqual when oracle values do not match", async function () { |
| 86 | + const {netter, caller, processorA, processorB} = await loadFixture(deployAll); |
| 87 | + await expect(netter.connect(caller).net(processorA, processorB, 3_000n * WETH + 1n, 3_000n * USDC)) |
| 88 | + .to.be.revertedWithCustomError(netter, "ValuesNotEqual"); |
| 89 | + await expect(netter.connect(caller).net(processorA, processorB, 3_000n * WETH - 1n, 3_000n * USDC)) |
| 90 | + .to.be.revertedWithCustomError(netter, "ValuesNotEqual"); |
| 91 | + }); |
| 92 | + |
| 93 | + it("forwards on both processors and emits Netted when values are equal", async function () { |
| 94 | + const {netter, caller, processorA, processorB, assetA, assetB, receiverA, receiverB} = |
| 95 | + await loadFixture(deployAll); |
| 96 | + |
| 97 | + const amountFromA = 4_000n * WETH; |
| 98 | + const amountFromB = 4_000n * USDC; |
| 99 | + |
| 100 | + await assetB.mint(processorA, amountFromA); |
| 101 | + await assetA.mint(processorB, amountFromB); |
| 102 | + |
| 103 | + const tx = await netter.connect(caller).net(processorA, processorB, amountFromA, amountFromB); |
| 104 | + |
| 105 | + await expect(tx).to.emit(netter, "Netted") |
| 106 | + .withArgs(processorA.target, processorB.target, amountFromA, amountFromB); |
| 107 | + await expect(tx).to.emit(processorA, "Forwarded") |
| 108 | + .withArgs(netter.target, assetB.target, amountFromA); |
| 109 | + await expect(tx).to.emit(processorB, "Forwarded") |
| 110 | + .withArgs(netter.target, assetA.target, amountFromB); |
| 111 | + |
| 112 | + expect(await assetB.balanceOf(processorA)).to.equal(0n); |
| 113 | + expect(await assetA.balanceOf(processorB)).to.equal(0n); |
| 114 | + expect(await assetB.balanceOf(receiverA)).to.equal(amountFromA); |
| 115 | + expect(await assetA.balanceOf(receiverB)).to.equal(amountFromB); |
| 116 | + }); |
| 117 | + |
| 118 | + it("forwards on both processors and emits Netted when values are equal, swapped order", async function () { |
| 119 | + const {netter, caller, processorA, processorB, assetA, assetB, receiverA, receiverB} = |
| 120 | + await loadFixture(deployAll); |
| 121 | + |
| 122 | + const amountFromA = 4_000n * WETH; |
| 123 | + const amountFromB = 4_000n * USDC; |
| 124 | + |
| 125 | + await assetB.mint(processorA, amountFromA); |
| 126 | + await assetA.mint(processorB, amountFromB); |
| 127 | + |
| 128 | + const tx = await netter.connect(caller).net(processorB, processorA, amountFromB, amountFromA); |
| 129 | + |
| 130 | + await expect(tx).to.emit(netter, "Netted") |
| 131 | + .withArgs(processorB.target, processorA.target, amountFromB, amountFromA); |
| 132 | + await expect(tx).to.emit(processorA, "Forwarded") |
| 133 | + .withArgs(netter.target, assetB.target, amountFromA); |
| 134 | + await expect(tx).to.emit(processorB, "Forwarded") |
| 135 | + .withArgs(netter.target, assetA.target, amountFromB); |
| 136 | + |
| 137 | + expect(await assetB.balanceOf(processorA)).to.equal(0n); |
| 138 | + expect(await assetA.balanceOf(processorB)).to.equal(0n); |
| 139 | + expect(await assetB.balanceOf(receiverA)).to.equal(amountFromA); |
| 140 | + expect(await assetA.balanceOf(receiverB)).to.equal(amountFromB); |
| 141 | + }); |
| 142 | + |
| 143 | + it("leaves unrelated balances untouched", async function () { |
| 144 | + const {netter, caller, processorA, processorB, assetA, assetB, receiverA, receiverB} = |
| 145 | + await loadFixture(deployAll); |
| 146 | + |
| 147 | + const amountFromA = 4_000n * WETH; |
| 148 | + const amountFromB = 4_000n * USDC; |
| 149 | + |
| 150 | + // processorA also holds some assetA; processorB also holds some assetB |
| 151 | + await assetB.mint(processorA, amountFromA); |
| 152 | + await assetA.mint(processorA, 1_000n * USDC); |
| 153 | + await assetA.mint(processorB, amountFromB); |
| 154 | + await assetB.mint(processorB, 2_000n * WETH); |
| 155 | + |
| 156 | + await netter.connect(caller).net(processorA, processorB, amountFromA, amountFromB); |
| 157 | + |
| 158 | + expect(await assetA.balanceOf(processorA)).to.equal(1_000n * USDC); |
| 159 | + expect(await assetB.balanceOf(processorB)).to.equal(2_000n * WETH); |
| 160 | + expect(await assetA.balanceOf(receiverA)).to.equal(0n); |
| 161 | + expect(await assetB.balanceOf(receiverB)).to.equal(0n); |
| 162 | + }); |
| 163 | + }); |
| 164 | +}); |
0 commit comments