-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
552 additions
and
119 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"solidity.packageDefaultDependenciesContractsDirectory": "src", | ||
"solidity.packageDefaultDependenciesDirectory": "lib" | ||
} |
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 |
---|---|---|
@@ -1,66 +1,11 @@ | ||
## Foundry | ||
# UniswapV2 Core Contract | ||
|
||
**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.** | ||
## Features | ||
|
||
Foundry consists of: | ||
* Foundry project. | ||
* Meet newest solidity version. | ||
|
||
- **Forge**: Ethereum testing framework (like Truffle, Hardhat and DappTools). | ||
- **Cast**: Swiss army knife for interacting with EVM smart contracts, sending transactions and getting chain data. | ||
- **Anvil**: Local Ethereum node, akin to Ganache, Hardhat Network. | ||
- **Chisel**: Fast, utilitarian, and verbose solidity REPL. | ||
## Refs | ||
|
||
## Documentation | ||
|
||
https://book.getfoundry.sh/ | ||
|
||
## Usage | ||
|
||
### Build | ||
|
||
```shell | ||
$ forge build | ||
``` | ||
|
||
### Test | ||
|
||
```shell | ||
$ forge test | ||
``` | ||
|
||
### Format | ||
|
||
```shell | ||
$ forge fmt | ||
``` | ||
|
||
### Gas Snapshots | ||
|
||
```shell | ||
$ forge snapshot | ||
``` | ||
|
||
### Anvil | ||
|
||
```shell | ||
$ anvil | ||
``` | ||
|
||
### Deploy | ||
|
||
```shell | ||
$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key> | ||
``` | ||
|
||
### Cast | ||
|
||
```shell | ||
$ cast <subcommand> | ||
``` | ||
|
||
### Help | ||
|
||
```shell | ||
$ forge --help | ||
$ anvil --help | ||
$ cast --help | ||
``` | ||
* <https://github.com/Uniswap/v2-core> | ||
* <https://github.com/Behodler/UniswapV2CoreFoundryFriendly> |
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 @@ | ||
forge-std/=lib/forge-std/src/ |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,90 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.26; | ||
|
||
import "./interfaces/IUniswapV2ERC20.sol"; | ||
import "./libraries/SafeMath.sol"; | ||
|
||
contract UniswapV2ERC20 is IUniswapV2ERC20 { | ||
using SafeMath for uint256; | ||
|
||
string public constant name = "Uniswap V2"; | ||
string public constant symbol = "UNI-V2"; | ||
uint8 public constant decimals = 18; | ||
uint256 public totalSupply; | ||
mapping(address => uint256) public balanceOf; | ||
mapping(address => mapping(address => uint256)) public allowance; | ||
|
||
bytes32 public DOMAIN_SEPARATOR; | ||
// keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); | ||
bytes32 public constant PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9; | ||
mapping(address => uint256) public nonces; | ||
|
||
constructor() { | ||
DOMAIN_SEPARATOR = keccak256( | ||
abi.encode( | ||
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"), | ||
keccak256(bytes(name)), | ||
keccak256(bytes("1")), | ||
block.chainid, | ||
address(this) | ||
) | ||
); | ||
} | ||
|
||
function _mint(address to, uint256 value) internal { | ||
totalSupply = totalSupply.add(value); | ||
balanceOf[to] = balanceOf[to].add(value); | ||
emit Transfer(address(0), to, value); | ||
} | ||
|
||
function _burn(address from, uint256 value) internal { | ||
balanceOf[from] = balanceOf[from].sub(value); | ||
totalSupply = totalSupply.sub(value); | ||
emit Transfer(from, address(0), value); | ||
} | ||
|
||
function _approve(address owner, address spender, uint256 value) private { | ||
allowance[owner][spender] = value; | ||
emit Approval(owner, spender, value); | ||
} | ||
|
||
function _transfer(address from, address to, uint256 value) private { | ||
balanceOf[from] = balanceOf[from].sub(value); | ||
balanceOf[to] = balanceOf[to].add(value); | ||
emit Transfer(from, to, value); | ||
} | ||
|
||
function approve(address spender, uint256 value) external returns (bool) { | ||
_approve(msg.sender, spender, value); | ||
return true; | ||
} | ||
|
||
function transfer(address to, uint256 value) external returns (bool) { | ||
_transfer(msg.sender, to, value); | ||
return true; | ||
} | ||
|
||
function transferFrom(address from, address to, uint256 value) external returns (bool) { | ||
if (allowance[from][msg.sender] != type(uint256).max) { | ||
allowance[from][msg.sender] = allowance[from][msg.sender].sub(value); | ||
} | ||
_transfer(from, to, value); | ||
return true; | ||
} | ||
|
||
function permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) | ||
external | ||
{ | ||
require(deadline >= block.timestamp, "UniswapV2: EXPIRED"); | ||
bytes32 digest = keccak256( | ||
abi.encodePacked( | ||
"\x19\x01", | ||
DOMAIN_SEPARATOR, | ||
keccak256(abi.encode(PERMIT_TYPEHASH, owner, spender, value, nonces[owner]++, deadline)) | ||
) | ||
); | ||
address recoveredAddress = ecrecover(digest, v, r, s); | ||
require(recoveredAddress != address(0) && recoveredAddress == owner, "UniswapV2: INVALID_SIGNATURE"); | ||
_approve(owner, spender, value); | ||
} | ||
} |
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,49 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.26; | ||
|
||
import "./interfaces/IUniswapV2Factory.sol"; | ||
import "./UniswapV2Pair.sol"; | ||
import "./interfaces/IUniswapV2Pair.sol"; | ||
|
||
contract UniswapV2Factory is IUniswapV2Factory { | ||
address public feeTo; | ||
address public feeToSetter; | ||
|
||
mapping(address => mapping(address => address)) public getPair; | ||
address[] public allPairs; | ||
|
||
constructor(address _feeToSetter) { | ||
feeToSetter = _feeToSetter; | ||
} | ||
|
||
function allPairsLength() external view returns (uint256) { | ||
return allPairs.length; | ||
} | ||
|
||
function createPair(address tokenA, address tokenB) external returns (address pair) { | ||
require(tokenA != tokenB, "UniswapV2: IDENTICAL_ADDRESSES"); | ||
(address token0, address token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA); | ||
require(token0 != address(0), "UniswapV2: ZERO_ADDRESS"); | ||
require(getPair[token0][token1] == address(0), "UniswapV2: PAIR_EXISTS"); // single check is sufficient | ||
bytes memory bytecode = type(UniswapV2Pair).creationCode; | ||
bytes32 salt = keccak256(abi.encodePacked(token0, token1)); | ||
assembly { | ||
pair := create2(0, add(bytecode, 32), mload(bytecode), salt) | ||
} | ||
IUniswapV2Pair(pair).initialize(token0, token1); | ||
getPair[token0][token1] = pair; | ||
getPair[token1][token0] = pair; // populate mapping in the reverse direction | ||
allPairs.push(pair); | ||
emit PairCreated(token0, token1, pair, allPairs.length); | ||
} | ||
|
||
function setFeeTo(address _feeTo) external { | ||
require(msg.sender == feeToSetter, "UniswapV2: FORBIDDEN"); | ||
feeTo = _feeTo; | ||
} | ||
|
||
function setFeeToSetter(address _feeToSetter) external { | ||
require(msg.sender == feeToSetter, "UniswapV2: FORBIDDEN"); | ||
feeToSetter = _feeToSetter; | ||
} | ||
} |
Oops, something went wrong.