Skip to content

Commit 9ce3174

Browse files
committed
feat(neuron-ui): calculate transaction fee with user-specified price
1 parent 1934317 commit 9ce3174

8 files changed

Lines changed: 44 additions & 11 deletions

File tree

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { useTranslation } from 'react-i18next'
33
import { Stack, Text, Label, Modal, TextField, PrimaryButton, DefaultButton } from 'office-ui-fabric-react'
44
import { StateWithDispatch, AppActions } from 'states/stateProvider/reducer'
55
import actionCreators from 'states/stateProvider/actionCreators'
6+
import { priceToFee } from 'utils/formatters'
67

78
const PasswordRequest = ({
89
app: {
9-
send: { txID, outputs, description },
10+
send: { txID, outputs, description, price, cycles },
1011
passwordRequest: { walletID = '', actionType = null, password = '' },
1112
},
1213
settings: { wallets = [] },
@@ -33,14 +34,16 @@ const PasswordRequest = ({
3334
break
3435
}
3536
case 'send': {
36-
dispatch(actionCreators.submitTransaction(txID, walletID, outputs, description, password))
37+
dispatch(
38+
actionCreators.submitTransaction(txID, walletID, outputs, description, password, priceToFee(price, cycles))
39+
)
3740
break
3841
}
3942
default: {
4043
break
4144
}
4245
}
43-
}, [dispatch, walletID, password, actionType, txID, description, outputs])
46+
}, [dispatch, walletID, password, actionType, txID, description, outputs, cycles, price])
4447

4548
const onChange = useCallback(
4649
(_e, value?: string) => {

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { StateWithDispatch } from 'states/stateProvider/reducer'
2222
import appState from 'states/initStates/app'
2323

2424
import { PlaceHolders, CapacityUnit } from 'utils/const'
25-
import { shannonToCKBFormatter } from 'utils/formatters'
25+
import { shannonToCKBFormatter, priceToFee } from 'utils/formatters'
2626

2727
import { useInitialize } from './hooks'
2828

@@ -160,7 +160,12 @@ const Send = ({
160160
</Stack>
161161
</Stack>
162162

163-
<TransactionFeePanel fee="10" cycles="10" price={send.price} onPriceChange={updateTransactionPrice} />
163+
<TransactionFeePanel
164+
fee={shannonToCKBFormatter(priceToFee(send.price, send.cycles))}
165+
cycles={send.cycles}
166+
price={send.price}
167+
onPriceChange={updateTransactionPrice}
168+
/>
164169

165170
<Stack horizontal verticalAlign="center" tokens={{ childrenGap: 20 }}>
166171
<Stack.Item styles={{ root: { width: labelWidth } }}>

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useState } from 'react'
2-
import { Stack, Label, TextField, Dropdown, Toggle, Icon } from 'office-ui-fabric-react'
2+
import { Stack, Label, TextField, Dropdown, Toggle, Icon, IDropdownOption } from 'office-ui-fabric-react'
33
import { Down } from 'grommet-icons'
44
import { useTranslation } from 'react-i18next'
55

@@ -18,6 +18,16 @@ registerIcons({
1818
},
1919
})
2020

21+
const calculateSpeed = (price: number) => {
22+
if (price >= 160) {
23+
return '180'
24+
}
25+
if (price >= 40) {
26+
return '60'
27+
}
28+
return '0'
29+
}
30+
2131
const TransactionFee: React.FunctionComponent<TransactionFee> = ({
2232
cycles,
2333
price,
@@ -34,6 +44,8 @@ const TransactionFee: React.FunctionComponent<TransactionFee> = ({
3444
</Stack.Item>
3545
)
3646

47+
const selectedSpeed = calculateSpeed(+price)
48+
3749
return (
3850
<Stack tokens={{ childrenGap: 15 }}>
3951
<Stack horizontal verticalAlign="end" horizontalAlign="space-between">
@@ -85,7 +97,7 @@ const TransactionFee: React.FunctionComponent<TransactionFee> = ({
8597
<Stack.Item>
8698
<Dropdown
8799
dropdownWidth={140}
88-
defaultSelectedKey="0"
100+
selectedKey={selectedSpeed}
89101
options={[
90102
{ key: '0', text: 'immediately' },
91103
{ key: '30', text: '~ 30s' },
@@ -95,6 +107,11 @@ const TransactionFee: React.FunctionComponent<TransactionFee> = ({
95107
onRenderCaretDown={() => {
96108
return <Icon iconName="ArrowDown" />
97109
}}
110+
onChange={(e: any, item?: IDropdownOption) => {
111+
if (item) {
112+
onPriceChange(e, item.key)
113+
}
114+
}}
98115
/>
99116
</Stack.Item>
100117
</Stack>

packages/neuron-ui/src/states/initStates/app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const appState: State.App = {
1515
},
1616
],
1717
price: '0',
18+
cycles: '0',
1819
description: '',
1920
loading: false,
2021
},

packages/neuron-ui/src/states/stateProvider/actionCreators/send.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ export default {
1111
walletID: string = '',
1212
items: TransactionOutput[] = [],
1313
description: string = '',
14-
password: string = ''
14+
password: string = '',
15+
fee: string = '0'
1516
) => {
1617
walletsCall.sendCapacity({
1718
id,
@@ -21,7 +22,7 @@ export default {
2122
capacity: CKBToShannonFormatter(item.amount, item.unit),
2223
})),
2324
password,
24-
fee: '0',
25+
fee,
2526
description,
2627
})
2728
return {

packages/neuron-ui/src/stories/TransactionFeePanel.stories.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import TransactionFeePanel from 'components/TransactionFeePanel'
55

66
const states = {
77
default: {
8-
cycles: '0',
9-
price: '0',
8+
cycles: '180',
9+
price: '10',
1010
fee: '0',
1111
onPriceChange: (args: any) => action(args),
1212
},

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ declare namespace State {
5555
txID: string
5656
outputs: Output[]
5757
price: string
58+
cycles: string
5859
description: string
5960
loading: boolean
6061
}

packages/neuron-ui/src/utils/formatters.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,16 @@ export const uniformTimeFormatter = (time: string | number | Date) => {
110110
return timeFormatter.format(+time).replace(/\//g, '-')
111111
}
112112

113+
export const priceToFee = (price: string, cycles: string) => {
114+
return (BigInt(price) * BigInt(cycles)).toString()
115+
}
116+
113117
export default {
114118
queryFormatter,
115119
currencyFormatter,
116120
CKBToShannonFormatter,
117121
shannonToCKBFormatter,
118122
localNumberFormatter,
119123
uniformTimeFormatter,
124+
priceToFee,
120125
}

0 commit comments

Comments
 (0)