From f00ac897051da81d2cb36bca5def6b527a1fac08 Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Wed, 12 Apr 2023 16:37:07 +0200 Subject: [PATCH] Add gas checks for new txs --- components/forms/ReDelegationForm.tsx | 8 +++++--- components/forms/RewardsForm.tsx | 6 ++++-- components/forms/UnDelegationForm.tsx | 6 ++++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/components/forms/ReDelegationForm.tsx b/components/forms/ReDelegationForm.tsx index b5d4909..91057da 100644 --- a/components/forms/ReDelegationForm.tsx +++ b/components/forms/ReDelegationForm.tsx @@ -3,7 +3,7 @@ import { Account, calculateFee } from "@cosmjs/stargate"; import { assert } from "@cosmjs/utils"; import axios from "axios"; import { NextRouter, withRouter } from "next/router"; -import React, { useState } from "react"; +import { useState } from "react"; import { useAppContext } from "../../context/AppContext"; import { checkAddress, exampleValidatorAddress } from "../../lib/displayHelpers"; import Button from "../inputs/Button"; @@ -32,8 +32,10 @@ const ReDelegationForm = (props: Props) => { txValidatorSrcAddress: string, txValidatorDstAddress: string, txAmount: string, - txGas: number, + gasLimit: number, ) => { + assert(Number.isSafeInteger(gasLimit) && gasLimit > 0, "gas limit must be a positive integer"); + const amountInAtomics = Decimal.fromUserInput( txAmount, Number(state.chain.displayDenomExponent), @@ -52,7 +54,7 @@ const ReDelegationForm = (props: Props) => { value: msgRedelegate, }; assert(gasPrice, "gasPrice missing"); - const fee = calculateFee(Number(txGas), gasPrice); + const fee = calculateFee(gasLimit, gasPrice); const { accountOnChain } = props; assert(accountOnChain, "accountOnChain missing"); return { diff --git a/components/forms/RewardsForm.tsx b/components/forms/RewardsForm.tsx index a3c1cad..c8e3ff7 100644 --- a/components/forms/RewardsForm.tsx +++ b/components/forms/RewardsForm.tsx @@ -27,7 +27,9 @@ const RewardsForm = (props: Props) => { const [_processing, setProcessing] = useState(false); const [addressError, setAddressError] = useState(""); - const createTransaction = (txValidatorAddress: string, txAmount: string, txGas: number) => { + const createTransaction = (txValidatorAddress: string, txAmount: string, gasLimit: number) => { + assert(Number.isSafeInteger(gasLimit) && gasLimit > 0, "gas limit must be a positive integer"); + const amountInAtomics = Decimal.fromUserInput( txAmount, Number(state.chain.displayDenomExponent), @@ -45,7 +47,7 @@ const RewardsForm = (props: Props) => { value: msgDelegatorReward, }; assert(gasPrice, "gasPrice missing"); - const fee = calculateFee(Number(txGas), gasPrice); + const fee = calculateFee(gasLimit, gasPrice); const { accountOnChain } = props; assert(accountOnChain, "accountOnChain missing"); return { diff --git a/components/forms/UnDelegationForm.tsx b/components/forms/UnDelegationForm.tsx index 6696d62..04ef4ba 100644 --- a/components/forms/UnDelegationForm.tsx +++ b/components/forms/UnDelegationForm.tsx @@ -27,7 +27,9 @@ const UnDelegationForm = (props: Props) => { const [_processing, setProcessing] = useState(false); const [addressError, setAddressError] = useState(""); - const createTransaction = (txValidatorAddress: string, txAmount: string, txGas: number) => { + const createTransaction = (txValidatorAddress: string, txAmount: string, gasLimit: number) => { + assert(Number.isSafeInteger(gasLimit) && gasLimit > 0, "gas limit must be a positive integer"); + const amountInAtomics = Decimal.fromUserInput( txAmount, Number(state.chain.displayDenomExponent), @@ -45,7 +47,7 @@ const UnDelegationForm = (props: Props) => { value: msgUndelegate, }; assert(gasPrice, "gasPrice missing"); - const fee = calculateFee(Number(txGas), gasPrice); + const fee = calculateFee(gasLimit, gasPrice); const { accountOnChain } = props; assert(accountOnChain, "accountOnChain missing"); return {