Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharr1411 committed Sep 27, 2024
1 parent d08cf8b commit f060c04
Show file tree
Hide file tree
Showing 7 changed files with 726 additions and 0 deletions.
29 changes: 29 additions & 0 deletions test/mocks/ERC20Mock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract ERC20Mock is ERC20 {
constructor(string memory name, string memory symbol, address initialAccount, uint256 initialBalance)
payable
ERC20(name, symbol)
{
_mint(initialAccount, initialBalance);
}

function mint(address account, uint256 amount) public {
_mint(account, amount);
}

function burn(address account, uint256 amount) public {
_burn(account, amount);
}

function transferInternal(address from, address to, uint256 value) public {
_transfer(from, to, value);
}

function approveInternal(address owner, address spender, uint256 value) public {
_approve(owner, spender, value);
}
}
38 changes: 38 additions & 0 deletions test/mocks/MockFailedMintDSC.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//SPDX-License-Identifier:MIT

pragma solidity ^0.8.24;

import {ERC20Burnable, ERC20} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
// /*
// * @titile:MockFailedMintDSC
// * @auther:Do Kwon (The Luna guy ;-;)
// * Collateral:Exogenous ( ETH & BTC )
// * Minting:Algorithmic
// * Relative Stability : Pegged to USD
// *
// * This is the contract meant to be governed by DSCEngine.
// * @notice This contract is just the ERC20 iplementation of our stablecoin system.
// */

error MockFailedMintDSC__MustBeMoreThanZero();
error MockFailedMintDSC__BurnAmountExceedsBalance();
error MockFailedMintDSC__CanNotBeZeroAddress();

contract MockFailedMintDSC is ERC20Burnable, Ownable {
constructor() ERC20("Decentralized Stable Coin", "DEC") Ownable(msg.sender) {}

function burn(uint256 _amount) public override onlyOwner {
uint256 balacnce = balanceOf(msg.sender);
if (_amount <= 0) revert MockFailedMintDSC__MustBeMoreThanZero();
if (balacnce < _amount) revert MockFailedMintDSC__BurnAmountExceedsBalance();
super.burn(_amount);
}

function mint(address _to, uint256 _amount) external onlyOwner returns (bool) {
if (_to == address(0)) revert MockFailedMintDSC__CanNotBeZeroAddress();
if (_amount <= 0) revert MockFailedMintDSC__MustBeMoreThanZero();
_mint(_to, _amount);
return false;
}
}
40 changes: 40 additions & 0 deletions test/mocks/MockFailedTransfer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import {ERC20Burnable, ERC20} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";

contract MockFailedTransfer is ERC20Burnable, Ownable {
error MockFailedMintDSC__AmountMustBeMoreThanZero();
error MockFailedMintDSC__BurnAmountExceedsBalance();
error MockFailedMintDSC__NotZeroAddress();

/*
In future versions of OpenZeppelin contracts package, Ownable must be declared with an address of the contract owner
as a parameter.
For example:
constructor() ERC20("MockFailedMintDSC", "DSC") Ownable(0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266) {}
Related code changes can be viewed in this commit:
https://github.com/OpenZeppelin/openzeppelin-contracts/commit/13d5e0466a9855e9305119ed383e54fc913fdc60
*/
constructor() ERC20("MockFailedTransfer", "DSC") Ownable(msg.sender) {}

function burn(uint256 _amount) public override onlyOwner {
uint256 balance = balanceOf(msg.sender);
if (_amount <= 0) {
revert MockFailedMintDSC__AmountMustBeMoreThanZero();
}
if (balance < _amount) {
revert MockFailedMintDSC__BurnAmountExceedsBalance();
}
super.burn(_amount);
}

function mint(address account, uint256 amount) public {
_mint(account, amount);
}

function transfer(address, /*recipient*/ uint256 /*amount*/ ) public pure override returns (bool) {
return false;
}
}
45 changes: 45 additions & 0 deletions test/mocks/MockFailedTransferFrom.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import {ERC20Burnable, ERC20} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";

contract MockFailedTransferFrom is ERC20Burnable, Ownable {
error MockFailedTransferFrom__AmountMustBeMoreThanZero();
error MockFailedTransferFrom__BurnAmountExceedsBalance();
error MockFailedTransferFrom__NotZeroAddress();

/*
In future versions of OpenZeppelin contracts package, Ownable must be declared with an address of the contract owner
as a parameter.
For example:
constructor() ERC20("MockFailedTransferFrom", "DSC") Ownable(0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266) {}
Related code changes can be viewed in this commit:
https://github.com/OpenZeppelin/openzeppelin-contracts/commit/13d5e0466a9855e9305119ed383e54fc913fdc60
*/
constructor() ERC20("MockFailedTransferFrom", "DSC") Ownable(msg.sender) {}

function burn(uint256 _amount) public override onlyOwner {
uint256 balance = balanceOf(msg.sender);
if (_amount <= 0) {
revert MockFailedTransferFrom__AmountMustBeMoreThanZero();
}
if (balance < _amount) {
revert MockFailedTransferFrom__BurnAmountExceedsBalance();
}
super.burn(_amount);
}

function mint(address account, uint256 amount) public {
_mint(account, amount);
}

function transferFrom(address, /*sender*/ address, /*recipient*/ uint256 /*amount*/ )
public
pure
override
returns (bool)
{
return false;
}
}
64 changes: 64 additions & 0 deletions test/mocks/MockMoreDebtDSC.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-License-Identifier: MIT

