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

Feat/dynamic symbol length limit #4635

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 5 additions & 2 deletions packages/assets-controllers/src/TokensController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import type {
TokensControllerMessenger,
TokensControllerState,
} from './TokensController';
import { MAX_SYMBOL_LENGTH } from './constants';

jest.mock('@ethersproject/contracts');
jest.mock('uuid', () => ({
Expand Down Expand Up @@ -1316,13 +1317,15 @@ describe('TokensController', () => {
buildMockEthersERC721Contract({ supportsInterface: false }),
);

const superLongSymbol = 'x'.repeat(MAX_SYMBOL_LENGTH + 1);

const result = controller.watchAsset({
asset: buildToken({ symbol: 'ABCDEFGHIJKLM' }),
asset: buildToken({ symbol: superLongSymbol }),
type: 'ERC20',
});

await expect(result).rejects.toThrow(
'Invalid symbol "ABCDEFGHIJKLM": longer than 11 characters',
`Invalid symbol "${superLongSymbol}": longer than ${MAX_SYMBOL_LENGTH} characters`,
);
});
});
Expand Down
5 changes: 3 additions & 2 deletions packages/assets-controllers/src/TokensController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import type {
TokenListToken,
} from './TokenListController';
import type { Token } from './TokenRatesController';
import { MAX_SYMBOL_LENGTH } from './constants';

/**
* @type SuggestedAssetMeta
Expand Down Expand Up @@ -868,9 +869,9 @@ export class TokensController extends BaseController<
throw rpcErrors.invalidParams(`Invalid symbol: not a string`);
}

if (asset.symbol.length > 11) {
if (asset.symbol.length < 1 || asset.symbol.length > MAX_SYMBOL_LENGTH) {
throw rpcErrors.invalidParams(
`Invalid symbol "${asset.symbol}": longer than 11 characters`,
`Invalid symbol "${asset.symbol}": longer than ${MAX_SYMBOL_LENGTH} characters`,
);
}

Expand Down
2 changes: 2 additions & 0 deletions packages/assets-controllers/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export enum Source {
Dapp = 'dapp',
Detected = 'detected',
}

export const MAX_SYMBOL_LENGTH = 100;