Skip to content

Commit

Permalink
dydx WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
bowd committed Apr 1, 2020
1 parent a4c1e25 commit b2b5e42
Show file tree
Hide file tree
Showing 16 changed files with 1,122 additions and 2 deletions.
7 changes: 5 additions & 2 deletions contracts/CompoundCollector.sol
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
pragma solidity >=0.4.25 <0.7.0;
pragma experimental ABIEncoderV2;

import "@openzeppelin/contracts/math/SafeMath.sol";

import "./interfaces/compound/IPriceOracle.sol";
import "./interfaces/compound/IComptroller.sol";
import "./interfaces/compound/ICToken.sol";

import "./IDefiPlatformCollector.sol";
import "./lib/Exponential.sol";
import "./lib/CarefulMath.sol";
import "./lib/compound/Exponential.sol";
import "./lib/PositionsHelper.sol";
import "./lib/DependencyRegistry.sol";

contract CompoundCollector is IDefiPlatformCollector, Ownable, DependencyRegistry, Exponential, PositionsHelper {
using SafeMath for uint256;

bytes32 platformID_ = 0x436f6d706f756e64000000000000000000000000000000000000000000000000; // Compound
function isDefiPlatformCollector() public pure returns (bool) { return true; }
function platformID() public view returns (bytes32) { return platformID_; }
Expand Down
91 changes: 91 additions & 0 deletions contracts/DYDXCollector.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
pragma solidity >=0.4.25 <0.7.0;
pragma experimental ABIEncoderV2;

import "@openzeppelin/contracts/math/SafeMath.sol";

import "./interfaces/dydx/ISoloMargin.sol";
import "./interfaces/IERC20.sol";

import "./IDefiPlatformCollector.sol";
import "./lib/PositionsHelper.sol";
import "./lib/DependencyRegistry.sol";

contract DYDXCollector is IDefiPlatformCollector, Ownable, DependencyRegistry, PositionsHelper {
using SafeMath for uint256;

bytes32 platformID_ = 0x6459645800000000000000000000000000000000000000000000000000000000; // dYdX
function isDefiPlatformCollector() public pure returns (bool) { return true; }
function platformID() public view returns (bytes32) { return platformID_; }

uint8 constant ISoloMarginIndex = 0;

constructor(address[] memory initialDeps) DependencyRegistry(initialDeps, 1) Ownable() public {}

function getPositionID(uint256 nonce, address market) internal pure returns (bytes memory) {
return abi.encode(nonce, market);
}

function getPositionCurrency(address market) internal view returns (bytes memory) {
IERC20 = token = IERC20(market);
return abi.encode(asset, token.name());
}

function getPoolLiquidity(address market) internal view returns (uint) {
IERC20 token = IERC20(market);
return token.balanceOf(getDependency(ISoloMarginIndex));
}

function getSupply(address target, uint256 marketId, Types.Wei amount) internal view returns (Defi.Position memory) {
ISoloMargin soloMargin = ISoloMargin(getDependency(ISoloMarginIndex));
address market = soloMargin.getMarketTokenAddress(marketId);

return Defi.Position(
getPositionID(market),
getPositionCurrency(market),
amount.value,
getPoolLiquidity(market),
0,
abi.encode(soloMargin.getMarketTotalPar(marketId))
);
}

function getBorrow(address target, uint256 marketId, Types.Wei amount) internal view returns (Defi.Position memory) {
return Defi.Position(
getPositionID(asset),
getPositionCurrency(asset),
amount.value,
0,
0,
abi.encode(soloMargin.getMarketTotalPar(marketId))
);
}

function getPositions(address target) public view returns (Defi.PlatformResult memory) {
ISoloMargin soloMargin = ISoloMargin(getDependency(ISoloMarginIndex));
Account.Info account = Account.Info(target, 0);
uint256 numMarkets = soloMargin.getNumMarkets();
Defi.Position[] supplies = new Defi.Positions[](markets.length);
Defi.Position[] borrows = new Defi.Positions[](markets.length);
uint8 supplyIndex = 0;
uint8 borrowIndex = 0;

for (uint8 mi = 0; mi < numMarkets; mi++) {
Types.Wei balance = soloMargin.getAccountWei(account, mi);
if (balance.value != 0) {
if (balance.sign == true) {
supplies[supplyIndex] = getSupply(target, mi, balance);
supplyIndex += 1;
} else {
borrows[borrowIndex] = getBorrow(target, mi, balance);
borrowIndex += 1;
}
}
}

return Defi.PlatformResult(
platformID_,
repackPositions(borrows, borrowIndex),
repackPositions(supplies, supplyIndex)
);
}
}
2 changes: 2 additions & 0 deletions contracts/MakerCollector.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pragma solidity >=0.4.25 <0.7.0;
pragma experimental ABIEncoderV2;

import "@openzeppelin/contracts/ownership/Ownable.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";

import "./interfaces/maker/IProxyRegistry.sol";
import "./interfaces/maker/IGetCdps.sol";
Expand All @@ -15,6 +16,7 @@ import "./lib/DependencyRegistry.sol";
import "./lib/PositionsHelper.sol";

contract MakerCollector is IDefiPlatformCollector, Ownable, DependencyRegistry, PositionsHelper {
using SafeMath for uint256;
bytes32 platformID_ = 0x4d616b65724d4344000000000000000000000000000000000000000000000000; // MakerMCD
function isDefiPlatformCollector() public pure returns (bool) { return true; }
function platformID() public view returns (bytes32) { return platformID_; }
Expand Down
7 changes: 7 additions & 0 deletions contracts/interfaces/IERC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pragma solidity >=0.4.25 <0.7.0;
pragma solidity ^0.4.0;

interface IERC20 {
function name() external view returns (string);
function balanceOf(address target) external view returns (uint256);
}
19 changes: 19 additions & 0 deletions contracts/interfaces/dydx/ISoloMargin.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pragma solidity >=0.4.25 <0.7.0;
pragma experimental ABIEncoderV2;

import { Account } from "../../lib/dydx/Account.sol";
import { Monetary } from "../../lib/dydx/Monetary.sol";
import { Types } from "../../lib/dydx/Types.sol";
import { Interest } from "../../lib/dydx/Interest.sol";

interface ISoloMargin {
function getNumMarkets() external view returns (uint256);
function getAccountValues(Account.Info calldata account) external view returns (Monetary.Value memory, Monetary.Value memory);
function getAccountStatus(Account.Info calldata account) external view returns (Account.Status);
function getAccountWei(Account.Info calldata account, uint256 marketId) external view returns (Types.Wei);
function getMarketTokenAddress(uint256 marketId) external view returns (address);
function getMarketCurrentIndex(uint256 marketId) external view returns (Interest.Index memory);
function getMarketInterestRate(uint256 marketId) external view returns (Interest.Rate memory);
function getMarketTotalPar(uint256 marketId) external view returns (Types.TotalPar memory);
}

File renamed without changes.
File renamed without changes.
44 changes: 44 additions & 0 deletions contracts/lib/dydx/Account.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
pragma solidity >0.5.7;
pragma experimental ABIEncoderV2;

import { Types } from "./Types.sol";


/**
* @title Account
* @author dYdX
*
* Library of structs and functions that represent an account
*/
library Account {
// ============ Enums ============

/*
* Most-recently-cached account status.
*
* Normal: Can only be liquidated if the account values are violating the global margin-ratio.
* Liquid: Can be liquidated no matter the account values.
* Can be vaporized if there are no more positive account values.
* Vapor: Has only negative (or zeroed) account values. Can be vaporized.
*
*/
enum Status {
Normal,
Liquid,
Vapor
}

// ============ Structs ============

// Represents the unique key that specifies an account
struct Info {
address owner; // The address that owns the account
uint256 number; // A nonce that allows a single address to control many accounts
}

// The complete storage for any account
struct Storage {
mapping (uint256 => Types.Par) balances; // Mapping from marketId to principal
Status status;
}
}
86 changes: 86 additions & 0 deletions contracts/lib/dydx/Decimal.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright 2019 dYdX Trading Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

pragma solidity 0.5.7;
pragma experimental ABIEncoderV2;

import { SafeMath } from "@openzeppelin-solidity/contracts/math/SafeMath.sol";
import { Math } from "./Math.sol";


/**
* @title Decimal
* @author dYdX
*
* Library that defines a fixed-point number with 18 decimal places.
*/
library Decimal {
using SafeMath for uint256;

// ============ Constants ============

uint256 constant BASE = 10**18;

// ============ Structs ============

struct D256 {
uint256 value;
}

// ============ Functions ============

function one()
internal
pure
returns (D256 memory)
{
return D256({ value: BASE });
}

function onePlus(
D256 memory d
)
internal
pure
returns (D256 memory)
{
return D256({ value: d.value.add(BASE) });
}

function mul(
uint256 target,
D256 memory d
)
internal
pure
returns (uint256)
{
return Math.getPartial(target, d.value, BASE);
}

function div(
uint256 target,
D256 memory d
)
internal
pure
returns (uint256)
{
return Math.getPartial(target, BASE, d.value);
}
}
Loading

0 comments on commit b2b5e42

Please sign in to comment.