Skip to content
This repository has been archived by the owner on Oct 24, 2023. It is now read-only.

Commit

Permalink
Leverage Contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
rahul-amun committed Jun 12, 2020
1 parent c919bec commit f1801e3
Show file tree
Hide file tree
Showing 57 changed files with 8,993 additions and 4,266 deletions.
6 changes: 0 additions & 6 deletions contracts/Abstract/InterfaceStorage.sol

This file was deleted.

2 changes: 1 addition & 1 deletion contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

- Creation and Redemption
- Daily Rebalance and Threshold Rebalance
- Composition PCF And Funding Rate [WIP]
- Composition PCF And Funding Rate

### List of Smart Contracts:

Expand Down
42 changes: 0 additions & 42 deletions contracts/TestStorageExample/Storage.sol

This file was deleted.

8 changes: 0 additions & 8 deletions contracts/TestStorageExample/StorageProxy.sol

This file was deleted.

17 changes: 0 additions & 17 deletions contracts/TestStorageExample/StorageVersionUpgradeExample.sol

This file was deleted.

8 changes: 0 additions & 8 deletions contracts/TestStorageExample/UpgradeableStorage.sol

This file was deleted.

1 change: 1 addition & 0 deletions contracts/leverage-tokens/.keep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Here will live leverage tokens smart contracts
28 changes: 28 additions & 0 deletions contracts/leverage-tokens/Abstract/InterfaceCalculator.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
pragma solidity ^0.5.0;


interface InterfaceCalculator {
function getTokensCreatedByCash(
uint256 mintingPrice,
uint256 cash,
uint256 gasFee
) external view returns (uint256 tokensCreated);

function getCashCreatedByTokens(
uint256 burningPrice,
uint256 elapsedTime,
uint256 tokens,
uint256 gasFee
) external view returns (uint256 stablecoinRedeemed);

function removeCurrentMintingFeeFromCash(uint256 _cash)
external
view
returns (uint256 cashAfterFee);

function removeMintingFeeFromCash(
uint256 _cash,
uint256 _mintingFee,
uint256 _minimumMintingFee
) external pure returns (uint256 cashAfterFee);
}
66 changes: 66 additions & 0 deletions contracts/leverage-tokens/Abstract/InterfaceStorageLeverage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
pragma solidity ^0.5.0;



interface InterfaceStorageLeverage {
function whitelistedAddresses(address) external view returns (bool);

function isPaused() external view returns (bool);

function isShutdown() external view returns (bool);

function tokenSwapManager() external view returns (address);

function bridge() external view returns (address);

function managementFee() external view returns (uint256);

function getExecutionPrice() external view returns (uint256);

function getMarkPrice() external view returns (uint256);

function getNotional() external view returns (uint256);

function getTokenValue() external view returns (uint256);

function getFundingRate() external view returns (uint256);

function getMintingFee(uint256 cash) external view returns (uint256);

function minimumMintingFee() external view returns (uint256);

function minRebalanceAmount() external view returns (uint8);

function delayedRedemptionsByUser(address) external view returns (uint256);

function setDelayedRedemptionsByUser(
uint256 amountToRedeem,
address whitelistedAddress
) external;

function setOrderByUser(
address whitelistedAddress,
string calldata orderType,
uint256 tokensGiven,
uint256 tokensRecieved,
uint256 mintingPrice,
uint256 orderIndex,
bool overwrite
) external;

function setAccounting(
uint256 _bestExecutionPrice,
uint256 _markPrice,
uint256 _notional,
uint256 _tokenValue,
uint256 _effectiveFundingRate
) external;

function setAccountingForLastActivityDay(
uint256 _bestExecutionPrice,
uint256 _markPrice,
uint256 _notional,
uint256 _tokenValue,
uint256 _effectiveFundingRate
) external;
}
118 changes: 118 additions & 0 deletions contracts/leverage-tokens/CalculatorLeverage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
pragma solidity ^0.5.0;

