Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
dianakocsis committed Oct 30, 2024
2 parents c85228e + 126cca6 commit 96a3bc5
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 @@
31514
31690
18 changes: 18 additions & 0 deletions src/libraries/SafeAddressMetadata.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {AddressStringUtil} from "./AddressStringUtil.sol";
/// @notice can produce symbols and decimals from inconsistent or absent ERC20 implementations
/// @dev Reference: https://github.com/Uniswap/solidity-lib/blob/master/contracts/libraries/SafeERC20Namer.sol
library SafeAddressMetadata {
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 addr The address
/// @param nativeLabel The native label
Expand All @@ -21,6 +23,9 @@ library SafeAddressMetadata {
// fallback to 6 uppercase hex of address
return addressToSymbol(addr);
}
if (bytes(symbol).length > MAX_SYMBOL_LENGTH) {
return truncateSymbol(symbol);
}
return symbol;
}

Expand Down Expand Up @@ -87,4 +92,17 @@ library SafeAddressMetadata {
}
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/SafeAddressMetadata.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 {SafeAddressMetadata} from "../../src/libraries/SafeAddressMetadata.sol";

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

0 comments on commit 96a3bc5

Please sign in to comment.