Contract 0x62d98d92ae026768c111fda6d905c2f6783a0c55

Contract Overview

Balance:
0 tCRO
Txn Hash Method
Block
From
To
Value [Txn Fee]
0x29b73c91cc56b4eebf1f44ecc0f2b325d40fa1c0a80e09d672ca57e6c2ef00ffSet Handler61306392022-11-17 2:28:5572 days 18 hrs ago0x111e164336e0d0a2b9554f96a2cca80680fba449 IN  0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO0.0928787143620
0x1d21414fac72d96f5335441fda57339d35e0ead6cdccb3e2eed3422ca063db90Set Handler61306102022-11-17 2:26:1772 days 18 hrs ago0x111e164336e0d0a2b9554f96a2cca80680fba449 IN  0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO0.0928787419370
0xfeccff2e7ff30698f97c857d610e6c14b900fcbbe951576b68247692c97ec76bSet Handler61305882022-11-17 2:24:1672 days 18 hrs ago0x111e164336e0d0a2b9554f96a2cca80680fba449 IN  0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO0.0928555211830
0x1b12f3c6e58b9ec3135755644cf49b044a9a39d3411f46747cb0b1955aacff1b0x6080604061305242022-11-17 2:18:2672 days 18 hrs ago0x111e164336e0d0a2b9554f96a2cca80680fba449 IN  Contract Creation0 tCRO1.9973962222840
[ Download CSV Export 
Latest 25 internal transaction
Parent Txn Hash Block From To Value
0x5a10a0a5d33e72f740ea24d7ba5ff0e73e4b7a001e0f86f9c595c0666dde606372762642023-01-28 13:59:577 hrs 8 mins ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0xea15d5d3345f5b8402cdd5d9d291c055829f69e1015b3ea9e1e2291ef56dced972760712023-01-28 13:42:267 hrs 26 mins ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0xba6772d7286facfcb1e8adde652162082143f33bc01159e5023c5b33cd83282872738372023-01-28 10:19:4810 hrs 48 mins ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0x1088a23b3d048ee7d957fc8a5b9a40d5333d864bae65668e274ae96d7d9a572f72738342023-01-28 10:19:3210 hrs 48 mins ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0x31a5d47e3fb5b01855357a940a69dfd95484411211bc49a885649b10103e12cc72738232023-01-28 10:18:3310 hrs 49 mins ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0xab251d01c7d672b1f8e04ec616e5dd6dfd1d8eb3e20db60408d75003db425d1872738212023-01-28 10:18:2210 hrs 50 mins ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0x041f33b9134e13e784b7f04b296e8936d1ac96e3c427b0f542a095c3a3cdea2772737052023-01-28 10:07:5211 hrs ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0x6fd88ca59406b89f28c06352e78fcfe5be3f226a4a5e9ecafdb02eb85a49166f72736912023-01-28 10:06:3511 hrs 1 min ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0xe96fde1413b608095194e80369e6a653b89df4cdf85ac9ae8f244844773333f672736772023-01-28 10:05:1911 hrs 3 mins ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0x364037a2665dcba2fefbc7eac159b23943e8d2ebaeb055323faffbee6f8b107072721022023-01-28 7:42:2213 hrs 26 mins ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0xdbd02913a9d674b7117f17faa16c9ddd6cf4d1d691d28c7390b0cffb96029fcd72720752023-01-28 7:39:5513 hrs 28 mins ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0xdbd02913a9d674b7117f17faa16c9ddd6cf4d1d691d28c7390b0cffb96029fcd72720752023-01-28 7:39:5513 hrs 28 mins ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0xb95774b05ede443f7f43e12be4ab2bcdfb77d97f7684a46cd9c2a969c4e9318f72720542023-01-28 7:38:0113 hrs 30 mins ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0x0ad40d710d7098320248beee7e4da69865f394d94c7fb7dbbf79f91ac49206a572720432023-01-28 7:37:0113 hrs 31 mins ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0x57458e87534226d76b8d6da1cbf1bb28b0195c7c04cbb9b3628aeabf23c2b92272687632023-01-28 2:39:2718 hrs 29 mins ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0x5cd7330dfb0495f65686a170769e4f913573e0aecdb72cb8db96b6b9148c5d1772533712023-01-27 3:22:561 day 17 hrs ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0x366c350f8ad35dbca93fbfd1752b728887fad4a4f98efcc6e112aadfbfb540c972533652023-01-27 3:22:231 day 17 hrs ago 0x63a64503779baa41ebc6f63ad1b37154041fe6eb 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0x4ab1f6adac8e6b45dc26bfc30585296ec38bf6d2b54a4ed982ffc17275e0935272527752023-01-27 2:28:501 day 18 hrs ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0xbb26182b76238431c630c3d874f0501836bfbbeb8e5229933558a42845ecb04072527652023-01-27 2:27:551 day 18 hrs ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0x561f22556c28cf39d603b530a7b5e2a7180a4018e6ec00c34a2b4d8a2d2ea3c172527492023-01-27 2:26:281 day 18 hrs ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0x561f22556c28cf39d603b530a7b5e2a7180a4018e6ec00c34a2b4d8a2d2ea3c172527492023-01-27 2:26:281 day 18 hrs ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0x4955b10765851acb0cd8e18c396ce842aca7230ef07401c96e9043874e2a1cec72527112023-01-27 2:23:011 day 18 hrs ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0x58ebbed30c353320b838c3d53fa136c45fa3e1d3e4fe02b697854b6b1722933c72527042023-01-27 2:22:231 day 18 hrs ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0x5f05827d4af62744c7ba56e1723d050df0826331dc5cfdb1bd6eba07a9dcb01872399152023-01-26 7:01:282 days 14 hrs ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
0x096af19f7a295d068ffb33bcf5235c521c31b00ffa218f74c5b47db4b2cb911672398712023-01-26 6:57:282 days 14 hrs ago 0x80da155776c91f9da570928981c837ad470c3700 0x62d98d92ae026768c111fda6d905c2f6783a0c550 tCRO
[ Download CSV Export 
Loading

Similar Match Source Code
Note: This contract matches the deployed ByteCode of the Source Code for Contract 0x2Eb64C8Da655224c012a4f53b7f182580D949D47

Contract Name:
ShortsTracker

Compiler Version
v0.6.12+commit.27d51765

Optimization Enabled:
Yes with 1 runs

Other Settings:
default evmVersion

Contract Source Code (Solidity Standard Json-Input format)

File 1 of 6 : Governable.sol
// SPDX-License-Identifier: MIT

pragma solidity 0.6.12;

contract Governable {
    address public gov;

    constructor() public {
        gov = msg.sender;
    }

    modifier onlyGov() {
        require(msg.sender == gov, "Governable: forbidden");
        _;
    }

    function setGov(address _gov) external onlyGov {
        gov = _gov;
    }
}

File 2 of 6 : IShortsTracker.sol
// SPDX-License-Identifier: MIT

pragma solidity 0.6.12;

interface IShortsTracker {
    function isGlobalShortDataReady() external view returns (bool);
    function globalShortAveragePrices(address _token) external view returns (uint256);
    function getNextGlobalShortData(
        address _account,
        address _collateralToken,
        address _indexToken,
        uint256 _nextPrice,
        uint256 _sizeDelta,
        bool _isIncrease
    ) external view returns (uint256, uint256);
    function updateGlobalShortData(
        address _account,
        address _collateralToken,
        address _indexToken,
        bool _isLong,
        uint256 _sizeDelta,
        uint256 _markPrice,
        bool _isIncrease
    ) external;
}

File 3 of 6 : IVault.sol
// SPDX-License-Identifier: MIT

pragma solidity 0.6.12;

import "./IVaultUtils.sol";

interface IVault {
    function isInitialized() external view returns (bool);
    function isSwapEnabled() external view returns (bool);
    function isLeverageEnabled() external view returns (bool);

    function setVaultUtils(IVaultUtils _vaultUtils) external;
    function setError(uint256 _errorCode, string calldata _error) external;

    function router() external view returns (address);
    function usdg() external view returns (address);
    function gov() external view returns (address);

    function whitelistedTokenCount() external view returns (uint256);
    function maxLeverage() external view returns (uint256);

    function minProfitTime() external view returns (uint256);
    function hasDynamicFees() external view returns (bool);
    function fundingInterval() external view returns (uint256);
    function totalTokenWeights() external view returns (uint256);
    function getTargetUsdgAmount(address _token) external view returns (uint256);

    function inManagerMode() external view returns (bool);
    function inPrivateLiquidationMode() external view returns (bool);

    function maxGasPrice() external view returns (uint256);

    function approvedRouters(address _account, address _router) external view returns (bool);
    function isLiquidator(address _account) external view returns (bool);
    function isManager(address _account) external view returns (bool);

    function minProfitBasisPoints(address _token) external view returns (uint256);
    function tokenBalances(address _token) external view returns (uint256);
    function lastFundingTimes(address _token) external view returns (uint256);

    function setMaxLeverage(uint256 _maxLeverage) external;
    function setInManagerMode(bool _inManagerMode) external;
    function setManager(address _manager, bool _isManager) external;
    function setIsSwapEnabled(bool _isSwapEnabled) external;
    function setIsLeverageEnabled(bool _isLeverageEnabled) external;
    function setMaxGasPrice(uint256 _maxGasPrice) external;
    function setUsdgAmount(address _token, uint256 _amount) external;
    function setBufferAmount(address _token, uint256 _amount) external;
    function setMaxGlobalShortSize(address _token, uint256 _amount) external;
    function setInPrivateLiquidationMode(bool _inPrivateLiquidationMode) external;
    function setLiquidator(address _liquidator, bool _isActive) external;

    function setFundingRate(uint256 _fundingInterval, uint256 _fundingRateFactor, uint256 _stableFundingRateFactor) external;

    function setFees(
        uint256 _taxBasisPoints,
        uint256 _stableTaxBasisPoints,
        uint256 _mintBurnFeeBasisPoints,
        uint256 _swapFeeBasisPoints,
        uint256 _stableSwapFeeBasisPoints,
        uint256 _marginFeeBasisPoints,
        uint256 _liquidationFeeUsd,
        uint256 _minProfitTime,
        bool _hasDynamicFees
    ) external;

    function setTokenConfig(
        address _token,
        uint256 _tokenDecimals,
        uint256 _redemptionBps,
        uint256 _minProfitBps,
        uint256 _maxUsdgAmount,
        bool _isStable,
        bool _isShortable
    ) external;

    function setPriceFeed(address _priceFeed) external;
    function withdrawFees(address _token, address _receiver) external returns (uint256);

    function directPoolDeposit(address _token) external;
    function buyUSDG(address _token, address _receiver) external returns (uint256);
    function sellUSDG(address _token, address _receiver) external returns (uint256);
    function swap(address _tokenIn, address _tokenOut, address _receiver) external returns (uint256);
    function increasePosition(address _account, address _collateralToken, address _indexToken, uint256 _sizeDelta, bool _isLong) external;
    function decreasePosition(address _account, address _collateralToken, address _indexToken, uint256 _collateralDelta, uint256 _sizeDelta, bool _isLong, address _receiver) external returns (uint256);
    function validateLiquidation(address _account, address _collateralToken, address _indexToken, bool _isLong, bool _raise) external view returns (uint256, uint256);
    function liquidatePosition(address _account, address _collateralToken, address _indexToken, bool _isLong, address _feeReceiver) external;
    function tokenToUsdMin(address _token, uint256 _tokenAmount) external view returns (uint256);

    function priceFeed() external view returns (address);
    function fundingRateFactor() external view returns (uint256);
    function stableFundingRateFactor() external view returns (uint256);
    function cumulativeFundingRates(address _token) external view returns (uint256);
    function getNextFundingRate(address _token) external view returns (uint256);
    function getFeeBasisPoints(address _token, uint256 _usdgDelta, uint256 _feeBasisPoints, uint256 _taxBasisPoints, bool _increment) external view returns (uint256);

    function liquidationFeeUsd() external view returns (uint256);
    function taxBasisPoints() external view returns (uint256);
    function stableTaxBasisPoints() external view returns (uint256);
    function mintBurnFeeBasisPoints() external view returns (uint256);
    function swapFeeBasisPoints() external view returns (uint256);
    function stableSwapFeeBasisPoints() external view returns (uint256);
    function marginFeeBasisPoints() external view returns (uint256);

    function allWhitelistedTokensLength() external view returns (uint256);
    function allWhitelistedTokens(uint256) external view returns (address);
    function whitelistedTokens(address _token) external view returns (bool);
    function stableTokens(address _token) external view returns (bool);
    function shortableTokens(address _token) external view returns (bool);
    function feeReserves(address _token) external view returns (uint256);
    function globalShortSizes(address _token) external view returns (uint256);
    function globalShortAveragePrices(address _token) external view returns (uint256);
    function maxGlobalShortSizes(address _token) external view returns (uint256);
    function tokenDecimals(address _token) external view returns (uint256);
    function tokenWeights(address _token) external view returns (uint256);
    function guaranteedUsd(address _token) external view returns (uint256);
    function poolAmounts(address _token) external view returns (uint256);
    function bufferAmounts(address _token) external view returns (uint256);
    function reservedAmounts(address _token) external view returns (uint256);
    function usdgAmounts(address _token) external view returns (uint256);
    function maxUsdgAmounts(address _token) external view returns (uint256);
    function getRedemptionAmount(address _token, uint256 _usdgAmount) external view returns (uint256);
    function getMaxPrice(address _token) external view returns (uint256);
    function getMinPrice(address _token) external view returns (uint256);

    function getDelta(address _indexToken, uint256 _size, uint256 _averagePrice, bool _isLong, uint256 _lastIncreasedTime) external view returns (bool, uint256);
    function getPosition(address _account, address _collateralToken, address _indexToken, bool _isLong) external view returns (uint256, uint256, uint256, uint256, uint256, uint256, bool, uint256);
}

File 4 of 6 : IVaultUtils.sol
// SPDX-License-Identifier: MIT

pragma solidity 0.6.12;

interface IVaultUtils {
    function updateCumulativeFundingRate(address _collateralToken, address _indexToken) external returns (bool);
    function validateIncreasePosition(address _account, address _collateralToken, address _indexToken, uint256 _sizeDelta, bool _isLong) external view;
    function validateDecreasePosition(address _account, address _collateralToken, address _indexToken, uint256 _collateralDelta, uint256 _sizeDelta, bool _isLong, address _receiver) external view;
    function validateLiquidation(address _account, address _collateralToken, address _indexToken, bool _isLong, bool _raise) external view returns (uint256, uint256);
    function getEntryFundingRate(address _collateralToken, address _indexToken, bool _isLong) external view returns (uint256);
    function getPositionFee(address _account, address _collateralToken, address _indexToken, bool _isLong, uint256 _sizeDelta) external view returns (uint256);
    function getFundingFee(address _account, address _collateralToken, address _indexToken, bool _isLong, uint256 _size, uint256 _entryFundingRate) external view returns (uint256);
    function getBuyUsdgFeeBasisPoints(address _token, uint256 _usdgAmount) external view returns (uint256);
    function getSellUsdgFeeBasisPoints(address _token, uint256 _usdgAmount) external view returns (uint256);
    function getSwapFeeBasisPoints(address _tokenIn, address _tokenOut, uint256 _usdgAmount) external view returns (uint256);
    function getFeeBasisPoints(address _token, uint256 _usdgDelta, uint256 _feeBasisPoints, uint256 _taxBasisPoints, bool _increment) external view returns (uint256);
}

File 5 of 6 : ShortsTracker.sol
// SPDX-License-Identifier: MIT

pragma solidity 0.6.12;

import "../libraries/math/SafeMath.sol";

import "../access/Governable.sol";
import "./interfaces/IShortsTracker.sol";
import "./interfaces/IVault.sol";

contract ShortsTracker is Governable, IShortsTracker {
    using SafeMath for uint256;

    event GlobalShortDataUpdated(address indexed token, uint256 globalShortSize, uint256 globalShortAveragePrice);

    uint256 public constant MAX_INT256 = uint256(type(int256).max);

    IVault public vault;

    mapping (address => bool) public isHandler;
    mapping (bytes32 => bytes32) public data;

    mapping (address => uint256) override public globalShortAveragePrices;
    bool override public isGlobalShortDataReady;

    modifier onlyHandler() {
        require(isHandler[msg.sender], "ShortsTracker: forbidden");
        _;
    }

    constructor(address _vault) public {
        vault = IVault(_vault);
    }

    function setHandler(address _handler, bool _isActive) external onlyGov {
        require(_handler != address(0), "ShortsTracker: invalid _handler");
        isHandler[_handler] = _isActive;
    }

    function _setGlobalShortAveragePrice(address _token, uint256 _averagePrice) internal {
        globalShortAveragePrices[_token] = _averagePrice;
    }

    function setIsGlobalShortDataReady(bool value) external onlyGov {
        isGlobalShortDataReady = value;
    }

    function updateGlobalShortData(
        address _account,
        address _collateralToken,
        address _indexToken,
        bool _isLong,
        uint256 _sizeDelta,
        uint256 _markPrice,
        bool _isIncrease
    ) override external onlyHandler {
        if (_isLong || _sizeDelta == 0) {
            return;
        }

        if (!isGlobalShortDataReady) {
            return;
        }

        (uint256 globalShortSize, uint256 globalShortAveragePrice) = getNextGlobalShortData(
            _account,
            _collateralToken,
            _indexToken,
            _markPrice,
            _sizeDelta,
            _isIncrease
        );
        _setGlobalShortAveragePrice(_indexToken, globalShortAveragePrice);

        emit GlobalShortDataUpdated(_indexToken, globalShortSize, globalShortAveragePrice);
    }

    function getGlobalShortDelta(address _token) public view returns (bool, uint256) {
        uint256 size = vault.globalShortSizes(_token);
        uint256 averagePrice = globalShortAveragePrices[_token];
        if (size == 0) { return (false, 0); }

        uint256 nextPrice = IVault(vault).getMaxPrice(_token);
        uint256 priceDelta = averagePrice > nextPrice ? averagePrice.sub(nextPrice) : nextPrice.sub(averagePrice);
        uint256 delta = size.mul(priceDelta).div(averagePrice);
        bool hasProfit = averagePrice > nextPrice;

        return (hasProfit, delta);
    }


    function setInitData(address[] calldata _tokens, uint256[] calldata _averagePrices) external onlyGov {
        require(!isGlobalShortDataReady, "ShortsTracker: already migrated");

        for (uint256 i = 0; i < _tokens.length; i++) {
            globalShortAveragePrices[_tokens[i]] = _averagePrices[i];
        }
        isGlobalShortDataReady = true;
    }

    function getNextGlobalShortData(
        address _account,
        address _collateralToken,
        address _indexToken,
        uint256 _nextPrice,
        uint256 _sizeDelta,
        bool _isIncrease
    ) override public view returns (uint256, uint256) {
        int256 realisedPnl = getRealisedPnl(_account,_collateralToken, _indexToken, _sizeDelta, _isIncrease);
        uint256 averagePrice = globalShortAveragePrices[_indexToken];
        uint256 priceDelta = averagePrice > _nextPrice ? averagePrice.sub(_nextPrice) : _nextPrice.sub(averagePrice);

        uint256 nextSize;
        uint256 delta;
        // avoid stack to deep
        {
            uint256 size = vault.globalShortSizes(_indexToken);
            nextSize = _isIncrease ? size.add(_sizeDelta) : size.sub(_sizeDelta);

            if (nextSize == 0) {
                return (0, 0);
            }

            if (averagePrice == 0) {
                return (nextSize, _nextPrice);
            }

            delta = size.mul(priceDelta).div(averagePrice);
        }

        uint256 nextAveragePrice = _getNextGlobalAveragePrice(
            averagePrice,
            _nextPrice,
            nextSize,
            delta,
            realisedPnl
        );

        return (nextSize, nextAveragePrice);
    }

    function getRealisedPnl(
        address _account,
        address _collateralToken,
        address _indexToken,
        uint256 _sizeDelta,
        bool _isIncrease
    ) public view returns (int256) {
        if (_isIncrease) {
            return 0;
        }

        IVault _vault = vault;
        (uint256 size, /*uint256 collateral*/, uint256 averagePrice, , , , , uint256 lastIncreasedTime) = _vault.getPosition(_account, _collateralToken, _indexToken, false);

        (bool hasProfit, uint256 delta) = _vault.getDelta(_indexToken, size, averagePrice, false, lastIncreasedTime);
        // get the proportional change in pnl
        uint256 adjustedDelta = _sizeDelta.mul(delta).div(size);
        require(adjustedDelta < MAX_INT256, "ShortsTracker: overflow");
        return hasProfit ? int256(adjustedDelta) : -int256(adjustedDelta);
    }

    function _getNextGlobalAveragePrice(
        uint256 _averagePrice,
        uint256 _nextPrice,
        uint256 _nextSize,
        uint256 _delta,
        int256 _realisedPnl
    ) public pure returns (uint256) {
        (bool hasProfit, uint256 nextDelta) = _getNextDelta(_delta, _averagePrice, _nextPrice, _realisedPnl);

        uint256 nextAveragePrice = _nextPrice
            .mul(_nextSize)
            .div(hasProfit ? _nextSize.sub(nextDelta) : _nextSize.add(nextDelta));

        return nextAveragePrice;
    }

    function _getNextDelta(
        uint256 _delta,
        uint256 _averagePrice,
        uint256 _nextPrice,
        int256 _realisedPnl
    ) internal pure returns (bool, uint256) {
        // global delta 10000, realised pnl 1000 => new pnl 9000
        // global delta 10000, realised pnl -1000 => new pnl 11000
        // global delta -10000, realised pnl 1000 => new pnl -11000
        // global delta -10000, realised pnl -1000 => new pnl -9000
        // global delta 10000, realised pnl 11000 => new pnl -1000 (flips sign)
        // global delta -10000, realised pnl -11000 => new pnl 1000 (flips sign)

        bool hasProfit = _averagePrice > _nextPrice;
        if (hasProfit) {
            // global shorts pnl is positive
            if (_realisedPnl > 0) {
                if (uint256(_realisedPnl) > _delta) {
                    _delta = uint256(_realisedPnl).sub(_delta);
                    hasProfit = false;
                } else {
                    _delta = _delta.sub(uint256(_realisedPnl));
                }
            } else {
                _delta = _delta.add(uint256(-_realisedPnl));
            }

            return (hasProfit, _delta);
        }

        if (_realisedPnl > 0) {
            _delta = _delta.add(uint256(_realisedPnl));
        } else {
            if (uint256(-_realisedPnl) > _delta) {
                _delta = uint256(-_realisedPnl).sub(_delta);
                hasProfit = true;
            } else {
                _delta = _delta.sub(uint256(-_realisedPnl));
            }
        }
        return (hasProfit, _delta);
    }
}

File 6 of 6 : SafeMath.sol
// SPDX-License-Identifier: MIT

pragma solidity 0.6.12;

/**
 * @dev Wrappers over Solidity's arithmetic operations with added overflow
 * checks.
 *
 * Arithmetic operations in Solidity wrap on overflow. This can easily result
 * in bugs, because programmers usually assume that an overflow raises an
 * error, which is the standard behavior in high level programming languages.
 * `SafeMath` restores this intuition by reverting the transaction when an
 * operation overflows.
 *
 * Using this library instead of the unchecked operations eliminates an entire
 * class of bugs, so it's recommended to use it always.
 */
library SafeMath {
    /**
     * @dev Returns the addition of two unsigned integers, reverting on
     * overflow.
     *
     * Counterpart to Solidity's `+` operator.
     *
     * Requirements:
     *
     * - Addition cannot overflow.
     */
    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        require(c >= a, "SafeMath: addition overflow");

        return c;
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, reverting on
     * overflow (when the result is negative).
     *
     * Counterpart to Solidity's `-` operator.
     *
     * Requirements:
     *
     * - Subtraction cannot overflow.
     */
    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        return sub(a, b, "SafeMath: subtraction overflow");
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, reverting with custom message on
     * overflow (when the result is negative).
     *
     * Counterpart to Solidity's `-` operator.
     *
     * Requirements:
     *
     * - Subtraction cannot overflow.
     */
    function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b <= a, errorMessage);
        uint256 c = a - b;

        return c;
    }

    /**
     * @dev Returns the multiplication of two unsigned integers, reverting on
     * overflow.
     *
     * Counterpart to Solidity's `*` operator.
     *
     * Requirements:
     *
     * - Multiplication cannot overflow.
     */
    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
        // benefit is lost if 'b' is also tested.
        // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
        if (a == 0) {
            return 0;
        }

        uint256 c = a * b;
        require(c / a == b, "SafeMath: multiplication overflow");

        return c;
    }

    /**
     * @dev Returns the integer division of two unsigned integers. Reverts on
     * division by zero. The result is rounded towards zero.
     *
     * Counterpart to Solidity's `/` operator. Note: this function uses a
     * `revert` opcode (which leaves remaining gas untouched) while Solidity
     * uses an invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        return div(a, b, "SafeMath: division by zero");
    }

    /**
     * @dev Returns the integer division of two unsigned integers. Reverts with custom message on
     * division by zero. The result is rounded towards zero.
     *
     * Counterpart to Solidity's `/` operator. Note: this function uses a
     * `revert` opcode (which leaves remaining gas untouched) while Solidity
     * uses an invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b > 0, errorMessage);
        uint256 c = a / b;
        // assert(a == b * c + a % b); // There is no case in which this doesn't hold

        return c;
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
     * Reverts when dividing by zero.
     *
     * Counterpart to Solidity's `%` operator. This function uses a `revert`
     * opcode (which leaves remaining gas untouched) while Solidity uses an
     * invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function mod(uint256 a, uint256 b) internal pure returns (uint256) {
        return mod(a, b, "SafeMath: modulo by zero");
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
     * Reverts with custom message when dividing by zero.
     *
     * Counterpart to Solidity's `%` operator. This function uses a `revert`
     * opcode (which leaves remaining gas untouched) while Solidity uses an
     * invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     *
     * - The divisor cannot be zero.
     */
    function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b != 0, errorMessage);
        return a % b;
    }
}

