Skip to content

Commit

Permalink
adapt code to work with release 15.10.2024 (#26)
Browse files Browse the repository at this point in the history
* adapt code to work with release 15.10.2024

* remove double import

* fix readme
  • Loading branch information
0xAleksaOpacic authored Oct 21, 2024
1 parent 7016f31 commit dbd9678
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 7,448 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ as well as performing operations like minting, swapping, and burning
2. **Run the Demo Tasks**:
- For the core demo (Factory and Pair):
```bash
npx hardhat demo --network nil
npx hardhat demo
```
- For the demo with Router (Factory, Pair, and Router):
```bash
npx hardhat demo-router --network nil
npx hardhat demo-router
```
- For the demo with Router (Sync calls):
```bash
npx hardhat demo-router-sync --network nil
npx hardhat demo-router-sync
```

### Manual Setup
Expand Down
13 changes: 7 additions & 6 deletions contracts/UniswapV2Pair.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "./interfaces/IUniswapV2Factory.sol";
import "@nilfoundation/smart-contracts/contracts/NilCurrencyBase.sol";
import "./libraries/SafeMath.sol";
import "@nilfoundation/smart-contracts/contracts/Nil.sol";
import {Currency} from "./Currency.sol";

contract UniswapV2Pair is NilCurrencyBase, IUniswapV2Pair {
using SafeMath for uint;
Expand All @@ -17,8 +18,8 @@ contract UniswapV2Pair is NilCurrencyBase, IUniswapV2Pair {
address public factory;
address public token0;
address public token1;
uint256 public tokenId0;
uint256 public tokenId1;
CurrencyId public tokenId0;
CurrencyId public tokenId1;

uint256 private reserve0; // uses single storage slot, accessible via getReserves
uint256 private reserve1; // uses single storage slot, accessible via getReserves
Expand All @@ -42,7 +43,7 @@ contract UniswapV2Pair is NilCurrencyBase, IUniswapV2Pair {
_reserve1 = reserve1;
}

function _safeTransfer(uint256 _tokenId, address _to, uint _value) private {
function _safeTransfer(CurrencyId _tokenId, address _to, uint _value) private {
sendCurrencyInternal(_to, _tokenId, _value);
}

Expand All @@ -51,7 +52,7 @@ contract UniswapV2Pair is NilCurrencyBase, IUniswapV2Pair {
}

// called once by the factory at time of deployment
function initialize(address _token0, address _token1, uint256 _tokenId0, uint256 _tokenId1) public {
function initialize(address _token0, address _token1, CurrencyId _tokenId0, CurrencyId _tokenId1) public {
token0 = _token0;
token1 = _token1;
tokenId0 = _tokenId0;
Expand Down Expand Up @@ -235,10 +236,10 @@ contract UniswapV2Pair is NilCurrencyBase, IUniswapV2Pair {
);
}

function token0Id() external view returns (uint256) {
function token0Id() external view returns (CurrencyId) {
return tokenId0;
}
function token1Id() external view returns (uint256) {
function token1Id() external view returns (CurrencyId) {
return tokenId1;
}

Expand Down
12 changes: 6 additions & 6 deletions contracts/UniswapV2Router01.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ contract UniswapV2Router01 is IUniswapV2Router01, NilCurrencyBase {

function _addLiquiditySync(
address pair,
uint256 tokenA,
uint256 tokenB,
CurrencyId tokenA,
CurrencyId tokenB,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
Expand Down Expand Up @@ -134,9 +134,9 @@ contract UniswapV2Router01 is IUniswapV2Router01, NilCurrencyBase {
if (tokens.length != 1) {
revert("UniswapV2Router: should contains only pair token");
}
uint256 token0Id = IUniswapV2Pair(pair).token0Id();
uint256 token1Id = IUniswapV2Pair(pair).token1Id();
uint256 tokenBId = tokens[0].id != token0Id ? token0Id : token1Id;
CurrencyId token0Id = IUniswapV2Pair(pair).token0Id();
CurrencyId token1Id = IUniswapV2Pair(pair).token1Id();
CurrencyId tokenBId = tokens[0].id != token0Id ? token0Id : token1Id;
(uint reserveA, uint reserveB) = UniswapV2Library.getReserves(pair, tokens[0].id, tokenBId);
amount = UniswapV2Library.getAmountOut(tokens[0].amount, reserveA, reserveB);
require(amount >= amountOutMin, 'UniswapV2Router: INSUFFICIENT_OUTPUT_AMOUNT');
Expand Down Expand Up @@ -168,7 +168,7 @@ contract UniswapV2Router01 is IUniswapV2Router01, NilCurrencyBase {
(bool success, bytes memory result) = Nil.syncCall(dst, gasleft(), 0, tokens, callData);
return (success, result);
} else {
Nil.asyncCall(dst, address(0), address(0), 0, Nil.FORWARD_REMAINING, false, 0, tokens, callData);
Nil.asyncCallWithTokens(dst, address(0), address(0), 0, Nil.FORWARD_REMAINING, false, 0, tokens, callData);
return (true, "");
}
}
Expand Down
9 changes: 6 additions & 3 deletions contracts/interfaces/IUniswapV2Pair.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
pragma solidity >=0.5.0;

import {Currency} from "../Currency.sol";
import "@nilfoundation/smart-contracts/contracts/Nil.sol";

interface IUniswapV2Pair {
event Mint(address indexed sender, uint amount0, uint amount1);
event Burn(
Expand All @@ -21,8 +24,8 @@ interface IUniswapV2Pair {
function factory() external view returns (address);
function token0() external view returns (address);
function token1() external view returns (address);
function token0Id() external view returns (uint256);
function token1Id() external view returns (uint256);
function token0Id() external view returns (CurrencyId);
function token1Id() external view returns (CurrencyId);
function getReserves()
external
view
Expand All @@ -41,5 +44,5 @@ interface IUniswapV2Pair {
) external;
function skim(address to) external;
function sync() external;
function initialize(address, address, uint256, uint256) external;
function initialize(address, address, CurrencyId, CurrencyId) external;
}
10 changes: 5 additions & 5 deletions contracts/libraries/UniswapV2Library.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ library UniswapV2Library {
using SafeMath for uint;

// returns sorted token addresses, used to handle return values from pairs sorted in this order
function sortTokens(uint256 tokenAId, uint256 tokenBId) internal pure returns (uint256 token0, uint256 token1) {
function sortTokens(CurrencyId tokenAId, CurrencyId tokenBId) internal pure returns (CurrencyId token0, CurrencyId token1) {
require(tokenAId != tokenBId, 'UniswapV2Library: IDENTICAL_ADDRESSES');
(token0, token1) = tokenAId < tokenBId ? (tokenAId, tokenBId) : (tokenBId, tokenAId);
require(token0 != uint256(0), 'UniswapV2Library: ZERO_ADDRESS');
(token0, token1) = CurrencyId.unwrap(tokenAId) < CurrencyId.unwrap(tokenBId) ? (tokenAId, tokenBId) : (tokenBId, tokenAId);
require(token0 != CurrencyId.wrap(address(0)), 'UniswapV2Library: ZERO_ADDRESS');
}

// fetches and sorts the reserves for a pair
function getReserves(address pair, uint256 tokenAId, uint256 tokenBId) internal view returns (uint reserveA, uint reserveB) {
(uint token0,) = sortTokens(tokenAId, tokenBId);
function getReserves(address pair, CurrencyId tokenAId, CurrencyId tokenBId) internal view returns (uint reserveA, uint reserveB) {
(CurrencyId token0,) = sortTokens(tokenAId, tokenBId);
(uint reserve0, uint reserve1) = IUniswapV2Pair(pair).getReserves();
(reserveA, reserveB) = tokenAId == token0 ? (reserve0, reserve1) : (reserve1, reserve0);
}
Expand Down
1 change: 1 addition & 0 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const config: NilHardhatUserConfig = {
ignition: {
requiredConfirmations: 1,
},
defaultNetwork: "nil",
solidity: {
version: "0.8.24", // or your desired version
settings: {
Expand Down
Loading

0 comments on commit dbd9678

Please sign in to comment.