// This is considered an Exogenous, Decentralized, Anchored (pegged), Crypto Collateralized low volitility coin

pragma solidity 0.8.24;

import {ERC20Burnable, ERC20} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {MockV3Aggregator} from "./MockV3Aggregator.sol";

/*
* @title DecentralizedStableCoin
* @author Patrick Collins
* Collateral: Exogenous
* Minting (Stability Mechanism): Decentralized (Algorithmic)
* Value (Relative Stability): Anchored (Pegged to USD)
* Collateral Type: Crypto
*
* This is the contract meant to be owned by DSCEngine. It is a ERC20 token that can be minted and burned by the
DSCEngine smart contract.
*/
contract MockMoreDebtDSC is ERC20Burnable, Ownable(msg.sender) {
error DecentralizedStableCoin__AmountMustBeMoreThanZero();
error DecentralizedStableCoin__BurnAmountExceedsBalance();
error DecentralizedStableCoin__NotZeroAddress();

address mockAggregator;

/*
In future versions of OpenZeppelin contracts package, Ownable must be declared with an address of the contract owner
as a parameter.
For example:
constructor() ERC20("DecentralizedStableCoin", "DSC") Ownable(0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266) {}
Related code changes can be viewed in this commit:
https://github.com/OpenZeppelin/openzeppelin-contracts/commit/13d5e0466a9855e9305119ed383e54fc913fdc60
*/
constructor(address _mockAggregator) ERC20("DecentralizedStableCoin", "DSC") {
mockAggregator = _mockAggregator;
}

function burn(uint256 _amount) public override onlyOwner {
// We crash the price
MockV3Aggregator(mockAggregator).updateAnswer(0);
uint256 balance = balanceOf(msg.sender);
if (_amount <= 0) {
revert DecentralizedStableCoin__AmountMustBeMoreThanZero();
}
if (balance < _amount) {
revert DecentralizedStableCoin__BurnAmountExceedsBalance();
}
super.burn(_amount);
}

function mint(address _to, uint256 _amount) external onlyOwner returns (bool) {
if (_to == address(0)) {
revert DecentralizedStableCoin__NotZeroAddress();
}
if (_amount <= 0) {
revert DecentralizedStableCoin__AmountMustBeMoreThanZero();
}
_mint(_to, _amount);
return true;
}
}
72 changes: 72 additions & 0 deletions test/mocks/MockV3Aggregator.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

/**
* @title MockV3Aggregator
* @notice Based on the FluxAggregator contract
* @notice Use this contract when you need to test
* other contract's ability to read data from an
* aggregator contract, but how the aggregator got
* its answer is unimportant
*/
contract MockV3Aggregator {
uint256 public constant version = 0;

uint8 public decimals;
int256 public latestAnswer;
uint256 public latestTimestamp;
uint256 public latestRound;

mapping(uint256 => int256) public getAnswer;
mapping(uint256 => uint256) public getTimestamp;
mapping(uint256 => uint256) private getStartedAt;

constructor(uint8 _decimals, int256 _initialAnswer) {
decimals = _decimals;
updateAnswer(_initialAnswer);
}

function updateAnswer(int256 _answer) public {
latestAnswer = _answer;
latestTimestamp = block.timestamp;
latestRound++;
getAnswer[latestRound] = _answer;
getTimestamp[latestRound] = block.timestamp;
getStartedAt[latestRound] = block.timestamp;
}

function updateRoundData(uint80 _roundId, int256 _answer, uint256 _timestamp, uint256 _startedAt) public {
latestRound = _roundId;
latestAnswer = _answer;
latestTimestamp = _timestamp;
getAnswer[latestRound] = _answer;
getTimestamp[latestRound] = _timestamp;
getStartedAt[latestRound] = _startedAt;
}

function getRoundData(uint80 _roundId)
external
view
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound)
{
return (_roundId, getAnswer[_roundId], getStartedAt[_roundId], getTimestamp[_roundId], _roundId);
}

function latestRoundData()
external
view
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound)
{
return (
uint80(latestRound),
getAnswer[latestRound],
getStartedAt[latestRound],
getTimestamp[latestRound],
uint80(latestRound)
);
}

function description() external pure returns (string memory) {
return "v0.6/tests/MockV3Aggregator.sol";
}
}
Loading

0 comments on commit f060c04

Please sign in to comment.