Skip to content

Commit

Permalink
feat: added recoverUserFunds and added view function for eq cov ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
MrBeaverTail committed May 13, 2022
1 parent 5fdff0a commit ef8a754
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
26 changes: 23 additions & 3 deletions contracts/pool/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import '../interfaces/IPool.sol';
* Note The ownership will be transferred to a governance contract once Platypus community can show to govern itself.
*
* The unique features of the Platypus make it an important subject in the study of evolutionary biology.
* + Added recover user funds (for funds mistakingly sent to this contract)
* + Added view function for eqCovRatio
*/
contract Pool is Initializable, OwnableUpgradeable, ReentrancyGuardUpgradeable, PausableUpgradeable, Core, IPool {
using DSMath for uint256;
Expand Down Expand Up @@ -409,7 +411,16 @@ contract Pool is Initializable, OwnableUpgradeable, ReentrancyGuardUpgradeable,
* @dev [ sum of Ai * fi / sum Li * fi ]
* @return equilibriumCoverageRatio system equilibrium coverage ratio
*/
function getEquilibriumCoverageRatio() private view returns (uint256) {
function getEquilibriumCoverageRatio() external view returns (uint256) {
return _getEquilibriumCoverageRatio();
}

/**
* @notice gets system equilibrium coverage ratio
* @dev [ sum of Ai * fi / sum Li * fi ]
* @return equilibriumCoverageRatio system equilibrium coverage ratio
*/
function _getEquilibriumCoverageRatio() private view returns (uint256) {
uint256 totalCash = 0;
uint256 totalLiability = 0;

Expand Down Expand Up @@ -494,7 +505,7 @@ contract Pool is Initializable, OwnableUpgradeable, ReentrancyGuardUpgradeable,
}

// get equilibrium coverage ratio
uint256 eqCov = getEquilibriumCoverageRatio();
uint256 eqCov = _getEquilibriumCoverageRatio();

// apply impairment gain if eqCov < 1
if (eqCov < ETH_UNIT) {
Expand Down Expand Up @@ -568,7 +579,7 @@ contract Pool is Initializable, OwnableUpgradeable, ReentrancyGuardUpgradeable,
);

// Get equilibrium coverage ratio before withdraw
uint256 eqCov = getEquilibriumCoverageRatio();
uint256 eqCov = _getEquilibriumCoverageRatio();

// Init enoughCash to true
enoughCash = true;
Expand Down Expand Up @@ -952,4 +963,13 @@ contract Pool is Initializable, OwnableUpgradeable, ReentrancyGuardUpgradeable,
function getTokenAddresses() external view override returns (address[] memory) {
return _assets.keys;
}

/**
* @notice Recover any funds mistakingly sent to this contract
* @param token the address of the token to retrieve
*/
function recoverUserFunds(address token) external onlyDev {
uint256 currentBalance = IERC20(token).balanceOf(address(this));
IERC20(token).safeTransfer(msg.sender, currentBalance);
}
}
3 changes: 3 additions & 0 deletions test/pool-main/Pool.impairment.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ describe('Pool', function () {
// quote withdrawal
const [quotedWithdrawal] = await this.pool.quotePotentialWithdraw(this.DAI.address, parseEther('1000'))

const eqCov = await this.pool.getEquilibriumCoverageRatio()
expect(eqCov).to.be.equal(parseEther('0.625'))

// withdraw 1000 DAI
const receipt = await this.pool
.connect(users[0])
Expand Down
20 changes: 20 additions & 0 deletions test/pool-main/Pool.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,24 @@ describe('Pool', function () {
await expect(this.pool.connect(users[0]).unpause()).to.be.revertedWith('FORBIDDEN')
})
})

describe('recover user funds', function () {
it('works', async function () {
const dummyToken = await TestERC20.connect(users[0]).deploy('Dummy', 'Dummy', 18, parseEther('1000'))
expect(await dummyToken.balanceOf(this.pool.address)).to.be.equal('0')

// OMG some guy wants to send money to the pool for no reason. why?
await dummyToken.connect(users[0]).transfer(this.pool.address, parseEther('500'))
expect(await dummyToken.balanceOf(this.pool.address)).to.be.equal(parseEther('500'))

// A random guy cannot take these funds
await expect(this.pool.connect(users[6]).recoverUserFunds(dummyToken.address)).to.be.revertedWith('FORBIDDEN')

// Hero dev can save those funds now
await this.pool.recoverUserFunds(dummyToken.address)

expect(await dummyToken.balanceOf(this.pool.address)).to.be.equal('0')
expect(await dummyToken.balanceOf(owner.address)).to.be.equal(parseEther('500'))
})
})
})

0 comments on commit ef8a754

Please sign in to comment.