Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix out of gas in traderjoe21 helper #16

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
/// @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 @@
* @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
Loading