Skip to content

Commit

Permalink
Merge pull request #190 from cosmos/fix/validate-microcoin
Browse files Browse the repository at this point in the history
Validate macrocoin, allow empty funds and IBC
  • Loading branch information
abefernan authored Feb 8, 2024
2 parents c3846ca + f8035a5 commit 85cd225
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const TxMsgCreateVestingAccountDetails = ({ msgValue }: TxMsgCreateVestingAccoun
</li>
<li>
<label>Amount:</label>
<div>{printableCoins(msgValue.amount, chain)}</div>
<div>{printableCoins(msgValue.amount, chain) || "None"}</div>
</li>
<li>
<label>From:</label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const TxMsgExecuteContractDetails = ({ msgValue }: TxMsgExecuteContractDetailsPr
</li>
<li>
<label>Funds:</label>
<div>{printableCoins(msgValue.funds, chain)}</div>
<div>{printableCoins(msgValue.funds, chain) || "None"}</div>
</li>
{parseError ? (
<li className="parse-error">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const TxMsgInstantiateContract2Details = ({ msgValue }: TxMsgInstantiateContract
</li>
<li>
<label>Funds:</label>
<div>{printableCoins(msgValue.funds, chain)}</div>
<div>{printableCoins(msgValue.funds, chain) || "None"}</div>
</li>
{parseError ? (
<li className="parse-error">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const TxMsgInstantiateContractDetails = ({ msgValue }: TxMsgInstantiateContractD
</li>
<li>
<label>Funds:</label>
<div>{printableCoins(msgValue.funds, chain)}</div>
<div>{printableCoins(msgValue.funds, chain) || "None"}</div>
</li>
{parseError ? (
<li className="parse-error">
Expand Down
2 changes: 1 addition & 1 deletion components/dataViews/TransactionInfo/TxMsgSendDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const TxMsgSendDetails = ({ msgValue }: TxMsgSendDetailsProps) => {
</li>
<li>
<label>Amount:</label>
<div>{printableCoins(msgValue.amount, chain)}</div>
<div>{printableCoins(msgValue.amount, chain) || "None"}</div>
</li>
<li>
<label>To:</label>
Expand Down
2 changes: 1 addition & 1 deletion components/dataViews/TransactionInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const TransactionInfo = ({ tx }: TransactionInfoProps) => {
</li>
<li>
<label>Fee:</label>
<div>{printableCoins(tx.fee.amount, chain)}</div>
<div>{printableCoins(tx.fee.amount, chain) || "None"}</div>
</li>
</>
) : null}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -58,6 +58,13 @@ const MsgCreateVestingAccountForm = ({
return false;
}

try {
displayCoinToBaseCoin({ 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");
Expand All @@ -69,16 +76,20 @@ const MsgCreateVestingAccountForm = ({

const microCoin = (() => {
try {
return macroCoinToMicroCoin({ denom: chain.displayDenom, amount }, chain.assets);
if (!amount || amount === "0") {
return null;
}

return displayCoinToBaseCoin({ 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,
});
Expand Down
11 changes: 9 additions & 2 deletions components/forms/CreateTxForm/MsgForm/MsgDelegateForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -46,12 +46,19 @@ const MsgDelegateForm = ({ delegatorAddress, setMsgGetter, deleteMsg }: MsgDeleg
return false;
}

try {
displayCoinToBaseCoin({ denom: chain.displayDenom, amount }, chain.assets);
} catch (e: unknown) {
setAmountError(e instanceof Error ? e.message : "Could not set decimals");
return false;
}

return true;
};

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" };
}
Expand Down
33 changes: 25 additions & 8 deletions components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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("");
Expand All @@ -71,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;
}
Expand All @@ -86,17 +93,27 @@ const MsgExecuteContractForm = ({
return false;
}

if (denom && amount) {
try {
displayCoinToBaseCoin({ 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);
if (!denom || !amount || amount === "0") {
return null;
}

return displayCoinToBaseCoin({ denom, amount }, chain.assets);
} catch {
return { denom, amount: "0" };
return null;
}
})();

Expand All @@ -113,7 +130,7 @@ const MsgExecuteContractForm = ({
sender: fromAddress,
contract: contractAddress,
msg: msgContentUtf8Array,
funds: [microCoin],
funds: microCoin ? [microCoin] : [],
});

const msg: MsgExecuteContractEncodeObject = { typeUrl: MsgTypeUrls.Execute, value: msgValue };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
}
Expand All @@ -116,6 +121,13 @@ const MsgInstantiateContract2Form = ({
return false;
}

try {
displayCoinToBaseCoin({ denom, amount }, chain.assets);
} catch (e: unknown) {
setAmountError(e instanceof Error ? e.message : "Could not set decimals");
return false;
}

return true;
};

Expand All @@ -124,9 +136,13 @@ const MsgInstantiateContract2Form = ({

const microCoin = (() => {
try {
return macroCoinToMicroCoin({ denom, amount }, chain.assets);
if (!denom || !amount || amount === "0") {
return null;
}

return displayCoinToBaseCoin({ denom, amount }, chain.assets);
} catch {
return { denom, amount: "0" };
return null;
}
})();

Expand Down Expand Up @@ -155,7 +171,7 @@ const MsgInstantiateContract2Form = ({
fixMsg: false,
salt: hexSalt,
msg: msgContentUtf8Array,
funds: [microCoin],
funds: microCoin ? [microCoin] : [],
});

const msg: MsgInstantiateContract2EncodeObject = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
}
Expand All @@ -102,6 +107,13 @@ const MsgInstantiateContractForm = ({
return false;
}

try {
displayCoinToBaseCoin({ denom, amount }, chain.assets);
} catch (e: unknown) {
setAmountError(e instanceof Error ? e.message : "Could not set decimals");
return false;
}

return true;
};

Expand All @@ -110,9 +122,13 @@ const MsgInstantiateContractForm = ({

const microCoin = (() => {
try {
return macroCoinToMicroCoin({ denom, amount }, chain.assets);
if (!denom || !amount || amount === "0") {
return null;
}

return displayCoinToBaseCoin({ denom, amount }, chain.assets);
} catch {
return { denom, amount: "0" };
return null;
}
})();

Expand All @@ -131,7 +147,7 @@ const MsgInstantiateContractForm = ({
label,
admin: adminAddress,
msg: msgContentUtf8Array,
funds: [microCoin],
funds: microCoin ? [microCoin] : [],
});

const msg: MsgInstantiateContractEncodeObject = {
Expand Down
11 changes: 9 additions & 2 deletions components/forms/CreateTxForm/MsgForm/MsgRedelegateForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -61,12 +61,19 @@ const MsgRedelegateForm = ({
return false;
}

try {
displayCoinToBaseCoin({ denom: chain.displayDenom, amount }, chain.assets);
} catch (e: unknown) {
setAmountError(e instanceof Error ? e.message : "Could not set decimals");
return false;
}

return true;
};

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" };
}
Expand Down
Loading

0 comments on commit 85cd225

Please sign in to comment.