Skip to content

Commit

Permalink
Fix out of gas in traderjoe21 helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jeyldii committed Sep 5, 2023
1 parent 0d2113d commit 9bd73c2
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions contracts/TraderJoeHelper_v2_1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import "./interfaces/ILBPair.sol";
/// @dev Helper contract for interacting with TraderJoe pair contracts.
// solhint-disable-next-line contract-name-camelcase
contract TraderJoeHelper_v2_1 {

/// @dev Represents data about a bin in a Trader Joe pair.
struct BinData {
uint256 id;
uint256 reserveX;
Expand All @@ -24,34 +22,44 @@ contract TraderJoeHelper_v2_1 {
* @return data An array of BinData structs containing data about each bin.
* @return i The bin id where data collection stopped. It is reset to 0 if end of bins was reached before collecting size bins.
*/
function getBins(ILBPair pair, uint24 offset, uint24 size)
external
view
returns (BinData[] memory data, uint24 i)
{
function getBins(
ILBPair pair,
uint24 offset,
uint24 size
) external view returns (BinData[] memory data, uint24 i) {
uint256 counter = 0;
data = new BinData[](size);
uint24 lastBin = pair.getNextNonEmptyBin(true, type(uint24).max);
uint24 prevId = lastBin;
for (
i = offset;
i < lastBin && counter < size;
i = pair.getNextNonEmptyBin(false, i)
) {
if (prevId == i) {
break;
}
(uint256 x, uint256 y) = pair.getBin(i);
if (x > 0 || y > 0) {
(data[counter].reserveX, data[counter].reserveY) = (x, y);
data[counter].id = i;
unchecked{ ++counter; }
unchecked {
++counter;
}
}
prevId = i;
}
if (i == lastBin && counter < size) {
(data[counter].reserveX, data[counter].reserveY) = pair.getBin(i);
data[counter].id = i;
unchecked{ ++counter; }
unchecked {
++counter;
}
i = 0;
}
// cut array size down
assembly ("memory-safe") { // solhint-disable-line no-inline-assembly
assembly ("memory-safe") {

Check warning on line 61 in contracts/TraderJoeHelper_v2_1.sol

View workflow job for this annotation

GitHub Actions / lint

Avoid to use inline assembly. It is acceptable only in rare cases
// solhint-disable-line no-inline-assembly
mstore(data, counter)
}
}
Expand Down

0 comments on commit 9bd73c2

Please sign in to comment.