-
Notifications
You must be signed in to change notification settings - Fork 375
/
Copy pathCalculateFee.tsx
133 lines (115 loc) · 3.96 KB
/
CalculateFee.tsx
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
import { CURRENCY_ENUM } from '@celo/utils/src'
import BigNumber from 'bignumber.js'
import React, { FunctionComponent, useEffect } from 'react'
import { useAsync, UseAsyncReturn } from 'react-async-hook'
import { useDispatch } from 'react-redux'
import { showError } from 'src/alert/actions'
import { ErrorMessages } from 'src/app/ErrorMessages'
import { MAX_COMMENT_LENGTH } from 'src/config'
import { getReclaimEscrowFee } from 'src/escrow/saga'
import { FeeType } from 'src/fees/actions'
import { getInviteFee } from 'src/invite/saga'
import { getSendFee } from 'src/send/saga'
import Logger from 'src/utils/Logger'
export type CalculateFeeChildren = (
asyncResult: UseAsyncReturn<BigNumber, never>
) => React.ReactNode
interface CommonProps {
children: CalculateFeeChildren
}
interface InviteProps extends CommonProps {
feeType: FeeType.INVITE
account: string
amount: BigNumber
comment?: string
}
interface SendProps extends CommonProps {
feeType: FeeType.SEND
account: string
recipientAddress: string
amount: BigNumber
comment?: string
includeDekFee: boolean
}
interface ExchangeProps extends CommonProps {
feeType: FeeType.EXCHANGE
// TODO
}
interface ReclaimEscrowProps extends CommonProps {
feeType: FeeType.RECLAIM_ESCROW
account: string
paymentID: string
}
export type PropsWithoutChildren =
| Omit<InviteProps, 'children'>
| Omit<SendProps, 'children'>
| Omit<ExchangeProps, 'children'>
| Omit<ReclaimEscrowProps, 'children'>
type Props = InviteProps | SendProps | ExchangeProps | ReclaimEscrowProps
// Max lengthed comment to fetch fee estimate before user finalizes comment
const MAX_PLACEHOLDER_COMMENT: string = '0'.repeat(MAX_COMMENT_LENGTH)
function useAsyncShowError<R, Args extends any[]>(
asyncFunction: ((...args: Args) => Promise<R>) | (() => Promise<R>),
params: Args
): UseAsyncReturn<R, Args> {
const asyncResult = useAsync(asyncFunction, params)
const dispatch = useDispatch()
useEffect(() => {
// Generic error banner
if (asyncResult.error) {
Logger.error('CalculateFee', 'Error calculating fee', asyncResult.error)
const errMsg = asyncResult.error.message?.includes('insufficientBalance')
? ErrorMessages.INSUFFICIENT_BALANCE
: ErrorMessages.CALCULATE_FEE_FAILED
dispatch(showError(errMsg))
}
}, [asyncResult.error])
return asyncResult
}
const CalculateInviteFee: FunctionComponent<InviteProps> = (props) => {
const asyncResult = useAsyncShowError(
(account: string, amount: BigNumber, comment: string = MAX_PLACEHOLDER_COMMENT) =>
getInviteFee(account, CURRENCY_ENUM.DOLLAR, amount.valueOf(), comment),
[props.account, props.amount, props.comment]
)
return props.children(asyncResult) as React.ReactElement
}
const CalculateSendFee: FunctionComponent<SendProps> = (props) => {
const asyncResult = useAsyncShowError(
(
account: string,
recipientAddress: string,
amount: BigNumber,
comment: string = MAX_PLACEHOLDER_COMMENT,
includeDekFee: boolean = false
) =>
getSendFee(
account,
CURRENCY_ENUM.DOLLAR,
{
recipientAddress,
amount: amount.valueOf(),
comment,
},
includeDekFee
),
[props.account, props.recipientAddress, props.amount, props.comment, props.includeDekFee]
)
return props.children(asyncResult) as React.ReactElement
}
const CalculateReclaimEscrowFee: FunctionComponent<ReclaimEscrowProps> = (props) => {
const asyncResult = useAsyncShowError(getReclaimEscrowFee, [props.account, props.paymentID])
return props.children(asyncResult) as React.ReactElement
}
const CalculateFee = (props: Props) => {
switch (props.feeType) {
case FeeType.INVITE:
return <CalculateInviteFee {...props} />
case FeeType.SEND:
return <CalculateSendFee {...props} />
case FeeType.RECLAIM_ESCROW:
return <CalculateReclaimEscrowFee {...props} />
}
throw new Error(`Unsupported feeType: ${props.feeType}`)
}
export default CalculateFee