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

prevent duplicate tokens being added in adminJS #1851

Closed
Closed
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
37 changes: 37 additions & 0 deletions src/server/adminJs/tabs/tokenTab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,43 @@ export const createToken = async (
organizations,
} = request.payload;
try {
if (!address || !decimals || !name || !networkId || !symbol) {
message = 'Please fill all required fields';
type = 'danger';
return {
notice: {
message,
type,
},
};
}
const duplicateAddress = await Token.createQueryBuilder('token')
.where('LOWER(token.address) = LOWER(:address)', { address })
.andWhere('token.networkId = :networkId', {
networkId: Number(networkId),
})
.getOne();

const duplicateSymbol = await Token.createQueryBuilder('token')
.where('LOWER(token.symbol) = LOWER(:symbol)', { symbol })
.andWhere('token.networkId = :networkId', {
networkId: Number(networkId),
})
.getOne();

if (duplicateSymbol || duplicateAddress) {
message = `Token ${
duplicateAddress ? 'address' : 'symbol'
} already exists!`;
type = 'danger';
return {
record: {},
notice: {
message,
type,
},
};
}
Comment on lines +157 to +169
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Improve error messaging when both address and symbol already exist

Currently, if both duplicateAddress and duplicateSymbol are true, the error message will only mention the address due to the conditional operator. This could confuse users if both the address and symbol are duplicates.

Consider updating the error message to inform the user when both the address and symbol already exist. Here's a suggested modification:

        if (duplicateSymbol || duplicateAddress) {
-         message = `Token ${
-           duplicateAddress ? 'address' : 'symbol'
-         } already exists!`;
+         if (duplicateAddress && duplicateSymbol) {
+           message = 'Token address and symbol already exist!';
+         } else if (duplicateAddress) {
+           message = 'Token address already exists!';
+         } else if (duplicateSymbol) {
+           message = 'Token symbol already exists!';
+         }
          type = 'danger';
          return {
            record: {},
            notice: {
              message,
              type,
            },
          };
        }

This change provides clearer feedback to the user, specifying exactly which fields are causing the duplication issue.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (duplicateSymbol || duplicateAddress) {
message = `Token ${
duplicateAddress ? 'address' : 'symbol'
} already exists!`;
type = 'danger';
return {
record: {},
notice: {
message,
type,
},
};
}
if (duplicateSymbol || duplicateAddress) {
if (duplicateAddress && duplicateSymbol) {
message = 'Token address and symbol already exist!';
} else if (duplicateAddress) {
message = 'Token address already exists!';
} else if (duplicateSymbol) {
message = 'Token symbol already exists!';
}
type = 'danger';
return {
record: {},
notice: {
message,
type,
},
};
}

newToken = Token.create({
name,
symbol,
Expand Down
Loading