Skip to content

Commit

Permalink
exchange -> pair
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahZinsmeister committed Mar 17, 2020
1 parent 170a739 commit 1136544
Show file tree
Hide file tree
Showing 13 changed files with 17,344 additions and 17,357 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
build/
node_modules/
34,327 changes: 17,158 additions & 17,169 deletions build/Combined-Json.json

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions build/IUniswapV2Factory.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{
"indexed": false,
"internalType": "address",
"name": "exchange",
"name": "pair",
"type": "address"
},
{
Expand All @@ -28,7 +28,7 @@
"type": "uint256"
}
],
"name": "ExchangeCreated",
"name": "PairCreated",
"type": "event"
},
{
Expand All @@ -40,11 +40,11 @@
"type": "uint256"
}
],
"name": "allExchanges",
"name": "allPairs",
"outputs": [
{
"internalType": "address",
"name": "exchange",
"name": "pair",
"type": "address"
}
],
Expand All @@ -55,7 +55,7 @@
{
"constant": true,
"inputs": [],
"name": "allExchangesLength",
"name": "allPairsLength",
"outputs": [
{
"internalType": "uint256",
Expand All @@ -81,11 +81,11 @@
"type": "address"
}
],
"name": "createExchange",
"name": "createPair",
"outputs": [
{
"internalType": "address",
"name": "exchange",
"name": "pair",
"type": "address"
}
],
Expand Down Expand Up @@ -137,11 +137,11 @@
"type": "address"
}
],
"name": "getExchange",
"name": "getPair",
"outputs": [
{
"internalType": "address",
"name": "exchange",
"name": "pair",
"type": "address"
}
],
Expand Down Expand Up @@ -213,7 +213,7 @@
{
"indexed": false,
"internalType": "address",
"name": "exchange",
"name": "pair",
"type": "address"
},
{
Expand All @@ -223,7 +223,7 @@
"type": "uint256"
}
],
"name": "ExchangeCreated",
"name": "PairCreated",
"type": "event"
},
{
Expand All @@ -235,11 +235,11 @@
"type": "uint256"
}
],
"name": "allExchanges",
"name": "allPairs",
"outputs": [
{
"internalType": "address",
"name": "exchange",
"name": "pair",
"type": "address"
}
],
Expand All @@ -250,7 +250,7 @@
{
"constant": true,
"inputs": [],
"name": "allExchangesLength",
"name": "allPairsLength",
"outputs": [
{
"internalType": "uint256",
Expand All @@ -276,11 +276,11 @@
"type": "address"
}
],
"name": "createExchange",
"name": "createPair",
"outputs": [
{
"internalType": "address",
"name": "exchange",
"name": "pair",
"type": "address"
}
],
Expand Down Expand Up @@ -332,11 +332,11 @@
"type": "address"
}
],
"name": "getExchange",
"name": "getPair",
"outputs": [
{
"internalType": "address",
"name": "exchange",
"name": "pair",
"type": "address"
}
],
Expand Down
File renamed without changes.
42 changes: 21 additions & 21 deletions build/UniswapV2Factory.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions build/UniswapV2Exchange.json → build/UniswapV2Pair.json

Large diffs are not rendered by default.

31 changes: 15 additions & 16 deletions contracts/UniswapV2Factory.sol
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
pragma solidity =0.5.16;

import './interfaces/IUniswapV2Factory.sol';
import './UniswapV2Exchange.sol';
import './interfaces/IUniswapV2Exchange.sol';
import './UniswapV2Pair.sol';

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

mapping(address => mapping(address => address)) public getExchange;
address[] public allExchanges;
mapping(address => mapping(address => address)) public getPair;
address[] public allPairs;

event ExchangeCreated(address indexed token0, address indexed token1, address exchange, uint);
event PairCreated(address indexed token0, address indexed token1, address pair, uint);

constructor(address _feeToSetter) public {
feeToSetter = _feeToSetter;
}

