|
1 | 1 | import React, { useState, useCallback, useEffect, useMemo } from 'react' |
2 | 2 |
|
3 | 3 | import { AppActions, StateDispatch } from 'states/stateProvider/reducer' |
4 | | -import { calculateCycles } from 'services/remote/wallets' |
| 4 | +import { generateTx } from 'services/remote/wallets' |
5 | 5 |
|
6 | | -import { outputsToTotalAmount, priceToFee } from 'utils/formatters' |
| 6 | +import { outputsToTotalAmount, CKBToShannonFormatter } from 'utils/formatters' |
7 | 7 | import { verifyAddress, verifyAmount, verifyAmountRange, verifyTransactionOutputs } from 'utils/validators' |
8 | | -import { ErrorCode } from 'utils/const' |
9 | | -import { MAX_DECIMAL_DIGITS } from '../../utils/const' |
| 8 | +import { ErrorCode, MAX_DECIMAL_DIGITS } from 'utils/const' |
| 9 | +import calculateFee from 'utils/calculateFee' |
| 10 | + |
10 | 11 | import { TransactionOutput } from '.' |
11 | 12 |
|
12 | | -let cyclesTimer: ReturnType<typeof setTimeout> |
| 13 | +let generateTxTimer: ReturnType<typeof setTimeout> |
13 | 14 |
|
14 | 15 | const useUpdateTransactionOutput = (dispatch: StateDispatch) => |
15 | 16 | useCallback( |
@@ -49,49 +50,47 @@ const useRemoveTransactionOutput = (dispatch: StateDispatch) => |
49 | 50 | const useOnTransactionChange = ( |
50 | 51 | walletID: string, |
51 | 52 | items: TransactionOutput[], |
| 53 | + price: string, |
52 | 54 | dispatch: StateDispatch, |
53 | 55 | setIsTransactionValid: Function, |
54 | 56 | setTotalAmount: Function |
55 | 57 | ) => { |
56 | 58 | useEffect(() => { |
57 | | - clearTimeout(cyclesTimer) |
58 | | - cyclesTimer = setTimeout(() => { |
| 59 | + clearTimeout(generateTxTimer) |
| 60 | + generateTxTimer = setTimeout(() => { |
| 61 | + dispatch({ |
| 62 | + type: AppActions.UpdateGeneratedTx, |
| 63 | + payload: null, |
| 64 | + }) |
59 | 65 | if (verifyTransactionOutputs(items)) { |
60 | 66 | setIsTransactionValid(true) |
61 | 67 | const totalAmount = outputsToTotalAmount(items) |
62 | 68 | setTotalAmount(totalAmount) |
63 | | - calculateCycles({ |
| 69 | + const realParams = { |
64 | 70 | walletID, |
65 | | - capacities: totalAmount, |
66 | | - }) |
67 | | - .then(response => { |
68 | | - if (response.status) { |
69 | | - if (Number.isNaN(+response.result)) { |
70 | | - throw new Error('Invalid Cycles') |
71 | | - } |
| 71 | + items: items.map(item => ({ |
| 72 | + address: item.address, |
| 73 | + capacity: CKBToShannonFormatter(item.amount, item.unit), |
| 74 | + })), |
| 75 | + feeRate: price, |
| 76 | + } |
| 77 | + generateTx(realParams) |
| 78 | + .then((res: any) => { |
| 79 | + if (res.status === 1) { |
72 | 80 | dispatch({ |
73 | | - type: AppActions.UpdateSendCycles, |
74 | | - payload: response.result, |
| 81 | + type: AppActions.UpdateGeneratedTx, |
| 82 | + payload: res.result, |
75 | 83 | }) |
76 | | - } else { |
77 | | - throw new Error('Cycles Not Calculated') |
78 | 84 | } |
79 | 85 | }) |
80 | | - .catch(() => { |
81 | | - dispatch({ |
82 | | - type: AppActions.UpdateSendCycles, |
83 | | - payload: '0', |
84 | | - }) |
| 86 | + .catch((err: Error) => { |
| 87 | + console.error(err) |
85 | 88 | }) |
86 | 89 | } else { |
87 | 90 | setIsTransactionValid(false) |
88 | | - dispatch({ |
89 | | - type: AppActions.UpdateSendCycles, |
90 | | - payload: '0', |
91 | | - }) |
92 | 91 | } |
93 | 92 | }, 300) |
94 | | - }, [walletID, items, dispatch, setIsTransactionValid, setTotalAmount]) |
| 93 | + }, [walletID, items, price, dispatch, setIsTransactionValid, setTotalAmount]) |
95 | 94 | } |
96 | 95 |
|
97 | 96 | const useOnSubmit = (items: TransactionOutput[], dispatch: StateDispatch) => |
@@ -170,12 +169,12 @@ const useClear = (dispatch: StateDispatch) => useCallback(() => clear(dispatch), |
170 | 169 |
|
171 | 170 | export const useInitialize = ( |
172 | 171 | items: TransactionOutput[], |
173 | | - price: string, |
174 | | - cycles: string, |
| 172 | + generatedTx: any | null, |
175 | 173 | dispatch: React.Dispatch<any>, |
176 | 174 | t: any |
177 | 175 | ) => { |
178 | | - const fee = useMemo(() => priceToFee(price, cycles), [price, cycles]) // in shannon |
| 176 | + const fee = useMemo(() => calculateFee(generatedTx), [generatedTx]) |
| 177 | + |
179 | 178 | const [isTransactionValid, setIsTransactionValid] = useState(false) |
180 | 179 | const [totalAmount, setTotalAmount] = useState('0') |
181 | 180 |
|
|
0 commit comments