Settings
{
  "optimizer": {
    "enabled": true,
    "runs": 1
  },
  "outputSelection": {
    "*": {
      "*": [
        "evm.bytecode",
        "evm.deployedBytecode",
        "devdoc",
        "userdoc",
        "metadata",
        "abi"
      ]
    }
  },
  "libraries": {}
}

Contract ABI

[{"inputs":[{"internalType":"address","name":"_vault","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"token","type":"address"},{"indexed":false,"internalType":"uint256","name":"globalShortSize","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"globalShortAveragePrice","type":"uint256"}],"name":"GlobalShortDataUpdated","type":"event"},{"inputs":[],"name":"MAX_INT256","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_averagePrice","type":"uint256"},{"internalType":"uint256","name":"_nextPrice","type":"uint256"},{"internalType":"uint256","name":"_nextSize","type":"uint256"},{"internalType":"uint256","name":"_delta","type":"uint256"},{"internalType":"int256","name":"_realisedPnl","type":"int256"}],"name":"_getNextGlobalAveragePrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"data","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_token","type":"address"}],"name":"getGlobalShortDelta","outputs":[{"internalType":"bool","name":"","type":"bool"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"},{"internalType":"address","name":"_collateralToken","type":"address"},{"internalType":"address","name":"_indexToken","type":"address"},{"internalType":"uint256","name":"_nextPrice","type":"uint256"},{"internalType":"uint256","name":"_sizeDelta","type":"uint256"},{"internalType":"bool","name":"_isIncrease","type":"bool"}],"name":"getNextGlobalShortData","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"},{"internalType":"address","name":"_collateralToken","type":"address"},{"internalType":"address","name":"_indexToken","type":"address"},{"internalType":"uint256","name":"_sizeDelta","type":"uint256"},{"internalType":"bool","name":"_isIncrease","type":"bool"}],"name":"getRealisedPnl","outputs":[{"internalType":"int256","name":"","type":"int256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"globalShortAveragePrices","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"gov","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"isGlobalShortDataReady","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"isHandler","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_gov","type":"address"}],"name":"setGov","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_handler","type":"address"},{"internalType":"bool","name":"_isActive","type":"bool"}],"name":"setHandler","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address[]","name":"_tokens","type":"address[]"},{"internalType":"uint256[]","name":"_averagePrices","type":"uint256[]"}],"name":"setInitData","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bool","name":"value","type":"bool"}],"name":"setIsGlobalShortDataReady","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"},{"internalType":"address","name":"_collateralToken","type":"address"},{"internalType":"address","name":"_indexToken","type":"address"},{"internalType":"bool","name":"_isLong","type":"bool"},{"internalType":"uint256","name":"_sizeDelta","type":"uint256"},{"internalType":"uint256","name":"_markPrice","type":"uint256"},{"internalType":"bool","name":"_isIncrease","type":"bool"}],"name":"updateGlobalShortData","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"vault","outputs":[{"internalType":"contract IVault","name":"","type":"address"}],"stateMutability":"view","type":"function"}]

