From 85a58c1784f478111dc36f6af6c113150af3a596 Mon Sep 17 00:00:00 2001 From: DE Date: Mon, 26 Sep 2022 16:18:34 +0700 Subject: [PATCH 1/2] update function getLiquidityInPipRange --- contracts/protocol/PositionManager.sol | 25 +-- .../libraries/position/LiquidityBitmap.sol | 157 +++++++++++++----- 2 files changed, 132 insertions(+), 50 deletions(-) diff --git a/contracts/protocol/PositionManager.sol b/contracts/protocol/PositionManager.sol index b8d9d5e..5f111c9 100644 --- a/contracts/protocol/PositionManager.sol +++ b/contracts/protocol/PositionManager.sol @@ -91,18 +91,18 @@ contract PositionManager is } - function initializePip() external { - // initialize singleSlot.pip - require(!_isInitiatedPip && singleSlot.pip == 0, "initialized"); - uint256 _price = priceFeed.getPrice(priceFeedKey); - uint128 _pip = uint128(_price * basisPoint/PRICE_FEED_TOKEN_DIGIT); - singleSlot.pip = _pip; - reserveSnapshots.push( - ReserveSnapshot(_pip, _now(), _blocknumber()) - ); - _isInitiatedPip = true; - emit ReserveSnapshotted(_pip, _now()); - } +// function initializePip() external { +// // initialize singleSlot.pip +// require(!_isInitiatedPip && singleSlot.pip == 0, "initialized"); +// uint256 _price = priceFeed.getPrice(priceFeedKey); +// uint128 _pip = uint128(_price * basisPoint/PRICE_FEED_TOKEN_DIGIT); +// singleSlot.pip = _pip; +// reserveSnapshots.push( +// ReserveSnapshot(_pip, _now(), _blocknumber()) +// ); +// _isInitiatedPip = true; +// emit ReserveSnapshotted(_pip, _now()); +// } function updatePartialFilledOrder(uint128 _pip, uint64 _orderId) public @@ -530,6 +530,7 @@ contract PositionManager is uint128(_dataLength) ); allInitializedPips = liquidityBitmap.findAllLiquidityInMultipleWords( + maxWordRangeForMarketOrder, _fromPip, _dataLength, _toHigher diff --git a/contracts/protocol/libraries/position/LiquidityBitmap.sol b/contracts/protocol/libraries/position/LiquidityBitmap.sol index 95c322d..5aa6ee8 100644 --- a/contracts/protocol/libraries/position/LiquidityBitmap.sol +++ b/contracts/protocol/libraries/position/LiquidityBitmap.sol @@ -129,48 +129,98 @@ library LiquidityBitmap { // find all pip has liquidity in multiple word function findAllLiquidityInMultipleWords( mapping(uint128 => uint256) storage _self, + uint128 _maxWords, uint128 _startPip, uint256 _dataLength, - bool _toHigher + bool _lte ) internal view returns (uint128[] memory) { uint128 startWord = _startPip >> 8; uint128 index = 0; uint128[] memory allPip = new uint128[](uint128(_dataLength)); - if (!_toHigher) { - for (uint128 i = startWord; i >= (startWord > 1000 ? startWord - 1000 : 1); i--) { - if (_self[i] != 0) { - uint128 next; - next = findHasLiquidityInOneWords( - _self, - i < startWord ? 256 * i + 255 : _startPip, - true - ); - if (next != 0) { - allPip[index] = next; - index++; - if (index >= _dataLength) break; - } - while (true) { + if (_lte) { + uint128 next; + if (startWord != 0) { + uint128 i = startWord; + for ( + i; + i > (startWord < _maxWords ? 0 : startWord - _maxWords); + i-- + ) { + if (_self[i] != 0) { next = findHasLiquidityInOneWords( _self, - next - 1, + i < startWord ? 256 * i + 255 : _startPip, true ); - if (next != 0 && index <= _dataLength) { + if (next != 0) { allPip[index] = next; index++; - if (index >= _dataLength) break; - } else { - break; + _dataLength--; + if (_dataLength == 0) return allPip; + ( + allPip, + index, + _dataLength + ) = findAllLiquidityInOneWord( + _self, + next, + allPip, + index, + _dataLength, + true + ); } } } - if (index == _dataLength) return allPip; + if (i == 0 && _self[0] != 0) { + next = findHasLiquidityInOneWords(_self, 255, true); + if (next != 0) { + allPip[index] = next; + index++; + _dataLength--; + if (_dataLength == 0) return allPip; + ( + allPip, + index, + _dataLength + ) = findAllLiquidityInOneWord( + _self, + next, + allPip, + index, + _dataLength, + true + ); + } + } + } else { + if (_self[startWord] != 0) { + next = findHasLiquidityInOneWords(_self, _startPip, true); + if (next != 0) { + allPip[index] = next; + index++; + _dataLength--; + if (_dataLength == 0) return allPip; + ( + allPip, + index, + _dataLength + ) = findAllLiquidityInOneWord( + _self, + next, + allPip, + index, + _dataLength, + true + ); + } + } } } else { - for (uint128 i = startWord; i <= startWord + 1000; i++) { + uint128 i = startWord; + for (i; i < startWord + _maxWords; i++) { + uint128 next; if (_self[i] != 0) { - uint128 next; next = findHasLiquidityInOneWords( _self, i > startWord ? 256 * i : _startPip, @@ -179,30 +229,61 @@ library LiquidityBitmap { if (next != 0) { allPip[index] = next; index++; - if (index >= _dataLength) break; - } - while (true) { - next = findHasLiquidityInOneWords( + _dataLength--; + if (_dataLength == 0) return allPip; + ( + allPip, + index, + _dataLength + ) = findAllLiquidityInOneWord( _self, - next + 1, + next, + allPip, + index, + _dataLength, false ); - if (next != 0 && index <= _dataLength) { - allPip[index] = next; - index++; - if (index >= _dataLength) break; - } else { - break; - } } } } - if (index == _dataLength) return allPip; } - return allPip; } + function findAllLiquidityInOneWord( + mapping(uint128 => uint256) storage _self, + uint128 _next, + uint128[] memory _allPip, + uint128 _index, + uint256 _dataLength, + bool _lte + ) + internal + view + returns ( + uint128[] memory, + uint128, + uint256 + ) + { + while (_dataLength != 0) { + _next = findHasLiquidityInOneWords( + _self, + _lte ? _next - 1 : _next + 1, + _lte + ); + if (_next != 0) { + _allPip[_index] = _next; + _index++; + _dataLength--; + if (_dataLength == 0) return (_allPip, _index, _dataLength); + } else { + break; + } + } + return (_allPip, _index, _dataLength); + } + function hasLiquidity( mapping(uint128 => uint256) storage _self, uint128 _pip From 3c47dd7d2827521e89de69ab97465ae2f50e9feb Mon Sep 17 00:00:00 2001 From: DE Date: Mon, 26 Sep 2022 17:07:06 +0700 Subject: [PATCH 2/2] update pass test case --- contracts/protocol/PositionManager.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/protocol/PositionManager.sol b/contracts/protocol/PositionManager.sol index 5f111c9..8753240 100644 --- a/contracts/protocol/PositionManager.sol +++ b/contracts/protocol/PositionManager.sol @@ -533,7 +533,7 @@ contract PositionManager is maxWordRangeForMarketOrder, _fromPip, _dataLength, - _toHigher + !_toHigher ); PipLiquidity[] memory allLiquidity = new PipLiquidity[](_dataLength);