dsa-connectors-old/contracts/connectors/dydx.sol

297 lines
11 KiB
Solidity
Raw Normal View History

2020-05-04 02:31:56 +00:00
pragma solidity ^0.6.0;
pragma experimental ABIEncoderV2;
// import files from common directory
import { TokenInterface , MemoryInterface, EventInterface} from "../common/interfaces.sol";
import { Stores } from "../common/stores.sol";
import { DSMath } from "../common/math.sol";
interface SoloMarginContract {
struct Info {
2020-05-16 17:51:37 +00:00
address owner;
uint256 number;
2020-05-04 02:31:56 +00:00
}
enum ActionType {
2020-05-16 17:51:37 +00:00
Deposit,
Withdraw,
Transfer,
Buy,
Sell,
Trade,
Liquidate,
Vaporize,
Call
2020-05-04 02:31:56 +00:00
}
enum AssetDenomination {
2020-05-16 17:51:37 +00:00
Wei,
Par
2020-05-04 02:31:56 +00:00
}
enum AssetReference {
2020-05-16 17:51:37 +00:00
Delta,
Target
2020-05-04 02:31:56 +00:00
}
struct AssetAmount {
2020-05-16 17:51:37 +00:00
bool sign;
2020-05-04 02:31:56 +00:00
AssetDenomination denomination;
AssetReference ref;
uint256 value;
}
struct ActionArgs {
ActionType actionType;
uint256 accountId;
AssetAmount amount;
uint256 primaryMarketId;
uint256 secondaryMarketId;
address otherAddress;
uint256 otherAccountId;
bytes data;
}
struct Wei {
2020-05-16 17:51:37 +00:00
bool sign;
2020-05-04 02:31:56 +00:00
uint256 value;
}
function operate(Info[] calldata accounts, ActionArgs[] calldata actions) external;
function getAccountWei(Info calldata account, uint256 marketId) external returns (Wei memory);
function getNumMarkets() external view returns (uint256);
function getMarketTokenAddress(uint256 marketId) external view returns (address);
}
contract DydxHelpers is DSMath, Stores {
/**
* @dev get WETH address
*/
function getWETHAddr() public pure returns (address weth) {
weth = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
}
/**
* @dev get Dydx Solo Address
*/
function getDydxAddress() public pure returns (address addr) {
addr = 0x1E0447b19BB6EcFdAe1e4AE1694b0C3659614e4e;
}
/**
* @dev Get Dydx Actions args.
*/
function getActionsArgs(uint256 marketId, uint256 amt, bool sign) internal view returns (SoloMarginContract.ActionArgs[] memory) {
SoloMarginContract.ActionArgs[] memory actions = new SoloMarginContract.ActionArgs[](1);
SoloMarginContract.AssetAmount memory amount = SoloMarginContract.AssetAmount(
sign,
SoloMarginContract.AssetDenomination.Wei,
SoloMarginContract.AssetReference.Delta,
amt
);
bytes memory empty;
SoloMarginContract.ActionType action = sign ? SoloMarginContract.ActionType.Deposit : SoloMarginContract.ActionType.Withdraw;
actions[0] = SoloMarginContract.ActionArgs(
action,
0,
amount,
marketId,
0,
address(this),
0,
empty
);
return actions;
}
/**
* @dev Get Dydx Acccount arg
*/
function getAccountArgs() internal view returns (SoloMarginContract.Info[] memory) {
SoloMarginContract.Info[] memory accounts = new SoloMarginContract.Info[](1);
accounts[0] = (SoloMarginContract.Info(address(this), 0));
return accounts;
}
/**
* @dev Get Dydx Position
*/
function getDydxPosition(SoloMarginContract solo, uint256 marketId) internal returns (uint tokenBal, bool tokenSign) {
SoloMarginContract.Wei memory tokenWeiBal = solo.getAccountWei(getAccountArgs()[0], marketId);
tokenBal = tokenWeiBal.value;
tokenSign = tokenWeiBal.sign;
}
/**
* @dev Get Dydx Market ID from token Address
*/
function getMarketId(SoloMarginContract solo, address token) internal view returns (uint _marketId) {
uint markets = solo.getNumMarkets();
address _token = token == getEthAddr() ? getWETHAddr() : token;
for (uint i = 0; i < markets; i++) {
if (_token == solo.getMarketTokenAddress(i)) {
_marketId = i;
break;
}
}
}
}
contract BasicResolver is DydxHelpers {
event LogDeposit(address indexed token, uint marketId, uint256 tokenAmt, uint256 getId, uint256 setId);
event LogWithdraw(address indexed token, uint marketId, uint256 tokenAmt, uint256 getId, uint256 setId);
event LogBorrow(address indexed token, uint marketId, uint256 tokenAmt, uint256 getId, uint256 setId);
event LogPayback(address indexed token, uint marketId, uint256 tokenAmt, uint256 getId, uint256 setId);
/**
2020-05-16 17:51:37 +00:00
* @dev Deposit ETH/ERC20_Token.
* @param token token address to deposit.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt token amount to deposit.
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setId Set token amount at this ID in `InstaMemory` Contract.
*/
2020-05-04 02:31:56 +00:00
function deposit(address token, uint amt, uint getId, uint setId) external payable{
SoloMarginContract dydxContract = SoloMarginContract(getDydxAddress());
uint _amt = getUint(getId, amt);
uint _marketId = getMarketId(dydxContract, token);
(uint depositedAmt, bool sign) = getDydxPosition(dydxContract, _marketId);
2020-05-15 18:29:48 +00:00
require(depositedAmt == 0 || sign, "token-borrowed");
2020-05-04 02:31:56 +00:00
if (token == getEthAddr()) {
TokenInterface tokenContract = TokenInterface(getWETHAddr());
_amt = _amt == uint(-1) ? address(this).balance : _amt;
tokenContract.deposit.value(_amt)();
2020-05-15 15:52:24 +00:00
tokenContract.approve(getDydxAddress(), _amt);
2020-05-04 02:31:56 +00:00
} else {
TokenInterface tokenContract = TokenInterface(token);
_amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt;
tokenContract.approve(getDydxAddress(), _amt);
}
dydxContract.operate(getAccountArgs(), getActionsArgs(_marketId, _amt, true));
setUint(setId, _amt);
emit LogDeposit(token, _marketId, _amt, getId, setId);
bytes32 _eventCode = keccak256("LogDeposit(address,uint256,uint256,uint256,uint256)");
bytes memory _eventParam = abi.encode(token, _marketId, _amt, getId, setId);
2020-05-16 19:03:41 +00:00
emitEvent(_eventCode, _eventParam);
2020-05-04 02:31:56 +00:00
}
2020-05-16 17:51:37 +00:00
/**
* @dev Withdraw ETH/ERC20_Token.
* @param token token address to withdraw.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt token amount to withdraw.
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setId Set token amount at this ID in `InstaMemory` Contract.
*/
2020-05-04 02:31:56 +00:00
function withdraw(address token, uint amt, uint getId, uint setId) external payable{
SoloMarginContract dydxContract = SoloMarginContract(getDydxAddress());
uint _amt = getUint(getId, amt);
uint _marketId = getMarketId(dydxContract, token);
(uint depositedAmt, bool sign) = getDydxPosition(dydxContract, _marketId);
2020-05-15 18:29:48 +00:00
require(sign, "try-payback");
2020-05-04 02:31:56 +00:00
_amt = _amt == uint(-1) ? depositedAmt : _amt;
require(_amt <= depositedAmt, "withdraw-exceeds");
dydxContract.operate(getAccountArgs(), getActionsArgs(_marketId, _amt, false));
if (token == getEthAddr()) {
TokenInterface tokenContract = TokenInterface(getWETHAddr());
tokenContract.approve(address(tokenContract), _amt);
tokenContract.withdraw(_amt);
}
setUint(setId, _amt);
emit LogWithdraw(token, _marketId, _amt, getId, setId);
bytes32 _eventCode = keccak256("LogWithdraw(address,uint256,uint256,uint256,uint256)");
bytes memory _eventParam = abi.encode(token, _marketId, _amt, getId, setId);
2020-05-16 19:03:41 +00:00
emitEvent(_eventCode, _eventParam);
2020-05-04 02:31:56 +00:00
}
/**
2020-05-16 17:51:37 +00:00
* @dev Borrow ETH/ERC20_Token.
* @param token token address to borrow.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt token amount to borrow.
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setId Set token amount at this ID in `InstaMemory` Contract.
2020-05-04 02:31:56 +00:00
*/
function borrow(address token, uint amt, uint getId, uint setId) external payable {
SoloMarginContract dydxContract = SoloMarginContract(getDydxAddress());
uint _amt = getUint(getId, amt);
uint _marketId = getMarketId(dydxContract, token);
(uint borrowedAmt, bool sign) = getDydxPosition(dydxContract, _marketId);
2020-05-15 18:29:48 +00:00
require(borrowedAmt == 0 || !sign, "token-deposited");
2020-05-04 02:31:56 +00:00
dydxContract.operate(getAccountArgs(), getActionsArgs(_marketId, _amt, false));
if (token == getEthAddr()) {
TokenInterface tokenContract = TokenInterface(getWETHAddr());
tokenContract.approve(address(tokenContract), _amt);
tokenContract.withdraw(_amt);
}
setUint(setId, _amt);
emit LogBorrow(token, _marketId, _amt, getId, setId);
bytes32 _eventCode = keccak256("LogBorrow(address,uint256,uint256,uint256,uint256)");
bytes memory _eventParam = abi.encode(token, _marketId, _amt, getId, setId);
2020-05-16 19:03:41 +00:00
emitEvent(_eventCode, _eventParam);
2020-05-04 02:31:56 +00:00
}
/**
2020-05-16 17:51:37 +00:00
* @dev Payback borrowed ETH/ERC20_Token.
* @param token token address to payback.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt token amount to payback.
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setId Set token amount at this ID in `InstaMemory` Contract.
*/
2020-05-04 02:31:56 +00:00
function payback(address token, uint amt, uint getId, uint setId) external payable {
SoloMarginContract dydxContract = SoloMarginContract(getDydxAddress());
uint _amt = getUint(getId, amt);
uint _marketId = getMarketId(dydxContract, token);
(uint borrowedAmt, bool sign) = getDydxPosition(dydxContract, _marketId);
2020-05-15 18:29:48 +00:00
require(!sign, "try-withdraw");
2020-05-04 02:31:56 +00:00
_amt = _amt == uint(-1) ? borrowedAmt : _amt;
require(_amt <= borrowedAmt, "payback-exceeds");
if (token == getEthAddr()) {
TokenInterface tokenContract = TokenInterface(getWETHAddr());
require(address(this).balance >= _amt, "not-enough-eth");
tokenContract.deposit.value(_amt)();
2020-05-15 15:52:24 +00:00
tokenContract.approve(getDydxAddress(), _amt);
2020-05-04 02:31:56 +00:00
} else {
TokenInterface tokenContract = TokenInterface(token);
require(tokenContract.balanceOf(address(this)) >= _amt, "not-enough-token");
tokenContract.approve(getDydxAddress(), _amt);
}
dydxContract.operate(getAccountArgs(), getActionsArgs(_marketId, _amt, true));
setUint(setId, _amt);
emit LogPayback(token, _marketId, _amt, getId, setId);
bytes32 _eventCode = keccak256("LogPayback(address,uint256,uint256,uint256,uint256)");
bytes memory _eventParam = abi.encode(token, _marketId, _amt, getId, setId);
2020-05-16 19:03:41 +00:00
emitEvent(_eventCode, _eventParam);
2020-05-04 02:31:56 +00:00
}
}
contract ConnectDydx is BasicResolver {
string public name = "Dydx-v1";
}