Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions packages/neuron-ui/src/components/ClearCache/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const IDs = {
rebuildCacheOption: 'rebuild-cache-option',
}

const ClearCache = ({ dispatch }: { dispatch: StateDispatch }) => {
const ClearCache = ({ dispatch, hideRebuild }: { dispatch: StateDispatch; hideRebuild?: boolean }) => {
const [t] = useTranslation()
const [clearedDate, setClearedDate] = useState(cacheClearDate.load())
const [isClearing, setIsClearing] = useState(false)
Expand Down Expand Up @@ -96,8 +96,12 @@ const ClearCache = ({ dispatch }: { dispatch: StateDispatch }) => {
<div className={styles.options}>
<input type="checkbox" id={IDs.refreshCacheOption} checked disabled />
<label htmlFor={IDs.refreshCacheOption}>{t(`${I18N_PATH}.options.refresh.label`)}</label>
<input type="checkbox" id={IDs.rebuildCacheOption} checked={isRebuild} onChange={toggleIsRebuild} />
<label htmlFor={IDs.rebuildCacheOption}>{t(`${I18N_PATH}.options.rebuild.label`)}</label>
{hideRebuild ? null : (
<>
<input type="checkbox" id={IDs.rebuildCacheOption} checked={isRebuild} onChange={toggleIsRebuild} />
<label htmlFor={IDs.rebuildCacheOption}>{t(`${I18N_PATH}.options.rebuild.label`)}</label>
</>
)}
</div>
<div className={styles.footer}>
<Button type="submit" label={t(`${I18N_PATH}.buttons.ok`)} onClick={handleSubmit} id={IDs.submitClearCache} />
Expand Down
17 changes: 13 additions & 4 deletions packages/neuron-ui/src/components/DataSetting/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import React, { useCallback } from 'react'
import React, { useCallback, useMemo } from 'react'
import { useTranslation } from 'react-i18next'
import Button from 'widgets/Button'
import ClearCache from 'components/ClearCache'
import { useDispatch } from 'states'
import { useDispatch, useState as useGlobalState } from 'states'
import { ReactComponent as Attention } from 'widgets/Icons/ExperimentalAttention.svg'
import CopyZone from 'widgets/CopyZone'
import { OpenFolder, InfoCircleOutlined } from 'widgets/Icons/icon'
import { shell } from 'electron'
import Spinner from 'widgets/Spinner'
import { LIGHT_NETWORK_TYPE } from 'utils/const'
import { useDataPath } from './hooks'

import styles from './index.module.scss'
Expand Down Expand Up @@ -87,10 +88,18 @@ const SetItem = () => {

const DataSetting = () => {
const dispatch = useDispatch()
const {
chain: { networkID },
settings: { networks = [] },
} = useGlobalState()
const isLightClient = useMemo(() => networks.find(n => n.id === networkID)?.type === LIGHT_NETWORK_TYPE, [
networkID,
networks,
])
return (
<div className={styles.root}>
<SetItem />
<ClearCache dispatch={dispatch} />
{isLightClient ? null : <SetItem />}
<ClearCache dispatch={dispatch} hideRebuild={isLightClient} />
</div>
)
}
Expand Down
35 changes: 33 additions & 2 deletions packages/neuron-ui/src/components/MultisigAddress/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import {
getMultisigBalances,
loadMultisigTxJson,
OfflineSignJSON,
getMultisigSyncProgress,
} from 'services/remote'
import { addressToScript, scriptToAddress } from '@nervosnetwork/ckb-sdk-utils'
import { addressToScript, scriptToAddress, scriptToHash } from '@nervosnetwork/ckb-sdk-utils'

export const useSearch = (clearSelected: () => void, onFilterConfig: (searchKey: string) => void) => {
const [keywords, setKeywords] = useState('')
Expand Down Expand Up @@ -279,27 +280,57 @@ export const useSubscription = ({
walletId,
isMainnet,
configs,
isLightClient,
}: {
walletId: string
isMainnet: boolean
configs: MultisigConfig[]
isLightClient: boolean
}) => {
const [multisigBanlances, setMultisigBanlances] = useState<Record<string, string>>({})
const [multisigSyncProgress, setMultisigSyncProgress] = useState<Record<string, number>>({})
const getAndSaveMultisigBalances = useCallback(() => {
getMultisigBalances({ isMainnet, multisigAddresses: configs.map(v => v.fullPayload) }).then(res => {
if (isSuccessResponse(res) && res.result) {
setMultisigBanlances(res.result)
}
})
}, [setMultisigBanlances, isMainnet, configs])
const hashToPayload = useMemo(
() =>
configs.reduce<Record<string, string>>(
(pre, cur) => ({ ...pre, [scriptToHash(addressToScript(cur.fullPayload))]: cur.fullPayload }),
{}
),
[configs]
)
const getAndSaveMultisigSyncProgress = useCallback(() => {
getMultisigSyncProgress(Object.keys(hashToPayload)).then(res => {
if (isSuccessResponse(res) && res.result) {
const tmp: Record<string, number> = {}
res.result.forEach(v => {
if (hashToPayload[v.hash]) {
tmp[hashToPayload[v.hash]] = v.blockStartNumber
}
})
setMultisigSyncProgress(tmp)
}
})
}, [hashToPayload])
useEffect(() => {
const dataUpdateSubscription = MultisigOutputUpdate.subscribe(() => {
getAndSaveMultisigBalances()
if (isLightClient) {
getAndSaveMultisigSyncProgress()
}
})
getAndSaveMultisigBalances()
if (isLightClient) {
getAndSaveMultisigSyncProgress()
}
return () => {
dataUpdateSubscription.unsubscribe()
}
}, [walletId, getAndSaveMultisigBalances])
return multisigBanlances
return { multisigBanlances, multisigSyncProgress }
}
19 changes: 16 additions & 3 deletions packages/neuron-ui/src/components/MultisigAddress/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { EditTextField } from 'widgets/TextField'
import { MultisigConfig } from 'services/remote'
import PasswordRequest from 'components/PasswordRequest'
import ApproveMultisigTx from 'components/ApproveMultisigTx'
import { LIGHT_NETWORK_TYPE } from 'utils/const'
import { useSearch, useConfigManage, useExportConfig, useActions, useSubscription } from './hooks'

import styles from './multisigAddress.module.scss'
Expand Down Expand Up @@ -52,6 +53,10 @@ const MultisigAddress = () => {
// eslint-disable-next-line
}, [i18n.language])
const isMainnet = isMainnetUtil(networks, networkID)
const isLightClient = useMemo(() => networks.find(n => n.id === networkID)?.type === LIGHT_NETWORK_TYPE, [
networks,
networkID,
])
const { openDialog, closeDialog, dialogRef, isDialogOpen } = useDialogWrapper()
const {
allConfigs,
Expand All @@ -65,7 +70,12 @@ const MultisigAddress = () => {
walletId,
isMainnet,
})
const multisigBanlances = useSubscription({ walletId, isMainnet, configs: allConfigs })
const { multisigBanlances, multisigSyncProgress } = useSubscription({
walletId,
isMainnet,
configs: allConfigs,
isLightClient,
})
const { deleteAction, infoAction, sendAction, approveAction } = useActions({ deleteConfigById })
const onClickItem = useCallback(
(multisigConfig: MultisigConfig) => (option: { key: string }) => {
Expand Down Expand Up @@ -138,8 +148,10 @@ const MultisigAddress = () => {
<th className={styles.checkBoxTh}>
<input type="checkbox" onChange={onChangeCheckedAll} checked={isAllSelected} />
</th>
{['address', 'alias', 'type', 'balance'].map(field => (
<th key={field}>{t(`multisig-address.table.${field}`)}</th>
{['address', 'alias', 'type', ...(isLightClient ? ['sync-block'] : []), 'balance'].map(field => (
<th key={field} data-field={field}>
{t(`multisig-address.table.${field}`)}
</th>
))}
</tr>
</thead>
Expand Down Expand Up @@ -173,6 +185,7 @@ const MultisigAddress = () => {
&nbsp;of&nbsp;
{v.n}
</td>
{isLightClient ? <td>{multisigSyncProgress?.[v.fullPayload] ?? 0}</td> : null}
<td>
{shannonToCKBFormatter(multisigBanlances[v.fullPayload])}
&nbsp;CKB
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
padding: 8px;
min-width: 40px;
text-align: left;

&[data-field="sync-block"] {
min-width: 70px;
}
}

.checkBoxTh {
Expand Down
1 change: 1 addition & 0 deletions packages/neuron-ui/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,7 @@
"type": "type",
"balance": "balance",
"copy-address": "Copy Address",
"sync-block": "Synced Block",
"actions": {
"info": "Info",
"send": "Send",
Expand Down
1 change: 1 addition & 0 deletions packages/neuron-ui/src/locales/zh-tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,7 @@
"type": "類型",
"balance": "余额",
"copy-address": "復製地址",
"sync-block": "同步高度",
"actions": {
"info": "詳情",
"send": "轉賬",
Expand Down
1 change: 1 addition & 0 deletions packages/neuron-ui/src/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,7 @@
"type": "类型",
"balance": "余额",
"copy-address": "复制地址",
"sync-block": "同步高度",
"actions": {
"info": "详情",
"send": "转账",
Expand Down
3 changes: 3 additions & 0 deletions packages/neuron-ui/src/services/remote/multisig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ export const generateMultisigSendAllTx = remoteApi<{
multisigConfig: MultisigConfig
}>('generate-multisig-send-all-tx')
export const loadMultisigTxJson = remoteApi<string, OfflineSignJSON>('load-multisig-tx-json')
export const getMultisigSyncProgress = remoteApi<string[], { hash: string; blockStartNumber: number }[]>(
'get-sync-progress-by-addresses'
)
1 change: 1 addition & 0 deletions packages/neuron-ui/src/services/remote/remoteApiWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ type Action =
| 'load-multisig-tx-json'
| 'get-hold-sudt-cell-capacity'
| 'start-migrate'
| 'get-sync-progress-by-addresses'

export const remoteApi = <P = any, R = any>(action: Action) => async (params: P): Promise<ControllerResponse<R>> => {
const res: SuccessFromController<R> | FailureFromController = await ipcRenderer.invoke(action, params).catch(() => ({
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-ui/src/types/App/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ declare namespace State {
name: string
remote: string
chain: 'ckb' | 'ckb_testnet' | 'ckb_dev' | string
type: 0 | 1
type: 0 | 1 | 2
}

interface Network extends NetworkProperty {
Expand Down
1 change: 1 addition & 0 deletions packages/neuron-ui/src/utils/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ export const DEPRECATED_CODE_HASH: Record<string, string> = {
}

export const LIGHT_CLIENT_TESTNET = 'light_client_testnet'
export const LIGHT_NETWORK_TYPE = 2
2 changes: 1 addition & 1 deletion packages/neuron-ui/src/widgets/InputSelect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const Select = ({ value, options, placeholder, disabled, onChange, className }:

const onDocumentClick = useCallback(
(e: any) => {
if (mounted.current && !root.current!.contains(e.target) && openRef.current) {
if (mounted.current && !root.current?.contains(e.target) && openRef.current) {
setOpen(false)
}
},
Expand Down
18 changes: 18 additions & 0 deletions packages/neuron-wallet/src/block-sync-renderer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import logger from 'utils/logger'
import CommonUtils from 'utils/common'
import queueWrapper from 'utils/queue'
import env from 'env'
import MultisigConfigDbChangedSubject from 'models/subjects/multisig-config-db-changed-subject'
import Multisig from 'services/multisig'
import { SyncAddressType } from 'database/chain/entities/sync-progress'
import { debounceTime } from 'rxjs/operators'

let network: Network | null
let child: ChildProcess | null = null
Expand Down Expand Up @@ -193,3 +197,17 @@ export const registerRequest = (c: ChildProcess, msg: Required<WorkerMessage>) =

AddressCreatedSubject.getSubject().subscribe(() => resetSyncTaskQueue.asyncPush(true))
WalletDeletedSubject.getSubject().subscribe(() => resetSyncTaskQueue.asyncPush(true))
MultisigConfigDbChangedSubject.getSubject()
.pipe(debounceTime(1000))
.subscribe(async () => {
if (!child) {
return
}
const appendScripts = await Multisig.getMultisigConfigForLight()
const msg: Required<WorkerMessage<
{ walletId: string; script: CKBComponents.Script; addressType: SyncAddressType }[]
>> = { type: 'call', channel: 'append_scripts', id: requestId++, message: appendScripts }
return registerRequest(child, msg).catch(err => {
logger.error(`Sync:\ffailed to append script to light client`, err)
})
})
11 changes: 11 additions & 0 deletions packages/neuron-wallet/src/block-sync-renderer/sync/connector.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SyncAddressType } from 'database/chain/entities/sync-progress'
import { Subject } from 'rxjs'

export interface BlockTips {
Expand Down Expand Up @@ -33,6 +34,13 @@ export interface LumosCell {
data?: string
}

export interface AppendScript {
walletId: string
script: CKBComponents.Script
addressType: SyncAddressType
scriptType: CKBRPC.ScriptType
}

export abstract class Connector<TransactionsSubjectParam = unknown> {
abstract blockTipsSubject: Subject<BlockTips>
abstract transactionsSubject: Subject<{ txHashes: CKBComponents.Hash[]; params: TransactionsSubjectParam }>
Expand All @@ -41,4 +49,7 @@ export abstract class Connector<TransactionsSubjectParam = unknown> {
abstract notifyCurrentBlockNumberProcessed(param: TransactionsSubjectParam): void
abstract stop(): void
abstract getLiveCellsByScript(query: LumosCellQuery): Promise<unknown>
async appendScript(_scripts: AppendScript[]) {
// do nothing
}
}
Loading