Skip to content

Commit

Permalink
Finish first pass of migration contract
Browse files Browse the repository at this point in the history
  • Loading branch information
chefnomi committed Aug 26, 2020
1 parent 5d3ed9f commit e7f1b0e
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 5 deletions.
30 changes: 26 additions & 4 deletions contracts/Migrator.sol
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
pragma solidity 0.6.12;

import "./uniswapv2/interfaces/IUniswapV2ERC20.sol";
import "./uniswapv2/interfaces/IUniswapV2Pair.sol";
import "./uniswapv2/interfaces/IUniswapV2Factory.sol";


contract Migrator {
address public chef;
address public factory;
uint256 public notBeforeBlock;
uint256 public desiredLiquidity = uint256(-1);

constructor(address _chef) public {
constructor(address _chef, address _factory, uint256 _notBeforeBlock) public {
chef = _chef;
factory = _factory;
notBeforeBlock = _notBeforeBlock;
}

function migrate(address orig) public {
require(msg.sender == chef);
// TODO
function migrate(address orig) public returns (address) {
require(msg.sender == chef, "not the chef");
require(block.number >= notBeforeBlock, "too early to migrate");
address token0 = IUniswapV2Pair(orig).token0();
address token1 = IUniswapV2Pair(orig).token1();
address pair = IUniswapV2Factory(factory).getPair(token0, token1);
if (pair == address(0)) {
pair = IUniswapV2Factory(factory).createPair(token0, token1);
}
uint256 lp = IUniswapV2ERC20(orig).balanceOf(msg.sender);
if (lp == 0) return pair;
desiredLiquidity = lp;
IUniswapV2ERC20(orig).transferFrom(msg.sender, orig, lp);
IUniswapV2Pair(orig).burn(pair);
IUniswapV2Pair(pair).mint(msg.sender);
desiredLiquidity = uint256(-1);
return pair;
}
}
2 changes: 1 addition & 1 deletion contracts/uniswapv2/UniswapV2Pair.sol
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ contract UniswapV2Pair is UniswapV2ERC20 {
address migrator = IUniswapV2Factory(factory).migrator();
if (msg.sender == migrator) {
liquidity = Migrator(migrator).desiredLiquidity();
require(liquidity > 0, "Bad desired liquidity");
require(liquidity > 0 && liquidity != uint256(-1), "Bad desired liquidity");
} else {
require(migrator == address(0), "Must not have migrator");
liquidity = Math.sqrt(amount0.mul(amount1)).sub(MINIMUM_LIQUIDITY);
Expand Down
23 changes: 23 additions & 0 deletions contracts/uniswapv2/interfaces/IUniswapV2ERC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
pragma solidity >=0.5.0;

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

function name() external pure returns (string memory);
function symbol() external pure returns (string memory);
function decimals() external pure returns (uint8);
function totalSupply() external view returns (uint);
function balanceOf(address owner) external view returns (uint);
function allowance(address owner, address spender) external view returns (uint);

function approve(address spender, uint value) external returns (bool);
function transfer(address to, uint value) external returns (bool);
function transferFrom(address from, address to, uint value) external returns (bool);

function DOMAIN_SEPARATOR() external view returns (bytes32);
function PERMIT_TYPEHASH() external pure returns (bytes32);
function nonces(address owner) external view returns (uint);

function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external;
}
52 changes: 52 additions & 0 deletions contracts/uniswapv2/interfaces/IUniswapV2Pair.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
pragma solidity >=0.5.0;

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

function name() external pure returns (string memory);
function symbol() external pure returns (string memory);
function decimals() external pure returns (uint8);
function totalSupply() external view returns (uint);
function balanceOf(address owner) external view returns (uint);
function allowance(address owner, address spender) external view returns (uint);

function approve(address spender, uint value) external returns (bool);
function transfer(address to, uint value) external returns (bool);
function transferFrom(address from, address to, uint value) external returns (bool);

function DOMAIN_SEPARATOR() external view returns (bytes32);
function PERMIT_TYPEHASH() external pure returns (bytes32);
function nonces(address owner) external view returns (uint);

function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external;

event Mint(address indexed sender, uint amount0, uint amount1);
event Burn(address indexed sender, uint amount0, uint amount1, address indexed to);
event Swap(
address indexed sender,
uint amount0In,
uint amount1In,
uint amount0Out,
uint amount1Out,
address indexed to
);
event Sync(uint112 reserve0, uint112 reserve1);

function MINIMUM_LIQUIDITY() external pure returns (uint);
function factory() external view returns (address);
function token0() external view returns (address);
function token1() external view returns (address);
function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
function price0CumulativeLast() external view returns (uint);
function price1CumulativeLast() external view returns (uint);
function kLast() external view returns (uint);

function mint(address to) external returns (uint liquidity);
function burn(address to) external returns (uint amount0, uint amount1);
function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
function skim(address to) external;
function sync() external;

function initialize(address, address) external;
}

0 comments on commit e7f1b0e

Please sign in to comment.