608060405234801561001057600080fd5b506040516111483803806111488339818101604052602081101561003357600080fd5b5051600080546001600160a01b03199081163317909155600180546001600160a01b03909316929091169190911790556110d6806100726000396000f3fe608060405234801561001057600080fd5b50600436106100d05760003560e01c80630147fb0c146100d5578063122e7b071461010457806312d43a511461010c5780633d30cabf1461013057806346ea87af146101515780635886b7111461018b57806362749803146101c05780639a11178f146101e65780639cb7de4b146101ee5780639cdeb5931461021c578063a83b75fd1461027f578063b364accb146102c3578063bbd9718714610304578063cfad57a2146103c2578063f3238cec146103e8578063fbfa77cf1461043a575b600080fd5b6100f2600480360360208110156100eb57600080fd5b5035610442565b60408051918252519081900360200190f35b6100f2610454565b61011461045f565b604080516001600160a01b039092168252519081900360200190f35b61014f6004803603602081101561014657600080fd5b5035151561046e565b005b6101776004803603602081101561016757600080fd5b50356001600160a01b03166104ce565b604080519115158252519081900360200190f35b6100f2600480360360a08110156101a157600080fd5b50803590602081013590604081013590606081013590608001356104e3565b6100f2600480360360208110156101d657600080fd5b50356001600160a01b031661053a565b61017761054c565b61014f6004803603604081101561020457600080fd5b506001600160a01b0381351690602001351515610555565b610266600480360360c081101561023257600080fd5b506001600160a01b03813581169160208101358216916040820135169060608101359060808101359060a001351515610628565b6040805192835260208301919091528051918290030190f35b6100f2600480360360a081101561029557600080fd5b506001600160a01b03813581169160208101358216916040820135169060608101359060800135151561077c565b6102e9600480360360208110156102d957600080fd5b50356001600160a01b031661095c565b60408051921515835260208301919091528051918290030190f35b61014f6004803603604081101561031a57600080fd5b810190602081018135600160201b81111561033457600080fd5b82018360208201111561034657600080fd5b803590602001918460208302840111600160201b8311171561036757600080fd5b919390929091602081019035600160201b81111561038457600080fd5b82018360208201111561039657600080fd5b803590602001918460208302840111600160201b831117156103b757600080fd5b509092509050610acf565b61014f600480360360208110156103d857600080fd5b50356001600160a01b0316610be1565b61014f600480360360e08110156103fe57600080fd5b506001600160a01b038135811691602081013582169160408201351690606081013515159060808101359060a08101359060c001351515610c50565b610114610d40565b60036020526000908152604090205481565b6001600160ff1b0381565b6000546001600160a01b031681565b6000546001600160a01b031633146104bb576040805162461bcd60e51b81526020600482015260156024820152600080516020611060833981519152604482015290519081900360640190fd5b6005805460ff1916911515919091179055565b60026020526000908152604090205460ff1681565b60008060006104f485898987610d4f565b91509150600061052b836105115761050c8884610e0c565b61051b565b61051b8884610e6d565b6105258a8a610eaf565b90610f08565b93505050505b95945050505050565b60046020526000908152604090205481565b60055460ff1681565b6000546001600160a01b031633146105a2576040805162461bcd60e51b81526020600482015260156024820152600080516020611060833981519152604482015290519081900360640190fd5b6001600160a01b0382166105fd576040805162461bcd60e51b815260206004820152601f60248201527f53686f727473547261636b65723a20696e76616c6964205f68616e646c657200604482015290519081900360640190fd5b6001600160a01b03919091166000908152600260205260409020805460ff1916911515919091179055565b600080600061063a898989888861077c565b6001600160a01b03881660009081526004602052604081205491925087821161066c576106678883610e6d565b610676565b6106768289610e6d565b6001546040805163114f1b5560e31b81526001600160a01b038d811660048301529151939450600093849384931691638a78daa8916024808301926020929190829003018186803b1580156106ca57600080fd5b505afa1580156106de573d6000803e3d6000fd5b505050506040513d60208110156106f457600080fd5b505190508861070c57610707818b610e6d565b610716565b610716818b610e0c565b92508261072f5760008097509750505050505050610771565b8461074557828b97509750505050505050610771565b610753856105258387610eaf565b9150506000610765858c85858a6104e3565b92975091955050505050505b965096945050505050565b6000811561078c57506000610531565b60015460408051634a3f088d60e01b81526001600160a01b038981166004830152888116602483015287811660448301526000606483018190529251931692829182918591634a3f088d91608480830192610100929190829003018186803b1580156107f757600080fd5b505afa15801561080b573d6000803e3d6000fd5b505050506040513d61010081101561082257600080fd5b50805160408083015160e0909301518151635c07eaab60e01b81526001600160a01b038d811660048301526024820185905260448201869052600060648301819052608483018490528451959950959750919550849391891692635c07eaab9260a480840193829003018186803b15801561089c57600080fd5b505afa1580156108b0573d6000803e3d6000fd5b505050506040513d60408110156108c657600080fd5b508051602090910151909250905060006108e4866105258c85610eaf565b90506001600160ff1b03811061093b576040805162461bcd60e51b815260206004820152601760248201527653686f727473547261636b65723a206f766572666c6f7760481b604482015290519081900360640190fd5b82610949578060000361094b565b805b9d9c50505050505050505050505050565b6001546040805163114f1b5560e31b81526001600160a01b038481166004830152915160009384938493911691638a78daa891602480820192602092909190829003018186803b1580156109af57600080fd5b505afa1580156109c3573d6000803e3d6000fd5b505050506040513d60208110156109d957600080fd5b50516001600160a01b03851660009081526004602052604090205490915081610a0a57600080935093505050610aca565b60015460408051637092736960e11b81526001600160a01b0388811660048301529151600093929092169163e124e6d291602480820192602092909190829003018186803b158015610a5b57600080fd5b505afa158015610a6f573d6000803e3d6000fd5b505050506040513d6020811015610a8557600080fd5b505190506000818311610aa157610a9c8284610e6d565b610aab565b610aab8383610e6d565b90506000610abd846105258785610eaf565b9290931195509093505050505b915091565b6000546001600160a01b03163314610b1c576040805162461bcd60e51b81526020600482015260156024820152600080516020611060833981519152604482015290519081900360640190fd5b60055460ff1615610b74576040805162461bcd60e51b815260206004820152601f60248201527f53686f727473547261636b65723a20616c7265616479206d6967726174656400604482015290519081900360640190fd5b60005b83811015610bcd57828282818110610b8b57fe5b9050602002013560046000878785818110610ba257fe5b602090810292909201356001600160a01b031683525081019190915260400160002055600101610b77565b50506005805460ff19166001179055505050565b6000546001600160a01b03163314610c2e576040805162461bcd60e51b81526020600482015260156024820152600080516020611060833981519152604482015290519081900360640190fd5b600080546001600160a01b0319166001600160a01b0392909216919091179055565b3360009081526002602052604090205460ff16610caf576040805162461bcd60e51b815260206004820152601860248201527729b437b93a39aa3930b1b5b2b91d103337b93134b23232b760411b604482015290519081900360640190fd5b8380610cb9575082155b15610cc357610d37565b60055460ff16610cd257610d37565b600080610ce3898989878988610628565b91509150610cf18782610f47565b604080518381526020810183905281516001600160a01b038a16927fd6137be44db128ffcf1ea1821dbe8f889f67f949be7656c2d8acba2a4a891a02928290030190a250505b50505050505050565b6001546001600160a01b031681565b6000808385118015610dae576000841315610d935786841115610d8157610d768488610e6d565b965060009050610d8e565b610d8b8785610e6d565b96505b610da4565b610da1876000869003610e0c565b96505b9150859050610e03565b6000841315610dc857610dc18785610e0c565b9650610dfd565b86846000031115610dec57610de1600085900388610e6d565b965060019050610dfd565b610dfa876000869003610e6d565b96505b91508590505b94509492505050565b600082820183811015610e64576040805162461bcd60e51b815260206004820152601b60248201527a536166654d6174683a206164646974696f6e206f766572666c6f7760281b604482015290519081900360640190fd5b90505b92915050565b6000610e6483836040518060400160405280601e81526020017f536166654d6174683a207375627472616374696f6e206f766572666c6f770000815250610f63565b600082610ebe57506000610e67565b82820282848281610ecb57fe5b0414610e645760405162461bcd60e51b81526004018080602001828103825260218152602001806110806021913960400191505060405180910390fd5b6000610e6483836040518060400160405280601a815260200179536166654d6174683a206469766973696f6e206279207a65726f60301b815250610ffa565b6001600160a01b03909116600090815260046020526040902055565b60008184841115610ff25760405162461bcd60e51b81526004018080602001828103825283818151815260200191508051906020019080838360005b83811015610fb7578181015183820152602001610f9f565b50505050905090810190601f168015610fe45780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b505050900390565b600081836110495760405162461bcd60e51b8152602060048201818152835160248401528351909283926044909101919085019080838360008315610fb7578181015183820152602001610f9f565b50600083858161105557fe5b049594505050505056fe476f7665726e61626c653a20666f7262696464656e0000000000000000000000536166654d6174683a206d756c7469706c69636174696f6e206f766572666c6f77a26469706673582212208a41a7230d78df959f007286c30e25b24fd39caabcdfbb4b9835d33babc9e34364736f6c634300060c00330000000000000000000000004b789d998ff47e06e7e4aed3045162d50dad76ab

Block Transaction Gas Used Reward
Age Block Fee Address BC Fee Address Voting Power Jailed Incoming
Block Uncle Number Difficulty Gas Used Reward
Loading