Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.

Commit

Permalink
update to pancake
Browse files Browse the repository at this point in the history
  • Loading branch information
pancake-swap committed Sep 16, 2020
1 parent 4dd5906 commit 6c0f9af
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 81 deletions.
25 changes: 1 addition & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1 @@
# Uniswap V2

[![Actions Status](https://github.com/Uniswap/uniswap-v2-core/workflows/CI/badge.svg)](https://github.com/Uniswap/uniswap-v2-core/actions)
[![Version](https://img.shields.io/npm/v/@uniswap/v2-core)](https://www.npmjs.com/package/@uniswap/v2-core)

In-depth documentation on Uniswap V2 is available at [uniswap.org](https://uniswap.org/docs).

The built contract artifacts can be browsed via [unpkg.com](https://unpkg.com/browse/@uniswap/v2-core@latest/).

# Local Development

The following assumes the use of `node@>=10`.

## Install Dependencies

`yarn`

## Compile Contracts

`yarn compile`

## Run Tests

`yarn test`
# Pancake Factory
19 changes: 19 additions & 0 deletions contracts/Migration.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.25 <0.7.0;

contract Migrations {
address public owner;
uint public last_completed_migration;

modifier restricted() {
if (msg.sender == owner) _;
}

constructor() public {
owner = msg.sender;
}

function setCompleted(uint completed) public restricted {
last_completed_migration = completed;
}
}
8 changes: 4 additions & 4 deletions contracts/UniswapV2ERC20.sol → contracts/PancakeERC20.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
pragma solidity =0.5.16;

import './interfaces/IUniswapV2ERC20.sol';
import './interfaces/IPancakeERC20.sol';
import './libraries/SafeMath.sol';

contract UniswapV2ERC20 is IUniswapV2ERC20 {
contract PancakeERC20 is IPancakeERC20 {
using SafeMath for uint;

string public constant name = 'Uniswap V2';
Expand Down Expand Up @@ -79,7 +79,7 @@ contract UniswapV2ERC20 is IUniswapV2ERC20 {
}

function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external {
require(deadline >= block.timestamp, 'UniswapV2: EXPIRED');
require(deadline >= block.timestamp, 'Pancake: EXPIRED');
bytes32 digest = keccak256(
abi.encodePacked(
'\x19\x01',
Expand All @@ -88,7 +88,7 @@ contract UniswapV2ERC20 is IUniswapV2ERC20 {
)
);
address recoveredAddress = ecrecover(digest, v, r, s);
require(recoveredAddress != address(0) && recoveredAddress == owner, 'UniswapV2: INVALID_SIGNATURE');
require(recoveredAddress != address(0) && recoveredAddress == owner, 'Pancake: INVALID_SIGNATURE');
_approve(owner, spender, value);
}
}
22 changes: 12 additions & 10 deletions contracts/UniswapV2Factory.sol → contracts/PancakeFactory.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
pragma solidity =0.5.16;

import './interfaces/IUniswapV2Factory.sol';
import './UniswapV2Pair.sol';
import './interfaces/IPancakeFactory.sol';
import './PancakePair.sol';

contract PancakeFactory is IPancakeFactory {
bytes32 public constant INIT_CODE_PAIR_HASH = keccak256(abi.encodePacked(type(PancakePair).creationCode));

contract UniswapV2Factory is IUniswapV2Factory {
address public feeTo;
address public feeToSetter;

Expand All @@ -21,29 +23,29 @@ contract UniswapV2Factory is IUniswapV2Factory {
}

function createPair(address tokenA, address tokenB) external returns (address pair) {
require(tokenA != tokenB, 'UniswapV2: IDENTICAL_ADDRESSES');
require(tokenA != tokenB, 'Pancake: 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;
require(token0 != address(0), 'Pancake: ZERO_ADDRESS');
require(getPair[token0][token1] == address(0), 'Pancake: PAIR_EXISTS'); // single check is sufficient
bytes memory bytecode = type(PancakePair).creationCode;
bytes32 salt = keccak256(abi.encodePacked(token0, token1));
assembly {
pair := create2(0, add(bytecode, 32), mload(bytecode), salt)
}
IUniswapV2Pair(pair).initialize(token0, token1);
IPancakePair(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');
require(msg.sender == feeToSetter, 'Pancake: FORBIDDEN');
feeTo = _feeTo;
}

function setFeeToSetter(address _feeToSetter) external {
require(msg.sender == feeToSetter, 'UniswapV2: FORBIDDEN');
require(msg.sender == feeToSetter, 'Pancake: FORBIDDEN');
feeToSetter = _feeToSetter;
}
}
42 changes: 21 additions & 21 deletions contracts/UniswapV2Pair.sol → contracts/PancakePair.sol
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
pragma solidity =0.5.16;

import './interfaces/IUniswapV2Pair.sol';
import './UniswapV2ERC20.sol';
import './interfaces/IPancakePair.sol';
import './PancakeERC20.sol';
import './libraries/Math.sol';
import './libraries/UQ112x112.sol';
import './interfaces/IERC20.sol';
import './interfaces/IUniswapV2Factory.sol';
import './interfaces/IUniswapV2Callee.sol';
import './interfaces/IPancakeFactory.sol';
import './interfaces/IPancakeCallee.sol';

contract UniswapV2Pair is IUniswapV2Pair, UniswapV2ERC20 {
contract PancakePair is IPancakePair, PancakeERC20 {
using SafeMath for uint;
using UQ112x112 for uint224;

Expand All @@ -29,7 +29,7 @@ contract UniswapV2Pair is IUniswapV2Pair, UniswapV2ERC20 {

uint private unlocked = 1;
modifier lock() {
require(unlocked == 1, 'UniswapV2: LOCKED');
require(unlocked == 1, 'Pancake: LOCKED');
unlocked = 0;
_;
unlocked = 1;
Expand All @@ -43,7 +43,7 @@ contract UniswapV2Pair is IUniswapV2Pair, UniswapV2ERC20 {

function _safeTransfer(address token, address to, uint value) private {
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(SELECTOR, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), 'UniswapV2: TRANSFER_FAILED');
require(success && (data.length == 0 || abi.decode(data, (bool))), 'Pancake: TRANSFER_FAILED');
}

event Mint(address indexed sender, uint amount0, uint amount1);
Expand All @@ -64,14 +64,14 @@ contract UniswapV2Pair is IUniswapV2Pair, UniswapV2ERC20 {

// called once by the factory at time of deployment
function initialize(address _token0, address _token1) external {
require(msg.sender == factory, 'UniswapV2: FORBIDDEN'); // sufficient check
require(msg.sender == factory, 'Pancake: FORBIDDEN'); // sufficient check
token0 = _token0;
token1 = _token1;
}

// update reserves and, on the first call per block, price accumulators
function _update(uint balance0, uint balance1, uint112 _reserve0, uint112 _reserve1) private {
require(balance0 <= uint112(-1) && balance1 <= uint112(-1), 'UniswapV2: OVERFLOW');
require(balance0 <= uint112(-1) && balance1 <= uint112(-1), 'Pancake: OVERFLOW');
uint32 blockTimestamp = uint32(block.timestamp % 2**32);
uint32 timeElapsed = blockTimestamp - blockTimestampLast; // overflow is desired
if (timeElapsed > 0 && _reserve0 != 0 && _reserve1 != 0) {
Expand All @@ -87,7 +87,7 @@ contract UniswapV2Pair is IUniswapV2Pair, UniswapV2ERC20 {

// if fee is on, mint liquidity equivalent to 1/6th of the growth in sqrt(k)
function _mintFee(uint112 _reserve0, uint112 _reserve1) private returns (bool feeOn) {
address feeTo = IUniswapV2Factory(factory).feeTo();
address feeTo = IPancakeFactory(factory).feeTo();
feeOn = feeTo != address(0);
uint _kLast = kLast; // gas savings
if (feeOn) {
Expand All @@ -96,7 +96,7 @@ contract UniswapV2Pair is IUniswapV2Pair, UniswapV2ERC20 {
uint rootKLast = Math.sqrt(_kLast);
if (rootK > rootKLast) {
uint numerator = totalSupply.mul(rootK.sub(rootKLast));
uint denominator = rootK.mul(5).add(rootKLast);
uint denominator = rootK.mul(3).add(rootKLast);
uint liquidity = numerator / denominator;
if (liquidity > 0) _mint(feeTo, liquidity);
}
Expand All @@ -122,7 +122,7 @@ contract UniswapV2Pair is IUniswapV2Pair, UniswapV2ERC20 {
} else {
liquidity = Math.min(amount0.mul(_totalSupply) / _reserve0, amount1.mul(_totalSupply) / _reserve1);
}
require(liquidity > 0, 'UniswapV2: INSUFFICIENT_LIQUIDITY_MINTED');
require(liquidity > 0, 'Pancake: INSUFFICIENT_LIQUIDITY_MINTED');
_mint(to, liquidity);

_update(balance0, balance1, _reserve0, _reserve1);
Expand All @@ -143,7 +143,7 @@ contract UniswapV2Pair is IUniswapV2Pair, UniswapV2ERC20 {
uint _totalSupply = totalSupply; // gas savings, must be defined here since totalSupply can update in _mintFee
amount0 = liquidity.mul(balance0) / _totalSupply; // using balances ensures pro-rata distribution
amount1 = liquidity.mul(balance1) / _totalSupply; // using balances ensures pro-rata distribution
require(amount0 > 0 && amount1 > 0, 'UniswapV2: INSUFFICIENT_LIQUIDITY_BURNED');
require(amount0 > 0 && amount1 > 0, 'Pancake: INSUFFICIENT_LIQUIDITY_BURNED');
_burn(address(this), liquidity);
_safeTransfer(_token0, to, amount0);
_safeTransfer(_token1, to, amount1);
Expand All @@ -157,29 +157,29 @@ contract UniswapV2Pair is IUniswapV2Pair, UniswapV2ERC20 {

// this low-level function should be called from a contract which performs important safety checks
function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external lock {
require(amount0Out > 0 || amount1Out > 0, 'UniswapV2: INSUFFICIENT_OUTPUT_AMOUNT');
require(amount0Out > 0 || amount1Out > 0, 'Pancake: INSUFFICIENT_OUTPUT_AMOUNT');
(uint112 _reserve0, uint112 _reserve1,) = getReserves(); // gas savings
require(amount0Out < _reserve0 && amount1Out < _reserve1, 'UniswapV2: INSUFFICIENT_LIQUIDITY');
require(amount0Out < _reserve0 && amount1Out < _reserve1, 'Pancake: INSUFFICIENT_LIQUIDITY');

uint balance0;
uint balance1;
{ // scope for _token{0,1}, avoids stack too deep errors
address _token0 = token0;
address _token1 = token1;
require(to != _token0 && to != _token1, 'UniswapV2: INVALID_TO');
require(to != _token0 && to != _token1, 'Pancake: INVALID_TO');
if (amount0Out > 0) _safeTransfer(_token0, to, amount0Out); // optimistically transfer tokens
if (amount1Out > 0) _safeTransfer(_token1, to, amount1Out); // optimistically transfer tokens
if (data.length > 0) IUniswapV2Callee(to).uniswapV2Call(msg.sender, amount0Out, amount1Out, data);
if (data.length > 0) IPancakeCallee(to).pancakeCall(msg.sender, amount0Out, amount1Out, data);
balance0 = IERC20(_token0).balanceOf(address(this));
balance1 = IERC20(_token1).balanceOf(address(this));
}
uint amount0In = balance0 > _reserve0 - amount0Out ? balance0 - (_reserve0 - amount0Out) : 0;
uint amount1In = balance1 > _reserve1 - amount1Out ? balance1 - (_reserve1 - amount1Out) : 0;
require(amount0In > 0 || amount1In > 0, 'UniswapV2: INSUFFICIENT_INPUT_AMOUNT');
require(amount0In > 0 || amount1In > 0, 'Pancake: INSUFFICIENT_INPUT_AMOUNT');
{ // scope for reserve{0,1}Adjusted, avoids stack too deep errors
uint balance0Adjusted = balance0.mul(1000).sub(amount0In.mul(3));
uint balance1Adjusted = balance1.mul(1000).sub(amount1In.mul(3));
require(balance0Adjusted.mul(balance1Adjusted) >= uint(_reserve0).mul(_reserve1).mul(1000**2), 'UniswapV2: K');
uint balance0Adjusted = balance0.mul(1000).sub(amount0In.mul(2));
uint balance1Adjusted = balance1.mul(1000).sub(amount1In.mul(2));
require(balance0Adjusted.mul(balance1Adjusted) >= uint(_reserve0).mul(_reserve1).mul(1000**2), 'Pancake: K');
}

_update(balance0, balance1, _reserve0, _reserve1);
Expand Down
5 changes: 5 additions & 0 deletions contracts/interfaces/IPancakeCallee.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma solidity >=0.5.0;

interface IPancakeCallee {
function pancakeCall(address sender, uint amount0, uint amount1, bytes calldata data) external;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pragma solidity >=0.5.0;

interface IUniswapV2ERC20 {
interface IPancakeERC20 {
event Approval(address indexed owner, address indexed spender, uint value);
event Transfer(address indexed from, address indexed to, uint value);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pragma solidity >=0.5.0;

interface IUniswapV2Factory {
interface IPancakeFactory {
event PairCreated(address indexed token0, address indexed token1, address pair, uint);

function feeTo() external view returns (address);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pragma solidity >=0.5.0;

interface IUniswapV2Pair {
interface IPancakePair {
event Approval(address indexed owner, address indexed spender, uint value);
event Transfer(address indexed from, address indexed to, uint value);

Expand Down
5 changes: 0 additions & 5 deletions contracts/interfaces/IUniswapV2Callee.sol

This file was deleted.

4 changes: 2 additions & 2 deletions contracts/test/ERC20.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pragma solidity =0.5.16;

import '../UniswapV2ERC20.sol';
import '../PancakeERC20.sol';

contract ERC20 is UniswapV2ERC20 {
contract ERC20 is PancakeERC20 {
constructor(uint _totalSupply) public {
_mint(msg.sender, _totalSupply);
}
Expand Down
23 changes: 11 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
{
"name": "@uniswap/v2-core",
"description": "🎛 Core contracts for the UniswapV2 protocol",
"name": "pancake/core",
"description": "🎛 Core contracts for the pancake protocol",
"version": "1.0.1",
"homepage": "https://uniswap.org",
"homepage": "https://pancakeswap.finance",
"repository": {
"type": "git",
"url": "https://github.com/Uniswap/uniswap-v2-core"
"url": "https://github.com/pancakeswap/pancake-swap-core.git"
},
"keywords": [
"uniswap",
"ethereum",
"v2",
"core",
"uniswap-v2"
],
"files": [
"contracts",
"build"
Expand All @@ -31,6 +24,7 @@
"prettier": "^1.19.1",
"rimraf": "^3.0.0",
"solc": "0.5.16",
"truffle-plugin-verify": "^0.4.0",
"ts-node": "^8.5.4",
"typescript": "^3.7.3"
},
Expand All @@ -44,5 +38,10 @@
"test": "mocha",
"prepublishOnly": "yarn test"
},
"license": "GPL-3.0-or-later"
"license": "GPL-3.0-or-later",
"dependencies": {
"truffle-hdwallet-provider": "^1.0.17",
"truffle": "^5.1.41",
"truffle-flattener": "^1.4.4"
}
}

2 comments on commit 6c0f9af

@KrazyCanuk
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be able to help out some give me a few days

@recmo
Copy link

@recmo recmo commented on 6c0f9af Feb 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔

Please sign in to comment.