Skip to content

Commit

Permalink
Merge pull request #872 from nervosnetwork/compute-cycles
Browse files Browse the repository at this point in the history
feat: compute cycles
  • Loading branch information
classicalliu authored Aug 19, 2019
2 parents 6f3b695 + 01e9165 commit 3d673f8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
20 changes: 20 additions & 0 deletions packages/neuron-wallet/src/controllers/wallets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,26 @@ export default class WalletsController {
}
}

@CatchControllerError
public static async computeCycles(params: { id: string; walletID: string; capacities: string }) {
if (!params) {
throw new IsRequired('Parameters')
}
try {
const walletsService = WalletsService.getInstance()
const cycles = await walletsService.computeCycles(params.walletID, params.capacities)
return {
status: ResponseCode.Success,
result: cycles,
}
} catch (err) {
return {
status: ResponseCode.Fail,
msg: `Error: "${err.message}"`,
}
}
}

@CatchControllerError
public static async updateAddressDescription({
walletID,
Expand Down
31 changes: 27 additions & 4 deletions packages/neuron-wallet/src/services/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { WalletListSubject, CurrentWalletSubject } from 'models/subjects/wallets
import dataUpdateSubject from 'models/subjects/data-update'
import CommandSubject from 'models/subjects/command'
import WindowManager from 'models/window-manager'
import CellsService from 'services/cells'

import NodeService from './node'
import FileService from './file'
Expand All @@ -27,6 +28,7 @@ const fileService = FileService.getInstance()

const MODULE_NAME = 'wallets'
const DEBOUNCE_TIME = 200
const SECP_CYCLES = BigInt('1440000')

export interface Wallet {
id: string
Expand Down Expand Up @@ -315,7 +317,7 @@ export default class WalletService {
throw new IsRequired('Password')
}

const addressInfos = await this.getAddressInfos()
const addressInfos = await this.getAddressInfos(walletID)

const addresses: string[] = addressInfos.map(info => info.address)

Expand Down Expand Up @@ -371,10 +373,31 @@ export default class WalletService {
return txHash
}

public computeCycles = async (walletID: string = '', capacities: string): Promise<string> => {
const wallet = await this.get(walletID)
if (!wallet) {
throw new WalletNotFound(walletID)
}

const addressInfos = await this.getAddressInfos(walletID)

const addresses: string[] = addressInfos.map(info => info.address)

const lockHashes: string[] = await LockUtils.addressesToAllLockHashes(addresses)

const { inputs } = await CellsService.gatherInputs(capacities, lockHashes, '0')
const cycles = SECP_CYCLES * BigInt(inputs.length)

return cycles.toString()
}

// path is a BIP44 full path such as "m/44'/309'/0'/0/0"
public getAddressInfos = async (): Promise<AddressInterface[]> => {
const walletId = this.getCurrent()!.id
const addrs = await AddressService.allAddressesByWalletId(walletId)
public getAddressInfos = async (walletID: string): Promise<AddressInterface[]> => {
const wallet = await this.get(walletID)
if (!wallet) {
throw new WalletNotFound(walletID)
}
const addrs = await AddressService.allAddressesByWalletId(walletID)
return addrs
}

Expand Down

0 comments on commit 3d673f8

Please sign in to comment.