From ed86c3fdd623defa18fe02f7479a6692b48efbe7 Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Thu, 4 Jan 2024 10:50:41 +0100 Subject: [PATCH 1/7] Add macrocoinToMicrocoin to validaiton --- .../CreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx | 7 +++++++ components/forms/CreateTxForm/MsgForm/MsgDelegateForm.tsx | 7 +++++++ .../forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx | 7 +++++++ .../CreateTxForm/MsgForm/MsgInstantiateContract2Form.tsx | 7 +++++++ .../CreateTxForm/MsgForm/MsgInstantiateContractForm.tsx | 7 +++++++ .../forms/CreateTxForm/MsgForm/MsgRedelegateForm.tsx | 7 +++++++ components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx | 7 +++++++ .../forms/CreateTxForm/MsgForm/MsgUndelegateForm.tsx | 7 +++++++ 8 files changed, 56 insertions(+) diff --git a/components/forms/CreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx index f067e54f..687035cd 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx @@ -58,6 +58,13 @@ const MsgCreateVestingAccountForm = ({ return false; } + try { + macroCoinToMicroCoin({ denom: chain.displayDenom, amount }, chain.assets); + } catch (e: unknown) { + setAmountError(e instanceof Error ? e.message : "Could not set decimals"); + return false; + } + const timeoutDate = new Date(Number(timestampFromDatetimeLocal(endTime, "ms"))); if (timeoutDate <= new Date()) { setEndTimeError("End time must be a date in the future"); diff --git a/components/forms/CreateTxForm/MsgForm/MsgDelegateForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgDelegateForm.tsx index 65bb303d..484081bc 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgDelegateForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgDelegateForm.tsx @@ -46,6 +46,13 @@ const MsgDelegateForm = ({ delegatorAddress, setMsgGetter, deleteMsg }: MsgDeleg return false; } + try { + macroCoinToMicroCoin({ denom: chain.displayDenom, amount }, chain.assets); + } catch (e: unknown) { + setAmountError(e instanceof Error ? e.message : "Could not set decimals"); + return false; + } + return true; }; diff --git a/components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx index f6894784..df552bde 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx @@ -86,6 +86,13 @@ const MsgExecuteContractForm = ({ return false; } + try { + macroCoinToMicroCoin({ denom, amount }, chain.assets); + } catch (e: unknown) { + setAmountError(e instanceof Error ? e.message : "Could not set decimals"); + return false; + } + return true; }; diff --git a/components/forms/CreateTxForm/MsgForm/MsgInstantiateContract2Form.tsx b/components/forms/CreateTxForm/MsgForm/MsgInstantiateContract2Form.tsx index a4d73cc1..8b4d1a3b 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgInstantiateContract2Form.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgInstantiateContract2Form.tsx @@ -116,6 +116,13 @@ const MsgInstantiateContract2Form = ({ return false; } + try { + macroCoinToMicroCoin({ denom, amount }, chain.assets); + } catch (e: unknown) { + setAmountError(e instanceof Error ? e.message : "Could not set decimals"); + return false; + } + return true; }; diff --git a/components/forms/CreateTxForm/MsgForm/MsgInstantiateContractForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgInstantiateContractForm.tsx index 25d03ec5..1f394109 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgInstantiateContractForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgInstantiateContractForm.tsx @@ -102,6 +102,13 @@ const MsgInstantiateContractForm = ({ return false; } + try { + macroCoinToMicroCoin({ denom, amount }, chain.assets); + } catch (e: unknown) { + setAmountError(e instanceof Error ? e.message : "Could not set decimals"); + return false; + } + return true; }; diff --git a/components/forms/CreateTxForm/MsgForm/MsgRedelegateForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgRedelegateForm.tsx index 9a905870..28205b2d 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgRedelegateForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgRedelegateForm.tsx @@ -61,6 +61,13 @@ const MsgRedelegateForm = ({ return false; } + try { + macroCoinToMicroCoin({ denom: chain.displayDenom, amount }, chain.assets); + } catch (e: unknown) { + setAmountError(e instanceof Error ? e.message : "Could not set decimals"); + return false; + } + return true; }; diff --git a/components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx index 64f65fe7..4e738db7 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx @@ -72,6 +72,13 @@ const MsgSendForm = ({ fromAddress, setMsgGetter, deleteMsg }: MsgSendFormProps) return false; } + try { + macroCoinToMicroCoin({ denom, amount }, chain.assets); + } catch (e: unknown) { + setAmountError(e instanceof Error ? e.message : "Could not set decimals"); + return false; + } + return true; }; diff --git a/components/forms/CreateTxForm/MsgForm/MsgUndelegateForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgUndelegateForm.tsx index d4b81e66..b3a707bd 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgUndelegateForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgUndelegateForm.tsx @@ -50,6 +50,13 @@ const MsgUndelegateForm = ({ return false; } + try { + macroCoinToMicroCoin({ denom: chain.displayDenom, amount }, chain.assets); + } catch (e: unknown) { + setAmountError(e instanceof Error ? e.message : "Could not set decimals"); + return false; + } + return true; }; From e880aaeb04db58b369ab000404ceeda5bb29a51a Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Mon, 8 Jan 2024 12:04:53 +0100 Subject: [PATCH 2/7] Use empty array for amount 0. Add check to execute --- .../MsgForm/MsgCreateVestingAccountForm.tsx | 4 ++-- .../MsgForm/MsgExecuteContractForm.tsx | 21 ++++++++++--------- .../MsgForm/MsgInstantiateContract2Form.tsx | 4 ++-- .../MsgForm/MsgInstantiateContractForm.tsx | 4 ++-- .../CreateTxForm/MsgForm/MsgSendForm.tsx | 4 ++-- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/components/forms/CreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx index 687035cd..93d55250 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx @@ -78,14 +78,14 @@ const MsgCreateVestingAccountForm = ({ try { return macroCoinToMicroCoin({ denom: chain.displayDenom, amount }, chain.assets); } catch { - return { denom: chain.displayDenom, amount: "0" }; + return null; } })(); const msgValue = MsgCodecs[MsgTypeUrls.CreateVestingAccount].fromPartial({ fromAddress, toAddress, - amount: [microCoin], + amount: microCoin ? [microCoin] : [], endTime: timestampFromDatetimeLocal(endTime, "s"), delayed, }); diff --git a/components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx index df552bde..6437a597 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx @@ -55,6 +55,8 @@ const MsgExecuteContractForm = ({ useEffect(() => { // eslint-disable-next-line no-shadow const { contractAddress, customDenom, amount } = trimmedInputs; + const denom = + selectedDenom.value === customDenomOption.value ? customDenom : selectedDenom.value.symbol; const isMsgValid = (): boolean => { setContractAddressError(""); @@ -86,24 +88,23 @@ const MsgExecuteContractForm = ({ return false; } - try { - macroCoinToMicroCoin({ denom, amount }, chain.assets); - } catch (e: unknown) { - setAmountError(e instanceof Error ? e.message : "Could not set decimals"); - return false; + if (denom && amount) { + try { + macroCoinToMicroCoin({ denom, amount }, chain.assets); + } catch (e: unknown) { + setAmountError(e instanceof Error ? e.message : "Could not set decimals"); + return false; + } } return true; }; - const denom = - selectedDenom.value === customDenomOption.value ? customDenom : selectedDenom.value.symbol; - const microCoin = (() => { try { return macroCoinToMicroCoin({ denom, amount }, chain.assets); } catch { - return { denom, amount: "0" }; + return null; } })(); @@ -120,7 +121,7 @@ const MsgExecuteContractForm = ({ sender: fromAddress, contract: contractAddress, msg: msgContentUtf8Array, - funds: [microCoin], + funds: microCoin ? [microCoin] : [], }); const msg: MsgExecuteContractEncodeObject = { typeUrl: MsgTypeUrls.Execute, value: msgValue }; diff --git a/components/forms/CreateTxForm/MsgForm/MsgInstantiateContract2Form.tsx b/components/forms/CreateTxForm/MsgForm/MsgInstantiateContract2Form.tsx index 8b4d1a3b..ff8edf86 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgInstantiateContract2Form.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgInstantiateContract2Form.tsx @@ -133,7 +133,7 @@ const MsgInstantiateContract2Form = ({ try { return macroCoinToMicroCoin({ denom, amount }, chain.assets); } catch { - return { denom, amount: "0" }; + return null; } })(); @@ -162,7 +162,7 @@ const MsgInstantiateContract2Form = ({ fixMsg: false, salt: hexSalt, msg: msgContentUtf8Array, - funds: [microCoin], + funds: microCoin ? [microCoin] : [], }); const msg: MsgInstantiateContract2EncodeObject = { diff --git a/components/forms/CreateTxForm/MsgForm/MsgInstantiateContractForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgInstantiateContractForm.tsx index 1f394109..0b478eee 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgInstantiateContractForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgInstantiateContractForm.tsx @@ -119,7 +119,7 @@ const MsgInstantiateContractForm = ({ try { return macroCoinToMicroCoin({ denom, amount }, chain.assets); } catch { - return { denom, amount: "0" }; + return null; } })(); @@ -138,7 +138,7 @@ const MsgInstantiateContractForm = ({ label, admin: adminAddress, msg: msgContentUtf8Array, - funds: [microCoin], + funds: microCoin ? [microCoin] : [], }); const msg: MsgInstantiateContractEncodeObject = { diff --git a/components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx index 4e738db7..b17d746a 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx @@ -89,14 +89,14 @@ const MsgSendForm = ({ fromAddress, setMsgGetter, deleteMsg }: MsgSendFormProps) try { return macroCoinToMicroCoin({ denom, amount }, chain.assets); } catch { - return { denom, amount: "0" }; + return null; } })(); const msgValue = MsgCodecs[MsgTypeUrls.Send].fromPartial({ fromAddress, toAddress, - amount: [microCoin], + amount: microCoin ? [microCoin] : [], }); const msg: MsgSendEncodeObject = { typeUrl: MsgTypeUrls.Send, value: msgValue }; From 3cfb2c15b71b6678fc1073a11067f705854e3291 Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Mon, 8 Jan 2024 12:43:05 +0100 Subject: [PATCH 3/7] Check for null microCoin and allow empty custom denom --- .../MsgForm/MsgCreateVestingAccountForm.tsx | 4 ++++ .../CreateTxForm/MsgForm/MsgExecuteContractForm.tsx | 13 ++++++++++++- .../MsgForm/MsgInstantiateContract2Form.tsx | 11 ++++++++++- .../MsgForm/MsgInstantiateContractForm.tsx | 11 ++++++++++- .../forms/CreateTxForm/MsgForm/MsgSendForm.tsx | 11 ++++++++++- 5 files changed, 46 insertions(+), 4 deletions(-) diff --git a/components/forms/CreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx index 93d55250..eafa188b 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx @@ -76,6 +76,10 @@ const MsgCreateVestingAccountForm = ({ const microCoin = (() => { try { + if (!amount || amount === "0") { + return null; + } + return macroCoinToMicroCoin({ denom: chain.displayDenom, amount }, chain.assets); } catch { return null; diff --git a/components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx index 6437a597..c77736ab 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx @@ -73,7 +73,12 @@ const MsgExecuteContractForm = ({ return false; } - if (selectedDenom.value === customDenomOption.value && !customDenom) { + if ( + selectedDenom.value === customDenomOption.value && + !customDenom && + amount && + amount !== "0" + ) { setCustomDenomError("Custom denom must be set because of selection above"); return false; } @@ -102,6 +107,10 @@ const MsgExecuteContractForm = ({ const microCoin = (() => { try { + if (!denom || !amount || amount === "0") { + return null; + } + return macroCoinToMicroCoin({ denom, amount }, chain.assets); } catch { return null; @@ -124,6 +133,8 @@ const MsgExecuteContractForm = ({ funds: microCoin ? [microCoin] : [], }); + console.log({ msgValue }); + const msg: MsgExecuteContractEncodeObject = { typeUrl: MsgTypeUrls.Execute, value: msgValue }; setMsgGetter({ isMsgValid, msg }); diff --git a/components/forms/CreateTxForm/MsgForm/MsgInstantiateContract2Form.tsx b/components/forms/CreateTxForm/MsgForm/MsgInstantiateContract2Form.tsx index ff8edf86..9505e0c3 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgInstantiateContract2Form.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgInstantiateContract2Form.tsx @@ -101,7 +101,12 @@ const MsgInstantiateContract2Form = ({ return false; } - if (selectedDenom.value === customDenomOption.value && !customDenom) { + if ( + selectedDenom.value === customDenomOption.value && + !customDenom && + amount && + amount !== "0" + ) { setCustomDenomError("Custom denom must be set because of selection above"); return false; } @@ -131,6 +136,10 @@ const MsgInstantiateContract2Form = ({ const microCoin = (() => { try { + if (!denom || !amount || amount === "0") { + return null; + } + return macroCoinToMicroCoin({ denom, amount }, chain.assets); } catch { return null; diff --git a/components/forms/CreateTxForm/MsgForm/MsgInstantiateContractForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgInstantiateContractForm.tsx index 0b478eee..7ce77d46 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgInstantiateContractForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgInstantiateContractForm.tsx @@ -87,7 +87,12 @@ const MsgInstantiateContractForm = ({ return false; } - if (selectedDenom.value === customDenomOption.value && !customDenom) { + if ( + selectedDenom.value === customDenomOption.value && + !customDenom && + amount && + amount !== "0" + ) { setCustomDenomError("Custom denom must be set because of selection above"); return false; } @@ -117,6 +122,10 @@ const MsgInstantiateContractForm = ({ const microCoin = (() => { try { + if (!denom || !amount || amount === "0") { + return null; + } + return macroCoinToMicroCoin({ denom, amount }, chain.assets); } catch { return null; diff --git a/components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx index b17d746a..cd289073 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx @@ -57,7 +57,12 @@ const MsgSendForm = ({ fromAddress, setMsgGetter, deleteMsg }: MsgSendFormProps) return false; } - if (selectedDenom.value === customDenomOption.value && !customDenom) { + if ( + selectedDenom.value === customDenomOption.value && + !customDenom && + amount && + amount !== "0" + ) { setCustomDenomError("Custom denom must be set because of selection above"); return false; } @@ -87,6 +92,10 @@ const MsgSendForm = ({ fromAddress, setMsgGetter, deleteMsg }: MsgSendFormProps) const microCoin = (() => { try { + if (!denom || !amount || amount === "0") { + return null; + } + return macroCoinToMicroCoin({ denom, amount }, chain.assets); } catch { return null; From 63ae9784bc90a120e1158bca2cd453238da78555 Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Mon, 8 Jan 2024 12:47:07 +0100 Subject: [PATCH 4/7] Show "None" when funds empty in Tx detail --- .../TransactionInfo/TxMsgCreateVestingAccountDetails.tsx | 2 +- .../dataViews/TransactionInfo/TxMsgExecuteContractDetails.tsx | 2 +- .../TransactionInfo/TxMsgInstantiateContract2Details.tsx | 2 +- .../TransactionInfo/TxMsgInstantiateContractDetails.tsx | 2 +- components/dataViews/TransactionInfo/TxMsgSendDetails.tsx | 2 +- components/dataViews/TransactionInfo/index.tsx | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/components/dataViews/TransactionInfo/TxMsgCreateVestingAccountDetails.tsx b/components/dataViews/TransactionInfo/TxMsgCreateVestingAccountDetails.tsx index 18efc7ab..ba902567 100644 --- a/components/dataViews/TransactionInfo/TxMsgCreateVestingAccountDetails.tsx +++ b/components/dataViews/TransactionInfo/TxMsgCreateVestingAccountDetails.tsx @@ -20,7 +20,7 @@ const TxMsgCreateVestingAccountDetails = ({ msgValue }: TxMsgCreateVestingAccoun
  • -
    {printableCoins(msgValue.amount, chain)}
    +
    {printableCoins(msgValue.amount, chain) || "None"}
  • diff --git a/components/dataViews/TransactionInfo/TxMsgExecuteContractDetails.tsx b/components/dataViews/TransactionInfo/TxMsgExecuteContractDetails.tsx index e1dee347..97ede4db 100644 --- a/components/dataViews/TransactionInfo/TxMsgExecuteContractDetails.tsx +++ b/components/dataViews/TransactionInfo/TxMsgExecuteContractDetails.tsx @@ -43,7 +43,7 @@ const TxMsgExecuteContractDetails = ({ msgValue }: TxMsgExecuteContractDetailsPr
  • -
    {printableCoins(msgValue.funds, chain)}
    +
    {printableCoins(msgValue.funds, chain) || "None"}
  • {parseError ? (
  • diff --git a/components/dataViews/TransactionInfo/TxMsgInstantiateContract2Details.tsx b/components/dataViews/TransactionInfo/TxMsgInstantiateContract2Details.tsx index aad8c316..39dfc536 100644 --- a/components/dataViews/TransactionInfo/TxMsgInstantiateContract2Details.tsx +++ b/components/dataViews/TransactionInfo/TxMsgInstantiateContract2Details.tsx @@ -59,7 +59,7 @@ const TxMsgInstantiateContract2Details = ({ msgValue }: TxMsgInstantiateContract
  • -
    {printableCoins(msgValue.funds, chain)}
    +
    {printableCoins(msgValue.funds, chain) || "None"}
  • {parseError ? (
  • diff --git a/components/dataViews/TransactionInfo/TxMsgInstantiateContractDetails.tsx b/components/dataViews/TransactionInfo/TxMsgInstantiateContractDetails.tsx index 3ad54de4..105a3eb5 100644 --- a/components/dataViews/TransactionInfo/TxMsgInstantiateContractDetails.tsx +++ b/components/dataViews/TransactionInfo/TxMsgInstantiateContractDetails.tsx @@ -55,7 +55,7 @@ const TxMsgInstantiateContractDetails = ({ msgValue }: TxMsgInstantiateContractD
  • -
    {printableCoins(msgValue.funds, chain)}
    +
    {printableCoins(msgValue.funds, chain) || "None"}
  • {parseError ? (
  • diff --git a/components/dataViews/TransactionInfo/TxMsgSendDetails.tsx b/components/dataViews/TransactionInfo/TxMsgSendDetails.tsx index 8a1561f5..fda8231f 100644 --- a/components/dataViews/TransactionInfo/TxMsgSendDetails.tsx +++ b/components/dataViews/TransactionInfo/TxMsgSendDetails.tsx @@ -17,7 +17,7 @@ const TxMsgSendDetails = ({ msgValue }: TxMsgSendDetailsProps) => {
  • -
    {printableCoins(msgValue.amount, chain)}
    +
    {printableCoins(msgValue.amount, chain) || "None"}
  • diff --git a/components/dataViews/TransactionInfo/index.tsx b/components/dataViews/TransactionInfo/index.tsx index 08196610..e0a959b5 100644 --- a/components/dataViews/TransactionInfo/index.tsx +++ b/components/dataViews/TransactionInfo/index.tsx @@ -68,7 +68,7 @@ const TransactionInfo = ({ tx }: TransactionInfoProps) => {
  • -
    {printableCoins(tx.fee.amount, chain)}
    +
    {printableCoins(tx.fee.amount, chain) || "None"}
  • ) : null} From 24b2b20135ee83478ef0e1d206072b5da97aa8dd Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Mon, 8 Jan 2024 13:04:23 +0100 Subject: [PATCH 5/7] Allow unregistered IBC tokens --- lib/coinHelpers.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/coinHelpers.ts b/lib/coinHelpers.ts index c20eac6d..8ae529e3 100644 --- a/lib/coinHelpers.ts +++ b/lib/coinHelpers.ts @@ -17,6 +17,11 @@ const macroCoinToMicroCoin = (macroCoin: Coin, assets: readonly RegistryAsset[]) ), ); + // Leave IBC coins as is if not found on registry assets + if (!asset && macroCoin.denom.toLowerCase().startsWith("ibc/")) { + return macroCoin; + } + assert(asset, `An asset with the given symbol ${macroCoin.denom} was not found`); const macroUnit = asset.denom_units.find( From 51128c1d4f38d071127b2c5a4ef760df7375e637 Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Thu, 8 Feb 2024 11:58:14 +0100 Subject: [PATCH 6/7] Remove console.log --- .../forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx | 2 -- 1 file changed, 2 deletions(-) diff --git a/components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx index c77736ab..413997b9 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx @@ -133,8 +133,6 @@ const MsgExecuteContractForm = ({ funds: microCoin ? [microCoin] : [], }); - console.log({ msgValue }); - const msg: MsgExecuteContractEncodeObject = { typeUrl: MsgTypeUrls.Execute, value: msgValue }; setMsgGetter({ isMsgValid, msg }); From f8035a5808190eb9c1ac0799369baefc8d639183 Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Thu, 8 Feb 2024 12:23:34 +0100 Subject: [PATCH 7/7] Rename macroCoinToMicroCoin to displayCoinToBaseCoin --- .../MsgForm/MsgCreateVestingAccountForm.tsx | 6 ++-- .../CreateTxForm/MsgForm/MsgDelegateForm.tsx | 6 ++-- .../MsgForm/MsgExecuteContractForm.tsx | 6 ++-- .../MsgForm/MsgInstantiateContract2Form.tsx | 6 ++-- .../MsgForm/MsgInstantiateContractForm.tsx | 6 ++-- .../MsgForm/MsgRedelegateForm.tsx | 6 ++-- .../CreateTxForm/MsgForm/MsgSendForm.tsx | 6 ++-- .../MsgForm/MsgUndelegateForm.tsx | 6 ++-- lib/coinHelpers.spec.ts | 30 +++++++++---------- lib/coinHelpers.ts | 17 ++++++----- 10 files changed, 49 insertions(+), 46 deletions(-) diff --git a/components/forms/CreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx index eafa188b..13d33a2e 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgCreateVestingAccountForm.tsx @@ -2,7 +2,7 @@ import { EncodeObject } from "@cosmjs/proto-signing"; import { useEffect, useState } from "react"; import { MsgGetter } from ".."; import { useChains } from "../../../../context/ChainsContext"; -import { macroCoinToMicroCoin } from "../../../../lib/coinHelpers"; +import { displayCoinToBaseCoin } from "../../../../lib/coinHelpers"; import { datetimeLocalFromTimestamp, timestampFromDatetimeLocal, @@ -59,7 +59,7 @@ const MsgCreateVestingAccountForm = ({ } try { - macroCoinToMicroCoin({ denom: chain.displayDenom, amount }, chain.assets); + displayCoinToBaseCoin({ denom: chain.displayDenom, amount }, chain.assets); } catch (e: unknown) { setAmountError(e instanceof Error ? e.message : "Could not set decimals"); return false; @@ -80,7 +80,7 @@ const MsgCreateVestingAccountForm = ({ return null; } - return macroCoinToMicroCoin({ denom: chain.displayDenom, amount }, chain.assets); + return displayCoinToBaseCoin({ denom: chain.displayDenom, amount }, chain.assets); } catch { return null; } diff --git a/components/forms/CreateTxForm/MsgForm/MsgDelegateForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgDelegateForm.tsx index 484081bc..7a68c6e5 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgDelegateForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgDelegateForm.tsx @@ -2,7 +2,7 @@ import { MsgDelegateEncodeObject } from "@cosmjs/stargate"; import { useEffect, useState } from "react"; import { MsgGetter } from ".."; import { useChains } from "../../../../context/ChainsContext"; -import { macroCoinToMicroCoin } from "../../../../lib/coinHelpers"; +import { displayCoinToBaseCoin } from "../../../../lib/coinHelpers"; import { checkAddress, exampleAddress, trimStringsObj } from "../../../../lib/displayHelpers"; import { MsgCodecs, MsgTypeUrls } from "../../../../types/txMsg"; import Input from "../../../inputs/Input"; @@ -47,7 +47,7 @@ const MsgDelegateForm = ({ delegatorAddress, setMsgGetter, deleteMsg }: MsgDeleg } try { - macroCoinToMicroCoin({ denom: chain.displayDenom, amount }, chain.assets); + displayCoinToBaseCoin({ denom: chain.displayDenom, amount }, chain.assets); } catch (e: unknown) { setAmountError(e instanceof Error ? e.message : "Could not set decimals"); return false; @@ -58,7 +58,7 @@ const MsgDelegateForm = ({ delegatorAddress, setMsgGetter, deleteMsg }: MsgDeleg const microCoin = (() => { try { - return macroCoinToMicroCoin({ denom: chain.displayDenom, amount }, chain.assets); + return displayCoinToBaseCoin({ denom: chain.displayDenom, amount }, chain.assets); } catch { return { denom: chain.displayDenom, amount: "0" }; } diff --git a/components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx index 413997b9..ec795bce 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx @@ -5,7 +5,7 @@ import { useEffect, useRef, useState } from "react"; import { MsgGetter } from ".."; import { useChains } from "../../../../context/ChainsContext"; import { ChainInfo } from "../../../../context/ChainsContext/types"; -import { macroCoinToMicroCoin } from "../../../../lib/coinHelpers"; +import { displayCoinToBaseCoin } from "../../../../lib/coinHelpers"; import { checkAddress, exampleAddress, trimStringsObj } from "../../../../lib/displayHelpers"; import { MsgCodecs, MsgTypeUrls } from "../../../../types/txMsg"; import Input from "../../../inputs/Input"; @@ -95,7 +95,7 @@ const MsgExecuteContractForm = ({ if (denom && amount) { try { - macroCoinToMicroCoin({ denom, amount }, chain.assets); + displayCoinToBaseCoin({ denom, amount }, chain.assets); } catch (e: unknown) { setAmountError(e instanceof Error ? e.message : "Could not set decimals"); return false; @@ -111,7 +111,7 @@ const MsgExecuteContractForm = ({ return null; } - return macroCoinToMicroCoin({ denom, amount }, chain.assets); + return displayCoinToBaseCoin({ denom, amount }, chain.assets); } catch { return null; } diff --git a/components/forms/CreateTxForm/MsgForm/MsgInstantiateContract2Form.tsx b/components/forms/CreateTxForm/MsgForm/MsgInstantiateContract2Form.tsx index 9505e0c3..d2d9ab25 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgInstantiateContract2Form.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgInstantiateContract2Form.tsx @@ -5,7 +5,7 @@ import { useEffect, useRef, useState } from "react"; import { MsgGetter } from ".."; import { useChains } from "../../../../context/ChainsContext"; import { ChainInfo } from "../../../../context/ChainsContext/types"; -import { macroCoinToMicroCoin } from "../../../../lib/coinHelpers"; +import { displayCoinToBaseCoin } from "../../../../lib/coinHelpers"; import { checkAddress, exampleAddress, trimStringsObj } from "../../../../lib/displayHelpers"; import { MsgCodecs, MsgTypeUrls } from "../../../../types/txMsg"; import Input from "../../../inputs/Input"; @@ -122,7 +122,7 @@ const MsgInstantiateContract2Form = ({ } try { - macroCoinToMicroCoin({ denom, amount }, chain.assets); + displayCoinToBaseCoin({ denom, amount }, chain.assets); } catch (e: unknown) { setAmountError(e instanceof Error ? e.message : "Could not set decimals"); return false; @@ -140,7 +140,7 @@ const MsgInstantiateContract2Form = ({ return null; } - return macroCoinToMicroCoin({ denom, amount }, chain.assets); + return displayCoinToBaseCoin({ denom, amount }, chain.assets); } catch { return null; } diff --git a/components/forms/CreateTxForm/MsgForm/MsgInstantiateContractForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgInstantiateContractForm.tsx index 7ce77d46..256b915c 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgInstantiateContractForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgInstantiateContractForm.tsx @@ -5,7 +5,7 @@ import { useEffect, useRef, useState } from "react"; import { MsgGetter } from ".."; import { useChains } from "../../../../context/ChainsContext"; import { ChainInfo } from "../../../../context/ChainsContext/types"; -import { macroCoinToMicroCoin } from "../../../../lib/coinHelpers"; +import { displayCoinToBaseCoin } from "../../../../lib/coinHelpers"; import { checkAddress, exampleAddress, trimStringsObj } from "../../../../lib/displayHelpers"; import { MsgCodecs, MsgTypeUrls } from "../../../../types/txMsg"; import Input from "../../../inputs/Input"; @@ -108,7 +108,7 @@ const MsgInstantiateContractForm = ({ } try { - macroCoinToMicroCoin({ denom, amount }, chain.assets); + displayCoinToBaseCoin({ denom, amount }, chain.assets); } catch (e: unknown) { setAmountError(e instanceof Error ? e.message : "Could not set decimals"); return false; @@ -126,7 +126,7 @@ const MsgInstantiateContractForm = ({ return null; } - return macroCoinToMicroCoin({ denom, amount }, chain.assets); + return displayCoinToBaseCoin({ denom, amount }, chain.assets); } catch { return null; } diff --git a/components/forms/CreateTxForm/MsgForm/MsgRedelegateForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgRedelegateForm.tsx index 28205b2d..c0af5f07 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgRedelegateForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgRedelegateForm.tsx @@ -2,7 +2,7 @@ import { EncodeObject } from "@cosmjs/proto-signing"; import { useEffect, useState } from "react"; import { MsgGetter } from ".."; import { useChains } from "../../../../context/ChainsContext"; -import { macroCoinToMicroCoin } from "../../../../lib/coinHelpers"; +import { displayCoinToBaseCoin } from "../../../../lib/coinHelpers"; import { checkAddress, exampleAddress, trimStringsObj } from "../../../../lib/displayHelpers"; import { MsgCodecs, MsgTypeUrls } from "../../../../types/txMsg"; import Input from "../../../inputs/Input"; @@ -62,7 +62,7 @@ const MsgRedelegateForm = ({ } try { - macroCoinToMicroCoin({ denom: chain.displayDenom, amount }, chain.assets); + displayCoinToBaseCoin({ denom: chain.displayDenom, amount }, chain.assets); } catch (e: unknown) { setAmountError(e instanceof Error ? e.message : "Could not set decimals"); return false; @@ -73,7 +73,7 @@ const MsgRedelegateForm = ({ const microCoin = (() => { try { - return macroCoinToMicroCoin({ denom: chain.displayDenom, amount }, chain.assets); + return displayCoinToBaseCoin({ denom: chain.displayDenom, amount }, chain.assets); } catch { return { denom: chain.displayDenom, amount: "0" }; } diff --git a/components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx index cd289073..02f7c50d 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx @@ -2,7 +2,7 @@ import { MsgSendEncodeObject } from "@cosmjs/stargate"; import { useEffect, useState } from "react"; import { MsgGetter } from ".."; import { useChains } from "../../../../context/ChainsContext"; -import { macroCoinToMicroCoin } from "../../../../lib/coinHelpers"; +import { displayCoinToBaseCoin } from "../../../../lib/coinHelpers"; import { checkAddress, exampleAddress, trimStringsObj } from "../../../../lib/displayHelpers"; import { RegistryAsset } from "../../../../types/chainRegistry"; import { MsgCodecs, MsgTypeUrls } from "../../../../types/txMsg"; @@ -78,7 +78,7 @@ const MsgSendForm = ({ fromAddress, setMsgGetter, deleteMsg }: MsgSendFormProps) } try { - macroCoinToMicroCoin({ denom, amount }, chain.assets); + displayCoinToBaseCoin({ denom, amount }, chain.assets); } catch (e: unknown) { setAmountError(e instanceof Error ? e.message : "Could not set decimals"); return false; @@ -96,7 +96,7 @@ const MsgSendForm = ({ fromAddress, setMsgGetter, deleteMsg }: MsgSendFormProps) return null; } - return macroCoinToMicroCoin({ denom, amount }, chain.assets); + return displayCoinToBaseCoin({ denom, amount }, chain.assets); } catch { return null; } diff --git a/components/forms/CreateTxForm/MsgForm/MsgUndelegateForm.tsx b/components/forms/CreateTxForm/MsgForm/MsgUndelegateForm.tsx index b3a707bd..b6658513 100644 --- a/components/forms/CreateTxForm/MsgForm/MsgUndelegateForm.tsx +++ b/components/forms/CreateTxForm/MsgForm/MsgUndelegateForm.tsx @@ -2,7 +2,7 @@ import { MsgUndelegateEncodeObject } from "@cosmjs/stargate"; import { useEffect, useState } from "react"; import { MsgGetter } from ".."; import { useChains } from "../../../../context/ChainsContext"; -import { macroCoinToMicroCoin } from "../../../../lib/coinHelpers"; +import { displayCoinToBaseCoin } from "../../../../lib/coinHelpers"; import { checkAddress, exampleAddress, trimStringsObj } from "../../../../lib/displayHelpers"; import { MsgCodecs, MsgTypeUrls } from "../../../../types/txMsg"; import Input from "../../../inputs/Input"; @@ -51,7 +51,7 @@ const MsgUndelegateForm = ({ } try { - macroCoinToMicroCoin({ denom: chain.displayDenom, amount }, chain.assets); + displayCoinToBaseCoin({ denom: chain.displayDenom, amount }, chain.assets); } catch (e: unknown) { setAmountError(e instanceof Error ? e.message : "Could not set decimals"); return false; @@ -62,7 +62,7 @@ const MsgUndelegateForm = ({ const microCoin = (() => { try { - return macroCoinToMicroCoin({ denom: chain.displayDenom, amount }, chain.assets); + return displayCoinToBaseCoin({ denom: chain.displayDenom, amount }, chain.assets); } catch { return { denom: chain.displayDenom, amount: "0" }; } diff --git a/lib/coinHelpers.spec.ts b/lib/coinHelpers.spec.ts index 61b59319..702905ab 100644 --- a/lib/coinHelpers.spec.ts +++ b/lib/coinHelpers.spec.ts @@ -1,5 +1,5 @@ import { RegistryAsset } from "../types/chainRegistry"; -import { macroCoinToMicroCoin } from "./coinHelpers"; +import { displayCoinToBaseCoin } from "./coinHelpers"; const assets: readonly RegistryAsset[] = [ { @@ -75,70 +75,70 @@ const assets: readonly RegistryAsset[] = [ describe("macroCoinToMicroCoin", () => { it("works with symbol", () => { - expect(macroCoinToMicroCoin({ denom: "JUNOX", amount: "12" }, assets)).toEqual({ + expect(displayCoinToBaseCoin({ denom: "JUNOX", amount: "12" }, assets)).toEqual({ denom: "ujunox", amount: "12000000", }); - expect(macroCoinToMicroCoin({ denom: "CHEQ", amount: "34" }, assets)).toEqual({ + expect(displayCoinToBaseCoin({ denom: "CHEQ", amount: "34" }, assets)).toEqual({ denom: "ncheq", amount: "34000000000", }); - expect(macroCoinToMicroCoin({ denom: "arUSD", amount: "56" }, assets)).toEqual({ + expect(displayCoinToBaseCoin({ denom: "arUSD", amount: "56" }, assets)).toEqual({ denom: "erc20/0x2Cbea61fdfDFA520Ee99700F104D5b75ADf50B0c", amount: "56000000000000000000", }); - expect(macroCoinToMicroCoin({ denom: "ETH", amount: "78" }, assets)).toEqual({ + expect(displayCoinToBaseCoin({ denom: "ETH", amount: "78" }, assets)).toEqual({ denom: "wei", amount: "78000000000000000000", }); }); it("works with base unit", () => { - expect(macroCoinToMicroCoin({ denom: "ujunox", amount: "12" }, assets)).toEqual({ + expect(displayCoinToBaseCoin({ denom: "ujunox", amount: "12" }, assets)).toEqual({ denom: "ujunox", amount: "12", }); - expect(macroCoinToMicroCoin({ denom: "ncheq", amount: "34" }, assets)).toEqual({ + expect(displayCoinToBaseCoin({ denom: "ncheq", amount: "34" }, assets)).toEqual({ denom: "ncheq", amount: "34", }); expect( - macroCoinToMicroCoin( + displayCoinToBaseCoin( { denom: "erc20/0x2Cbea61fdfDFA520Ee99700F104D5b75ADf50B0c", amount: "56" }, assets, ), ).toEqual({ denom: "erc20/0x2Cbea61fdfDFA520Ee99700F104D5b75ADf50B0c", amount: "56" }); - expect(macroCoinToMicroCoin({ denom: "wei", amount: "78" }, assets)).toEqual({ + expect(displayCoinToBaseCoin({ denom: "wei", amount: "78" }, assets)).toEqual({ denom: "wei", amount: "78", }); }); it("works with biggest unit", () => { - expect(macroCoinToMicroCoin({ denom: "junox", amount: "12" }, assets)).toEqual({ + expect(displayCoinToBaseCoin({ denom: "junox", amount: "12" }, assets)).toEqual({ denom: "ujunox", amount: "12000000", }); - expect(macroCoinToMicroCoin({ denom: "cheq", amount: "34" }, assets)).toEqual({ + expect(displayCoinToBaseCoin({ denom: "cheq", amount: "34" }, assets)).toEqual({ denom: "ncheq", amount: "34000000000", }); - expect(macroCoinToMicroCoin({ denom: "arusd", amount: "56" }, assets)).toEqual({ + expect(displayCoinToBaseCoin({ denom: "arusd", amount: "56" }, assets)).toEqual({ denom: "erc20/0x2Cbea61fdfDFA520Ee99700F104D5b75ADf50B0c", amount: "56000000000000000000", }); - expect(macroCoinToMicroCoin({ denom: "eth", amount: "78" }, assets)).toEqual({ + expect(displayCoinToBaseCoin({ denom: "eth", amount: "78" }, assets)).toEqual({ denom: "wei", amount: "78000000000000000000", }); }); it("works with intermediate unit", () => { - expect(macroCoinToMicroCoin({ denom: "gwei", amount: "78" }, assets)).toEqual({ + expect(displayCoinToBaseCoin({ denom: "gwei", amount: "78" }, assets)).toEqual({ denom: "wei", amount: "78000000000", }); - expect(macroCoinToMicroCoin({ denom: "GWEI", amount: "78" }, assets)).toEqual({ + expect(displayCoinToBaseCoin({ denom: "GWEI", amount: "78" }, assets)).toEqual({ denom: "wei", amount: "78000000000", }); diff --git a/lib/coinHelpers.ts b/lib/coinHelpers.ts index 8ae529e3..d2dc5bd5 100644 --- a/lib/coinHelpers.ts +++ b/lib/coinHelpers.ts @@ -3,8 +3,8 @@ import { Coin } from "@cosmjs/stargate"; import { assert } from "@cosmjs/utils"; import { RegistryAsset } from "../types/chainRegistry"; -const macroCoinToMicroCoin = (macroCoin: Coin, assets: readonly RegistryAsset[]): Coin => { - const lowerCaseDenom = macroCoin.denom.toLowerCase(); +const displayCoinToBaseCoin = (displayCoin: Coin, assets: readonly RegistryAsset[]): Coin => { + const lowerCaseDenom = displayCoin.denom.toLowerCase(); const asset = assets.find( (currentAsset) => @@ -18,11 +18,11 @@ const macroCoinToMicroCoin = (macroCoin: Coin, assets: readonly RegistryAsset[]) ); // Leave IBC coins as is if not found on registry assets - if (!asset && macroCoin.denom.toLowerCase().startsWith("ibc/")) { - return macroCoin; + if (!asset && displayCoin.denom.toLowerCase().startsWith("ibc/")) { + return displayCoin; } - assert(asset, `An asset with the given symbol ${macroCoin.denom} was not found`); + assert(asset, `An asset with the given symbol ${displayCoin.denom} was not found`); const macroUnit = asset.denom_units.find( (currentUnit) => lowerCaseDenom === currentUnit.denom.toLowerCase(), @@ -33,9 +33,12 @@ const macroCoinToMicroCoin = (macroCoin: Coin, assets: readonly RegistryAsset[]) assert(baseUnit, `A base unit with exponent = 0 was not found`); const denom = baseUnit.denom; - const amount = Decimal.fromUserInput(macroCoin.amount.trim() || "0", macroUnit.exponent).atomics; + const amount = Decimal.fromUserInput( + displayCoin.amount.trim() || "0", + macroUnit.exponent, + ).atomics; return { denom, amount }; }; -export { macroCoinToMicroCoin }; +export { displayCoinToBaseCoin };