Skip to content

Commit 2be05b3

Browse files
committed
feat: compute cycles
1 parent 8a18b7a commit 2be05b3

2 files changed

Lines changed: 46 additions & 4 deletions

File tree

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,26 @@ export default class WalletsController {
367367
}
368368
}
369369

370+
@CatchControllerError
371+
public static async computeCycles(params: { id: string; walletID: string; capacities: string }) {
372+
if (!params) {
373+
throw new IsRequired('Parameters')
374+
}
375+
try {
376+
const walletsService = WalletsService.getInstance()
377+
const cycles = await walletsService.computeCycles(params.walletID, params.capacities)
378+
return {
379+
status: ResponseCode.Success,
380+
result: cycles,
381+
}
382+
} catch (err) {
383+
return {
384+
status: ResponseCode.Fail,
385+
msg: `Error: "${err.message}"`,
386+
}
387+
}
388+
}
389+
370390
@CatchControllerError
371391
public static async updateAddressDescription({
372392
walletID,

packages/neuron-wallet/src/services/wallets.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { WalletListSubject, CurrentWalletSubject } from 'models/subjects/wallets
1616
import dataUpdateSubject from 'models/subjects/data-update'
1717
import CommandSubject from 'models/subjects/command'
1818
import WindowManager from 'models/window-manager'
19+
import CellsService from 'services/cells'
1920

2021
import NodeService from './node'
2122
import FileService from './file'
@@ -315,7 +316,7 @@ export default class WalletService {
315316
throw new IsRequired('Password')
316317
}
317318

318-
const addressInfos = await this.getAddressInfos()
319+
const addressInfos = await this.getAddressInfos(walletID)
319320

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

@@ -371,10 +372,31 @@ export default class WalletService {
371372
return txHash
372373
}
373374

375+
public computeCycles = async (walletID: string = '', capacities: string): Promise<string> => {
376+
const wallet = await this.get(walletID)
377+
if (!wallet) {
378+
throw new WalletNotFound(walletID)
379+
}
380+
381+
const addressInfos = await this.getAddressInfos(walletID)
382+
383+
const addresses: string[] = addressInfos.map(info => info.address)
384+
385+
const lockHashes: string[] = await LockUtils.addressesToAllLockHashes(addresses)
386+
387+
const { inputs } = await CellsService.gatherInputs(capacities, lockHashes, '0')
388+
const cycles = BigInt(1387008) * BigInt(inputs.length)
389+
390+
return cycles.toString()
391+
}
392+
374393
// path is a BIP44 full path such as "m/44'/309'/0'/0/0"
375-
public getAddressInfos = async (): Promise<AddressInterface[]> => {
376-
const walletId = this.getCurrent()!.id
377-
const addrs = await AddressService.allAddressesByWalletId(walletId)
394+
public getAddressInfos = async (walletID: string): Promise<AddressInterface[]> => {
395+
const wallet = await this.get(walletID)
396+
if (!wallet) {
397+
throw new WalletNotFound(walletID)
398+
}
399+
const addrs = await AddressService.allAddressesByWalletId(walletID)
378400
return addrs
379401
}
380402

0 commit comments

Comments
 (0)