1
1
import { FormattedMessage } from 'react-intl'
2
+ import { useState } from 'react'
2
3
import CSSModules from 'react-css-modules'
3
4
import styles from './index.scss'
4
5
import utils from 'common/utils'
5
6
import ADDRESSES from 'common/helpers/constants/ADDRESSES'
6
7
import actions from 'redux/actions'
7
8
import { feedback , externalConfig , constants , transactions , routing } from 'helpers'
8
9
import { ComponentState , BlockReasons , Actions , Direction } from './types'
9
- import { GWEI_DECIMALS , COIN_DECIMALS , LIQUIDITY_SOURCE_DATA , SEC_PER_MINUTE } from './constants'
10
+ import {
11
+ SWAP_API ,
12
+ GWEI_DECIMALS ,
13
+ COIN_DECIMALS ,
14
+ LIQUIDITY_SOURCE_DATA ,
15
+ SEC_PER_MINUTE ,
16
+ } from './constants'
10
17
import Button from 'components/controls/Button/Button'
18
+ import ReviewSwapModal from './ReviewSwapModal'
11
19
12
20
type FooterProps = {
21
+ history : any
13
22
parentState : ComponentState
14
23
insufficientBalanceA : boolean
15
24
insufficientBalanceB : boolean
16
25
isSourceMode : boolean
17
26
sourceAction : Actions
18
27
reportError : ( e : IError ) => void
19
- resetSwapData : ( ) => void
20
- resetSpendedAmount : ( ) => void
28
+ resetSwapData : VoidFunction
29
+ resetSpendedAmount : VoidFunction
21
30
setBlockReason : ( a : BlockReasons ) => void
22
31
isApiRequestBlocking : ( ) => boolean
23
32
setPending : ( a : boolean ) => void
24
- onInputDataChange : ( ) => void
33
+ onInputDataChange : VoidFunction
34
+ finalizeApiSwapData : ( ) => Promise < void >
25
35
baseChainWallet : IUniversalObj
26
36
}
27
37
28
38
function Footer ( props : FooterProps ) {
29
39
const {
40
+ history,
30
41
parentState,
31
42
isSourceMode,
32
43
sourceAction,
@@ -40,6 +51,7 @@ function Footer(props: FooterProps) {
40
51
setPending,
41
52
baseChainWallet,
42
53
onInputDataChange,
54
+ finalizeApiSwapData,
43
55
} = props
44
56
const {
45
57
blockReason,
@@ -60,12 +72,22 @@ function Footer(props: FooterProps) {
60
72
error,
61
73
slippage,
62
74
currentLiquidityPair,
75
+ swapFee,
76
+ serviceFee,
77
+ fiat,
63
78
} = parentState
64
79
80
+ const [ finalizeSwap , setFinalizeSwap ] = useState < boolean > ( false )
81
+
82
+ const startSwapReview = async ( ) => {
83
+ setFinalizeSwap ( true )
84
+ await finalizeApiSwapData ( )
85
+ }
86
+
65
87
const approve = async ( direction ) => {
66
- const spender = isSourceMode
88
+ const spender : `0x${ number } ` = isSourceMode
67
89
? LIQUIDITY_SOURCE_DATA [ network . networkVersion ] ?. router
68
- : externalConfig . swapContract . zerox
90
+ : externalConfig . swapContract [ SWAP_API [ network . networkVersion ] . spender ]
69
91
70
92
let wallet = fromWallet
71
93
let amount = spendedAmount
@@ -97,6 +119,10 @@ function Footer(props: FooterProps) {
97
119
98
120
const apiSwap = async ( ) => {
99
121
if ( isSourceMode ) return
122
+ if ( ! swapData ) throw new Error ( 'No swap data. Can not complete swap' )
123
+ if ( swapData . to !== externalConfig . swapContract [ SWAP_API [ network . networkVersion ] . spender ] ) {
124
+ return console . log ( '%c0x constant proxy is not equal to swap transaction proxy' , 'color:red' )
125
+ }
100
126
101
127
const baseCurrency = fromWallet . standard ? fromWallet . baseCurrency : fromWallet . currency
102
128
const assetName = fromWallet . standard ? fromWallet . tokenKey : fromWallet . currency
@@ -107,10 +133,6 @@ function Footer(props: FooterProps) {
107
133
setPending ( true )
108
134
109
135
try {
110
- if ( ! swapData ) {
111
- throw new Error ( 'No swap data. Can not complete swap' )
112
- }
113
-
114
136
if ( gasLimit ) swapData . gas = gasLimit
115
137
if ( gasPrice ) swapData . gasPrice = utils . amount . formatWithDecimals ( gasPrice , GWEI_DECIMALS )
116
138
@@ -120,7 +142,6 @@ function Footer(props: FooterProps) {
120
142
121
143
if ( txHash ) {
122
144
const txInfoUrl = transactions . getTxRouter ( assetName . toLowerCase ( ) , txHash )
123
-
124
145
routing . redirectTo ( txInfoUrl )
125
146
}
126
147
@@ -131,6 +152,7 @@ function Footer(props: FooterProps) {
131
152
}
132
153
133
154
setPending ( false )
155
+ setFinalizeSwap ( false )
134
156
}
135
157
136
158
const directSwap = async ( ) => {
@@ -227,7 +249,11 @@ function Footer(props: FooterProps) {
227
249
228
250
const doNotMakeApiRequest = isApiRequestBlocking ( )
229
251
230
- const commonBlockReasons = isPending || ( blockReason !== BlockReasons . NotApproved && ! ! error && ( ! error . message ?. match ( 'transfer amount exceeds allowance' ) ) )
252
+ const commonBlockReasons =
253
+ isPending ||
254
+ ( blockReason !== BlockReasons . NotApproved &&
255
+ ! ! error &&
256
+ ! error . message ?. match ( 'transfer amount exceeds allowance' ) )
231
257
const formFilled = ! ! spendedAmount && ! ! receivedAmount
232
258
233
259
const approvingDoesNotMakeSense =
@@ -252,6 +278,24 @@ function Footer(props: FooterProps) {
252
278
253
279
return (
254
280
< div styleName = "footer" >
281
+ { finalizeSwap && (
282
+ < ReviewSwapModal
283
+ isPending = { isPending }
284
+ data = { swapData }
285
+ onSwap = { apiSwap }
286
+ onClose = { ( ) => setFinalizeSwap ( false ) }
287
+ history = { history }
288
+ swapFee = { swapFee }
289
+ fiat = { fiat }
290
+ serviceFee = { serviceFee }
291
+ slippage = { slippage }
292
+ network = { network }
293
+ spendedAmount = { spendedAmount }
294
+ baseChainWallet = { baseChainWallet }
295
+ fromWallet = { fromWallet }
296
+ toWallet = { toWallet }
297
+ />
298
+ ) }
255
299
{ needApproveA ? (
256
300
< Button
257
301
pending = { isPending }
@@ -281,8 +325,8 @@ function Footer(props: FooterProps) {
281
325
/>
282
326
</ Button >
283
327
) : ! isSourceMode ? (
284
- < Button pending = { isPending } disabled = { ! apiSwapIsAvailable } onClick = { apiSwap } brand >
285
- < FormattedMessage id = "swap " defaultMessage = "Swap " />
328
+ < Button pending = { isPending } disabled = { ! apiSwapIsAvailable } onClick = { startSwapReview } brand >
329
+ < FormattedMessage id = "reviewSwap " defaultMessage = "Review swap " />
286
330
</ Button >
287
331
) : sourceAction === Actions . Swap ? (
288
332
< Button pending = { isPending } disabled = { ! directSwapIsAvailable } onClick = { directSwap } brand >
0 commit comments