Skip to content

Commit

Permalink
OK-34478: Support transfer custom token without symbol (#6388)
Browse files Browse the repository at this point in the history
* feat: Add eth_feeHistory

* feat: Support custom token without symbol

* chore: i18n
  • Loading branch information
originalix authored Dec 21, 2024
1 parent a4f210d commit 3a326f1
Show file tree
Hide file tree
Showing 25 changed files with 182 additions and 46 deletions.
76 changes: 72 additions & 4 deletions packages/kit-bg/src/services/ServiceToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,23 @@ class ServiceToken extends ServiceBase {
}

resp.data.data.tokens.data = resp.data.data.tokens.data.map((token) => ({
...token,
...this.mergeTokenMetadataWithCustomData({
token,
customTokens,
networkId,
}),
accountId,
networkId,
mergeAssets: vaultSettings.mergeDeriveAssetsEnabled,
}));

resp.data.data.riskTokens.data = resp.data.data.riskTokens.data.map(
(token) => ({
...token,
...this.mergeTokenMetadataWithCustomData({
token,
customTokens,
networkId,
}),
accountId,
networkId,
mergeAssets: vaultSettings.mergeDeriveAssetsEnabled,
Expand All @@ -212,7 +220,11 @@ class ServiceToken extends ServiceBase {

resp.data.data.smallBalanceTokens.data =
resp.data.data.smallBalanceTokens.data.map((token) => ({
...token,
...this.mergeTokenMetadataWithCustomData({
token,
customTokens,
networkId,
}),
accountId,
networkId,
mergeAssets: vaultSettings.mergeDeriveAssetsEnabled,
Expand Down Expand Up @@ -275,6 +287,32 @@ class ServiceToken extends ServiceBase {
return resp.data.data;
}

private mergeTokenMetadataWithCustomData<T extends IToken>({
token,
customTokens,
networkId,
}: {
token: T;
customTokens: IAccountToken[];
networkId: string;
}): T {
if (!token.symbol || !token.name) {
const customToken = customTokens.find(
(t) =>
t.address?.toLowerCase() === token.address?.toLowerCase() &&
t.networkId === networkId,
);
if (customToken) {
return {
...token,
symbol: token.symbol || customToken.symbol,
name: token.name || customToken.name,
};
}
}
return token;
}

_updateAccountLocalTokensDebounced = debounce(
async () => {
await this.backgroundApi.simpleDb.localTokens.updateAccountTokenListByCache(
Expand Down Expand Up @@ -449,6 +487,22 @@ class ServiceToken extends ServiceBase {
tokenIdOnNetwork,
});

if (localToken) {
if (!localToken.symbol || !localToken.name) {
const customTokens =
await this.backgroundApi.serviceCustomToken.getCustomTokens({
accountId,
networkId,
});
return this.mergeTokenMetadataWithCustomData({
token: localToken,
customTokens,
networkId,
});
}
return localToken;
}

if (localToken) return localToken;

try {
Expand All @@ -458,7 +512,21 @@ class ServiceToken extends ServiceBase {
contractList: [tokenIdOnNetwork],
});

const tokenInfo = tokensDetails[0].info;
let tokenInfo = tokensDetails[0].info;

if (!tokenInfo.symbol || !tokenInfo.name) {
const customTokens =
await this.backgroundApi.serviceCustomToken.getCustomTokens({
accountId,
networkId,
});

tokenInfo = this.mergeTokenMetadataWithCustomData({
token: tokenInfo,
customTokens,
networkId,
});
}

void this.updateLocalTokens({
networkId,
Expand Down
14 changes: 13 additions & 1 deletion packages/kit/src/views/AssetList/hooks/useAddToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export function useAddTokenForm({
const symbolValue = form.watch('symbol');
const decimalsValue = form.watch('decimals');
const [isEmptyContract, setIsEmptyContract] = useState(false);
const [isSymbolEditable, setIsSymbolEditable] = useState(false);

const firstRenderRef = useRef(true);
const setIsEmptyContractState = useCallback(
Expand Down Expand Up @@ -85,6 +86,8 @@ export function useAddTokenForm({
contractAddressValue,
symbolValue,
decimalsValue,
isSymbolEditable,
setIsSymbolEditable,
};
}

Expand All @@ -96,6 +99,7 @@ export function useAddToken({
selectedNetworkIdValue,
contractAddressValue,
setIsEmptyContractState,
setIsSymbolEditable,
}: {
token?: IAccountToken;
walletId: string;
Expand All @@ -104,6 +108,7 @@ export function useAddToken({
selectedNetworkIdValue: string;
contractAddressValue: string;
setIsEmptyContractState: (value: boolean) => void;
setIsSymbolEditable: (value: boolean) => void;
checkAccountIsExist: () => Promise<{
hasExistAccountFlag: boolean;
accountIdForNetwork: string;
Expand Down Expand Up @@ -152,7 +157,13 @@ export function useAddToken({
searchResult[0]?.info
) {
const [firstToken] = searchResult;
form.setValue('symbol', firstToken.info.symbol);
const symbol = firstToken.info.symbol?.trim();
if (symbol) {
form.setValue('symbol', symbol);
setIsSymbolEditable(false);
} else {
setIsSymbolEditable(true);
}
form.setValue(
'decimals',
new BigNumber(firstToken.info.decimals).toString(),
Expand All @@ -161,6 +172,7 @@ export function useAddToken({
setIsEmptyContractState(false);
} else {
form.setValue('symbol', '');
setIsSymbolEditable(false);
form.setValue('decimals', '');
setIsEmptyContractState(true);
}
Expand Down
19 changes: 16 additions & 3 deletions packages/kit/src/views/AssetList/pages/AddCustomTokenModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ function AddCustomTokenModal() {
contractAddressValue,
symbolValue,
decimalsValue,
isSymbolEditable,
setIsSymbolEditable,
} = useAddTokenForm({
token,
networkId,
Expand All @@ -142,6 +144,7 @@ function AddCustomTokenModal() {
selectedNetworkIdValue,
contractAddressValue,
setIsEmptyContractState,
setIsSymbolEditable,
checkAccountIsExist,
});

Expand Down Expand Up @@ -214,13 +217,13 @@ function AddCustomTokenModal() {
try {
const tokenInfo = {
address: contractAddress,
...searchedTokenRef.current,
symbol,
decimals: new BigNumber(decimals).toNumber(),
...searchedTokenRef.current,
accountId: accountIdForNetwork,
networkId: selectedNetworkIdValue,
allNetworkAccountId: isAllNetwork ? accountId : undefined,
name: searchedTokenRef.current?.name ?? '',
name: searchedTokenRef.current?.name || symbol || '',
isNative: searchedTokenRef.current?.isNative ?? false,
$key: `${selectedNetworkIdValue}_${contractAddress}`,
};
Expand Down Expand Up @@ -349,14 +352,24 @@ function AddCustomTokenModal() {
label={intl.formatMessage({
id: ETranslations.manage_token_custom_token_symbol,
})}
rules={{
required: isSymbolEditable
? {
value: true,
message: intl.formatMessage({
id: ETranslations.manage_token_token_required,
}),
}
: false,
}}
name="symbol"
>
<Input
size="large"
$gtMd={{
size: 'medium',
}}
editable={false}
editable={isSymbolEditable}
/>
</Form.Field>
<Form.Field
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/src/locale/enum/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
auth_error_password_not_match = 'auth.error_password_not_match',
auth_error_password_too_short = 'auth.error_password_too_short',
auth_new_passcode_form_label = 'auth.new_passcode_form_label',
auth_new_passcode_same_as_old = 'auth.new_passcode_same_as_old',
auth_new_passwcode_form_placeholder = 'auth.new_passwcode_form_placeholder',
auth_new_password_form_label = 'auth.new_password_form_label',
auth_new_password_form_placeholder = 'auth.new_password_form_placeholder',
Expand Down Expand Up @@ -1277,6 +1278,7 @@
manage_token_native_token_cant_remove = 'manage_token.native_token_cant_remove',
manage_token_popular_token = 'manage_token.popular_token',
manage_token_title = 'manage_token.title',
manage_token_token_required = 'manage_token.token_required',
manger_token_custom_token_address_required = 'manger_token.custom_token_address_required',
manual_backup = 'manual_backup',
market_1d = 'market.1d',
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/src/locale/json/bn.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"auth.error_password_not_match": "পাসওয়ার্ডগুলি মিলছে না",
"auth.error_password_too_short": "পাসওয়ার্ডটি অবশ্যই কমপক্ষে {length} অক্ষরের হতে হবে",
"auth.new_passcode_form_label": "নতুন পাসকোড",
"auth.new_passcode_same_as_old": "নতুন পাসকোডটি পুরোনোটির থেকে ভিন্ন হতে হবে",
"auth.new_passwcode_form_placeholder": "একটি শক্তিশালী পাসকোড তৈরি করুন",
"auth.new_password_form_label": "নতুন পাসওয়ার্ড",
"auth.new_password_form_placeholder": "একটি শক্তিশালী পাসওয়ার্ড তৈরি করুন",
Expand Down Expand Up @@ -1272,6 +1273,7 @@
"manage_token.native_token_cant_remove": "নেটিভ টোকেন মুছে ফেলা যাবে না",
"manage_token.popular_token": "সাধারণ টোকেন",
"manage_token.title": "টোকেন পরিচালনা করুন",
"manage_token.token_required": "প্রতীক প্রয়োজন।",
"manger_token.custom_token_address_required": "চুক্তির ঠিকানা প্রয়োজন।",
"manual_backup": "ম্যানুয়াল ব্যাকআপ",
"market.1d": "১ দিন",
Expand Down
6 changes: 4 additions & 2 deletions packages/shared/src/locale/json/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"auth.error_password_not_match": "Passwörter stimmen nicht überein",
"auth.error_password_too_short": "Das Passwort muss mindestens {length} Zeichen lang sein",
"auth.new_passcode_form_label": "Neuer Zugangscode",
"auth.new_passcode_same_as_old": "Der neue Zugangscode muss sich vom alten unterscheiden",
"auth.new_passwcode_form_placeholder": "Erstellen Sie ein starkes Passwort",
"auth.new_password_form_label": "Neues Passwort",
"auth.new_password_form_placeholder": "Erstellen Sie ein starkes Passwort",
Expand Down Expand Up @@ -258,7 +259,7 @@
"dapp_connect.lnurl_withdraw_request": "LNURL Abhebungsanforderung",
"dapp_connect.login_successful": "Login erfolgreich",
"dapp_connect.malicious_site_detected": "Von mehreren Quellen als bösartig markiert.",
"dapp_connect.malicious_site_warning": "Bösartige Seite.",
"dapp_connect.malicious_site_warning": "Bösartige Seite",
"dapp_connect.message": "Nachricht",
"dapp_connect.message_to_the_payer": "Eine Nachricht an den Zahler",
"dapp_connect.msg_authentication_failed_verify_again": "Authentifizierung fehlgeschlagen, bitte überprüfen Sie erneut.",
Expand Down Expand Up @@ -305,7 +306,7 @@
"dapp_connect.signature_request": "Unterschriftsanforderung",
"dapp_connect.site_not_using_private_connection_warning": "Die Seite verwendet keine private Verbindung, Angreifer könnten in der Lage sein, die Informationen, die Sie über diese Seite senden und erhalten, einzusehen und zu ändern.",
"dapp_connect.site_suspected_of_malicious_behavior_warning": "Diese Seite steht im Verdacht, bösartiges Verhalten zu zeigen. Die Fortsetzung der Genehmigung von Anfragen kann Sicherheitsrisiken darstellen.",
"dapp_connect.suspected_malicious_behavior": "Verdächtiges schädliches Verhalten.",
"dapp_connect.suspected_malicious_behavior": "Verdächtiges schädliches Verhalten",
"dapp_connect.suspected_malicious_behavior_on_this_site": "Verdacht auf böswilliges Verhalten",
"dapp_connect.the_sites_connection_is_not_secure": "Die Verbindung der Website ist nicht sicher.",
"dapp_connect.url_contains_unusual_characters": "Verdächtige URL",
Expand Down Expand Up @@ -1272,6 +1273,7 @@
"manage_token.native_token_cant_remove": "Der native Token kann nicht entfernt werden",
"manage_token.popular_token": "Gemeinsames Token",
"manage_token.title": "Token verwalten",
"manage_token.token_required": "Symbol ist erforderlich.",
"manger_token.custom_token_address_required": "Vertragsadresse erforderlich.",
"manual_backup": "Manuelle Sicherung",
"market.1d": "1T",
Expand Down
8 changes: 5 additions & 3 deletions packages/shared/src/locale/json/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"auth.error_password_not_match": "Passwords do not match",
"auth.error_password_too_short": "Password must be at least {length} characters",
"auth.new_passcode_form_label": "New passcode",
"auth.new_passcode_same_as_old": "New passcode must differ from the old one",
"auth.new_passwcode_form_placeholder": "Create a robust password",
"auth.new_password_form_label": "New password",
"auth.new_password_form_placeholder": "Create a strong password",
Expand Down Expand Up @@ -166,7 +167,7 @@
"browser.can_be_re_enabled_in_settings": "Can be turned back on in settings.",
"browser.copy_link": "Copy link",
"browser.dapp_listed_by": "dApp listed by",
"browser.disable": "Disable",
"browser.disable": "Global Disable",
"browser.fetching_dapp_info": "Fetching dApp info...",
"browser.hide_on_this_site": "Hide on this site",
"browser.import": "Import",
Expand Down Expand Up @@ -258,7 +259,7 @@
"dapp_connect.lnurl_withdraw_request": "LNURL Withdraw Request",
"dapp_connect.login_successful": "Login Successful",
"dapp_connect.malicious_site_detected": "Marked as malicious by multiple sources.",
"dapp_connect.malicious_site_warning": "Malicious site.",
"dapp_connect.malicious_site_warning": "Malicious site",
"dapp_connect.message": "Message",
"dapp_connect.message_to_the_payer": "A message to the payer",
"dapp_connect.msg_authentication_failed_verify_again": "Authentication failed, please verify again.",
Expand Down Expand Up @@ -305,7 +306,7 @@
"dapp_connect.signature_request": "Signature request",
"dapp_connect.site_not_using_private_connection_warning": "The site does not use a private connection, attackers may be able to view and change the information you send and get through this site.",
"dapp_connect.site_suspected_of_malicious_behavior_warning": "This site is suspected of malicious behavior, continuing to approve requests may pose security risks.",
"dapp_connect.suspected_malicious_behavior": "Suspected malicious behavior.",
"dapp_connect.suspected_malicious_behavior": "Suspected malicious behavior",
"dapp_connect.suspected_malicious_behavior_on_this_site": "Suspected malicious behavior",
"dapp_connect.the_sites_connection_is_not_secure": "The site's connection is not secure.",
"dapp_connect.url_contains_unusual_characters": "Suspicious URL",
Expand Down Expand Up @@ -1272,6 +1273,7 @@
"manage_token.native_token_cant_remove": "Native token can't be removed",
"manage_token.popular_token": "Common token",
"manage_token.title": "Manage token",
"manage_token.token_required": "Symbol is required.",
"manger_token.custom_token_address_required": "Contract address required.",
"manual_backup": "Manual backup",
"market.1d": "1D",
Expand Down
8 changes: 5 additions & 3 deletions packages/shared/src/locale/json/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"auth.error_password_not_match": "Passwords do not match",
"auth.error_password_too_short": "Password must be at least {length} characters",
"auth.new_passcode_form_label": "New passcode",
"auth.new_passcode_same_as_old": "New passcode must differ from the old one",
"auth.new_passwcode_form_placeholder": "Create a strong passcode",
"auth.new_password_form_label": "New password",
"auth.new_password_form_placeholder": "Create a strong password",
Expand Down Expand Up @@ -166,7 +167,7 @@
"browser.can_be_re_enabled_in_settings": "Can be re-enabled in settings.",
"browser.copy_link": "Copy link",
"browser.dapp_listed_by": "dApp listed by",
"browser.disable": "Disable",
"browser.disable": "Global Disable",
"browser.fetching_dapp_info": "Fetching dApp info...",
"browser.hide_on_this_site": "Hide on this site",
"browser.import": "Import",
Expand Down Expand Up @@ -258,7 +259,7 @@
"dapp_connect.lnurl_withdraw_request": "LNURL Withdraw Request",
"dapp_connect.login_successful": "Login Successful",
"dapp_connect.malicious_site_detected": "Marked as malicious by multiple sources.",
"dapp_connect.malicious_site_warning": "Malicious site.",
"dapp_connect.malicious_site_warning": "Malicious site",
"dapp_connect.message": "Message",
"dapp_connect.message_to_the_payer": "A message to the payer",
"dapp_connect.msg_authentication_failed_verify_again": "Authentication failed, please verify again.",
Expand Down Expand Up @@ -305,7 +306,7 @@
"dapp_connect.signature_request": "Signature request",
"dapp_connect.site_not_using_private_connection_warning": "The site does not use a private connection, attackers may be able to view and change the information you send and get through this site.",
"dapp_connect.site_suspected_of_malicious_behavior_warning": "This site is suspected of malicious behavior, continuing to approve requests may pose security risks.",
"dapp_connect.suspected_malicious_behavior": "Suspected malicious behavior.",
"dapp_connect.suspected_malicious_behavior": "Suspected malicious behavior",
"dapp_connect.suspected_malicious_behavior_on_this_site": "Suspected malicious behavior",
"dapp_connect.the_sites_connection_is_not_secure": "The site's connection is not secure.",
"dapp_connect.url_contains_unusual_characters": "Suspicious URL",
Expand Down Expand Up @@ -1272,6 +1273,7 @@
"manage_token.native_token_cant_remove": "Native token can't be removed",
"manage_token.popular_token": "Common token",
"manage_token.title": "Manage token",
"manage_token.token_required": "Symbol is required.",
"manger_token.custom_token_address_required": "Contract address required.",
"manual_backup": "Manual backup",
"market.1d": "1D",
Expand Down
Loading

0 comments on commit 3a326f1

Please sign in to comment.