Skip to content

Commit

Permalink
OK-18469: Optimize the display precision of WETH. (#2761)
Browse files Browse the repository at this point in the history
* OK-18469: improving wrap process precision

* OK-18417: disable watching account as swap sending account

* OK-18358: swap recipient

* chore: update i18n
  • Loading branch information
kwoktung committed Mar 24, 2023
1 parent f857072 commit f04ce80
Show file tree
Hide file tree
Showing 25 changed files with 90 additions and 46 deletions.
1 change: 1 addition & 0 deletions packages/components/src/locale/ar.json
Expand Up @@ -184,6 +184,7 @@
"action__pause": "يوقف",
"action__permanently_delete": "حذف بشكل دائم",
"action__pick_an_emoji": "اختر رمزًا تعبيريًا",
"action__place_limit_order": "وضع أمر الحد",
"action__preview": "معاينة",
"action__profit_and_loss": "الربح والخسارة",
"action__protection": "حماية",
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/locale/bn.json
Expand Up @@ -184,6 +184,7 @@
"action__pause": "বিরতি",
"action__permanently_delete": "স্থায়ীভাবে মুছে ফেলুন",
"action__pick_an_emoji": "একটি ইমোজি বেছে নিন",
"action__place_limit_order": "স্থান সীমা অর্ডার",
"action__preview": "পূর্বরূপ",
"action__profit_and_loss": "লাভ এবং ক্ষতি",
"action__protection": "সুরক্ষা",
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/locale/de.json
Expand Up @@ -184,6 +184,7 @@
"action__pause": "Pause",
"action__permanently_delete": "Permanent löschen",
"action__pick_an_emoji": "Ein Emoji auswählen",
"action__place_limit_order": "Limit-Order platzieren",
"action__preview": "Vorschau",
"action__profit_and_loss": "Gewinn-und Verlust",
"action__protection": "Schutz",
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/locale/en-US.json
Expand Up @@ -184,6 +184,7 @@
"action__pause": "Pause",
"action__permanently_delete": "Permanently Delete",
"action__pick_an_emoji": "Pick an Emoji",
"action__place_limit_order": "Place Limit Order",
"action__preview": "Preview",
"action__profit_and_loss": "Profit and Loss",
"action__protection": "Protection",
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/locale/es.json
Expand Up @@ -184,6 +184,7 @@
"action__pause": "Pausa",
"action__permanently_delete": "Borrar permanentemente",
"action__pick_an_emoji": "Elegir un Emoji",
"action__place_limit_order": "Colocar orden de límite",
"action__preview": "Avance",
"action__profit_and_loss": "Ganancia y perdida",
"action__protection": "Proteccion",
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/locale/fil.json
Expand Up @@ -184,6 +184,7 @@
"action__pause": "I-pause",
"action__permanently_delete": "Permanenteng Tanggalin",
"action__pick_an_emoji": "Pumili ng Emoji",
"action__place_limit_order": "Place Limit Order",
"action__preview": "Silipin",
"action__profit_and_loss": "Kita at lugi",
"action__protection": "Proteksyon",
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/locale/fr_FR.json
Expand Up @@ -184,6 +184,7 @@
"action__pause": "Pause",
"action__permanently_delete": "Effacé définitivement",
"action__pick_an_emoji": "Choisissez un Emoji",
"action__place_limit_order": "Passer un ordre à cours limité",
"action__preview": "Aperçu",
"action__profit_and_loss": "Profit et perte",
"action__protection": "protection",
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/locale/hi_IN.json
Expand Up @@ -184,6 +184,7 @@
"action__pause": "रोकना",
"action__permanently_delete": "स्थायी रूप से हटाना",
"action__pick_an_emoji": "एक इमोजी चुनें",
"action__place_limit_order": "प्लेस लिमिट ऑर्डर",
"action__preview": "पूर्वावलोकन",
"action__profit_and_loss": "लाभ और हानि",
"action__protection": "संरक्षण",
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/locale/id.json
Expand Up @@ -184,6 +184,7 @@
"action__pause": "Berhenti sebentar",
"action__permanently_delete": "Hapus Permanen",
"action__pick_an_emoji": "Pilih Emoji",
"action__place_limit_order": "Tempatkan Limit Order",
"action__preview": "Pratinjau",
"action__profit_and_loss": "Laba rugi",
"action__protection": "Perlindungan",
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/locale/it_IT.json
Expand Up @@ -184,6 +184,7 @@
"action__pause": "Pausa",
"action__permanently_delete": "Elimina definitivamente",
"action__pick_an_emoji": "Scegli un Emoji",
"action__place_limit_order": "Effettua un ordine limite",
"action__preview": "Anteprima",
"action__profit_and_loss": "Profitti e perdite",
"action__protection": "Protezione",
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/locale/ja_JP.json
Expand Up @@ -184,6 +184,7 @@
"action__pause": "一時停止",
"action__permanently_delete": "完全に削除する",
"action__pick_an_emoji": "絵文字を選ぶ",
"action__place_limit_order": "限価注文を入れる",
"action__preview": "プレビュー",
"action__profit_and_loss": "損益",
"action__protection": "保護",
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/locale/ko_KR.json
Expand Up @@ -184,6 +184,7 @@
"action__pause": "정지시키다",
"action__permanently_delete": "영구 삭제",
"action__pick_an_emoji": "이모티콘 선택",
"action__place_limit_order": "지정가 주문하기",
"action__preview": "시사",
"action__profit_and_loss": "이익과 손실",
"action__protection": "보호",
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/locale/mn_MN.json
Expand Up @@ -184,6 +184,7 @@
"action__pause": "Түр зогсоох",
"action__permanently_delete": "Бүрмөсөн устгах",
"action__pick_an_emoji": "Эможи сонгоно уу",
"action__place_limit_order": "Хязгаарлалтын захиалга",
"action__preview": "Урьдчилан үзэх",
"action__profit_and_loss": "Орлого ба зарлага",
"action__protection": "Хамгаалалт",
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/locale/pt.json
Expand Up @@ -184,6 +184,7 @@
"action__pause": "Pausa",
"action__permanently_delete": "Excluir permanentemente",
"action__pick_an_emoji": "Escolha um emoji",
"action__place_limit_order": "Colocar Ordem Limitada",
"action__preview": "Visualização",
"action__profit_and_loss": "Lucros e perdas",
"action__protection": "Proteção",
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/locale/ru.json
Expand Up @@ -184,6 +184,7 @@
"action__pause": "Пауза",
"action__permanently_delete": "Удалить навсегда",
"action__pick_an_emoji": "Выберите эмодзи",
"action__place_limit_order": "Разместить лимитный ордер",
"action__preview": "Предварительный просмотр",
"action__profit_and_loss": "Доходы и расходы",
"action__protection": "Защита",
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/locale/th_TH.json
Expand Up @@ -184,6 +184,7 @@
"action__pause": "หยุด",
"action__permanently_delete": "ลบอย่างถาวร",
"action__pick_an_emoji": "เลือกอีโมจิ",
"action__place_limit_order": "วางคำสั่งจำกัด",
"action__preview": "ดูตัวอย่าง",
"action__profit_and_loss": "กำไรและขาดทุน",
"action__protection": "การป้องกัน",
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/locale/uk_UA.json
Expand Up @@ -184,6 +184,7 @@
"action__pause": "Пауза",
"action__permanently_delete": "Видалити назавжди",
"action__pick_an_emoji": "Виберіть Emoji",
"action__place_limit_order": "Розмістити лімітне замовлення",
"action__preview": "Попередній перегляд",
"action__profit_and_loss": "Прибуток і збиток",
"action__protection": "Безпека",
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/locale/vi.json
Expand Up @@ -184,6 +184,7 @@
"action__pause": "Tạm ngừng",
"action__permanently_delete": "Xóa vĩnh viễn",
"action__pick_an_emoji": "Chọn một biểu tượng cảm xúc",
"action__place_limit_order": "Đặt lệnh giới hạn",
"action__preview": "Xem trước",
"action__profit_and_loss": "Lợi nhuận và thua lỗ",
"action__protection": "Sự bảo vệ",
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/locale/zh-CN.json
Expand Up @@ -184,6 +184,7 @@
"action__pause": "暂停",
"action__permanently_delete": "永久删除",
"action__pick_an_emoji": "选择表情符号",
"action__place_limit_order": "下达限价订单",
"action__preview": "预览",
"action__profit_and_loss": "盈亏分析",
"action__protection": "安全防护",
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/locale/zh_HK.json
Expand Up @@ -184,6 +184,7 @@
"action__pause": "暫停",
"action__permanently_delete": "永久刪除",
"action__pick_an_emoji": "選擇表情符號",
"action__place_limit_order": "下達限價訂單",
"action__preview": "預覽",
"action__profit_and_loss": "盈虧分析",
"action__protection": "安全防護",
Expand Down
29 changes: 15 additions & 14 deletions packages/kit-bg/src/services/ServiceSwap.ts
Expand Up @@ -53,7 +53,7 @@ import type {
} from '@onekeyhq/kit/src/views/Swap/typings';
import {
convertBuildParams,
getNetworkIdImpl,
recipientMustBeSendingAccount,
stringifyTokens,
} from '@onekeyhq/kit/src/views/Swap/utils';
import {
Expand Down Expand Up @@ -482,19 +482,20 @@ export default class ServiceSwap extends ServiceBase {
(s) => s.swap.allowAnotherRecipientAddress,
);
if (inputToken && outputToken) {
const implA = getNetworkIdImpl(inputToken.networkId);
const implB = getNetworkIdImpl(outputToken.networkId);
if (implA === implB && !allowAnotherRecipientAddress) {
const sendingAccount = appSelector((s) => s.swap.sendingAccount);
if (sendingAccount) {
return {
accountId: sendingAccount.id,
address: sendingAccount.address,
name: sendingAccount.name,
networkId: inputToken.networkId,
networkImpl: inputToken.impl,
};
}
const shouldBeSendingAccount = recipientMustBeSendingAccount(
inputToken,
outputToken,
allowAnotherRecipientAddress,
);
const sendingAccount = appSelector((s) => s.swap.sendingAccount);
if (sendingAccount && shouldBeSendingAccount) {
return {
accountId: sendingAccount.id,
address: sendingAccount.address,
name: sendingAccount.name,
networkId: inputToken.networkId,
networkImpl: inputToken.impl,
};
}
}
return recipient;
Expand Down
20 changes: 11 additions & 9 deletions packages/kit/src/views/Swap/PickAccount/index.tsx
Expand Up @@ -42,15 +42,17 @@ const MyWallet = () => {
async function main() {
for (let i = 0; i < wallets.length; i += 1) {
const wallet = wallets[i];
const accounts = await backgroundApiProxy.engine.getAccounts(
wallet.accounts,
networkId,
);
if (accounts && accounts.length > 0) {
setSections((prev) => [
...prev,
{ wallet, data: accounts, key: wallet.id },
]);
if (wallet.type !== 'watching') {
const accounts = await backgroundApiProxy.engine.getAccounts(
wallet.accounts,
networkId,
);
if (accounts && accounts.length > 0) {
setSections((prev) => [
...prev,
{ wallet, data: accounts, key: wallet.id },
]);
}
}
}
}
Expand Down
Expand Up @@ -16,6 +16,7 @@ import {
useThemeValue,
} from '@onekeyhq/components';
import { shortenAddress } from '@onekeyhq/components/src/utils';
import { IMPL_SOL } from '@onekeyhq/shared/src/engine/engineConsts';

import backgroundApiProxy from '../../../../background/instance/backgroundApiProxy';
import { FormatCurrency } from '../../../../components/Format';
Expand All @@ -28,7 +29,11 @@ import {
import { useSwapRecipient } from '../../hooks/useSwap';
import { useTokenBalance, useTokenPrice } from '../../hooks/useSwapTokenUtils';
import { SwapRoutes } from '../../typings';
import { formatAmount, getNetworkIdImpl } from '../../utils';
import {
formatAmount,
getNetworkIdImpl,
recipientMustBeSendingAccount,
} from '../../utils';
import { NetworkName } from '../NetworkName';
import { TokenDisplay } from '../TokenDisplay';

Expand Down Expand Up @@ -292,9 +297,12 @@ const TokenInputReceivingAddressFieldShowControl = () => {
(s) => s.swap.allowAnotherRecipientAddress,
);
if (inputToken && outputToken) {
const implA = getNetworkIdImpl(inputToken.networkId);
const implB = getNetworkIdImpl(outputToken.networkId);
if (implA !== implB || allowAnotherRecipientAddress) {
const shouldBeSendingAccount = recipientMustBeSendingAccount(
inputToken,
outputToken,
allowAnotherRecipientAddress,
);
if (!shouldBeSendingAccount) {
return <TokenInputReceivingAddressField />;
}
}
Expand Down Expand Up @@ -342,7 +350,7 @@ const SendToAnotherAddress = () => {
if (inputToken && outputToken) {
const implA = getNetworkIdImpl(inputToken.networkId);
const implB = getNetworkIdImpl(outputToken.networkId);
if (implA === implB) {
if (implA === implB && implA !== IMPL_SOL) {
return (
<Box
display="flex"
Expand Down
35 changes: 18 additions & 17 deletions packages/kit/src/views/Swap/hooks/useSwap.tsx
Expand Up @@ -7,10 +7,10 @@ import { useAppSelector, useDebounce } from '../../../hooks';
import { SwapError } from '../typings';
import {
formatAmount,
getNetworkIdImpl,
getTokenAmountString,
getTokenAmountValue,
greaterThanZeroOrUndefined,
recipientMustBeSendingAccount,
} from '../utils';

import { useTokenBalance } from './useSwapTokenUtils';
Expand Down Expand Up @@ -67,18 +67,19 @@ export function useSwapRecipient() {
);
return useMemo(() => {
if (inputToken && outputToken) {
const implA = getNetworkIdImpl(inputToken.networkId);
const implB = getNetworkIdImpl(outputToken.networkId);
if (implA === implB && !allowAnotherRecipientAddress) {
if (sendingAccount) {
return {
accountId: sendingAccount.id,
address: sendingAccount.address,
name: sendingAccount.name,
networkId: inputToken.networkId,
networkImpl: inputToken.impl,
};
}
const shouldBeSendingAccount = recipientMustBeSendingAccount(
inputToken,
outputToken,
allowAnotherRecipientAddress,
);
if (shouldBeSendingAccount && sendingAccount) {
return {
accountId: sendingAccount.id,
address: sendingAccount.address,
name: sendingAccount.name,
networkId: inputToken.networkId,
networkImpl: inputToken.impl,
};
}
}
return recipient;
Expand Down Expand Up @@ -155,17 +156,17 @@ export function useDerivedSwapState() {
outputToken,
greaterThanZeroOrUndefined(swapQuote?.buyAmount),
);

const precision = swapQuote?.wrapperTxInfo ? 18 : undefined;
const formattedAmounts = useMemo(() => {
const dependentField = independentField === 'INPUT' ? 'OUTPUT' : 'INPUT';
return {
[independentField]: typedValue,
[dependentField]:
dependentField === 'INPUT'
? formatAmount(inputAmount?.value)
: formatAmount(outputAmount?.value),
? formatAmount(inputAmount?.value, precision)
: formatAmount(outputAmount?.value, precision),
} as { 'INPUT'?: string; 'OUTPUT'?: string };
}, [independentField, inputAmount, outputAmount, typedValue]);
}, [independentField, inputAmount, outputAmount, typedValue, precision]);

return {
inputAmount,
Expand Down
14 changes: 13 additions & 1 deletion packages/kit/src/views/Swap/utils.ts
Expand Up @@ -10,7 +10,7 @@ import {
calculateTotalFeeNative,
calculateTotalFeeRange,
} from '@onekeyhq/engine/src/vaults/utils/feeInfoUtils';
import { IMPL_EVM } from '@onekeyhq/shared/src/engine/engineConsts';
import { IMPL_EVM, IMPL_SOL } from '@onekeyhq/shared/src/engine/engineConsts';

import { QuoterType } from './typings';

Expand Down Expand Up @@ -345,3 +345,15 @@ export function isSimpleTx(tx: TransactionDetails) {
const quoterType = getQuoteType(tx);
return from?.networkId === to?.networkId && quoterType !== QuoterType.swftc;
}

export function recipientMustBeSendingAccount(
tokenA: Token,
tokenB: Token,
allowAnotherRecipientAddress?: boolean,
) {
const implA = getNetworkIdImpl(tokenA.networkId);
const implB = getNetworkIdImpl(tokenB.networkId);
return (
implA === implB && (!allowAnotherRecipientAddress || implA === IMPL_SOL)
);
}

0 comments on commit f04ce80

Please sign in to comment.