Skip to content

Commit

Permalink
simplify string conversion.
Browse files Browse the repository at this point in the history
  • Loading branch information
stas committed Dec 5, 2024
1 parent efe9b6f commit 3455071
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions contracts/LpSugar.vy
Original file line number Diff line number Diff line change
Expand Up @@ -1090,30 +1090,32 @@ def _safe_decimals(_token: address) -> uint8:
@view
def _safe_symbol(_token: address) -> String[100]:
"""
@notice Returns the `ERC20.symbol()` result safely
@notice Returns the `ERC20.symbol()` result safely, truncates to max 100 chars
@param _token The token to call
"""
# Large input size is required by Vyper's _abi_decode()
response: Bytes[2048] = b""
response = raw_call(
response: Bytes[192] = raw_call(
_token,
method_id("symbol()"),
max_outsize=2048,
# 192, is min necessary
max_outsize=192,
gas=50000,
is_delegate_call=False,
is_static_call=True,
revert_on_failure=False
)[1]

# Check response as revert_on_failure is set to False
# ABI decoded string size cannot be larger than Bytes size - 64 bytes
if len(response) > 0:
decoded_symbol: String[1984] = abi_decode(response, String[1984])
end: uint256 = min(100, len(decoded_symbol))
truncated_symbol: String[100] = convert(slice(decoded_symbol, 0, end), String[100])
return truncated_symbol
# Cache the len first, see:
# https://github.com/vyperlang/vyper/commit/3d9c537142fb99b2672f21e2057f5f202cde194f
end: uint256 = min(100, len(response))

return abi_decode(
slice(response, 0, end),
String[100]
)

return "-NA-"
return "-???-"

@internal
@view
Expand Down

0 comments on commit 3455071

Please sign in to comment.