From ad183dfabb9c9743df96e09f836311fdc87277f6 Mon Sep 17 00:00:00 2001 From: Mikhail Mikheev Date: Fri, 24 May 2019 18:10:47 +0400 Subject: [PATCH] createTransaction action wip --- src/logic/safe/transactions/send.js | 2 +- src/routes/safe/store/actions/addSafe.js | 2 +- .../safe/store/actions/addTransactions.js | 6 --- .../safe/store/actions/createTransaction.js | 44 +++++++++++++++++++ src/routes/safe/store/reducer/transactions.js | 4 +- 5 files changed, 48 insertions(+), 10 deletions(-) delete mode 100644 src/routes/safe/store/actions/addTransactions.js create mode 100644 src/routes/safe/store/actions/createTransaction.js diff --git a/src/logic/safe/transactions/send.js b/src/logic/safe/transactions/send.js index 563a8fd5..ee4e93cd 100644 --- a/src/logic/safe/transactions/send.js +++ b/src/logic/safe/transactions/send.js @@ -6,7 +6,7 @@ import { isEther } from '~/logic/tokens/utils/tokenHelpers' import { type Token } from '~/logic/tokens/store/model/token' import { getSafeEthereumInstance } from '../safeFrontendOperations' -const CALL = 0 +export const CALL = 0 const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000' export const executeTransaction = async ( diff --git a/src/routes/safe/store/actions/addSafe.js b/src/routes/safe/store/actions/addSafe.js index af89c35c..0753e14d 100644 --- a/src/routes/safe/store/actions/addSafe.js +++ b/src/routes/safe/store/actions/addSafe.js @@ -31,7 +31,7 @@ const saveSafe = ( threshold: number, ownersName: string[], ownersAddress: string[], -) => async (dispatch: ReduxDispatch) => { +) => (dispatch: ReduxDispatch) => { const owners: List = buildOwnersFrom(ownersName, ownersAddress) const safe: Safe = SafeRecord({ diff --git a/src/routes/safe/store/actions/addTransactions.js b/src/routes/safe/store/actions/addTransactions.js deleted file mode 100644 index 4d5cc734..00000000 --- a/src/routes/safe/store/actions/addTransactions.js +++ /dev/null @@ -1,6 +0,0 @@ -// @flow -import { createAction } from 'redux-actions' - -export const ADD_TRANSACTIONS = 'ADD_TRANSACTIONS' - -export default createAction(ADD_TRANSACTIONS) diff --git a/src/routes/safe/store/actions/createTransaction.js b/src/routes/safe/store/actions/createTransaction.js new file mode 100644 index 00000000..dc041599 --- /dev/null +++ b/src/routes/safe/store/actions/createTransaction.js @@ -0,0 +1,44 @@ +// @flow +import type { Dispatch as ReduxDispatch } 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 { type GlobalState } from '~/store' +import { isEther } from '~/logic/tokens/utils/tokenHelpers' +import { getSafeEthereumInstance } from '~/logic/safe/safeFrontendOperations' +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) + +export const createTransaction = async (safeAddress: string, to: string, valueInEth: string, token: Token) => async ( + dispatch: ReduxDispatch, +) => { + const safeInstance = await getSafeEthereumInstance(safeAddress) + const web3 = getWeb3() + const from = web3.currentProvider.selectedAddress + const threshold = await safeInstance.getThreshold() + const nonce = await safeInstance.nonce() + const valueInWei = web3.utils.toWei(valueInEth, 'ether') + const isExecution = threshold.toNumber() === 1 + + let txData = EMPTY_DATA + if (!isEther(token.symbol)) { + const StandardToken = await getStandardTokenContract() + const sendToken = await StandardToken.at(token.address) + + txData = sendToken.contract.transfer(to, valueInWei).encodeABI() + } + + let txHash + if (isExecution) { + txHash = await executeTransaction(safeInstance, to, valueInWei, txData, CALL, nonce, from) + } else { + // txHash = await approveTransaction(safeAddress, to, valueInWei, txData, CALL, nonce) + } + // dispatch(addTransactions(txHash)) + + return txHash +} diff --git a/src/routes/safe/store/reducer/transactions.js b/src/routes/safe/store/reducer/transactions.js index 01dbc6bf..68e8be77 100644 --- a/src/routes/safe/store/reducer/transactions.js +++ b/src/routes/safe/store/reducer/transactions.js @@ -1,7 +1,7 @@ // @flow import { List, Map } from 'immutable' import { handleActions, type ActionType } from 'redux-actions' -import addTransactions, { ADD_TRANSACTIONS } from '~/routes/safe/store/actions/addTransactions' +import { ADD_TRANSACTIONS } from '~/routes/safe/store/actions/createTransaction' import { type Transaction } from '~/routes/safe/store/models/transaction' export const TRANSACTIONS_REDUCER_ID = 'transactions' @@ -10,7 +10,7 @@ export type State = Map> export default handleActions( { - [ADD_TRANSACTIONS]: (state: State, action: ActionType): State => action.payload, + [ADD_TRANSACTIONS]: (state: State, action: ActionType): State => action.payload, }, Map(), )