import "@openzeppelin/upgrades/contracts/Initializable.sol";
import "@openzeppelin/contracts-ethereum-package/contracts/math/SafeMath.sol";

import "../short-tokens/utils/Math.sol";
import "../short-tokens/utils/DateTimeLibrary.sol";
import "./Abstract/InterfaceStorageLeverage.sol";
import "../short-tokens/Abstract/InterfaceInverseToken.sol";


/**
* @dev uint256 are expected to use last 18 numbers as decimal points except when specifid differently in @params
*/
contract CalculatorLeverage is Initializable {
using SafeMath for uint256;

InterfaceStorageLeverage public persistentStorage;
InterfaceInverseToken public inverseToken;

function initialize(
address _persistentStorageAddress,
address _inverseTokenAddress
) public initializer {
persistentStorage = InterfaceStorageLeverage(_persistentStorageAddress);
inverseToken = InterfaceInverseToken(_inverseTokenAddress);
}

// Start fill in the values
// Creation 3x Long

function getTokensCreatedByCash(
uint256 mintingPrice,
uint256 cash,
uint256 gasFee
) public view returns (uint256 tokensCreated) {
// Cash Remove Gas Fee
// Cash Remove Minting Fee (Cash Proceeds)
// Cash Proceeds / Minting Price (# of Tokens to Mint)

uint256 cashAfterGas = DSMath.sub(cash, gasFee);
uint256 cashAfterFee = removeCurrentMintingFeeFromCash(cashAfterGas);

uint256 tokensMinted = DSMath.wdiv(cashAfterFee, mintingPrice);

return tokensMinted;
}

function getCashCreatedByTokens(
uint256 burningPrice,
uint256 elapsedTime,
uint256 tokens,
uint256 gasFee
) public view returns (uint256 stablecoinAfterFees) {
uint256 stablecoin = DSMath.wmul(tokens, burningPrice);
uint256 stablecoinAfterGas = DSMath.sub(stablecoin, gasFee);
uint256 stablecoinRedeemed = removeCurrentMintingFeeFromCash(
stablecoinAfterGas
);
uint256 managementFeeDaily = DSMath.wdiv(
persistentStorage.managementFee(),
365 ether
);
uint256 managementFeeHourly = DSMath.wdiv(
DSMath.wmul(managementFeeDaily, elapsedTime),
24 ether
);
uint256 normalizedManagementFee = DSMath.sub(
1 ether,
DSMath.wdiv(managementFeeHourly, 100 ether)
);

stablecoinAfterFees = DSMath.wmul(
stablecoinRedeemed,
normalizedManagementFee
);
}

function removeCurrentMintingFeeFromCash(uint256 _cash)
public
view
returns (uint256 cashAfterFee)
{
uint256 creationFee = persistentStorage.getMintingFee(_cash);
uint256 minimumMintingFee = persistentStorage.minimumMintingFee();
cashAfterFee = removeMintingFeeFromCash(
_cash,
creationFee,
minimumMintingFee
);
}

function removeMintingFeeFromCash(
uint256 _cash,
uint256 _mintingFee,
uint256 _minimumMintingFee
) public pure returns (uint256 cashAfterFee) {
uint256 creationFeeInCash = DSMath.wmul(_cash, _mintingFee);
if (_minimumMintingFee > creationFeeInCash) {
creationFeeInCash = _minimumMintingFee;
}
cashAfterFee = DSMath.sub(_cash, creationFeeInCash);
}

int256 constant WAD = 10**18;

function addInt256(int256 x, int256 y) internal pure returns (int256 z) {
require((z = x + y) >= x, "ds-math-add-overflow");
}

function mulInt256(int256 x, int256 y) internal pure returns (int256 z) {
require(y == 0 || (z = x * y) / y == x, "ds-math-mul-overflow");
}

function wmulInt256(int256 x, int256 y) internal pure returns (int256 z) {
z = addInt256(mulInt256(x, y), WAD / 2) / WAD;
}
}
Loading

0 comments on commit f1801e3

Please sign in to comment.