function allExchangesLength() external view returns (uint) {
return allExchanges.length;
function allPairsLength() external view returns (uint) {
return allPairs.length;
}

function createExchange(address tokenA, address tokenB) external returns (address exchange) {
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(getExchange[token0][token1] == address(0), 'UniswapV2: EXCHANGE_EXISTS'); // single check is sufficient
bytes memory exchangeBytecode = type(UniswapV2Exchange).creationCode;
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 {
exchange := create2(0, add(exchangeBytecode, 32), mload(exchangeBytecode), salt)
pair := create2(0, add(bytecode, 32), mload(bytecode), salt)
}
IUniswapV2Exchange(exchange).initialize(token0, token1);
getExchange[token0][token1] = exchange;
getExchange[token1][token0] = exchange; // populate mapping in the reverse direction
allExchanges.push(exchange);
emit ExchangeCreated(token0, token1, exchange, allExchanges.length);
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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
pragma solidity =0.5.16;

import './interfaces/IUniswapV2Exchange.sol';
import './interfaces/IUniswapV2Pair.sol';
import './UniswapV2ERC20.sol';
import './libraries/Math.sol';
import './libraries/UQ112x112.sol';
import './interfaces/IERC20.sol';
import './interfaces/IUniswapV2Factory.sol';
import './interfaces/IUniswapV2Callee.sol';

contract UniswapV2Exchange is IUniswapV2Exchange, UniswapV2ERC20 {
contract UniswapV2Pair is IUniswapV2Pair, UniswapV2ERC20 {
using SafeMath for uint;
using UQ112x112 for uint224;

Expand Down
10 changes: 5 additions & 5 deletions contracts/interfaces/IUniswapV2Factory.sol
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
pragma solidity =0.5.16;

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

function feeTo() external view returns (address);
function feeToSetter() external view returns (address);

function getExchange(address tokenA, address tokenB) external view returns (address exchange);
function allExchanges(uint) external view returns (address exchange);
function allExchangesLength() external view returns (uint);
function getPair(address tokenA, address tokenB) external view returns (address pair);
function allPairs(uint) external view returns (address pair);
function allPairsLength() external view returns (uint);

function createExchange(address tokenA, address tokenB) external returns (address exchange);
function createPair(address tokenA, address tokenB) external returns (address pair);

function setFeeTo(address) external;
function setFeeToSetter(address) external;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pragma solidity =0.5.16;

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

Expand Down
48 changes: 24 additions & 24 deletions test/UniswapV2Factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { solidity, MockProvider, createFixtureLoader } from 'ethereum-waffle'
import { getCreate2Address } from './shared/utilities'
import { factoryFixture } from './shared/fixtures'

import UniswapV2Exchange from '../build/UniswapV2Exchange.json'
import UniswapV2Pair from '../build/UniswapV2Pair.json'

chai.use(solidity)

Expand All @@ -31,43 +31,43 @@ describe('UniswapV2Factory', () => {
factory = fixture.factory
})

it('feeTo, feeToSetter, allExchanges, allExchangesLength', async () => {
it('feeTo, feeToSetter, allPairsLength', async () => {
expect(await factory.feeTo()).to.eq(AddressZero)
expect(await factory.feeToSetter()).to.eq(wallet.address)
expect(await factory.allExchangesLength()).to.eq(0)
expect(await factory.allPairsLength()).to.eq(0)
})

async function createExchange(tokens: [string, string]) {
const bytecode = `0x${UniswapV2Exchange.evm.bytecode.object}`
async function createPair(tokens: [string, string]) {
const bytecode = `0x${UniswapV2Pair.evm.bytecode.object}`
const create2Address = getCreate2Address(factory.address, tokens, bytecode)
await expect(factory.createExchange(...tokens))
.to.emit(factory, 'ExchangeCreated')
await expect(factory.createPair(...tokens))
.to.emit(factory, 'PairCreated')
.withArgs(TEST_ADDRESSES[0], TEST_ADDRESSES[1], create2Address, bigNumberify(1))

await expect(factory.createExchange(...tokens)).to.be.reverted // UniswapV2: EXCHANGE_EXISTS
await expect(factory.createExchange(...tokens.slice().reverse())).to.be.reverted // UniswapV2: EXCHANGE_EXISTS
expect(await factory.getExchange(...tokens)).to.eq(create2Address)
expect(await factory.getExchange(...tokens.slice().reverse())).to.eq(create2Address)
expect(await factory.allExchanges(0)).to.eq(create2Address)
expect(await factory.allExchangesLength()).to.eq(1)
await expect(factory.createPair(...tokens)).to.be.reverted // UniswapV2: PAIR_EXISTS
await expect(factory.createPair(...tokens.slice().reverse())).to.be.reverted // UniswapV2: PAIR_EXISTS
expect(await factory.getPair(...tokens)).to.eq(create2Address)
expect(await factory.getPair(...tokens.slice().reverse())).to.eq(create2Address)
expect(await factory.allPairs(0)).to.eq(create2Address)
expect(await factory.allPairsLength()).to.eq(1)

const exchange = new Contract(create2Address, JSON.stringify(UniswapV2Exchange.abi), provider)
expect(await exchange.factory()).to.eq(factory.address)
expect(await exchange.token0()).to.eq(TEST_ADDRESSES[0])
expect(await exchange.token1()).to.eq(TEST_ADDRESSES[1])
const pair = new Contract(create2Address, JSON.stringify(UniswapV2Pair.abi), provider)
expect(await pair.factory()).to.eq(factory.address)
expect(await pair.token0()).to.eq(TEST_ADDRESSES[0])
expect(await pair.token1()).to.eq(TEST_ADDRESSES[1])
}

it('createExchange', async () => {
await createExchange(TEST_ADDRESSES)
it('createPair', async () => {
await createPair(TEST_ADDRESSES)
})

it('createExchange:reverse', async () => {
await createExchange(TEST_ADDRESSES.slice().reverse() as [string, string])
it('createPair:reverse', async () => {
await createPair(TEST_ADDRESSES.slice().reverse() as [string, string])
})

it('createExchange:gas', async () => {
const gasCost = await factory.estimate.createExchange(...TEST_ADDRESSES)
console.log(`Gas required for createExchange: ${gasCost}`)
it('createPair:gas', async () => {
const gasCost = await factory.estimate.createPair(...TEST_ADDRESSES)
console.log(`Gas required for createPair: ${gasCost}`)
})

it('setFeeTo', async () => {
Expand Down
Loading

0 comments on commit 1136544

Please sign in to comment.