This repository has been archived by the owner on Oct 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from pinkiebell/pinkie/harness
WIP: test harness
- Loading branch information
Showing
5 changed files
with
312 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.6.12; | ||
import "../interfaces/IStrategy.sol"; | ||
import "@boringcrypto/boring-solidity/contracts/libraries/BoringMath.sol"; | ||
import "@boringcrypto/boring-solidity/contracts/libraries/BoringERC20.sol"; | ||
|
||
// solhint-disable not-rely-on-time | ||
|
||
contract DummyStrategyMock is IStrategy { | ||
using BoringMath for uint256; | ||
using BoringERC20 for IERC20; | ||
|
||
IERC20 private immutable token; | ||
address private immutable bentoBox; | ||
|
||
int256 public _harvestProfit; | ||
|
||
modifier onlyBentoBox() { | ||
require(msg.sender == bentoBox, "Ownable: caller is not the owner"); | ||
_; | ||
} | ||
|
||
constructor(address bentoBox_, IERC20 token_) public { | ||
bentoBox = bentoBox_; | ||
token = token_; | ||
} | ||
|
||
function setHarvestProfit(int256 val) public { | ||
_harvestProfit = val; | ||
} | ||
|
||
// Send the assets to the Strategy and call skim to invest them | ||
function skim(uint256) external override onlyBentoBox { | ||
return; | ||
} | ||
|
||
// Harvest any profits made converted to the asset and pass them to the caller | ||
function harvest( | ||
uint256 balance, | ||
address /*sender*/ | ||
) external override onlyBentoBox returns (int256 amountAdded) { | ||
if (_harvestProfit > 0) { | ||
amountAdded = int256(balance) + _harvestProfit; | ||
} else { | ||
amountAdded = int256(balance) - _harvestProfit; | ||
} | ||
} | ||
|
||
// Withdraw assets. The returned amount can differ from the requested amount due to rounding or if the request was more than there is. | ||
function withdraw(uint256 amount) external override onlyBentoBox returns (uint256 actualAmount) { | ||
actualAmount = token.balanceOf(address(this)); | ||
if (amount > actualAmount) { | ||
actualAmount = amount; | ||
} | ||
token.safeTransfer(msg.sender, actualAmount); | ||
} | ||
|
||
// Withdraw all assets in the safest way possible. This shouldn't fail. | ||
function exit(uint256 balance) external override onlyBentoBox returns (int256 amountAdded) { | ||
uint256 moneyToBeTransferred = token.balanceOf(address(this)); | ||
amountAdded = int256(moneyToBeTransferred) - int256(balance); | ||
// that is here to reach some branches in BentoBox | ||
if (_harvestProfit > 0) { | ||
amountAdded += _harvestProfit; | ||
} else { | ||
amountAdded -= _harvestProfit; | ||
} | ||
token.safeTransfer(msg.sender, moneyToBeTransferred); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const assert = require("assert") | ||
const { | ||
getBigNumber, | ||
prepare, | ||
setMasterContractApproval, | ||
deploymentsFixture, | ||
} = require("./utilities") | ||
|
||
describe("HelloWorld", function () { | ||
const APPROVAL_AMOUNT = 1000 | ||
|
||
before(async function () { | ||
await prepare(this, [ | ||
"HelloWorld", | ||
"ReturnFalseERC20Mock", | ||
]) | ||
}) | ||
|
||
it("Setup", async function () { | ||
await deploymentsFixture(this, async (cmd) => { | ||
await cmd.addToken("tokenA", "Token A", "A", 18, this.ReturnFalseERC20Mock) | ||
}) | ||
|
||
await this.HelloWorld.new("helloWorld", this.bentoBox.address, this.tokenA.address) | ||
}) | ||
|
||
it("should reject deposit: no token- nor master contract approval", async function () { | ||
await assert.rejects(this.helloWorld.deposit(APPROVAL_AMOUNT)) | ||
}) | ||
|
||
it("approve BentoBox", async function () { | ||
await this.tokenA.approve(this.bentoBox.address, getBigNumber(APPROVAL_AMOUNT, await this.tokenA.decimals())) | ||
}) | ||
|
||
it("should reject deposit: user approved, master contract not approved", async function () { | ||
await assert.rejects(this.helloWorld.deposit(APPROVAL_AMOUNT)) | ||
}) | ||
|
||
it("approve master contract", async function () { | ||
await setMasterContractApproval(this.bentoBox, this.alice, this.alice, this.alicePrivateKey, this.helloWorld.address, true) | ||
}) | ||
|
||
it("should allow deposit", async function () { | ||
await this.helloWorld.deposit(APPROVAL_AMOUNT) | ||
assert.equal((await this.helloWorld.balance()).toString(), APPROVAL_AMOUNT.toString()) | ||
}) | ||
|
||
it("should allow withdraw", async function () { | ||
await this.helloWorld.withdraw() | ||
}) | ||
}) |
Oops, something went wrong.