WA-238 Transaction tests for storing confirmations
This commit is contained in:
parent
302fc13613
commit
60d7f9056e
|
@ -0,0 +1,181 @@
|
|||
// @flow
|
||||
import { List, Map } from 'immutable'
|
||||
import { createTransaction, loadSafeTransactions } from '~/routes/safe/component/Transactions/transactions'
|
||||
import { type Transaction } from '~/routes/safe/store/model/transaction'
|
||||
import { SafeFactory } from '~/routes/safe/store/test/builder/safe.builder'
|
||||
import { type Safe } from '~/routes/safe/store/model/safe'
|
||||
import { type Owner } from '~/routes/safe/store/model/owner'
|
||||
import { type Confirmation } from '~/routes/safe/store/model/confirmation'
|
||||
|
||||
const testSizeOfSafesWith = (transactions, size) => {
|
||||
expect(transactions).not.toBe(undefined)
|
||||
expect(transactions).not.toBe(null)
|
||||
expect(transactions.size).toBe(size)
|
||||
}
|
||||
|
||||
const testSizeOfTransactions = (safeTxs, size) => {
|
||||
if (!safeTxs) { throw new Error() }
|
||||
expect(safeTxs.count()).toBe(size)
|
||||
expect(safeTxs.get(0)).not.toBe(undefined)
|
||||
expect(safeTxs.get(0)).not.toBe(null)
|
||||
}
|
||||
|
||||
const testTransactionFrom = (safeTxs, pos, name, nonce, value, threshold, destination, firstOwner, secondOwner) => {
|
||||
if (!safeTxs) { throw new Error() }
|
||||
const tx: Transaction | typeof undefined = safeTxs.get(pos)
|
||||
|
||||
if (!tx) { throw new Error() }
|
||||
expect(tx.get('name')).toBe(name)
|
||||
expect(tx.get('value')).toBe(value)
|
||||
expect(tx.get('threshold')).toBe(threshold)
|
||||
expect(tx.get('destination')).toBe(destination)
|
||||
expect(tx.get('confirmations').count()).toBe(2)
|
||||
expect(tx.get('nonce')).toBe(nonce)
|
||||
|
||||
const confirmations: List<Confirmation> = tx.get('confirmations')
|
||||
const firstConfirmation: Confirmation | typeof undefined = confirmations.get(0)
|
||||
if (!firstConfirmation) { throw new Error() }
|
||||
expect(firstConfirmation.get('owner')).not.toBe(undefined)
|
||||
expect(firstConfirmation.get('owner')).toEqual(firstOwner)
|
||||
expect(firstConfirmation.get('status')).toBe(false)
|
||||
|
||||
const secondConfirmation: Confirmation | typeof undefined = confirmations.get(1)
|
||||
if (!secondConfirmation) { throw new Error() }
|
||||
expect(secondConfirmation.get('owner')).not.toBe(undefined)
|
||||
expect(secondConfirmation.get('owner')).toEqual(secondOwner)
|
||||
expect(secondConfirmation.get('status')).toBe(false)
|
||||
}
|
||||
|
||||
describe('Transactions Suite', () => {
|
||||
let safe: Safe
|
||||
let destination: string
|
||||
let value: number
|
||||
let owners: List<Owner>
|
||||
beforeEach(async () => {
|
||||
localStorage.clear()
|
||||
|
||||
safe = SafeFactory.twoOwnersSafe('foo', 'bar')
|
||||
destination = 'baz'
|
||||
value = 2
|
||||
owners = safe.get('owners')
|
||||
|
||||
const firstOwner = owners.get(0)
|
||||
if (!firstOwner) { throw new Error() }
|
||||
const secondOwner = owners.get(1)
|
||||
if (!secondOwner) { throw new Error() }
|
||||
})
|
||||
|
||||
it('adds first confirmation to stored safe', async () => {
|
||||
// GIVEN
|
||||
const txName = 'Buy butteries for project'
|
||||
const nonce: number = 10
|
||||
createTransaction(txName, nonce, destination, value, owners, safe.get('name'), safe.get('address'), safe.get('confirmations'))
|
||||
|
||||
// WHEN
|
||||
const transactions: Map<string, List<Transaction>> = loadSafeTransactions()
|
||||
|
||||
// THEN
|
||||
testSizeOfSafesWith(transactions, 1)
|
||||
|
||||
const safeTransactions: List<Transaction> | typeof undefined = transactions.get(safe.get('address'))
|
||||
if (!safeTransactions) { throw new Error() }
|
||||
testSizeOfTransactions(safeTransactions, 1)
|
||||
|
||||
testTransactionFrom(safeTransactions, 0, txName, nonce, value, 2, destination, owners.get(0), owners.get(1))
|
||||
})
|
||||
|
||||
it('adds second confirmation to stored safe with one confirmation', async () => {
|
||||
// GIVEN
|
||||
const firstTxName = 'Buy butteries for project'
|
||||
const firstNonce: number = Date.now()
|
||||
createTransaction(firstTxName, firstNonce, destination, value, owners, safe.get('name'), safe.get('address'), safe.get('confirmations'))
|
||||
|
||||
const secondTxName = 'Buy printers for project'
|
||||
const secondNonce: number = firstNonce + 100
|
||||
createTransaction(secondTxName, secondNonce, destination, value, owners, safe.get('name'), safe.get('address'), safe.get('confirmations'))
|
||||
|
||||
// WHEN
|
||||
const transactions: Map<string, List<Transaction>> = loadSafeTransactions()
|
||||
|
||||
// THEN
|
||||
testSizeOfSafesWith(transactions, 1)
|
||||
|
||||
const safeTxs: List<Transaction> | typeof undefined = transactions.get(safe.get('address'))
|
||||
if (!safeTxs) { throw new Error() }
|
||||
testSizeOfTransactions(safeTxs, 2)
|
||||
|
||||
testTransactionFrom(safeTxs, 0, firstTxName, firstNonce, value, 2, destination, owners.get(0), owners.get(1))
|
||||
testTransactionFrom(safeTxs, 1, secondTxName, secondNonce, value, 2, destination, owners.get(0), owners.get(1))
|
||||
})
|
||||
|
||||
it('adds second confirmation to stored safe having two safes with one confirmation each', async () => {
|
||||
const txName = 'Buy batteris for Alplha project'
|
||||
const nonce = 10
|
||||
createTransaction(txName, nonce, destination, value, owners, safe.get('name'), safe.get('address'), safe.get('confirmations'))
|
||||
|
||||
const secondSafe = SafeFactory.dailyLimitSafe(10, 2)
|
||||
const txSecondName = 'Buy batteris for Beta project'
|
||||
const txSecondNonce = 10
|
||||
createTransaction(
|
||||
txSecondName, txSecondNonce, destination, value,
|
||||
secondSafe.get('owners'), secondSafe.get('name'), secondSafe.get('address'), secondSafe.get('confirmations'),
|
||||
)
|
||||
|
||||
let transactions: Map<string, List<Transaction>> = loadSafeTransactions()
|
||||
testSizeOfSafesWith(transactions, 2)
|
||||
|
||||
const firstSafeTxs: List<Transaction> | typeof undefined = transactions.get(safe.get('address'))
|
||||
if (!firstSafeTxs) { throw new Error() }
|
||||
testSizeOfTransactions(firstSafeTxs, 1)
|
||||
|
||||
const secondSafeTxs: List<Transaction> | typeof undefined = transactions.get(secondSafe.get('address'))
|
||||
if (!secondSafeTxs) { throw new Error() }
|
||||
testSizeOfTransactions(secondSafeTxs, 1)
|
||||
|
||||
// WHEN
|
||||
const txFirstName = 'Buy paper for Alplha project'
|
||||
const txFirstNonce = 11
|
||||
createTransaction(
|
||||
txFirstName, txFirstNonce, destination, value,
|
||||
safe.get('owners'), safe.get('name'), safe.get('address'), safe.get('confirmations'),
|
||||
)
|
||||
|
||||
transactions = loadSafeTransactions()
|
||||
|
||||
// THEN
|
||||
testSizeOfSafesWith(transactions, 2)
|
||||
testSizeOfTransactions(transactions.get(safe.get('address')), 2)
|
||||
testSizeOfTransactions(transactions.get(secondSafe.get('address')), 1)
|
||||
|
||||
// Test 2 transactions of first safe
|
||||
testTransactionFrom(
|
||||
transactions.get(safe.address), 0,
|
||||
txName, nonce, value, 2, destination,
|
||||
owners.get(0), owners.get(1),
|
||||
)
|
||||
testTransactionFrom(
|
||||
transactions.get(safe.address), 1,
|
||||
txFirstName, txFirstNonce, value, 2, destination,
|
||||
owners.get(0), owners.get(1),
|
||||
)
|
||||
|
||||
// Test one transaction of second safe
|
||||
testTransactionFrom(
|
||||
transactions.get(secondSafe.address), 0,
|
||||
txSecondName, txSecondNonce, value, 2, destination,
|
||||
secondSafe.get('owners').get(0), secondSafe.get('owners').get(1),
|
||||
)
|
||||
})
|
||||
|
||||
it('does not allow to store same transaction twice', async () => {
|
||||
// GIVEN
|
||||
const txName = 'Buy butteries for project'
|
||||
const nonce: number = 10
|
||||
createTransaction(txName, nonce, destination, value, owners, safe.get('name'), safe.get('address'), safe.get('confirmations'))
|
||||
|
||||
// WHEN
|
||||
const createTxFnc = () => createTransaction(txName, nonce, destination, value, owners, safe.get('name'), safe.get('address'), safe.get('confirmations'))
|
||||
expect(createTxFnc).toThrow(/Transaction with same nonce/)
|
||||
})
|
||||
})
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
// @flow
|
||||
import { List, Map } from 'immutable'
|
||||
import { type Owner, makeOwner } from '~/routes/safe/store/model/owner'
|
||||
import { load, TX_KEY } from '~/utils/localStorage'
|
||||
import { type Confirmation, type ConfirmationProps, makeConfirmation } from '~/routes/safe/store/model/confirmation'
|
||||
import { makeTransaction, type Transaction, type TransactionProps } from '~/routes/safe/store/model/transaction'
|
||||
|
||||
const buildConfirmationsFrom = (owners: List<Owner>): List<Confirmation> =>
|
||||
owners.map((owner: Owner) => makeConfirmation({ owner, status: false }))
|
||||
|
||||
export const createTransaction = (
|
||||
name: string,
|
||||
nonce: number,
|
||||
destination: string,
|
||||
value: number,
|
||||
owners: List<Owner>,
|
||||
safeName: string,
|
||||
safeAddress: string,
|
||||
safeThreshold: number,
|
||||
) => {
|
||||
const confirmations: List<Confirmation> = buildConfirmationsFrom(owners)
|
||||
const transaction: Transaction = makeTransaction({
|
||||
name, nonce, value, confirmations, destination, threshold: safeThreshold,
|
||||
})
|
||||
|
||||
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}`)
|
||||
}
|
||||
|
||||
safeTransactions[safeAddress] = txsRecord.push(transaction)
|
||||
|
||||
localStorage.setItem(TX_KEY, JSON.stringify(safeTransactions))
|
||||
}
|
||||
|
||||
export const loadSafeTransactions = () => {
|
||||
const safes = load(TX_KEY) || {}
|
||||
|
||||
return Map().withMutations((map: Map<string, List<Confirmation>>) =>
|
||||
Object.keys(safes).map((safe: string) => {
|
||||
const safeTxs = safes[safe]
|
||||
const safeTxsRecord = safeTxs.map((tx: TransactionProps) => {
|
||||
const { confirmations } = tx
|
||||
const txRecord = makeTransaction({
|
||||
...tx,
|
||||
confirmations: List(confirmations.map((conf: ConfirmationProps) =>
|
||||
makeConfirmation({ ...conf, owner: makeOwner(conf.owner) }))),
|
||||
})
|
||||
|
||||
return txRecord
|
||||
})
|
||||
|
||||
return map.set(safe, List(safeTxsRecord))
|
||||
}))
|
||||
}
|
||||
|
||||
/* TO USE as a selector
|
||||
export const getTransactionsOf = async (safeAddress: string) => {
|
||||
const safesTransactions = loadSafeTransactions()
|
||||
const safeTxs = List(safesTransactions.get(safeAddress))
|
||||
|
||||
return safeTxs
|
||||
}
|
||||
*/
|
|
@ -64,7 +64,7 @@ export class SafeFactory {
|
|||
.get()
|
||||
|
||||
static dailyLimitSafe = (dailyLimit: number, spentToday: number) => aSafe()
|
||||
.withAddress('0x03db1a8b26d08df23337e9276a36b474510f0026')
|
||||
.withAddress('0x03db1a8b26d08df23337e9276a36b474510f0027')
|
||||
.withName('Adol & Tobias Safe')
|
||||
.withConfirmations(2)
|
||||
.withOwner(
|
||||
|
|
Loading…
Reference in New Issue