Skip to content

Commit 01d02e0

Browse files
committed
feat(neuron-ui): update the cycles on the outputs changing
1 parent 3a40dcc commit 01d02e0

6 files changed

Lines changed: 87 additions & 3 deletions

File tree

packages/neuron-ui/src/components/Send/hooks.ts

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import React, { useCallback, useEffect } from 'react'
22
import { IDropdownOption } from 'office-ui-fabric-react'
33

44
import { AppActions, StateDispatch } from 'states/stateProvider/reducer'
5+
import { calculateCycles } from 'services/remote/wallets'
56

67
import { Message } from 'utils/const'
78
import { verifyAddress, verifyAmountRange } from 'utils/validators'
9+
import { CKBToShannonFormatter } from 'utils/formatters'
810
import { TransactionOutput } from '.'
911

10-
const validateTransactionParams = ({ items, dispatch }: { items: TransactionOutput[]; dispatch: StateDispatch }) => {
12+
let cyclesTimer: ReturnType<typeof setTimeout>
13+
14+
const validateTransactionParams = ({ items, dispatch }: { items: TransactionOutput[]; dispatch?: StateDispatch }) => {
1115
const errorAction = {
1216
type: AppActions.AddNotification,
1317
payload: {
@@ -17,7 +21,9 @@ const validateTransactionParams = ({ items, dispatch }: { items: TransactionOutp
1721
},
1822
}
1923
if (!items.length || !items[0].address) {
20-
dispatch(errorAction)
24+
if (dispatch) {
25+
dispatch(errorAction)
26+
}
2127
return false
2228
}
2329
const invalid = items.some(
@@ -42,7 +48,7 @@ const validateTransactionParams = ({ items, dispatch }: { items: TransactionOutp
4248
return false
4349
}
4450
)
45-
if (invalid) {
51+
if (invalid && dispatch) {
4652
dispatch(errorAction)
4753
return false
4854
}
@@ -84,6 +90,42 @@ const useRemoveTransactionOutput = (dispatch: StateDispatch) =>
8490
[dispatch]
8591
)
8692

93+
const useOnTransactionChange = (walletID: string, items: TransactionOutput[], dispatch: StateDispatch) => {
94+
useEffect(() => {
95+
clearTimeout(cyclesTimer)
96+
cyclesTimer = setTimeout(() => {
97+
if (validateTransactionParams({ items })) {
98+
calculateCycles({
99+
walletID,
100+
items: items.map(item => ({
101+
address: item.address,
102+
capacity: CKBToShannonFormatter(item.amount, item.unit),
103+
})),
104+
})
105+
.then(response => {
106+
if (response.status) {
107+
if (Number.isNaN(+response.result)) {
108+
throw new Error('Invalid Cycles')
109+
}
110+
dispatch({
111+
type: AppActions.UpdateSendCycles,
112+
payload: response.result,
113+
})
114+
} else {
115+
throw new Error('Cycles Not Calculated')
116+
}
117+
})
118+
.catch(() => {
119+
dispatch({
120+
type: AppActions.UpdateSendCycles,
121+
payload: '0',
122+
})
123+
})
124+
}
125+
}, 300)
126+
}, [walletID, items, dispatch])
127+
}
128+
87129
const useOnSubmit = (items: TransactionOutput[], dispatch: StateDispatch) =>
88130
useCallback(
89131
(walletID: string = '') => () => {
@@ -188,6 +230,7 @@ export const useInitialize = (
188230
}, [address, dispatch, history, updateTransactionOutput])
189231

190232
return {
233+
useOnTransactionChange,
191234
updateTransactionOutput,
192235
onItemChange,
193236
onCapacityUnitChange,

packages/neuron-ui/src/components/Send/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const Send = ({
4545
}: React.PropsWithoutRef<StateWithDispatch & RouteComponentProps<{ address: string }>>) => {
4646
const { t } = useTranslation()
4747
const {
48+
useOnTransactionChange,
4849
updateTransactionOutput,
4950
onItemChange,
5051
onSubmit,
@@ -54,6 +55,7 @@ const Send = ({
5455
onDescriptionChange,
5556
onClear,
5657
} = useInitialize(address, send.outputs, dispatch, history)
58+
useOnTransactionChange(walletID, send.outputs, dispatch)
5759
const leftStackWidth = '70%'
5860
const labelWidth = '140px'
5961
const actionSpacer = (

packages/neuron-ui/src/services/remote/wallets.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export const updateAddressDescription = controllerMethodWrapper(CONTROLLER_NAME)
4242
controller => (params: Controller.UpdateAddressDescriptionParams) => controller.updateAddressDescription(params)
4343
)
4444

45+
export const calculateCycles = controllerMethodWrapper(CONTROLLER_NAME)(
46+
controller => (params: Controller.CalculateCycles) => controller.calculateCycles(params)
47+
)
48+
4549
export default {
4650
updateWallet,
4751
getWalletList,
@@ -52,6 +56,7 @@ export default {
5256
backupWallet,
5357
getCurrentWallet,
5458
sendCapacity,
59+
calculateCycles,
5560
getAddressesByWalletID,
5661
updateAddressDescription,
5762
}

packages/neuron-ui/src/states/stateProvider/reducer.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export enum AppActions {
2626
RemoveSendOutput = 'removeSendOutput',
2727
UpdateSendOutput = 'updateSendOutput',
2828
UpdateSendPrice = 'updateSendPrice',
29+
UpdateSendCycles = 'updateSendCycles',
2930
UpdateSendDescription = 'updateSendDescription',
3031
ClearSendState = 'clearSendState',
3132
UpdateSendLoading = 'updateSendLoading',
@@ -336,6 +337,21 @@ export const reducer = (
336337
},
337338
}
338339
}
340+
case AppActions.UpdateSendCycles: {
341+
/**
342+
* payload: new cycles
343+
*/
344+
return {
345+
...state,
346+
app: {
347+
...app,
348+
send: {
349+
...app.send,
350+
cycles: payload,
351+
},
352+
},
353+
}
354+
}
339355
case AppActions.UpdateSendDescription: {
340356
/**
341357
* payload: new description

packages/neuron-ui/src/types/Controller/index.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ declare namespace Controller {
4545
description: string
4646
}
4747

48+
interface CalculateCycles {
49+
walletID: string
50+
items: {
51+
address: string
52+
capacity: string
53+
}
54+
}
55+
4856
type GetAddressesByWalletIDParams = string
4957
interface UpdateAddressDescriptionParams {
5058
walletID: string

packages/neuron-wallet/src/controllers/wallets/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,16 @@ export default class WalletsController {
392392
}
393393
}
394394

395+
@CatchControllerError
396+
public static async calculateCycles(params: { walletID: string; items: { address: string; capacity: string }[] }) {
397+
// TODO: This is a mock cycles
398+
const cycles = params.items.filter(item => +item.capacity > 0).length.toString()
399+
return {
400+
status: ResponseCode.Success,
401+
result: cycles,
402+
}
403+
}
404+
395405
@CatchControllerError
396406
public static async updateAddressDescription({
397407
walletID,

0 commit comments

Comments
 (0)