diff --git a/wallet/src/common-files/utils/contract.js b/wallet/src/common-files/utils/contract.js index 5da223c66..406f7c470 100644 --- a/wallet/src/common-files/utils/contract.js +++ b/wallet/src/common-files/utils/contract.js @@ -10,6 +10,10 @@ const { proposerUrl } = global.config; const options = global.config.WEB3_OPTIONS; +// This is hardcoded because we just use it for all estimation. +const gasEstimateEndpoint = + 'https://vqxy02tr5e.execute-api.us-east-2.amazonaws.com/production/estimateGas'; + // returns a web3 contract instance export async function getContractInstance(contractName, deployedAddress) { const web3 = Web3.connection(); @@ -41,18 +45,31 @@ export function getContractAddress(contractName) { */ export async function submitTransaction(unsignedTransaction, contractAddress, fee) { const web3 = Web3.connection(); - let gasPrice = 20000000000; - const gas = (await web3.eth.getBlock('latest')).gasLimit; - const blockGasPrice = 2 * Number(await web3.eth.getGasPrice()); - if (blockGasPrice > gasPrice) gasPrice = blockGasPrice; + const blockGasPrice = Number(await web3.eth.getGasPrice()); const from = await Web3.getAccount(); + let proposedGasPrice = blockGasPrice; // This is the backup value if external estimation fails; + try { + // Call the endpoint to estimate the gas fee. + const res = (await axios.get(gasEstimateEndpoint)).data.result; + proposedGasPrice = Number(res?.ProposeGasPrice) * 10 ** 9 || blockGasPrice; + } catch (error) { + console.log('Gas Estimation Failed: ', error); + } + // Estimate the gasLimit + const gasLimit = await web3.eth.estimateGas({ + from, + to: contractAddress, + data: unsignedTransaction, + }); + + const gasLimitWithBuffer = Math.ceil(Number(gasLimit) * 1.1); // 10% seems a reasonable buffer. + const tx = { from, to: contractAddress, data: unsignedTransaction, - gas: web3.utils.toHex(gas), - gasPrice: web3.utils.toHex(gasPrice), - // maxPriorityFeePerGas: web3.utils.toHex(1 * 10 ** 9), + gas: web3.utils.toHex(gasLimitWithBuffer), + gasPrice: web3.utils.toHex(proposedGasPrice), }; if (fee) tx.value = web3.utils.toHex(fee); diff --git a/wallet/src/components/Input/index.tsx b/wallet/src/components/Input/index.tsx index ab2d41079..95c5cbd75 100644 --- a/wallet/src/components/Input/index.tsx +++ b/wallet/src/components/Input/index.tsx @@ -9,7 +9,7 @@ interface InputProps extends InputHTMLAttributes { prefix?: string; } -const Input: React.FC = ({ mask, prefix, ...props }) => { +const Input: React.FC = ({ mask, ...props }) => { const handleKeyUp = useCallback( (e: React.FormEvent) => { if (mask === 'cep') { diff --git a/wallet/src/components/Input/masks.ts b/wallet/src/components/Input/masks.ts index 9ba97976c..5401649ce 100644 --- a/wallet/src/components/Input/masks.ts +++ b/wallet/src/components/Input/masks.ts @@ -1,6 +1,6 @@ import React from 'react'; -export function cep(e: React.FormEvent) { +export function cep(e: React.FormEvent): React.FormEvent { e.currentTarget.maxLength = 9; let { value } = e.currentTarget; value = value.replace(/\D/g, ''); @@ -9,7 +9,7 @@ export function cep(e: React.FormEvent) { return e; } -export function currency(e: React.FormEvent) { +export function currency(e: React.FormEvent): React.FormEvent { let { value } = e.currentTarget; value = value.replace(/\D/g, ''); value = value.replace(/(\d)(\d{2})$/, '$1,$2'); @@ -19,7 +19,7 @@ export function currency(e: React.FormEvent) { return e; } -export function cpf(e: React.FormEvent) { +export function cpf(e: React.FormEvent): React.FormEvent { e.currentTarget.maxLength = 14; let { value } = e.currentTarget; if (!value.match(/^(\d{3}).(\d{3}).(\d{3})-(\d{2})$/)) { diff --git a/wallet/src/hooks/Account/index.tsx b/wallet/src/hooks/Account/index.tsx index bb568de47..47a1153e2 100644 --- a/wallet/src/hooks/Account/index.tsx +++ b/wallet/src/hooks/Account/index.tsx @@ -20,7 +20,8 @@ const AccountContext = createContext({} as AccountContextTyp * context use * @param children */ -const AccountProvider = ({ children }: any) => { +// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types +const AccountProvider = ({ children }: any): JSX.Element => { const [accountInstance, setAccountInstance] = useState(accountType); return ( @@ -38,7 +39,7 @@ const AccountProvider = ({ children }: any) => { /** * Hook to allow the context call */ -const useAccount = () => { +const useAccount = (): AccountContextType => { const context = useContext(AccountContext); if (!context) {