From 5e73564e4f9c86a50e319070fdcbec77d0d44935 Mon Sep 17 00:00:00 2001 From: Josh Levine Date: Thu, 11 Nov 2021 21:13:57 -0600 Subject: [PATCH] fix(Periphery): make a pass on swap rounding bug --- pkg/core/src/Periphery.sol | 2 +- pkg/core/src/adapters/BaseAdapter.sol | 2 +- pkg/deployments/deploy/simulated/02-series.js | 26 +++++++++++++++++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/pkg/core/src/Periphery.sol b/pkg/core/src/Periphery.sol index 47859827d..5bc632d0f 100644 --- a/pkg/core/src/Periphery.sol +++ b/pkg/core/src/Periphery.sol @@ -348,7 +348,7 @@ contract Periphery is Trust { // (5) Combine zeros and claim uint256 tBal = divider.combine(adapter, maturity, zBal); - return (keccak256("ERC3156FlashBorrower.onFlashLoan"), tBal - amount); + return (keccak256("ERC3156FlashBorrower.onFlashLoan"), amount - tBal); } return (keccak256("ERC3156FlashBorrower.onFlashLoan"), 0); } diff --git a/pkg/core/src/adapters/BaseAdapter.sol b/pkg/core/src/adapters/BaseAdapter.sol index 6e275d548..87bc0ecd1 100644 --- a/pkg/core/src/adapters/BaseAdapter.sol +++ b/pkg/core/src/adapters/BaseAdapter.sol @@ -87,7 +87,7 @@ abstract contract BaseAdapter is Initializable { require(target.transfer(address(receiver), amount), Errors.FlashTransferFailed); (bytes32 keccak, uint256 value) = IPeriphery(receiver).onFlashLoan(data, msg.sender, adapter, maturity, amount); require(keccak == CALLBACK_SUCCESS, Errors.FlashCallbackFailed); - require(target.transferFrom(address(receiver), address(this), amount), Errors.FlashRepayFailed); + require(target.transferFrom(address(receiver), address(this), amount - 1), Errors.FlashRepayFailed); return (true, value); } diff --git a/pkg/deployments/deploy/simulated/02-series.js b/pkg/deployments/deploy/simulated/02-series.js index 7195d8c7b..ce223c135 100644 --- a/pkg/deployments/deploy/simulated/02-series.js +++ b/pkg/deployments/deploy/simulated/02-series.js @@ -24,7 +24,10 @@ module.exports = async function ({ ethers, getNamedAccounts }) { const mockAdapterImpl = await ethers.getContract("MockAdapter"); const adapter = new ethers.Contract(adapterAddress, mockAdapterImpl.interface, signer); - const { zero: zeroAddress } = await periphery.callStatic.sponsorSeries(adapter.address, seriesMaturity); + const { zero: zeroAddress, claim: claimAddress } = await periphery.callStatic.sponsorSeries( + adapter.address, + seriesMaturity, + ); console.log(`Initializing Series maturing on ${dayjs(seriesMaturity * 1000)} for ${targetName}`); await periphery.sponsorSeries(adapter.address, seriesMaturity).then(tx => tx.wait()); @@ -33,6 +36,7 @@ module.exports = async function ({ ethers, getNamedAccounts }) { const { abi: tokenAbi } = await deployments.getArtifact("Token"); const zero = new ethers.Contract(zeroAddress, tokenAbi, signer); + const claim = new ethers.Contract(claimAddress, tokenAbi, signer); const zeroBalance = await zero.balanceOf(deployer); @@ -44,12 +48,30 @@ module.exports = async function ({ ethers, getNamedAccounts }) { console.log("Minting Underyling for the Balancer Vault"); await underlying.mint(mockBalancerVault.address, zeroBalance).then(tx => tx.wait()); - console.log(`Sanity check swap`); + console.log(`--- Sanity check swap ---`); await target.approve(periphery.address, ethers.constants.MaxUint256).then(tx => tx.wait()); + console.log("swapping target for zeros"); await periphery .swapTargetForZeros(adapter.address, seriesMaturity, ethers.utils.parseEther("1"), 0) .then(tx => tx.wait()); + + console.log("swapping target for claims"); + await periphery + .swapTargetForClaims(adapter.address, seriesMaturity, ethers.utils.parseEther("1")) + .then(tx => tx.wait()); + + console.log("swapping zeros for target"); + await zero.approve(periphery.address, ethers.constants.MaxUint256).then(tx => tx.wait()); + await periphery + .swapZerosForTarget(adapter.address, seriesMaturity, ethers.utils.parseEther("0.5"), 0) + .then(tx => tx.wait()); + + console.log("swapping claims for target"); + await claim.approve(periphery.address, ethers.constants.MaxUint256).then(tx => tx.wait()); + await periphery + .swapClaimsForTarget(adapter.address, seriesMaturity, ethers.utils.parseEther("0.5")) + .then(tx => tx.wait()); } } };