@backt/protocol
Version:
Backt smart contracts implementation
103 lines (89 loc) • 3.34 kB
JavaScript
var _bignumber = require('bignumber.js');
var _bignumber2 = _interopRequireDefault(_bignumber);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
_bignumber2.default.config({ DECIMAL_PLACES: 30 });
/**
* @see ContractForDifference.sol calculateCollateralAmount() for the CFD
* Solidity implementation.
*
* Formulas are:
* Cl = depositBalanceLong + N * (P - S) / S
* Cs = depositBalanceShort - N * (P - S) / S
*/
var calculateCollateral = function calculateCollateral(_ref) {
var strikePrice = _ref.strikePrice,
marketPrice = _ref.marketPrice,
notionalAmount = _ref.notionalAmount,
depositBalance = _ref.depositBalance,
calcBuyerSide = _ref.calcBuyerSide;
var difference = notionalAmount.times(marketPrice.minus(strikePrice).dividedBy(strikePrice));
return calcBuyerSide ? depositBalance.plus(difference) : depositBalance.minus(difference);
};
/**
* @see ContractForDifference.sol cutOffPrice() for Solidity implementation.
*
* Base Formulas are:
* Buyer: 1.05 * S - depositBalanceLong * S / N
* Seller: 0.95 * S + depositBalanceShort * S / N
*/
var cutOffPrice = function cutOffPrice(_ref2) {
var strikePrice = _ref2.strikePrice,
notionalAmount = _ref2.notionalAmount,
depositBalance = _ref2.depositBalance,
buyerSide = _ref2.buyerSide;
var strikeFivePercent = strikePrice.times(buyerSide === true ? 1.05 : 0.95);
var difference = depositBalance.times(strikePrice).dividedBy(notionalAmount);
// can occur when buyer has leverage less then 1X (ie. deposits > notional)
if (buyerSide === true && strikeFivePercent.lt(difference)) {
return new _bignumber2.default(0);
}
return buyerSide === true ? strikeFivePercent.minus(difference) : strikeFivePercent.plus(difference);
};
/**
* Fee for creator is half an update fee PLUS 0.3% of the notional.
* @see ContractForDifference.sol creatorFee() for Solidity implementation.
*/
var creatorFee = function creatorFee(notionalAmount) {
return notionalAmount.times(0.003);
};
/**
* Fee for joiner - via either deposit() or buy()
* Fee is 0.5% of the notional.
* @see ContractForDifference.sol joinerFee() for Solidity implementation.
*/
var joinerFee = function joinerFee(notionalAmount) {
return notionalAmount.times(0.005);
};
/**
* Calculate new notional amount after a side has been sold at a new strike price.
*
* @see ContractForDifference.sol calculateNewNotional() for the CFD
* Solidity implementation.
*
* Formula is:
* N2 = N1 * S2 / S1
* Where:
* N1 = previous notional
* S1 = previous strike price
* S2 = sale strike price
*
* @param _oldNotional Existing notional.
* @param _oldStrikePrice Existing strike price.
* @param _newStrikePrice New / Sale strike price.
* @return newNotional Result of the calculation.
*
*/
var calculateNewNotional = function calculateNewNotional(_ref3) {
var oldNotional = _ref3.oldNotional,
oldStrikePrice = _ref3.oldStrikePrice,
newStrikePrice = _ref3.newStrikePrice;
return oldNotional.times(newStrikePrice.dividedBy(oldStrikePrice));
};
module.exports = {
calculateCollateral: calculateCollateral,
calculateNewNotional: calculateNewNotional,
cutOffPrice: cutOffPrice,
creatorFee: creatorFee,
joinerFee: joinerFee
};
;