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

Validate macrocoin, allow empty funds and IBC #190

Merged
merged 7 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
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 @@ -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");
Expand All @@ -69,16 +76,20 @@ const MsgCreateVestingAccountForm = ({

const microCoin = (() => {
try {
if (!amount || amount === "0") {
return null;
}

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,
});
Expand Down
7 changes: 7 additions & 0 deletions components/forms/CreateTxForm/MsgForm/MsgDelegateForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
31 changes: 25 additions & 6 deletions components/forms/CreateTxForm/MsgForm/MsgExecuteContractForm.tsx
Original file line number Diff line number Diff line change
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 {
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 {
if (!denom || !amount || amount === "0") {
return null;
}

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

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

console.log({ msgValue });
Copy link
Member

Choose a reason for hiding this comment

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

Should we remove this?


const msg: MsgExecuteContractEncodeObject = { typeUrl: MsgTypeUrls.Execute, value: msgValue };

setMsgGetter({ isMsgValid, msg });
Expand Down
Original file line number Diff line number Diff line change
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 {
macroCoinToMicroCoin({ 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 {
if (!denom || !amount || amount === "0") {
return null;
}

return macroCoinToMicroCoin({ 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 @@ -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 {
macroCoinToMicroCoin({ denom, amount }, chain.assets);
Copy link
Member

Choose a reason for hiding this comment

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

I'm confused by the name of this function because the case in #183 shows we have a base denom in atto.

Should this be renamed to displayCoinToBaseCoin or similar?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah the implementation has exceeded the logic behind the initial name I gave it

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

return macroCoinToMicroCoin({ 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
7 changes: 7 additions & 0 deletions components/forms/CreateTxForm/MsgForm/MsgRedelegateForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
22 changes: 19 additions & 3 deletions components/forms/CreateTxForm/MsgForm/MsgSendForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -72,6 +77,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;
};

Expand All @@ -80,16 +92,20 @@ const MsgSendForm = ({ fromAddress, setMsgGetter, deleteMsg }: MsgSendFormProps)

const microCoin = (() => {
try {
if (!denom || !amount || amount === "0") {
return null;
}

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 };
Expand Down
7 changes: 7 additions & 0 deletions components/forms/CreateTxForm/MsgForm/MsgUndelegateForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
5 changes: 5 additions & 0 deletions lib/coinHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down