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

Pashov L-06 limit symbol length #376

Merged
merged 7 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .forge-snapshots/positionDescriptor bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
31443
31619
18 changes: 18 additions & 0 deletions src/libraries/SafeCurrencyMetadata.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {AddressStringUtil} from "./AddressStringUtil.sol";
library SafeCurrencyMetadata {
using CurrencyLibrary for Currency;

uint8 constant MAX_SYMBOL_LENGTH = 12;

/// @notice attempts to extract the token symbol. if it does not implement symbol, returns a symbol derived from the address
/// @param currency The currency
/// @param nativeLabel The native label
Expand All @@ -25,6 +27,9 @@ library SafeCurrencyMetadata {
// fallback to 6 uppercase hex of address
return addressToSymbol(currencyAddress);
}
if (bytes(symbol).length > MAX_SYMBOL_LENGTH) {
return truncateSymbol(symbol);
}
return symbol;
}

Expand Down Expand Up @@ -92,4 +97,17 @@ library SafeCurrencyMetadata {
}
return "";
}

/// @notice truncates the symbol to the MAX_SYMBOL_LENGTH
/// @dev assumes the string is already longer than MAX_SYMBOL_LENGTH (or the same)
/// @param str the symbol
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be great to have 1 little
@dev assumes the string is already longer than MAX_SYMBOL_LENGTH

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

/// @return the truncated symbol
function truncateSymbol(string memory str) internal pure returns (string memory) {
hensha256 marked this conversation as resolved.
Show resolved Hide resolved
bytes memory strBytes = bytes(str);
bytes memory truncatedBytes = new bytes(MAX_SYMBOL_LENGTH);
for (uint256 i = 0; i < MAX_SYMBOL_LENGTH; i++) {
truncatedBytes[i] = strBytes[i];
}
return string(truncatedBytes);
}
}
16 changes: 16 additions & 0 deletions test/libraries/SafeCurrencyMetadata.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;

import "forge-std/Test.sol";
import {SafeCurrencyMetadata} from "../../src/libraries/SafeCurrencyMetadata.sol";

contract SafeCurrencyMetadataTest is Test {
function test_truncateSymbol_succeeds() public pure {
// 12 characters
assertEq(SafeCurrencyMetadata.truncateSymbol("123456789012"), "123456789012");
// 13 characters
assertEq(SafeCurrencyMetadata.truncateSymbol("1234567890123"), "123456789012");
// 14 characters
assertEq(SafeCurrencyMetadata.truncateSymbol("12345678901234"), "123456789012");
}
}
Loading