forked from centrifuge/tinlake-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransactions.ts
305 lines (264 loc) · 9.38 KB
/
transactions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import * as React from 'react'
import { AnyAction, Action } from 'redux'
import { useSelector } from 'react-redux'
import { ThunkAction } from 'redux-thunk'
import { HYDRATE } from 'next-redux-wrapper'
import { initTinlake } from '../services/tinlake'
import * as actions from '../services/tinlake/actions'
import config from '../config'
// TODO: should be imported from @centrifuge/axis-web3-wallet
export interface WalletTransaction {
description: string
status: 'unconfirmed' | 'pending' | 'succeeded' | 'failed'
externalLink?: string
showIfClosed?: boolean
failedReason?: string
}
// This refers to any function in ../services/tinlake/actions which aligns to the TinlakeAction type
export type TransactionAction = {
[P in keyof typeof actions]: typeof actions[P] extends actions.TinlakeAction ? P : never
}[keyof typeof actions]
// Can be extended by components which create and subscribe to transactions
export interface TransactionProps {
createTransaction: <A extends TransactionAction>(
description: string,
actionName: A,
args: Parameters<typeof actions[A]>
) => Promise<string>
}
// Actions
const START_PROCESSING = 'tinlake-ui/transactions/START_PROCESSING'
const STOP_PROCESSING = 'tinlake-ui/transactions/STOP_PROCESSING'
const SET_ACTIVE_TRANSACTION = 'tinlake-ui/transactions/SET_ACTIVE_TRANSACTION'
const QUEUE_TRANSACTION = 'tinlake-ui/transactions/QUEUE_TRANSACTION'
const DEQUEUE_TRANSACTION = 'tinlake-ui/transactions/DEQUEUE_TRANSACTION'
export type TransactionId = string
export type TransactionStatus = 'unconfirmed' | 'pending' | 'succeeded' | 'failed'
interface TinlakeConfig {
addresses?: any
contractConfig?: {
JUNIOR_OPERATOR: 'ALLOWANCE_OPERATOR'
SENIOR_OPERATOR: 'ALLOWANCE_OPERATOR' | 'PROPORTIONAL_OPERATOR'
}
}
export interface Transaction {
id: string
description: string
actionName: TransactionAction
actionArgs: any[]
status: TransactionStatus
result?: any
tinlakeConfig: TinlakeConfig
showIfClosed: boolean
failedReason?: string
updatedAt?: number
}
export interface TransactionState {
processing: boolean
active: { [key: string]: Transaction }
queue: { [key: string]: Transaction }
}
const initialState: TransactionState = {
processing: false,
active: {},
queue: {},
}
// Reducer
export default function reducer(
state: TransactionState = initialState,
action: AnyAction = { type: '' }
): TransactionState {
switch (action.type) {
case HYDRATE:
// TODO: change pending transactions to failed transactions
return { ...state, ...action.payload.transactionQueue }
case START_PROCESSING:
return {
...state,
processing: true,
}
case STOP_PROCESSING:
return {
...state,
processing: false,
}
case SET_ACTIVE_TRANSACTION:
return {
...state,
active: {
...state.active,
[action.id]: {
...action.transaction,
updatedAt: new Date().getTime(),
},
},
}
case QUEUE_TRANSACTION:
return {
...state,
queue: {
...state.queue,
[action.id]: {
...action.transaction,
updatedAt: new Date().getTime(),
},
},
}
case DEQUEUE_TRANSACTION:
const newQueue = state.queue
if (action.id in state.queue) {
delete newQueue[action.id]
}
return {
...state,
queue: newQueue,
}
default:
return state
}
}
// Action creators
const SUCCESS_STATUS = '0x1'
export function createTransaction<A extends TransactionAction>(
description: string,
actionName: A,
args: Parameters<typeof actions[A]>
): ThunkAction<Promise<string>, { transactions: TransactionState }, undefined, Action> {
return async (dispatch, getState) => {
// Generate a unique id
const id: TransactionId = (new Date().getTime() + Math.floor(Math.random() * 1000000)).toString()
/**
* We store the tinlake config, remove the tinlake service from the state (as it's not serializable and can therefore not be stored in Redux state),
* and then re-initialize Tinlake.js with the same config when processing the transaction.
* */
const tinlakeConfig = {
addresses: args[0].contractAddresses,
contractConfig: args[0].contractConfig,
}
const actionArgs = args.slice(1)
const unconfirmedTx: Transaction = {
id,
description,
actionName,
actionArgs,
tinlakeConfig,
status: 'pending',
showIfClosed: true,
}
dispatch({ id, transaction: unconfirmedTx, type: QUEUE_TRANSACTION })
// Start processing this transaction if no transaction is currently being processed
if (!getState().transactions.processing) {
dispatch({ type: START_PROCESSING })
dispatch(processTransaction(unconfirmedTx))
}
return id
}
}
export function processTransaction(
unconfirmedTx: Transaction
): ThunkAction<Promise<void>, { transactions: TransactionState }, undefined, Action> {
return async (dispatch, getState) => {
// Dequeue
const id = unconfirmedTx.id
dispatch({ id, transaction: unconfirmedTx, type: DEQUEUE_TRANSACTION })
dispatch({ id, transaction: unconfirmedTx, type: SET_ACTIVE_TRANSACTION })
let hasCompleted = false
// Hide pending tx after 10s
setTimeout(async () => {
if (!hasCompleted) {
const hiddenPendingTx: Transaction = {
...unconfirmedTx,
showIfClosed: false,
}
await dispatch({ id, transaction: hiddenPendingTx, dontChangeUpdatedAt: true, type: SET_ACTIVE_TRANSACTION })
}
}, 10000)
// Start transaction
const tinlake = initTinlake(unconfirmedTx.tinlakeConfig)
const outcomeTx: Transaction = {
...unconfirmedTx,
showIfClosed: true,
}
// This is a hack to grab a human-friendly error. We should eventually refactor Tinlake.js to return this error directly.
const errorMessageRegex = /\"message\"\:\"\s?(.*)[\.?][\"?],/
try {
const actionCall = actions[unconfirmedTx.actionName as keyof typeof actions]
const response = await (actionCall as any)(tinlake, ...unconfirmedTx.actionArgs)
hasCompleted = true
const outcome = (response as any).status === SUCCESS_STATUS
console.log(outcome)
outcomeTx.status = outcome ? 'succeeded' : 'failed'
outcomeTx.result = response
if (errorMessageRegex.test(response.error)) {
const matches = response.error.toString().match(errorMessageRegex)
if (matches) outcomeTx.failedReason = matches[1]
} else if (outcomeTx.status === 'failed' && outcomeTx.result.message) {
outcomeTx.failedReason = outcomeTx.result.message
}
} catch (error) {
console.error(
`Failed to process action ${unconfirmedTx.actionName}(${unconfirmedTx.actionArgs.join(',')})`,
error
)
outcomeTx.status = 'failed'
if (errorMessageRegex.test(error.toString())) {
const matches = error.toString().match(errorMessageRegex)
if (matches) outcomeTx.failedReason = matches[1]
} else {
}
}
await dispatch({ id, transaction: outcomeTx, type: SET_ACTIVE_TRANSACTION })
// Hide succeeded/failed tx after 5s
setTimeout(async () => {
// TODO: this shouldn't update the 'updatedAt' property, since it pushes up transactions which haven't actually changed
const hiddenTx: Transaction = {
...outcomeTx,
showIfClosed: false,
}
await dispatch({ id, transaction: hiddenTx, dontChangeUpdatedAt: true, type: SET_ACTIVE_TRANSACTION })
}, 5000)
// Process next transaction in queue
if (Object.keys(getState().transactions.queue).length > 0) {
const nextTransactionId = Object.keys(getState().transactions.queue)[0]
const nextTransaction = getState().transactions.queue[nextTransactionId]
dispatch(processTransaction(nextTransaction))
} else {
dispatch({ type: STOP_PROCESSING })
}
}
}
// Selectors
const sortByMostRecent = (a: Transaction, b: Transaction) =>
a.updatedAt && b.updatedAt ? b.updatedAt - a.updatedAt : 0
export function selectWalletTransactions(state?: TransactionState): WalletTransaction[] {
if (!state) return []
// Retrieve active transactions, sort them by most recent, and then convert them into the type required for the wallet
const transactions: WalletTransaction[] = Object.keys(state.active)
.map((id: string) => state.active[id])
.sort(sortByMostRecent)
.map((tx: Transaction) => {
const externalLink = tx.result?.txHash
? `https://${config.network === 'Kovan' ? 'kovan.' : ''}etherscan.io/tx/${tx.result.txHash}`
: undefined
return {
externalLink,
description: tx.description,
status: tx.status,
showIfClosed: tx.showIfClosed,
failedReason: tx.failedReason,
}
})
return transactions
}
export function getTransaction(state?: TransactionState, txId?: TransactionId): Transaction | undefined {
if (!state || !txId || !(txId in state.active)) return undefined
return state.active[txId]
}
// Hooks
export const useTransactionState = (): [TransactionStatus | undefined, any, (txId: TransactionId) => void] => {
const [txId, setTxId] = React.useState<TransactionId | undefined>(undefined)
const tx = useSelector((state: { transactions: TransactionState }) =>
txId ? state.transactions.active[txId] : undefined
)
return [tx?.status, tx?.result, setTxId]
}