Skip to content

Commit 488b8a7

Browse files
authored
fix(FlashMintExtended): reduce contract size (#171)
* Fix contract size * Fix tests * Optimize tests
1 parent 2278397 commit 488b8a7

File tree

3 files changed

+63
-75
lines changed

3 files changed

+63
-75
lines changed

contracts/exchangeIssuance/FlashMintLeveragedExtended.sol

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ contract FlashMintLeveragedExtended is FlashMintLeveraged, Ownable {
176176
wethBalanceBefore
177177
);
178178
uint256 wethObtained = IERC20(addresses.weth).balanceOf(address(this)).sub(wethBalanceBefore);
179-
require(wethObtained >= _outputTokenAmount, "FlashMintLeveragedExtended: insufficient wethObtained");
180-
require(wethObtained - _outputTokenAmount <= maxGasRebate, "FlashMintLeveragedExtended: maxGasRebate exceeded");
179+
require(wethObtained >= _outputTokenAmount, "IWO");
180+
require(wethObtained - _outputTokenAmount <= maxGasRebate, "MGR");
181181
IWETH(addresses.weth).withdraw(wethObtained);
182182
(payable(msg.sender)).sendValue(wethObtained);
183183
return setBalanceBefore.sub(_setToken.balanceOf(msg.sender));
@@ -478,7 +478,7 @@ contract FlashMintLeveragedExtended is FlashMintLeveraged, Ownable {
478478
internal
479479
{
480480
uint256 outputTokenObtained = IERC20(_outputToken).balanceOf(address(this)).sub(_outputTokenBalanceBefore);
481-
require(outputTokenObtained >= _outputTokenAmount, "FlashMintLeveragedExtended: insufficient outputTokenObtained");
481+
require(outputTokenObtained >= _outputTokenAmount, "IOTO");
482482
IERC20(_outputToken).transfer(msg.sender, _outputTokenAmount);
483483
_swapTokenForETHAndReturnToUser(_outputToken, outputTokenObtained - _outputTokenAmount, _swapDataOutputTokenForETH);
484484
}
@@ -498,11 +498,11 @@ contract FlashMintLeveragedExtended is FlashMintLeveraged, Ownable {
498498
internal
499499
returns(uint256)
500500
{
501-
require(_inputTokenAmount > _maxDust, "FlashMintLeveragedExtended: _inputToken must be more than _maxDust");
501+
require(_inputTokenAmount > _maxDust, "MD");
502502

503503
uint256 iterations = 0;
504504
while (_inputTokenAmount > _maxDust) {
505-
require(iterations < maxIterations, "FlashMintLeveragedExtended: exceeded Max Iterations");
505+
require(iterations < maxIterations, "MI");
506506
uint256 inputTokenAmountSpent = _initiateIssuanceAndReturnInputAmountSpent(
507507
_setToken,
508508
_minSetAmount,
@@ -688,15 +688,15 @@ contract FlashMintLeveragedExtended is FlashMintLeveraged, Ownable {
688688
if(_swapData.path.length == 0) {
689689
return;
690690
}
691-
require(_swapData.path[0] == _inputToken, "FlashMintLeveragedExtended: InputToken not first in path");
691+
require(_swapData.path[0] == _inputToken, "ITNF");
692692
require(_swapData.path[_swapData.path.length - 1] == addresses.weth, "FlashMintLeveragedExtended: WETH not last in path");
693693
ethObtained = addresses.swapExactTokensForTokens(
694694
_inputAmount,
695695
0,
696696
_swapData
697697
);
698698
}
699-
require(ethObtained <= maxGasRebate, "FlashMintLeveragedExtended: maxGasRebate exceeded");
699+
require(ethObtained <= maxGasRebate, "MGR");
700700

701701
IWETH(addresses.weth).withdraw(ethObtained);
702702
msg.sender.transfer(ethObtained);

hardhat.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ const config: HardhatUserConfig = {
5252
networks: {
5353
hardhat: {
5454
forking: process.env.FORK ? forkingConfig : undefined,
55-
allowUnlimitedContractSize: true,
5655
accounts: getHardhatPrivateKeys(),
5756
// @ts-ignore
5857
timeout: INTEGRATIONTEST_TIMEOUT,

test/integration/arbitrum/flashMintLeveragedExtended.spec.ts

Lines changed: 56 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,6 @@ if (process.env.INTEGRATIONTEST) {
129129
await weth.transfer(owner.address, ether(100));
130130
await aweth.transfer(owner.address, ether(100));
131131

132-
// This is done to avoid flaky "Invalid transfer in, results in undercollateralization" error
133-
// See: https://github.com/IndexCoop/index-protocol/blob/1a587d93d273d9004d03f1235c395f6f7cd147dc/test/protocol/modules/v1/debtIssuanceModuleV2.spec.ts#L730
134-
// TODO: Review if we have to do this in production.
135-
await aweth.transfer(setToken.address, ether(0.000001));
136-
137132
await aweth
138133
.connect(owner.wallet)
139134
.approve(addresses.setFork.debtIssuanceModuleV2, ether(10));
@@ -191,10 +186,10 @@ if (process.env.INTEGRATIONTEST) {
191186
).to.equal(MAX_UINT_256);
192187
});
193188

194-
["collateralToken", "USDC", "ETH"].forEach(inputTokenName => {
189+
["USDC", "ETH"].forEach(inputTokenName => {
195190
describe(`When input/output token is ${inputTokenName}`, () => {
196191
let amountIn: BigNumber;
197-
beforeEach(async () => {
192+
before(async () => {
198193
amountIn = ether(0.4);
199194
if (inputTokenName === "USDC") {
200195
amountIn = utils.parseUnits("2500", 6);
@@ -220,8 +215,11 @@ if (process.env.INTEGRATIONTEST) {
220215
let subjectSetToken: Address;
221216
let subjectMaxAmountIn: BigNumber;
222217
let subjectInputToken: Address;
218+
let setBalancebefore: BigNumber;
219+
let inputBalanceBefore: BigNumber;
220+
let quotedInputAmount: BigNumber;
223221

224-
beforeEach(async () => {
222+
before(async () => {
225223
subjectSetAmount = ether(1);
226224
swapDataDebtToCollateral = {
227225
path: [addresses.tokens.USDC, addresses.tokens.weth],
@@ -255,6 +253,13 @@ if (process.env.INTEGRATIONTEST) {
255253
subjectInputToken = inputToken.address;
256254
}
257255
subjectSetToken = setToken.address;
256+
setBalancebefore = await setToken.balanceOf(owner.address);
257+
inputBalanceBefore =
258+
inputTokenName === "ETH"
259+
? await owner.wallet.getBalance()
260+
: await inputToken.balanceOf(owner.address);
261+
quotedInputAmount = await subjectQuote();
262+
await subject();
258263
});
259264

260265
async function subject() {
@@ -287,35 +292,22 @@ if (process.env.INTEGRATIONTEST) {
287292
}
288293

289294
it("should issue the correct amount of tokens", async () => {
290-
const setBalancebefore = await setToken.balanceOf(owner.address);
291-
await subject();
292295
const setBalanceAfter = await setToken.balanceOf(owner.address);
293296
const setObtained = setBalanceAfter.sub(setBalancebefore);
294297
expect(setObtained).to.eq(subjectSetAmount);
295298
});
296299

297300
it("should spend less than specified max amount", async () => {
298-
const inputBalanceBefore =
299-
inputTokenName === "ETH"
300-
? await owner.wallet.getBalance()
301-
: await inputToken.balanceOf(owner.address);
302-
await subject();
303301
const inputBalanceAfter =
304302
inputTokenName === "ETH"
305303
? await owner.wallet.getBalance()
306304
: await inputToken.balanceOf(owner.address);
307305
const inputSpent = inputBalanceBefore.sub(inputBalanceAfter);
308-
expect(inputSpent.gt(0)).to.be.true;
309-
expect(inputSpent.lte(subjectMaxAmountIn)).to.be.true;
306+
expect(inputSpent).to.be.gt(0);
307+
expect(inputSpent).to.be.lte(subjectMaxAmountIn);
310308
});
311309

312310
it("should quote the correct input amount", async () => {
313-
const quotedInputAmount = await subjectQuote();
314-
const inputBalanceBefore =
315-
inputTokenName === "ETH"
316-
? await owner.wallet.getBalance()
317-
: await inputToken.balanceOf(owner.address);
318-
await subject();
319311
const inputBalanceAfter =
320312
inputTokenName === "ETH"
321313
? await owner.wallet.getBalance()
@@ -344,7 +336,12 @@ if (process.env.INTEGRATIONTEST) {
344336
let subjectPriceEstimateInflater: BigNumber;
345337
let subjectMaxDust: BigNumber;
346338

347-
beforeEach(async () => {
339+
let setBalancebefore: BigNumber;
340+
let ethBalanceBefore: BigNumber;
341+
let gasCosts: BigNumber;
342+
let inputBalanceBefore: BigNumber;
343+
344+
before(async () => {
348345
swapDataDebtToCollateral = {
349346
path: [addresses.tokens.USDC, addresses.tokens.weth],
350347
fees: [500],
@@ -379,6 +376,15 @@ if (process.env.INTEGRATIONTEST) {
379376
subjectMinSetAmount = ether(1);
380377
subjectSetToken = setToken.address;
381378
swapDataInputTokenToETH = swapDataInputTokenToCollateral; // Assumes Collateral Token is WETH
379+
setBalancebefore = await setToken.balanceOf(owner.address);
380+
ethBalanceBefore = await owner.wallet.getBalance();
381+
inputBalanceBefore =
382+
inputTokenName === "ETH"
383+
? await owner.wallet.getBalance()
384+
: await inputToken.balanceOf(owner.address);
385+
const tx = await subject();
386+
const receipt = await tx.wait();
387+
gasCosts = receipt.gasUsed.mul(tx.gasPrice);
382388
});
383389

384390
async function subject() {
@@ -407,34 +413,19 @@ if (process.env.INTEGRATIONTEST) {
407413
}
408414

409415
it("should issue at least minSetAmount of set tokens", async () => {
410-
const setBalancebefore = await setToken.balanceOf(owner.address);
411-
await subject();
412416
const setBalanceAfter = await setToken.balanceOf(owner.address);
413417
const setObtained = setBalanceAfter.sub(setBalancebefore);
414418
expect(setObtained).to.gte(subjectMinSetAmount);
415419
});
416420

417421
if (inputTokenName !== "ETH") {
418422
it("should give gas rebaste", async () => {
419-
const ethBalanceBefore = await owner.wallet.getBalance();
420-
const tx = await subject();
421-
const receipt = await tx.wait();
422-
const gasCosts = receipt.gasUsed.mul(tx.gasPrice);
423423
const ethBalanceAfter = await owner.wallet.getBalance();
424424
expect(ethBalanceBefore.sub(ethBalanceAfter)).to.lt(gasCosts);
425425
});
426426
}
427427

428428
it("should spend exactly inputAmount", async () => {
429-
const inputBalanceBefore =
430-
inputTokenName === "ETH"
431-
? await owner.wallet.getBalance()
432-
: await inputToken.balanceOf(owner.address);
433-
434-
const tx = await subject();
435-
const receipt = await tx.wait();
436-
const gasCosts = receipt.gasUsed.mul(tx.gasPrice);
437-
438429
const inputBalanceAfter =
439430
inputTokenName === "ETH"
440431
? await owner.wallet.getBalance()
@@ -468,6 +459,10 @@ if (process.env.INTEGRATIONTEST) {
468459
let subjectOutputToken: Address;
469460
let subjectPriceEstimateInflater: BigNumber;
470461
let subjectMaxDust: BigNumber;
462+
let setBalanceBefore: BigNumber;
463+
let ethBalanceBefore: BigNumber;
464+
let outputBalanceBefore: BigNumber;
465+
let gasCosts: BigNumber;
471466

472467
async function subject() {
473468
if (inputTokenName === "ETH") {
@@ -498,7 +493,7 @@ if (process.env.INTEGRATIONTEST) {
498493
);
499494
}
500495

501-
beforeEach(async () => {
496+
before(async () => {
502497
subjectPriceEstimateInflater = ether(0.9);
503498
subjectMaxSetAmount = ether(1);
504499
subjectAmountOut =
@@ -547,35 +542,32 @@ if (process.env.INTEGRATIONTEST) {
547542
if (inputTokenName !== "ETH") {
548543
subjectOutputToken = outputToken.address;
549544
}
545+
setBalanceBefore = await setToken.balanceOf(owner.address);
546+
ethBalanceBefore = await owner.wallet.getBalance();
547+
outputBalanceBefore =
548+
inputTokenName === "ETH"
549+
? await owner.wallet.getBalance()
550+
: await outputToken.balanceOf(owner.address);
551+
552+
const tx = await subject();
553+
const receipt = await tx.wait();
554+
gasCosts = receipt.gasUsed.mul(tx.gasPrice);
550555
});
551556

552557
it("should redeem at most subjectMaxSetAmount", async () => {
553-
const setBalanceBefore = await setToken.balanceOf(owner.address);
554-
await subject();
555558
const setBalanceAfter = await setToken.balanceOf(owner.address);
556559
const setRedeemed = setBalanceBefore.sub(setBalanceAfter);
557560
expect(setRedeemed).to.lte(subjectMaxSetAmount);
558561
});
559562

560563
if (inputTokenName !== "ETH") {
561564
it("should give gas rebaste", async () => {
562-
const ethBalanceBefore = await owner.wallet.getBalance();
563-
const tx = await subject();
564-
const receipt = await tx.wait();
565-
const gasCosts = receipt.gasUsed.mul(tx.gasPrice);
566565
const ethBalanceAfter = await owner.wallet.getBalance();
567566
expect(ethBalanceBefore.sub(ethBalanceAfter)).to.lt(gasCosts);
568567
});
569568
}
570569

571570
it("should return exactly specified of output tokens", async () => {
572-
const outputBalanceBefore =
573-
inputTokenName === "ETH"
574-
? await owner.wallet.getBalance()
575-
: await outputToken.balanceOf(owner.address);
576-
const tx = await subject();
577-
const receipt = await tx.wait();
578-
const gasCosts = receipt.gasUsed.mul(tx.gasPrice);
579571
const outputBalanceAfter =
580572
inputTokenName === "ETH"
581573
? await owner.wallet.getBalance()
@@ -603,6 +595,9 @@ if (process.env.INTEGRATIONTEST) {
603595
let subjectSetAmount: BigNumber;
604596
let subjectMinAmountOut: BigNumber;
605597
let subjectOutputToken: Address;
598+
let setBalanceBefore: BigNumber;
599+
let outputBalanceBefore: BigNumber;
600+
let outputAmountQuote: BigNumber;
606601

607602
async function subject() {
608603
if (inputTokenName === "ETH") {
@@ -633,7 +628,7 @@ if (process.env.INTEGRATIONTEST) {
633628
);
634629
}
635630

636-
beforeEach(async () => {
631+
before(async () => {
637632
subjectSetAmount = ether(1);
638633
swapDataCollateralToDebt = {
639634
path: [collateralTokenAddress, addresses.tokens.USDC],
@@ -664,22 +659,22 @@ if (process.env.INTEGRATIONTEST) {
664659
if (inputTokenName !== "ETH") {
665660
subjectOutputToken = outputToken.address;
666661
}
662+
setBalanceBefore = await setToken.balanceOf(owner.address);
663+
outputBalanceBefore =
664+
inputTokenName === "ETH"
665+
? await owner.wallet.getBalance()
666+
: await outputToken.balanceOf(owner.address);
667+
outputAmountQuote = await subjectQuote();
668+
await subject();
667669
});
668670

669671
it("should redeem the correct amount of tokens", async () => {
670-
const setBalanceBefore = await setToken.balanceOf(owner.address);
671-
await subject();
672672
const setBalanceAfter = await setToken.balanceOf(owner.address);
673673
const setRedeemed = setBalanceBefore.sub(setBalanceAfter);
674674
expect(setRedeemed).to.eq(subjectSetAmount);
675675
});
676676

677677
it("should return at least the specified minimum of output tokens", async () => {
678-
const outputBalanceBefore =
679-
inputTokenName === "ETH"
680-
? await owner.wallet.getBalance()
681-
: await outputToken.balanceOf(owner.address);
682-
await subject();
683678
const outputBalanceAfter =
684679
inputTokenName === "ETH"
685680
? await owner.wallet.getBalance()
@@ -689,18 +684,12 @@ if (process.env.INTEGRATIONTEST) {
689684
});
690685

691686
it("should quote the correct output amount", async () => {
692-
const outputBalanceBefore =
693-
inputTokenName === "ETH"
694-
? await owner.wallet.getBalance()
695-
: await outputToken.balanceOf(owner.address);
696-
await subject();
697687
const outputBalanceAfter =
698688
inputTokenName === "ETH"
699689
? await owner.wallet.getBalance()
700690
: await outputToken.balanceOf(owner.address);
701691
const outputObtained = outputBalanceAfter.sub(outputBalanceBefore);
702692

703-
const outputAmountQuote = await subjectQuote();
704693
expect(outputAmountQuote).to.gt(preciseMul(outputObtained, ether(0.99)));
705694
expect(outputAmountQuote).to.lt(preciseMul(outputObtained, ether(1.01)));
706695
});

0 commit comments

Comments
 (0)