Skip to content

Commit

Permalink
feat: compute cycles
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Aug 16, 2019
1 parent 8a18b7a commit 2be05b3
Show file tree
Hide file tree
Showing 2 changed files with 46 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 @@ -367,6 +367,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
30 changes: 26 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 Down Expand Up @@ -315,7 +316,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 +372,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 = BigInt(1387008) * 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 2be05b3

Please sign in to comment.