Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Margintrading using limitorder #404

Merged
merged 27 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
955601c
Modified marginTrade()
smbsp Dec 14, 2021
d551c2f
Ran Prettier
smbsp Dec 14, 2021
51f2d08
Modified the logic to check position size
smbsp Dec 16, 2021
90afb73
Ran Prettier
smbsp Dec 16, 2021
b6e0701
Modified the legacy 'minReturn' parameter
smbsp Dec 16, 2021
fc5e448
Merge branch 'development' of https://github.com/DistributedCollectiv…
smbsp Jan 19, 2022
5ac2d54
Ran Prettier
smbsp Jan 19, 2022
9bb0ca2
Fixed failing test
smbsp Jan 19, 2022
d0f432b
Merge branch 'development' of https://github.com/DistributedCollectiv…
smbsp Jan 21, 2022
f3e4d75
Fixed Typo
smbsp Jan 21, 2022
9231303
Fixed script and updated addresses
smbsp Jan 21, 2022
d226c8c
Ran prettier
smbsp Jan 21, 2022
a4b17a9
Merge branch 'development' of https://github.com/DistributedCollectiv…
smbsp Jan 21, 2022
f5682fd
Merge branch 'development' of https://github.com/DistributedCollectiv…
smbsp Feb 2, 2022
d7ff47e
Ran prettier
smbsp Feb 2, 2022
fd84c97
Incorporating review comments
smbsp Feb 2, 2022
29f5194
Ran prettier
smbsp Feb 2, 2022
2d69e9d
Updated addresses
smbsp Feb 2, 2022
371b63b
Ran prettier
smbsp Feb 2, 2022
20ca32d
Merge branch 'development' of https://github.com/DistributedCollectiv…
smbsp Feb 3, 2022
6286c57
Ran prettier
smbsp Feb 3, 2022
897b8b0
Changed variable name
smbsp Feb 16, 2022
8a431e0
Merge branch 'development' of https://github.com/DistributedCollectiv…
smbsp Feb 16, 2022
3c67fbd
Ran Prettier
smbsp Feb 16, 2022
0a000da
Modified Prettier
smbsp Apr 20, 2022
29a104c
Merge branch 'development' of https://github.com/DistributedCollectiv…
smbsp Apr 20, 2022
6fbef0f
Ran prettier
smbsp Apr 20, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions contracts/connectors/loantoken/LoanTokenLogicStandard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ contract LoanTokenLogicStandard is LoanTokenLogicStorage {
* @param collateralTokenSent The amount of collateral tokens provided by the user.
* @param collateralTokenAddress The token address of collateral.
* @param trader The account that performs this trade.
* @param minReturn Minimum amount (position size) in the collateral tokens
* @param minEntryPrice Value of loan token in collateral.
* @param loanDataBytes Additional loan data (not in use for token swaps).
*
* @return New principal and new collateral added to trade.
Expand All @@ -312,7 +312,7 @@ contract LoanTokenLogicStandard is LoanTokenLogicStorage {
uint256 collateralTokenSent,
address collateralTokenAddress,
address trader,
uint256 minReturn, // minimum position size in the collateral tokens
uint256 minEntryPrice, // value of loan token in collateral
bytes memory loanDataBytes /// Arbitrary order data.
)
public
Expand All @@ -325,8 +325,6 @@ contract LoanTokenLogicStandard is LoanTokenLogicStorage {
{
_checkPause();

checkPriceDivergence(leverageAmount, loanTokenSent, collateralTokenSent, collateralTokenAddress, minReturn);

if (collateralTokenAddress == address(0)) {
collateralTokenAddress = wrbtcTokenAddress;
}
Expand Down Expand Up @@ -367,6 +365,7 @@ contract LoanTokenLogicStandard is LoanTokenLogicStorage {
sentAmounts[1] /// depositAmount
);

smbsp marked this conversation as resolved.
Show resolved Hide resolved
checkPriceDivergence(loanTokenSent.add(sentAmounts[1]), collateralTokenAddress, minEntryPrice);
require(_getAmountInRbtc(loanTokenAddress, sentAmounts[1]) > TINY_AMOUNT, "principal too small");

/// @dev Converting to initialMargin
Expand All @@ -393,7 +392,7 @@ contract LoanTokenLogicStandard is LoanTokenLogicStorage {
* @param collateralTokenSent The amount of collateral tokens provided by the user.
* @param collateralTokenAddress The token address of collateral.
* @param trader The account that performs this trade.
* @param minReturn Minimum position size in the collateral tokens
* @param minEntryPrice Value of loan token in collateral.
* @param affiliateReferrer The address of the referrer from affiliates program.
* @param loanDataBytes Additional loan data (not in use for token swaps).
*
Expand All @@ -406,7 +405,7 @@ contract LoanTokenLogicStandard is LoanTokenLogicStorage {
uint256 collateralTokenSent,
address collateralTokenAddress,
address trader,
uint256 minReturn, /// Minimum position size in the collateral tokens.
uint256 minEntryPrice, /// Value of loan token in collateral
address affiliateReferrer, /// The user was brought by the affiliate (referrer).
bytes calldata loanDataBytes /// Arbitrary order data.
)
Expand All @@ -427,7 +426,7 @@ contract LoanTokenLogicStandard is LoanTokenLogicStorage {
collateralTokenSent,
collateralTokenAddress,
trader,
minReturn,
minEntryPrice,
loanDataBytes
);
}
Expand Down Expand Up @@ -879,16 +878,23 @@ contract LoanTokenLogicStandard is LoanTokenLogicStorage {
}
}

/**
* @notice Check if entry price lies above a minimum
*
* @param loanTokenSent The amount of deposit.
* @param collateralTokenAddress The token address of collateral.
* @param minEntryPrice Value of loan token in collateral
* */
function checkPriceDivergence(
uint256 leverageAmount,
uint256 loanTokenSent,
uint256 collateralTokenSent,
address collateralTokenAddress,
uint256 minReturn
uint256 minEntryPrice
) public view {
(, uint256 estimatedCollateral, ) =
getEstimatedMarginDetails(leverageAmount, loanTokenSent, collateralTokenSent, collateralTokenAddress);
require(estimatedCollateral >= minReturn, "coll too low");
/// @dev See how many collateralTokens we would get if exchanging this amount of loan tokens to collateral tokens.
uint256 collateralTokensReceived =
ProtocolLike(sovrynContractAddress).getSwapExpectedReturn(loanTokenAddress, collateralTokenAddress, loanTokenSent);
uint256 collateralTokenAmount = (collateralTokensReceived.mul(WEI_PRECISION)).div(loanTokenSent);
require(collateralTokenAmount >= minEntryPrice, "entry price above the minimum");
smbsp marked this conversation as resolved.
Show resolved Hide resolved
}

/* Internal functions */
Expand Down
12 changes: 5 additions & 7 deletions contracts/interfaces/ILoanTokenModules.sol
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ interface ILoanTokenModules {
uint256 collateralTokenSent,
address collateralTokenAddress,
address trader,
uint256 minReturn, // minimum position size in the collateral tokens
uint256 minEntryPrice, // Value of loan token in collateral.
bytes calldata loanDataBytes /// Arbitrary order data.
)
external
Expand All @@ -105,9 +105,9 @@ interface ILoanTokenModules {
uint256 collateralTokenSent,
address collateralTokenAddress,
address trader,
uint256 minReturn, /// Minimum position size in the collateral tokens.
address affiliateReferrer, /// The user was brought by the affiliate (referrer).
bytes calldata loanDataBytes /// Arbitrary order data.
uint256 minEntryPrice, // Value of loan token in collateral.
address affiliateReferrer, // The user was brought by the affiliate (referrer).
bytes calldata loanDataBytes // Arbitrary order data.
)
external
payable
Expand Down Expand Up @@ -186,11 +186,9 @@ interface ILoanTokenModules {
) external view returns (uint256 borrowAmount);

function checkPriceDivergence(
uint256 leverageAmount,
uint256 loanTokenSent,
uint256 collateralTokenSent,
address collateralTokenAddress,
uint256 minReturn
uint256 minEntryPrice
) external view;

function getMaxEscrowAmount(uint256 leverageAmount) external view returns (uint256 maxEscrowAmount);
Expand Down
4 changes: 2 additions & 2 deletions scripts/contractInteraction/loan_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,8 @@ def replaceLoanTokenLogicOnAllContracts():

# Can use the same Loan Protocol Settings with the LoanTokenLogicLM
print("Registering Loan Protocol Settings Module to LoanTOkenLogicBeaconWrbtc")
data = logicContractWrbtc.registerLoanTokenModule.encode_input(loanTokenSettingsLowerAdmin.address)
sendWithMultisig(conf.contracts['multisig'], logicContractWrbtc.address, data, conf.acct)
data = loanTokenLogicBeaconWrbtc.registerLoanTokenModule.encode_input(loanTokenSettingsLowerAdmin.address)
sendWithMultisig(conf.contracts['multisig'], loanTokenLogicBeaconWrbtc.address, data, conf.acct)


def replaceLoanTokenLogic(loanTokenAddress, logicAddress):
Expand Down
4 changes: 2 additions & 2 deletions scripts/contractInteraction/testnet_contracts.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
"iXUSDProxy": "",
"iUSDTProxy": "",
"iBProProxy": "",
"LoanTokenLogicLM": "0xFB0b4DBCDf49802bE24Da19f66AE9a62942aEC19",
"LoanTokenLogicLM": "0x5B7f1AF3160957832761704f5Bc60aa082Af2f15",
"LoanTokenLogicBeaconLM": "0x961F75cb98BAaAA9Ce5efb707d2fe22E8CBdC074",
"LoanTokenLogicWrbtc": "0x3E686322ac52DF2380a96dC71264d296A8eBe289",
"LoanTokenLogicWrbtc": "0xB073ddD17e94ee5f7F8D4A90D9cd26787a3133F7",
"LoanTokenLogicBeaconWrbtc": "0x4A3d7163F9Bc3D3Be0a467452A880D3c25a83e70",
"sovrynProtocol": "0x25380305f223B32FDB844152abD2E82BC5Ad99c3",
"DoC": "0xCB46c0ddc60D18eFEB0E586C17Af6ea36452Dae0",
Expand Down
20 changes: 7 additions & 13 deletions tests/loan-token/TradingTestToken.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -645,17 +645,11 @@ contract("LoanTokenTrading", (accounts) => {
expect(args["positionSize"]).to.equal(sovryn_collateral_token_balance_diff);
});

it("checkPriceDivergence should success if min position size is less than or equal to collateral", async () => {
it("checkPriceDivergence should succeed if entry price is less than or equal to a minimum", async () => {
await set_demand_curve(loanToken);
await SUSD.transfer(loanToken.address, wei("500", "ether"));

await loanToken.checkPriceDivergence(
new BN(2).mul(oneEth),
wei("0.01", "ether"),
wei("0.01", "ether"),
RBTC.address,
wei("0.02", "ether")
);
await loanToken.checkPriceDivergence(wei("1", "ether"), RBTC.address, wei("0.0001", "ether"));
});

/// @dev For test coverage, it's required to perform a margin trade using WRBTC as collateral
Expand Down Expand Up @@ -692,7 +686,7 @@ contract("LoanTokenTrading", (accounts) => {
10000, // collateral token sent
RBTC.address, // collateralTokenAddress (RBTC)
accounts[1], // trader,
20000, // slippage
20000, // minEntryPrice
"0x", // loanDataBytes (only required with ether)
{ from: accounts[2] }
),
Expand All @@ -709,18 +703,18 @@ contract("LoanTokenTrading", (accounts) => {
oneEth.toString(), // collateral token sent
RBTC.address, // collateralTokenAddress (RBTC)
accounts[1], // trader,
oneEth.mul(new BN(2)).toString(), // slippage
200000, // minEntryPrice
"0x", // loanDataBytes (only required with ether)
{ from: accounts[2] }
);
});

it("checkPriceDivergence should revert if min position size is greater than collateral", async () => {
it("checkPriceDivergence should revert if entry price lies above a minimum", async () => {
await set_demand_curve(loanToken);

await expectRevert(
loanToken.checkPriceDivergence(new BN(2).mul(oneEth), wei("2", "ether"), 0, RBTC.address, wei("1", "ether")),
"coll too low"
loanToken.checkPriceDivergence(wei("2", "ether"), RBTC.address, wei("1", "ether")),
"entry price above the minimum"
);
});
});
Expand Down