WA-521 Simplified createTransaction function, not storing in localStorage anything
This commit is contained in:
parent
f5b736c2f1
commit
8075f352db
|
@ -1,15 +1,8 @@
|
||||||
// @flow
|
// @flow
|
||||||
import { List } from 'immutable'
|
|
||||||
import { type Owner } from '~/routes/safe/store/model/owner'
|
|
||||||
import { load, TX_KEY } from '~/utils/localStorage'
|
|
||||||
import { type Confirmation, makeConfirmation } from '~/routes/safe/store/model/confirmation'
|
|
||||||
import { makeTransaction, type Transaction, type TransactionProps } from '~/routes/safe/store/model/transaction'
|
|
||||||
import { getGnosisSafeContract } from '~/wallets/safeContracts'
|
import { getGnosisSafeContract } from '~/wallets/safeContracts'
|
||||||
import { getWeb3 } from '~/wallets/getWeb3'
|
import { getWeb3 } from '~/wallets/getWeb3'
|
||||||
import { type Safe } from '~/routes/safe/store/model/safe'
|
import { type Safe } from '~/routes/safe/store/model/safe'
|
||||||
import { sameAddress } from '~/wallets/ethAddresses'
|
|
||||||
import { EMPTY_DATA } from '~/wallets/ethTransactions'
|
import { EMPTY_DATA } from '~/wallets/ethTransactions'
|
||||||
import { storeSubject } from '~/utils/localStorage/transactions'
|
|
||||||
import { executeTransaction, approveTransaction } from '~/wallets/safeOperations'
|
import { executeTransaction, approveTransaction } from '~/wallets/safeOperations'
|
||||||
|
|
||||||
export const TX_NAME_PARAM = 'txName'
|
export const TX_NAME_PARAM = 'txName'
|
||||||
|
@ -18,65 +11,6 @@ export const TX_VALUE_PARAM = 'txValue'
|
||||||
|
|
||||||
export const EXECUTED_CONFIRMATION_HASH = 'EXECUTED'
|
export const EXECUTED_CONFIRMATION_HASH = 'EXECUTED'
|
||||||
|
|
||||||
// Exported for testing it, should not use it. Use #transactions fnc.
|
|
||||||
export const buildConfirmationsFrom =
|
|
||||||
(owners: List<Owner>, creator: string, confirmationHash: string): List<Confirmation> => {
|
|
||||||
if (!owners) {
|
|
||||||
throw new Error('This safe has no owners')
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!owners.find((owner: Owner) => sameAddress(owner.get('address'), creator))) {
|
|
||||||
throw new Error('The creator of the tx is not an owner')
|
|
||||||
}
|
|
||||||
|
|
||||||
return owners.map((owner: Owner) => makeConfirmation({
|
|
||||||
owner,
|
|
||||||
status: sameAddress(owner.get('address'), creator),
|
|
||||||
hash: sameAddress(owner.get('address'), creator) ? confirmationHash : undefined,
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
export const buildExecutedConfirmationFrom = (owners: List<Owner>, creator: string): List<Confirmation> =>
|
|
||||||
buildConfirmationsFrom(owners, creator, EXECUTED_CONFIRMATION_HASH)
|
|
||||||
|
|
||||||
export const storeTransaction = (
|
|
||||||
name: string,
|
|
||||||
nonce: number,
|
|
||||||
destination: string,
|
|
||||||
value: number,
|
|
||||||
creator: string,
|
|
||||||
confirmations: List<Confirmation>,
|
|
||||||
tx: string,
|
|
||||||
safeAddress: string,
|
|
||||||
safeThreshold: number,
|
|
||||||
data: string,
|
|
||||||
) => {
|
|
||||||
const notMinedWhenOneOwnerSafe = confirmations.count() === 1 && !tx
|
|
||||||
if (notMinedWhenOneOwnerSafe) {
|
|
||||||
throw new Error('The tx should be mined before storing it in safes with one owner')
|
|
||||||
}
|
|
||||||
// fetch actual transactions from endpoint and check nonce is higher than the last one
|
|
||||||
// send tx to service
|
|
||||||
// store subject in local storage (for testing it the actual name should be '')
|
|
||||||
|
|
||||||
const transaction: Transaction = makeTransaction({
|
|
||||||
name: '', nonce, value, confirmations, destination, threshold: safeThreshold, tx, data,
|
|
||||||
})
|
|
||||||
|
|
||||||
const safeTransactions = load(TX_KEY) || {}
|
|
||||||
const transactions = safeTransactions[safeAddress]
|
|
||||||
const txsRecord = transactions ? List(transactions) : List([])
|
|
||||||
|
|
||||||
if (txsRecord.find((txs: TransactionProps) => txs.nonce === nonce)) {
|
|
||||||
throw new Error(`Transaction with same nonce: ${nonce} already created for safe: ${safeAddress}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
storeSubject(safeAddress, nonce, name)
|
|
||||||
|
|
||||||
safeTransactions[safeAddress] = txsRecord.push(transaction)
|
|
||||||
localStorage.setItem(TX_KEY, JSON.stringify(safeTransactions))
|
|
||||||
}
|
|
||||||
|
|
||||||
const hasOneOwner = (safe: Safe) => {
|
const hasOneOwner = (safe: Safe) => {
|
||||||
const owners = safe.get('owners')
|
const owners = safe.get('owners')
|
||||||
if (!owners) {
|
if (!owners) {
|
||||||
|
@ -102,26 +36,14 @@ export const createTransaction = async (
|
||||||
data: string = EMPTY_DATA,
|
data: string = EMPTY_DATA,
|
||||||
) => {
|
) => {
|
||||||
const web3 = getWeb3()
|
const web3 = getWeb3()
|
||||||
const owners = safe.get('owners')
|
|
||||||
const safeAddress = safe.get('address')
|
const safeAddress = safe.get('address')
|
||||||
const threshold = safe.get('threshold')
|
const threshold = safe.get('threshold')
|
||||||
const valueInWei = web3.toWei(value, 'ether')
|
const valueInWei = web3.toWei(value, 'ether')
|
||||||
const CALL = 0
|
const CALL = 0
|
||||||
|
|
||||||
const isExecution = hasOneOwner(safe) || threshold === 1
|
const isExecution = hasOneOwner(safe) || threshold === 1
|
||||||
if (isExecution) {
|
|
||||||
const txHash = await executeTransaction(safeAddress, to, valueInWei, data, CALL, nonce, sender)
|
|
||||||
// TODO Remove when TX History service is fully integrated
|
|
||||||
const executedConfirmations: List<Confirmation> = buildExecutedConfirmationFrom(owners, sender)
|
|
||||||
|
|
||||||
// TODO Remove when TX History service is fully integrated
|
return isExecution
|
||||||
return storeTransaction(name, nonce, to, value, sender, executedConfirmations, txHash, safeAddress, threshold, data)
|
? executeTransaction(safeAddress, to, valueInWei, data, CALL, nonce, sender)
|
||||||
}
|
: approveTransaction(safeAddress, to, valueInWei, data, CALL, nonce, sender)
|
||||||
|
|
||||||
const txHash = await approveTransaction(safeAddress, to, valueInWei, data, CALL, nonce, sender)
|
|
||||||
// TODO Remove when TX History service is fully integrated
|
|
||||||
const confirmations: List<Confirmation> = buildConfirmationsFrom(owners, sender, txHash)
|
|
||||||
|
|
||||||
// TODO Remove when TX History service is fully integrated
|
|
||||||
return storeTransaction(name, nonce, to, value, sender, confirmations, '', safeAddress, threshold, data)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue