Skip to content

Commit

Permalink
Pashov L-06 limit symbol length (#376)
Browse files Browse the repository at this point in the history
* Pashov L-06

* change max length to 12 instead of 6

* test file

* format

* fix bytecode

* comment
  • Loading branch information
dianakocsis authored Oct 29, 2024
1 parent 825b20a commit 126cca6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
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
/// @return the truncated symbol
function truncateSymbol(string memory str) internal pure returns (string memory) {
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");
}
}

0 comments on commit 126cca6

Please sign in to comment.