diff --git a/test/demo-contracts/DAIMock.sol b/test/demo-contracts/DAIMock.sol new file mode 100644 index 0000000..963a46b --- /dev/null +++ b/test/demo-contracts/DAIMock.sol @@ -0,0 +1,72 @@ +// SPDX-License-Identifier: MIT + +// The source code of this contract uses the following contracts +// UniswapV2SwapExamples: https://solidity-by-example.org/defi/uniswap-v2/ + +pragma solidity ^0.8.24; + +contract DAIMock { + + uint private UINT_MAX = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff; + + uint256 public totalSupply; + + mapping (address => uint) public balanceOf; + mapping (address => mapping (address => uint)) public allowance; + + event Approval(address indexed src, address indexed guy, uint wad); + event Transfer(address indexed src, address indexed dst, uint wad); + + function decimals() external returns (uint8) { + uint8 dec = 18; + return dec; + } + + function mint(address usr, uint wad) public { + balanceOf[usr] = balanceOf[usr] + wad; + totalSupply = totalSupply + wad; + emit Transfer(address(0), usr, wad); + } + + function mintOnDeposit(address usr, uint wad) public { + mint(usr, wad); + } + + function burn(address usr, uint wad) public { + if(balanceOf[usr] >= wad){ + balanceOf[usr] = balanceOf[usr] - wad; + totalSupply = totalSupply - wad; + } + } + + function approve(address usr, uint wad) external returns (bool) { + allowance[msg.sender][usr] = wad; + emit Approval(msg.sender, usr, wad); + return true; + } + + + function transfer(address dst, uint wad) public returns (bool) { + return transferFrom(msg.sender, dst, wad); + } + + function transferFrom(address src, address dst, uint wad) + public returns (bool) + { + require(balanceOf[src] >= wad, "Dai/insufficient-balance"); + if (src != msg.sender && allowance[src][msg.sender] != UINT_MAX) { + require(allowance[src][msg.sender] >= wad, "Dai/insufficient-allowance"); + allowance[src][msg.sender] = allowance[src][msg.sender] - wad; + } + balanceOf[src] = balanceOf[src] - wad; + balanceOf[dst] = balanceOf[dst] + wad; + emit Transfer(src, dst, wad); + return true; + } + + function safeTransferFrom(address from, address to, uint256 value) external{ + transferFrom(from, to, value); + } + + +} diff --git a/test/demo-contracts/USDCMock.sol b/test/demo-contracts/USDCMock.sol new file mode 100644 index 0000000..109b1fe --- /dev/null +++ b/test/demo-contracts/USDCMock.sol @@ -0,0 +1,102 @@ +// SPDX-License-Identifier: MIT + +// The source code of this contract uses the following contracts +// UniswapV2SwapExamples: https://solidity-by-example.org/defi/uniswap-v2/ + +pragma solidity ^0.8.24; + +contract USDCMock { + uint256 private UINT256_MAX = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff; + + uint256 private _totalSupply; + + mapping(address account => uint256) private _balances; + mapping(address account => mapping(address spender => uint256)) private _allowances; + + event Transfer(address indexed from, address indexed to, uint256 value); + event Approval(address indexed owner, address indexed spender, uint256 value); + + function decimals() external returns (uint8) { + uint8 dec = 18; + return dec; + } + + function mint(address account, uint256 value) public { + require(account != address(0), "USDC: invalid receiver"); + _update(address(0), account, value); + } + + function balanceOf(address account) public returns (uint256) { + return _balances[account]; + } + + function transfer(address to, uint256 value) public returns (bool) { + address owner = msg.sender; + _transfer(owner, to, value); + return true; + } + + function allowance(address owner, address spender) public returns (uint256) { + return _allowances[owner][spender]; + } + + function approve(address spender, uint256 value) public returns (bool) { + address owner = msg.sender; + _approve(owner, spender, value, true); + return true; + } + + function transferFrom(address from, address to, uint256 value) public returns (bool) { + address spender = msg.sender; + _spendAllowance(from, spender, value); + _transfer(from, to, value); + return true; + } + + function _transfer(address from, address to, uint256 value) private { + require(from != address(0), "USDC: invalid sender"); + require(to != address(0), "USDC: invalid receiver"); + _update(from, to, value); + } + + function _update(address from, address to, uint256 value) private { + if (from == address(0)) { + _totalSupply = _totalSupply + value; + } else { + uint256 fromBalance = _balances[from]; + require(fromBalance >= value, "USDC: insufficient balance"); + _balances[from] = fromBalance - value; + + } + + if (to == address(0)) { + _totalSupply = _totalSupply - value; + + } else { + _balances[to] = _balances[to] + value; + } + + emit Transfer(from, to, value); + } + + + function _approve(address owner, address spender, uint256 value, bool emitEvent) private { + require(owner != address(0), "USDC: invalid approver"); + require(spender != address(0), "USDC: invalid spender"); + _allowances[owner][spender] = value; + if (emitEvent) { + emit Approval(owner, spender, value); + } + } + + function _spendAllowance(address owner, address spender, uint256 value) private { + uint256 currentAllowance = allowance(owner, spender); + if (currentAllowance != UINT256_MAX) { + require(currentAllowance >= value, "USDC: insufficient allowance"); + _approve(owner, spender, currentAllowance - value, false); + + } + } + + +} diff --git a/test/demo-contracts/WETHMock.sol b/test/demo-contracts/WETHMock.sol new file mode 100644 index 0000000..95b3fbf --- /dev/null +++ b/test/demo-contracts/WETHMock.sol @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: MIT + +// The source code of this contract uses the following contracts +// UniswapV2SwapExamples: https://solidity-by-example.org/defi/uniswap-v2/ + +pragma solidity ^0.8.24; + +contract WETHMock { + + uint256 private UINT256_MAX = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff; + + mapping (address => uint256) public balanceOf; + mapping (address => mapping (address => uint256)) public allowance; + + event Approval(address indexed owner, address indexed spender, uint256 value); + event Transfer(address indexed from, address indexed to, uint256 value); + + function decimals() external returns (uint8) { + uint8 dec = 18; + return dec; + } + + function deposit() external payable { + balanceOf[msg.sender] = balanceOf[msg.sender] + msg.value; + emit Transfer(address(0), msg.sender, msg.value); + } + + function approve(address spender, uint256 value) external returns (bool) { + allowance[msg.sender][spender] = value; + emit Approval(msg.sender, spender, value); + + return true; + } + + function transfer(address to, uint256 value) external returns (bool) { + if (to != address(0) && to != address(this)) { // Transfer + uint256 balance = balanceOf[msg.sender]; + require(balance >= value, "WETH: transfer amount exceeds balance"); + + balanceOf[msg.sender] = balance - value; + balanceOf[to] = balanceOf[to] + value; + emit Transfer(msg.sender, to, value); + } else { // Withdraw + uint256 balance = balanceOf[msg.sender]; + require(balance >= value, "WETH: burn amount exceeds balance"); + balanceOf[msg.sender] = balance - value; + emit Transfer(msg.sender, address(0), value); + + (bool success, ) = msg.sender.call{value: value}(""); + require(success, "WETH: ETH transfer failed"); + } + + return true; + } + + function transferFrom(address from, address to, uint256 value) external returns (bool) { + if (from != msg.sender) { + uint256 allowed = allowance[from][msg.sender]; + if (allowed != UINT256_MAX) { + require(allowed >= value, "WETH: request exceeds allowance"); + uint256 reduced = allowed - value; + allowance[from][msg.sender] = reduced; + emit Approval(from, msg.sender, reduced); + } + } + + if (to != address(0) && to != address(this)) { + uint256 balance = balanceOf[from]; + require(balance >= value, "WETH: transfer amount exceeds balance"); + + balanceOf[from] = balance - value; + balanceOf[to] = balanceOf[to] + value; + emit Transfer(from, to, value); + } else { + uint256 balance = balanceOf[from]; + require(balance >= value, "WETH: burn amount exceeds balance"); + balanceOf[from] = balance - value; + emit Transfer(from, address(0), value); + + (bool success, ) = msg.sender.call{value: value}(""); + require(success, "WETH: ETH transfer failed"); + } + + return true; + } +}