-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,122 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.