From 8ec1d6f528a1ff26b9eb72129040ad175366c82d Mon Sep 17 00:00:00 2001 From: mmv Date: Tue, 18 Jun 2019 15:55:53 +0400 Subject: [PATCH] refactor createTRansaction method to accept safe settings txs --- src/logic/safe/transactions/send.js | 2 +- .../SendModal/screens/ReviewTx/index.jsx | 180 ++++++++++-------- src/routes/safe/components/Layout.jsx | 1 - src/routes/safe/container/index.jsx | 2 +- .../safe/store/actions/createTransaction.js | 27 +-- 5 files changed, 108 insertions(+), 104 deletions(-) diff --git a/src/logic/safe/transactions/send.js b/src/logic/safe/transactions/send.js index 9252e274..0dad49f7 100644 --- a/src/logic/safe/transactions/send.js +++ b/src/logic/safe/transactions/send.js @@ -12,7 +12,7 @@ const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000' export const executeTransaction = async ( safeInstance: any, to: string, - valueInWei: number, + valueInWei: number | string, data: string, operation: number | string, nonce: string | number, diff --git a/src/routes/safe/components/Balances/SendModal/screens/ReviewTx/index.jsx b/src/routes/safe/components/Balances/SendModal/screens/ReviewTx/index.jsx index 3d2ac30a..45c24258 100644 --- a/src/routes/safe/components/Balances/SendModal/screens/ReviewTx/index.jsx +++ b/src/routes/safe/components/Balances/SendModal/screens/ReviewTx/index.jsx @@ -17,8 +17,12 @@ import { copyToClipboard } from '~/utils/clipboard' import Hairline from '~/components/layout/Hairline' import SafeInfo from '~/routes/safe/components/Balances/SendModal/SafeInfo' import { setImageToPlaceholder } from '~/routes/safe/components/Balances/utils' +import { getStandardTokenContract } from '~/logic/tokens/store/actions/fetchTokens' +import { EMPTY_DATA } from '~/logic/wallets/ethTransactions' +import { getWeb3 } from '~/logic/wallets/getWeb3' import ArrowDown from '../assets/arrow-down.svg' import { secondary } from '~/theme/variables' +import { isEther } from '~/logic/tokens/utils/tokenHelpers' import { styles } from './style' type Props = { @@ -50,87 +54,109 @@ const ReviewTx = ({ createTransaction, }: Props) => ( - {({ openSnackbar }) => ( - - - - Send Funds - - 2 of 2 - - - - - - - - - - Arrow Down - - - - - - - - Recipient + {({ openSnackbar }) => { + const submitTx = async () => { + const web3 = getWeb3() + const isSendingETH = isEther(tx.token.symbol) + const txRecipient = isSendingETH ? tx.recipientAddress : tx.token.address + let txData = EMPTY_DATA + let txAmount = web3.utils.toWei(tx.amount, 'ether') + + + if (!isSendingETH) { + const StandardToken = await getStandardTokenContract() + const tokenInstance = await StandardToken.at(tx.token.address) + + txData = tokenInstance.contract.methods.transfer(tx.recipientAddress, txAmount).encodeABI() + // txAmount should be 0 if we send tokens + // the real value is encoded in txData and will be used by the contract + // if txAmount > 0 it would send ETH from the safe + txAmount = 0 + } + + createTransaction(safeAddress, txRecipient, txAmount, txData, openSnackbar) + onClose() + } + + return ( + + + + Send Funds + 2 of 2 + + + - - - - - - - {tx.recipientAddress} - - - + + + + + + Arrow Down + + + + + + + + Recipient - + + + + + + + + {tx.recipientAddress} + + + + + + + + + Amount + + + + {tx.token.name} + + {tx.amount} + {' '} + {tx.token.symbol} + + + + + + + - - - Amount - - - - {tx.token.name} - - {tx.amount} - {' '} - {tx.token.symbol} - - - - - - - - - - )} + + ) + }} ) diff --git a/src/routes/safe/components/Layout.jsx b/src/routes/safe/components/Layout.jsx index d7c4ac96..ee6cfc94 100644 --- a/src/routes/safe/components/Layout.jsx +++ b/src/routes/safe/components/Layout.jsx @@ -18,7 +18,6 @@ import { sm, xs, secondary, smallFontSize, } from '~/theme/variables' import { copyToClipboard } from '~/utils/clipboard' -import type { Safe } from '~/routes/safe/store/models/safe' import Balances from './Balances' import Settings from './Settings' diff --git a/src/routes/safe/container/index.jsx b/src/routes/safe/container/index.jsx index bd0a7b68..cf6c8378 100644 --- a/src/routes/safe/container/index.jsx +++ b/src/routes/safe/container/index.jsx @@ -11,7 +11,7 @@ export type Props = Actions & granted: boolean, } -const TIMEOUT = process.env.NODE_ENV === 'test' ? 1500 : 15000 +const TIMEOUT = process.env.NODE_ENV === 'test' ? 1500 : 5000 class SafeView extends React.Component { componentDidMount() { diff --git a/src/routes/safe/store/actions/createTransaction.js b/src/routes/safe/store/actions/createTransaction.js index ceec6e1a..6ab785fa 100644 --- a/src/routes/safe/store/actions/createTransaction.js +++ b/src/routes/safe/store/actions/createTransaction.js @@ -1,15 +1,11 @@ // @flow import type { Dispatch as ReduxDispatch, GetState } from 'redux' import { createAction } from 'redux-actions' -import { getWeb3 } from '~/logic/wallets/getWeb3' import { EMPTY_DATA } from '~/logic/wallets/ethTransactions' -import { type Token } from '~/logic/tokens/store/model/token' import { userAccountSelector } from '~/logic/wallets/store/selectors' import { type GlobalState } from '~/store' -import { isEther } from '~/logic/tokens/utils/tokenHelpers' import { getGnosisSafeInstanceAt } from '~/logic/contracts/safeContracts' import { executeTransaction, CALL } from '~/logic/safe/transactions' -import { getStandardTokenContract } from '~/logic/tokens/store/actions/fetchTokens' export const ADD_TRANSACTIONS = 'ADD_TRANSACTIONS' export const addTransactions = createAction(ADD_TRANSACTIONS) @@ -17,40 +13,23 @@ export const addTransactions = createAction(ADD_TRANSACTIONS) const createTransaction = ( safeAddress: string, to: string, - valueInEth: string, - token: Token, - openSnackbar: Function, + valueInWei: string, txData: string = EMPTY_DATA, + openSnackbar: Function, ) => async (dispatch: ReduxDispatch, getState: GetState) => { - const isSendingETH = isEther(token.symbol) const state: GlobalState = getState() const safeInstance = await getGnosisSafeInstanceAt(safeAddress) - const web3 = getWeb3() const from = userAccountSelector(state) const threshold = await safeInstance.getThreshold() const nonce = await safeInstance.nonce() - const txRecipient = isSendingETH ? to : token.address - const valueInWei = web3.utils.toWei(valueInEth, 'ether') - let txAmount = valueInWei const isExecution = threshold.toNumber() === 1 - let txData = EMPTY_DATA - if (!isSendingETH) { - const StandardToken = await getStandardTokenContract() - const sendToken = await StandardToken.at(token.address) - - txData = sendToken.contract.methods.transfer(to, valueInWei).encodeABI() - // txAmount should be 0 if we send tokens - // the real value is encoded in txData and will be used by the contract - // if txAmount > 0 it would send ETH from the safe - txAmount = 0 - } let txHash if (isExecution) { openSnackbar('Transaction has been submitted', 'success') - txHash = await executeTransaction(safeInstance, txRecipient, txAmount, txData, CALL, nonce, from) + txHash = await executeTransaction(safeInstance, to, valueInWei, txData, CALL, nonce, from) openSnackbar('Transaction has been confirmed', 'success') } else { // txHash = await approveTransaction(safeAddress, to, valueInWei, txData, CALL, nonce)