Skip to content

Commit

Permalink
Merge pull request #3 from vetherasset/kovan-deploy-singleton-pool
Browse files Browse the repository at this point in the history
Kovan deploy singleton pool
  • Loading branch information
t4sk authored Oct 29, 2021
2 parents 37e872c + 453a3ab commit c04c571
Show file tree
Hide file tree
Showing 47 changed files with 3,611 additions and 84 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,37 @@ The Vader monorepo (internal)
## Setup

Node version that should be utilized is 12.16.2, other versions can show unwarranted errors. For Node.JS version management, `nvm` is recommended.

```shell
npm i
# put your wallet seed here
touch .secret
# put your environment variables here
cp .env.sample .env
```

## Deploy

```shell
# deploy (run migration script from x to y)
npx truffle migrate -f x --to y --network kovan
# verify contract
npx truffle run verify MyContract --network kovan
```

## Networks & Addresses

### Kovan

- Vether: [0x438f70Ab08AB3F74833c439643C3fC1939cE2929](https://kovan.etherscan.io/address/0x438f70Ab08AB3F74833c439643C3fC1939cE2929)
- Vader: [0xE8bD273f54a990007b1069a15b66B584abc86e04](https://kovan.etherscan.io/address/0xE8bD273f54a990007b1069a15b66B584abc86e04)
- Converter: [0xF79c9406c14AF5Aa8b3F1E5E538A026aDf4D0ff5](https://kovan.etherscan.io/address/0xF79c9406c14AF5Aa8b3F1E5E538A026aDf4D0ff5)
- LinearVesting: [0xEa66FB7590147A5C901E14034f243e1cF8f958ff](https://kovan.etherscan.io/address/0xEa66FB7590147A5C901E14034f243e1cF8f958ff)
- VaderMath: [0x0A23bde5E3930EfEaa546A4b4F10a1b7A9cC1e6C](https://kovan.etherscan.io/address/0x0A23bde5E3930EfEaa546A4b4F10a1b7A9cC1e6C)
- VaderPoolV2: [0x795bE6b0BF54AF587385604B9DB869E797db69E0](https://kovan.etherscan.io/address/0x795bE6b0BF54AF587385604B9DB869E797db69E0)
- VaderRouterV2: [0x80362414e23E64c404a8581779b28f037B8d5A05](https://kovan.etherscan.io/address/0x80362414e23E64c404a8581779b28f037B8d5A05)
- VaderReserve: [0x176207eD5Ae8c41F766E8C31112c7cDdE5Fb32AA](https://kovan.etherscan.io/address/0x176207eD5Ae8c41F766E8C31112c7cDdE5Fb32AA)
- USDV: [0xE90E0A75694Fc97576868243AD0364d10291f48A](https://kovan.etherscan.io/address/0xE90E0A75694Fc97576868243AD0364d10291f48A)
- Vault (Mock): [0x85380a961CE380CdA8977E3A9D26bA91D9C379B1](https://kovan.etherscan.io/address/0x85380a961CE380CdA8977E3A9D26bA91D9C379B1)
- GovernorAlpha: [0x34e9Db9bC8c668E2fa922c1d22913DCd587607D8](https://kovan.etherscan.io/address/0x34e9Db9bC8c668E2fa922c1d22913DCd587607D8)
- Timelock: [0x7574D631E9b402917cC478ac568b465D7F726033](https://kovan.etherscan.io/address/0x7574D631E9b402917cC478ac568b465D7F726033)
171 changes: 163 additions & 8 deletions contracts/dex-v2/pool/BasePoolV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

pragma solidity =0.8.9;

import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
Expand All @@ -15,13 +14,29 @@ import "../../external/libraries/UQ112x112.sol";

import "../../interfaces/dex-v2/pool/IBasePoolV2.sol";

contract BasePoolV2 is
IBasePoolV2,
GasThrottle,
ERC721,
Ownable,
ReentrancyGuard
{
/*
* @dev Implementation of {BasePoolV2} contract.
*
* The BasePoolV2 contract keeps track of all the Vader pools in the form of
* pairs. Each pair tracked through {pairInfo} mapping and is mapped against the
* foreign asset for which the pair is created.
*
* Has function to deposited liquidity to any of pair by specifying the mapped
* foreign asset against the pair.
*
* The minted liquidity is associated with a position, that is tracked by a minted NFT.
* The NFT has information about the pair for which it represents the liquidity.
*
* The contract allows redeeming of liquidity against a particular pool by burning the
* associated position representing NFT.
*
* The contract allows swapping of native to foreign assets and vice versa within a pair and
* allows foreign to foreign asset swap across two different pairs.
*
* Keeps track of the cumulative prices for both native and foreign assets for
* pairs and updates them after minting and burning of liquidity, and swapping of assets.
**/
contract BasePoolV2 is IBasePoolV2, GasThrottle, ERC721, ReentrancyGuard {
/* ========== LIBRARIES ========== */

// Used for safe token transfers
Expand All @@ -32,13 +47,31 @@ contract BasePoolV2 is

/* ========== STATE VARIABLES ========== */

// Address of native asset (Vader or USDV).
IERC20 public immutable override nativeAsset;

// Denotes what tokens are actively supported by the system
mapping(IERC20 => bool) public override supported;

/*
* @dev A mapping of foreign asset to the pool's pair.
* Each pair is represents a pool of native and foreign assets and
* contains data such as the reserves of native and foreign assets and
* the liquidity units issues against the deposits of these assets.
**/
mapping(IERC20 => PairInfo) public pairInfo;

/*
* @dev A mapping representing positions of liquidity providers. Each position
* is an Non-fungible token that is mapped against amounts of native and foreign assets
* deposited across different pools of pairs the timestamp at which the position
* is created and the amount of liquidity of a particular pool assigned to the LP.
*
* Each position in the mapping is mapped against {positionId}.
**/
mapping(uint256 => Position) public positions;

// A unique id the of the position created when liquidity is added to a pool.
uint256 public positionId;

// uint112 private _reserveNative; // uses single storage slot, accessible via getReserves
Expand All @@ -47,12 +80,20 @@ contract BasePoolV2 is

/* ========== CONSTRUCTOR ========== */

/*
* @dev Initializes the contract by setting address of native asset.
**/
constructor(IERC20 _nativeAsset) ERC721("Vader LP", "VLP") {
nativeAsset = IERC20(_nativeAsset);
}

/* ========== VIEWS ========== */

/*
* @dev Accepts address of foreign asset {foreignAsset} to determine the pair (pool)
* and returns reserves amounts of native and foreign assets, and the last timestamp
* when cumulative prices for these assets were updated.
**/
function getReserves(IERC20 foreignAsset)
public
view
Expand All @@ -70,6 +111,10 @@ contract BasePoolV2 is
);
}

/*
* @dev Accepts {id} of a liquidity position and returns foreign asset's
* address for that particular liquidity position.
**/
function positionForeignAsset(uint256 id)
external
view
Expand All @@ -79,8 +124,35 @@ contract BasePoolV2 is
return positions[id].foreignAsset;
}

function pairSupply(IERC20 foreignAsset)
external
view
override
returns (uint256)
{
return pairInfo[foreignAsset].totalSupply;
}

/* ========== MUTATIVE FUNCTIONS ========== */

/*
* @dev Allows depositing of liquidity to a pool/pair by accepting native and foreign assets
* and mints an NFT to the {to} address which records in {positions} mapping, the amounts
* of the native and foreign assets deposited and the liquidity units minted against.
*
* The pool/pair to accept the native and foreign assets against is determined by {foreignAsset}.
*
* Updates the total supply of liquidity units by adding currently minted liquidity units
* to {pair.totalSupply} of pair/pool.
*
* Updates the cumulative prices of native and foreign assets in pool/pair after minting the appropriate
* liquidity units.
*
* Requirements:
* - Amounts of native and foreign must be approved to the pool prior to calling the `mint` function.
* - The amount of {liquidity} to be minted must be greater than 0.
* - The param {foreignAsset} must be a supported token.
**/
function mint(
IERC20 foreignAsset,
uint256 nativeDeposit,
Expand Down Expand Up @@ -144,6 +216,25 @@ contract BasePoolV2 is
emit PositionOpened(from, to, id, liquidity);
}

/*
* @dev Allows redeeming of liquidity units by burning the NFT with {id} associated with the liquidity
* position.
*
* Computes the amounts of native and foreign assets from pool/pair against which the NFT with Id {id}
* was minted. The computed assets' amounts depends upon current reserves of assets and
* the liquidity associated with the position, and is transferred to the {to} address.
*
* Burns the redeemed NFT token and decreases {pair.totalSupply} by the {liquidity}
* associated with that NFT token.
*
* Updates the cumulative prices for native and foreign assets in pool/pair after transferring the assets
* to the {to} address.
*
* Requirements:
* - The NFT token being redeemed must be transferred to the contract prior to calling `_burn`.
* - The amount of native and foreign assets computed for transfer to {to} address must be greater
* than 0.
**/
function _burn(uint256 id, address to)
internal
nonReentrant
Expand Down Expand Up @@ -189,6 +280,25 @@ contract BasePoolV2 is
emit Burn(msg.sender, amountNative, amountForeign, to);
}

/*
* @dev Allows swapping between two foreign assets from two different pools/pairs.
*
* It receives amount {foreignAmountIn} in {foreignAssetA} and returns the swapped amount in {foreignAssetB}.
*
* The amount {foreignAmountIn} is swapped to the native asset from the pair against {foreignAssetA} and the
* received native asset is swapped to foreign asset from the pair against {foreignAssetB}.
*
* Updates the cumulative prices for native and foreign assets across pools against assets {foreignAssetA} and
* {foreignAssetB}.
*
* Requirements:
* - The amount {foreignAmountIn} in {foreignAssetA} must be transferred to the contract prior to calling
* the function `doubleSwap`.
* - The intermediary native asset retrieved from first swap must be greater than 0 and the reserve for native asset.
* - The foreign amount received from second swap must be greater than 0 and the reserve for foreign asset in the pair/pool
* against that particular foreign asset.
* - The params {foreignAssetA} and {foreignAssetB} must be the supported tokens.
**/
function doubleSwap(
IERC20 foreignAssetA,
IERC20 foreignAssetB,
Expand Down Expand Up @@ -276,6 +386,28 @@ contract BasePoolV2 is
foreignAssetB.safeTransfer(to, foreignAmountOut);
}

/*
* @dev Allows swapping between native and foreign assets from within a single pair/pool determined
* by {foreignAsset}.
*
* It receives the source asset and computes the destination asset and transfers it to the {to} address.
*
* Updates the cumulative prices for native and foreign assets for the pair involved after performing swap.
*
* Returns the amount of destination tokens resulting from the swap.
*
* Requirements:
* - Param {nativeAmountIn} must be zero and {foreignAmountIn} must be non-zero
* if the destination asset in swap is native asset.
* - Param {foreignAmountIn} must be zero and {nativeAmountIn} must be non zero
* if the destination asset in swap is foreign asset.
* - Param {to} cannot be the addresses of native or foreign assets.
* - The source asset amount in the swap must be transferred to the pool prior to calling `swap`.
* - The source asset amount in the swap cannot exceed the source asset's reserve.
* - The destination asset's amount in the swap must be greater than 0 and not exceed destination
* asset's reserve.
* - The param {foreignAsset} must be a supported token.
**/
function swap(
IERC20 foreignAsset,
uint256 nativeAmountIn,
Expand Down Expand Up @@ -354,6 +486,11 @@ contract BasePoolV2 is
return nativeAmountOut > 0 ? nativeAmountOut : foreignAmountOut;
}

/*
* @dev Allows withdrawing of unaccounted/unrealised foreign asset from the contract.
*
* Determines the realised amount of foreign asset from the pair against {foreignAsset}.
**/
function rescue(IERC20 foreignAsset) external {
uint256 foreignBalance = foreignAsset.balanceOf(address(this));
uint256 reserveForeign = pairInfo[foreignAsset].reserveForeign;
Expand All @@ -367,6 +504,16 @@ contract BasePoolV2 is

/* ========== INTERNAL FUNCTIONS ========== */

/*
* @dev Internally called to update the cumulative prices for native and foreign assets for
* the pair against {foreignAsset}. The updated prices depend upon the last reserves and
* updates the reserves for both of the assets corresponding to their
* current balances along with the timestamp.
*
* Requirements:
* - Params {balanceNative} and {balanceForeign} must not overflow type `uint112`.
*
**/
function _update(
IERC20 foreignAsset,
uint256 balanceNative,
Expand Down Expand Up @@ -405,6 +552,10 @@ contract BasePoolV2 is

/* ========== PRIVATE FUNCTIONS ========== */

/*
* @dev Private function that returns if the param {token} is a supported token
* or not.
**/
function _supportedToken(IERC20 token) private view {
require(
supported[token],
Expand All @@ -414,6 +565,10 @@ contract BasePoolV2 is

/* ========== MODIFIERS ========== */

/*
* @dev Modifier that only allows continuation of exection if the param
* {token} is a supported token.
**/
modifier supportedToken(IERC20 token) {
_supportedToken(token);
_;
Expand Down
Loading

0 comments on commit c04c571

Please sign in to comment.