Skip to content

Fix: Automatically use unicode syntax for token names with non-ASCII characters #476

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

MKVEERENDRA
Copy link

Fix: Use unicode string literal for token names with non-ASCII characters (#475)

  • Token names with accents/symbols (e.g., "MyTokeć") now use Solidity’s unicode"..." syntax to prevent compilation errors.
  • Double quotes in names/symbols are escaped (\") to avoid syntax issues.
  • Developers can now use any Unicode name without manual code adjustments.

Fixes #475

Fix: Use unicode string literal for token names with non-ASCII characters (OpenZeppelin#475)

- Token names with accents/symbols (e.g., "MyTokeć") now use Solidity’s `unicode"..."` syntax  
  to prevent compilation errors.
- Double quotes in names/symbols are escaped (`\"`) to avoid syntax issues.
- Developers can now use any Unicode name without manual code adjustments.

Fixes OpenZeppelin#475
@MKVEERENDRA MKVEERENDRA changed the title Update erc20.ts Fix: Automatically use unicode syntax for token names with non-ASCII characters Mar 5, 2025
@ericglau ericglau requested a review from CoveMB March 5, 2025 18:59
@@ -109,13 +109,22 @@ export function buildERC20(opts: ERC20Options): ContractBuilder {

return c;
}
// Helper function to format string literals with the `unicode` keyword if they contain non-ASCII characters.
// Also escapes double quotes in the string.
function formatLiteral(str: string): string {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's try to make the function name clear and specific enough so we might not need the comment

@@ -109,13 +109,22 @@ export function buildERC20(opts: ERC20Options): ContractBuilder {

return c;
}
// Helper function to format string literals with the `unicode` keyword if they contain non-ASCII characters.
// Also escapes double quotes in the string.
function formatLiteral(str: string): string {
Copy link
Contributor

Choose a reason for hiding this comment

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

We could move this in the utils folder


function addBase(c: ContractBuilder, name: string, symbol: string) {
const ERC20 = {
name: 'ERC20',
path: '@openzeppelin/contracts/token/ERC20/ERC20.sol',
};
c.addParent(ERC20, [name, symbol]);
// Use formatLiteral to wrap name and symbol appropriately
c.addParent(ERC20, [formatLiteral(name), formatLiteral(symbol)]);
Copy link
Contributor

Choose a reason for hiding this comment

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

great, we should apply this to other contract (not only ERC20) as well maybe in the contract builder

@CoveMB
Copy link
Contributor

CoveMB commented Mar 10, 2025

Hey @MKVEERENDRA thanks for submitting this fix ⚡
left some comments, also we might want to add some test

From formatLiteral to sanitize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Solidity: If a dev passes a unicode string as token name, unicode"..." string literal should be used
2 participants