forked from dydxprotocol/solo
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finished testing and added coverage report for BorrowPositionProxy
- Loading branch information
Corey
committed
Aug 19, 2022
1 parent
1f2b8a0
commit 9aa63d2
Showing
26 changed files
with
479 additions
and
87 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
80 changes: 80 additions & 0 deletions
80
.temp_contracts/external/interfaces/IBorrowPositionProxy.sol
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,80 @@ | ||
/* | ||
Copyright 2022 Dolomite. | ||
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; | ||
|
||
|
||
interface IBorrowPositionProxy { | ||
|
||
// ========================= Events ========================= | ||
|
||
event BorrowPositionOpen(address indexed _borrower, uint indexed _borrowAccountIndex); | ||
|
||
// ========================= Functions ========================= | ||
|
||
/** | ||
* @param _fromAccountIndex The index from which `msg.sender` will be sourcing the deposit | ||
* @param _toAccountIndex The index into which `msg.sender` will be depositing | ||
* @param _collateralMarketId The ID of the market being deposited | ||
* @param _amountWei The amount, in Wei, to deposit | ||
*/ | ||
function openBorrowPosition( | ||
uint _fromAccountIndex, | ||
uint _toAccountIndex, | ||
uint _collateralMarketId, | ||
uint _amountWei | ||
) external; | ||
|
||
/** | ||
* @notice This method can only be called once the user's debt has been reduced to zero. Sends funds from account | ||
* index | ||
* | ||
* @param _borrowAccountIndex The index from which `msg.sender` collateral will be withdrawn | ||
* @param _toAccountIndex The index into which `msg.sender` will be depositing leftover collateral | ||
* @param _collateralMarketIds The IDs of the markets being withdrawn, to close the position | ||
*/ | ||
function closeBorrowPosition( | ||
uint _borrowAccountIndex, | ||
uint _toAccountIndex, | ||
uint[] calldata _collateralMarketIds | ||
) external; | ||
|
||
/** | ||
* @param _fromAccountIndex The index from which `msg.sender` will be withdrawing assets | ||
* @param _toAccountIndex The index into which `msg.sender` will be depositing assets | ||
* @param _marketId The ID of the market being transferred | ||
* @param _amountWei The amount, in Wei, to transfer | ||
*/ | ||
function transferBetweenAccounts( | ||
uint _fromAccountIndex, | ||
uint _toAccountIndex, | ||
uint _marketId, | ||
uint _amountWei | ||
) external; | ||
|
||
/** | ||
* @param _fromAccountIndex The index from which `msg.sender` will be depositing assets | ||
* @param _borrowAccountIndex The index of the borrow position for that will receive the deposited assets | ||
* @param _marketId The ID of the market being transferred | ||
*/ | ||
function repayAllForBorrowPosition( | ||
uint _fromAccountIndex, | ||
uint _borrowAccountIndex, | ||
uint _marketId | ||
) external; | ||
} |
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 |
---|---|---|
|
@@ -30,4 +30,4 @@ contract IChainlinkAggregator { | |
|
||
function latestAnswer() public view returns (int256); | ||
|
||
} | ||
} |
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
182 changes: 182 additions & 0 deletions
182
.temp_contracts/external/proxies/BorrowPositionProxy.sol
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,182 @@ | ||
/* | ||
Copyright 2022 Dolomite. | ||
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 { Address } from "@openzeppelin/contracts/utils/Address.sol"; | ||
import { ReentrancyGuard } from "@openzeppelin/contracts/utils/ReentrancyGuard.sol"; | ||
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
|
||
import { IDolomiteMargin } from "../../protocol/interfaces/IDolomiteMargin.sol"; | ||
|
||
import { Account } from "../../protocol/lib/Account.sol"; | ||
import { Actions } from "../../protocol/lib/Actions.sol"; | ||
import { Require } from "../../protocol/lib/Require.sol"; | ||
import { Types } from "../../protocol/lib/Types.sol"; | ||
|
||
import { OnlyDolomiteMargin } from "../helpers/OnlyDolomiteMargin.sol"; | ||
|
||
import { IBorrowPositionProxy } from "../interfaces/IBorrowPositionProxy.sol"; | ||
|
||
|
||
/** | ||
* @title BorrowPositionProxy | ||
* @author Dolomite | ||
* | ||
* @dev Contract for opening borrow positions to make indexing easier. This lowers gas costs on Arbitrum by minimizing | ||
* call data, too. | ||
*/ | ||
contract BorrowPositionProxy is IBorrowPositionProxy, OnlyDolomiteMargin, ReentrancyGuard { | ||
|
||
constructor ( | ||
address dolomiteMargin | ||
) | ||
public | ||
OnlyDolomiteMargin(dolomiteMargin) | ||
{} | ||
|
||
function openBorrowPosition( | ||
uint _fromAccountIndex, | ||
uint _toAccountIndex, | ||
uint _marketId, | ||
uint _amountWei | ||
) external { | ||
Account.Info[] memory accounts = new Account.Info[](2); | ||
accounts[0] = Account.Info(msg.sender, _fromAccountIndex); | ||
accounts[1] = Account.Info(msg.sender, _toAccountIndex); | ||
|
||
Actions.ActionArgs[] memory actions = new Actions.ActionArgs[](1); | ||
Types.AssetAmount memory assetAmount = Types.AssetAmount({ | ||
sign: false, | ||
denomination: Types.AssetDenomination.Wei, | ||
ref: Types.AssetReference.Delta, | ||
value: _amountWei | ||
}); | ||
actions[0] = Actions.ActionArgs({ | ||
actionType : Actions.ActionType.Transfer, | ||
accountId : 0, | ||
amount : assetAmount, | ||
primaryMarketId : _marketId, | ||
secondaryMarketId : 0, | ||
otherAddress : address(0), | ||
otherAccountId : 1, | ||
data : bytes("") | ||
}); | ||
|
||
// Emit this before the call to DolomiteMargin so indexers get it before the Transfer events are emitted | ||
emit BorrowPositionOpen(msg.sender, _toAccountIndex); | ||
|
||
IDolomiteMargin(DOLOMITE_MARGIN).operate(accounts, actions); | ||
} | ||
|
||
function closeBorrowPosition( | ||
uint _borrowAccountIndex, | ||
uint _toAccountIndex, | ||
uint[] calldata _collateralMarketIds | ||
) external { | ||
Account.Info[] memory accounts = new Account.Info[](2); | ||
accounts[0] = Account.Info(msg.sender, _borrowAccountIndex); | ||
accounts[1] = Account.Info(msg.sender, _toAccountIndex); | ||
|
||
Actions.ActionArgs[] memory actions = new Actions.ActionArgs[](_collateralMarketIds.length); | ||
Types.AssetAmount memory assetAmount = Types.AssetAmount({ | ||
sign: false, | ||
denomination: Types.AssetDenomination.Wei, | ||
ref: Types.AssetReference.Target, | ||
value: 0 | ||
}); | ||
|
||
for (uint i = 0; i < _collateralMarketIds.length; i++) { | ||
actions[i] = Actions.ActionArgs({ | ||
actionType : Actions.ActionType.Transfer, | ||
accountId : 0, | ||
amount : assetAmount, | ||
primaryMarketId : _collateralMarketIds[i], | ||
secondaryMarketId : 0, | ||
otherAddress : address(0), | ||
otherAccountId : 1, | ||
data : bytes("") | ||
}); | ||
} | ||
|
||
IDolomiteMargin(DOLOMITE_MARGIN).operate(accounts, actions); | ||
} | ||
|
||
function transferBetweenAccounts( | ||
uint _fromAccountIndex, | ||
uint _toAccountIndex, | ||
uint _marketId, | ||
uint _amountWei | ||
) external { | ||
Account.Info[] memory accounts = new Account.Info[](2); | ||
accounts[0] = Account.Info(msg.sender, _fromAccountIndex); | ||
accounts[1] = Account.Info(msg.sender, _toAccountIndex); | ||
|
||
Actions.ActionArgs[] memory actions = new Actions.ActionArgs[](1); | ||
Types.AssetAmount memory assetAmount = Types.AssetAmount({ | ||
sign: false, | ||
denomination: Types.AssetDenomination.Wei, | ||
ref: Types.AssetReference.Delta, | ||
value: _amountWei | ||
}); | ||
actions[0] = Actions.ActionArgs({ | ||
actionType : Actions.ActionType.Transfer, | ||
accountId : 0, | ||
amount : assetAmount, | ||
primaryMarketId : _marketId, | ||
secondaryMarketId : 0, | ||
otherAddress : address(0), | ||
otherAccountId : 1, | ||
data : bytes("") | ||
}); | ||
|
||
IDolomiteMargin(DOLOMITE_MARGIN).operate(accounts, actions); | ||
} | ||
|
||
function repayAllForBorrowPosition( | ||
uint _fromAccountIndex, | ||
uint _borrowAccountIndex, | ||
uint _marketId | ||
) external { | ||
Account.Info[] memory accounts = new Account.Info[](2); | ||
accounts[0] = Account.Info(msg.sender, _borrowAccountIndex); | ||
accounts[1] = Account.Info(msg.sender, _fromAccountIndex); | ||
|
||
Actions.ActionArgs[] memory actions = new Actions.ActionArgs[](1); | ||
// This works by transferring all debt from _borrowAccountIndex to _fromAccountIndex | ||
Types.AssetAmount memory assetAmount = Types.AssetAmount({ | ||
sign: false, | ||
denomination: Types.AssetDenomination.Wei, | ||
ref: Types.AssetReference.Target, | ||
value: 0 | ||
}); | ||
actions[0] = Actions.ActionArgs({ | ||
actionType : Actions.ActionType.Transfer, | ||
accountId : 0, | ||
amount : assetAmount, | ||
primaryMarketId : _marketId, | ||
secondaryMarketId : 0, | ||
otherAddress : address(0), | ||
otherAccountId : 1, | ||
data : bytes("") | ||
}); | ||
|
||
IDolomiteMargin(DOLOMITE_MARGIN).operate(accounts, actions); | ||
} | ||
} |
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
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
Oops, something went wrong.