From ea04ae976e188198f49544590feb6c8a339b2d55 Mon Sep 17 00:00:00 2001 From: Mikhail Mikheev Date: Thu, 10 Oct 2019 13:14:16 +0400 Subject: [PATCH] Add gas costs estimation to change owners for multiple owners safe --- package.json | 2 +- .../ChangeThreshold/index.jsx | 43 ++++++++++++++++--- .../Settings/ThresholdSettings/index.jsx | 1 + yarn.lock | 8 ++-- 4 files changed, 44 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index d26e68e2..630aba60 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "@gnosis.pm/util-contracts": "2.0.4", "@material-ui/core": "4.5.0", "@material-ui/icons": "4.4.3", - "@testing-library/jest-dom": "4.1.1", + "@testing-library/jest-dom": "4.1.2", "@welldone-software/why-did-you-render": "3.3.7", "axios": "0.19.0", "bignumber.js": "9.0.0", diff --git a/src/routes/safe/components/Settings/ThresholdSettings/ChangeThreshold/index.jsx b/src/routes/safe/components/Settings/ThresholdSettings/ChangeThreshold/index.jsx index c7a015f4..55554423 100644 --- a/src/routes/safe/components/Settings/ThresholdSettings/ChangeThreshold/index.jsx +++ b/src/routes/safe/components/Settings/ThresholdSettings/ChangeThreshold/index.jsx @@ -1,5 +1,5 @@ // @flow -import React from 'react' +import React, { useState, useEffect } from 'react' import { List } from 'immutable' import { withStyles } from '@material-ui/core/styles' import Close from '@material-ui/icons/Close' @@ -18,11 +18,16 @@ import Block from '~/components/layout/Block' import Row from '~/components/layout/Row' import Col from '~/components/layout/Col' import type { Owner } from '~/routes/safe/store/models/owner' +import { getWeb3 } from '~/logic/wallets/getWeb3' +import { formatAmount } from '~/logic/tokens/utils/formatAmount' +import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts' +import { estimateApprovalTxGasCosts } from '~/logic/safe/transactions/gasNew' import { styles } from './style' type Props = { onClose: () => void, classes: Object, + safeAddress: string, threshold: number, owners: List, onChangeThreshold: Function, @@ -31,8 +36,33 @@ type Props = { const THRESHOLD_FIELD_NAME = 'threshold' const ChangeThreshold = ({ - onClose, owners, threshold, classes, onChangeThreshold, + onClose, owners, threshold, classes, onChangeThreshold, safeAddress, }: Props) => { + const [gasCosts, setGasCosts] = useState('< 0.001') + + useEffect(() => { + let isCurrent = true + const estimateGasCosts = async () => { + const web3 = getWeb3() + const { fromWei, toBN } = web3.utils + + const safeInstance = await getGnosisSafeInstanceAt(safeAddress) + const txData = safeInstance.contract.methods.changeThreshold('1').encodeABI() + const estimatedGasCosts = await estimateApprovalTxGasCosts(safeAddress, safeAddress, txData) + const gasCostsAsEth = fromWei(toBN(estimatedGasCosts), 'ether') + const formattedGasCosts = formatAmount(gasCostsAsEth) + if (isCurrent) { + setGasCosts(formattedGasCosts) + } + } + + estimateGasCosts() + + return () => { + isCurrent = false + } + }, []) + const handleSubmit = async (values) => { const newThreshold = values[THRESHOLD_FIELD_NAME] @@ -62,9 +92,7 @@ const ChangeThreshold = ({ - - Any transaction requires the confirmation of: - + Any transaction requires the confirmation of: @@ -96,6 +124,11 @@ const ChangeThreshold = ({ + + + {`You're about to create a transaction and will have to confirm it with your currently connected wallet. Make sure you have ${gasCosts} (fee price) ETH in this wallet to fund this confirmation.`} + + diff --git a/src/routes/safe/components/Settings/ThresholdSettings/index.jsx b/src/routes/safe/components/Settings/ThresholdSettings/index.jsx index 8afaf2a8..c674d32f 100644 --- a/src/routes/safe/components/Settings/ThresholdSettings/index.jsx +++ b/src/routes/safe/components/Settings/ThresholdSettings/index.jsx @@ -98,6 +98,7 @@ const ThresholdSettings = ({ onClose={toggleModal} owners={owners} threshold={threshold} + safeAddress={safeAddress} onChangeThreshold={onChangeThreshold} /> diff --git a/yarn.lock b/yarn.lock index 9e5143d3..39b1958a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2566,10 +2566,10 @@ pretty-format "^24.8.0" wait-for-expect "^1.3.0" -"@testing-library/jest-dom@4.1.1": - version "4.1.1" - resolved "https://registry.yarnpkg.com/@testing-library/jest-dom/-/jest-dom-4.1.1.tgz#fe063a97784c8e58fc0498869e689151faeceda3" - integrity sha512-4tMKJ6loIPPoERJoeqSWd0CQDsU+RPdXB1V4dgHgUpJVrObjb+TsWTG2VL32tVeUMZoXqk7cUh14YQXceFzfxg== +"@testing-library/jest-dom@4.1.2": + version "4.1.2" + resolved "https://registry.yarnpkg.com/@testing-library/jest-dom/-/jest-dom-4.1.2.tgz#e523047191379abd67cf0896dfd93cabc7e33eab" + integrity sha512-fNf2rCfu0dBD4DmpzqR2ibsaFlFojrWI/EuU8LLqv73CzFIMvT2RMq88p5JVRe4DfeNj0mu0MQ5FTG4mQ0qFaA== dependencies: "@babel/runtime" "^7.5.1" chalk "^2.4.1"