harb/onchain/test/helpers/CSVHelper.sol
2025-07-08 10:33:10 +02:00

91 lines
2.8 KiB
Solidity

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
/**
* @title CSVHelper
* @dev Library for managing CSV data in Solidity, including converting values to strings and writing CSV data.
*/
library CSVHelper {
/**
* @notice Creates a standard CSV header for liquidity position data.
* @return The CSV header as a string.
*/
function createPositionsHeader() internal pure returns (string memory) {
return
"precedingAction, currentTick, floorTickLower, floorTickUpper, floorEth, floorHarb, anchorTickLower, anchorTickUpper, anchorEth, anchorHarb, discoveryTickLower, discoveryTickUpper, discoveryEth, discoveryHarb";
}
function createTimeSeriesHeader() internal pure returns (string memory) {
return "time, price, harbTotalSupply, supplyChange, stakeOutstandingShares, avgTaxRate, sentiment, taxCollected";
}
/**
* @notice Appends new CSV data to the existing CSV string.
* @param csv The current CSV string.
* @param newRow The new row to append.
* @return The updated CSV string.
*/
function appendRow(string memory csv, string memory newRow) internal pure returns (string memory) {
return string(abi.encodePacked(csv, "\n", newRow));
}
/**
* @notice Converts a `uint256` to a string.
* @param _i The integer to convert.
* @return The string representation of the integer.
*/
function uintToStr(uint256 _i) internal pure returns (string memory) {
if (_i == 0) {
return "0";
}
uint256 j = _i;
uint256 len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint256 k = len;
while (_i != 0) {
k = k - 1;
uint8 temp = (48 + uint8(_i - _i / 10 * 10));
bstr[k] = bytes1(temp);
_i /= 10;
}
return string(bstr);
}
/**
* @notice Converts an `int256` to a string.
* @param _i The integer to convert.
* @return The string representation of the integer.
*/
function intToStr(int256 _i) internal pure returns (string memory) {
if (_i == 0) {
return "0";
}
bool negative = _i < 0;
uint256 absValue = uint256(negative ? -_i : _i);
uint256 len;
uint256 j = absValue;
while (j != 0) {
len++;
j /= 10;
}
if (negative) {
len++; // Increase length for the minus sign.
}
bytes memory bstr = new bytes(len);
uint256 k = len;
while (absValue != 0) {
k = k - 1;
uint8 temp = (48 + uint8(absValue - absValue / 10 * 10));
bstr[k] = bytes1(temp);
absValue /= 10;
}
if (negative) {
bstr[0] = "-";
}
return string(bstr);
}
}