harb/onchain/test/helpers/CSVHelper.sol
giteadmin bb34d0725f feature/simulations (#11)
this pull request:
- creates a unit test that can take any scenario file (default: `out/scenario.json` and play it back on the deployment
- during the playback a debug trace generated in `timeSeries.csv`
- extracts the sentimenter into a separate upgradeable contract

Co-authored-by: JulesCrown <admin@noip.localhost>
Co-authored-by: giteadmin <gite@admin.com>
Reviewed-on: http://gitea.loseyourip.com:4000/dark-meme-society/harb/pulls/11
2024-11-07 15:33:40 +00:00

90 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);
}
}