Skip to content

Commit 41953bf

Browse files
committed
feat: add dao methods to controller
1 parent 796228c commit 41953bf

5 files changed

Lines changed: 158 additions & 4 deletions

File tree

packages/neuron-wallet/src/controllers/api.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import { ConnectionStatusSubject } from 'models/subjects/node'
1919
import { SystemScriptSubject } from 'models/subjects/system-script'
2020
import { MapApiResponse } from 'decorators'
2121
import { ResponseCode } from 'utils/const'
22-
import { TransactionWithoutHash } from 'types/cell-types'
22+
import { TransactionWithoutHash, OutPoint } from 'types/cell-types'
23+
import DaoController from './dao'
2324

2425
/**
2526
* @class ApiController
@@ -202,6 +203,37 @@ export default class ApiController {
202203
return WalletsController.generateTx(params)
203204
}
204205

206+
@MapApiResponse
207+
public static async generateDepositTx(params: {
208+
walletID: string,
209+
capacity: string,
210+
fee: string,
211+
feeRate: string,
212+
}) {
213+
return WalletsController.generateDepositTx(params)
214+
}
215+
216+
@MapApiResponse
217+
public static async startWithdrawFromDao(params: {
218+
walletID: string,
219+
outPoint: OutPoint,
220+
fee: string,
221+
feeRate: string,
222+
}) {
223+
return WalletsController.startWithdrawFromDao(params)
224+
}
225+
226+
@MapApiResponse
227+
public static async withdrawFormDao(params: {
228+
walletID: string,
229+
depositOutPoint: OutPoint,
230+
withdrawingOutPoint: OutPoint,
231+
fee: string,
232+
feeRate: string,
233+
}) {
234+
return WalletsController.withdrawFormDao(params)
235+
}
236+
205237
@MapApiResponse
206238
public static async computeCycles(params: { walletID: string; capacities: string }) {
207239
return WalletsController.computeCycles(params)
@@ -266,6 +298,15 @@ export default class ApiController {
266298
showWindow(`${env.mainURL}#/transaction/${hash}`, i18n.t(`messageBox.transaction.title`, { hash }))
267299
}
268300

301+
// Dao
302+
303+
@MapApiResponse
304+
public static async getDaoCells(
305+
params: Controller.Params.GetDaoCellsParams,
306+
) {
307+
return DaoController.getDaoCells(params)
308+
}
309+
269310
// Misc
270311

271312
@MapApiResponse
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { PaginationResult } from 'services/tx'
2+
import { Cell } from 'types/cell-types'
3+
import CellsService from '../services/cells'
4+
import { ServiceHasNoResponse } from 'exceptions'
5+
import { ResponseCode } from 'utils/const'
6+
import AddressesService from 'services/addresses'
7+
import LockUtils from 'models/lock-utils'
8+
9+
export default class DaoController {
10+
public static async getDaoCells(
11+
params: Controller.Params.GetDaoCellsParams
12+
): Promise<Controller.Response<PaginationResult<Cell>>> {
13+
const { walletID, pageNo, pageSize } = params
14+
const addresses = (await AddressesService.allAddressesByWalletId(walletID)).map(addr => addr.address)
15+
const lockHashes: string[] = new LockUtils(await LockUtils.systemScript())
16+
.addressesToAllLockHashes(addresses)
17+
const cells = await CellsService.getDaoCells(lockHashes, pageNo, pageSize)
18+
19+
if (!cells) {
20+
throw new ServiceHasNoResponse('DaoCells')
21+
}
22+
23+
return {
24+
status: ResponseCode.Success,
25+
result: { ...params, ...cells },
26+
}
27+
}
28+
}

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

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
import i18n from 'utils/i18n'
2020
import AddressService from 'services/addresses'
2121
import WalletCreatedSubject from 'models/subjects/wallet-created-subject'
22-
import { TransactionWithoutHash } from 'types/cell-types'
22+
import { TransactionWithoutHash, OutPoint } from 'types/cell-types'
2323

2424
export default class WalletsController {
2525
public static async getAll(): Promise<Controller.Response<Pick<Wallet, 'id' | 'name'>[]>> {
@@ -400,6 +400,74 @@ export default class WalletsController {
400400
}
401401
}
402402

403+
public static async generateDepositTx(params: {
404+
walletID: string,
405+
capacity: string,
406+
fee: string,
407+
feeRate: string,
408+
}) {
409+
if (!params) {
410+
throw new IsRequired('Parameters')
411+
}
412+
const walletsService = WalletsService.getInstance()
413+
const tx = await walletsService.generateDepositTx(
414+
params.walletID,
415+
params.capacity,
416+
params.fee,
417+
params.feeRate,
418+
)
419+
return {
420+
status: ResponseCode.Success,
421+
result: tx,
422+
}
423+
}
424+
425+
public static async startWithdrawFromDao(params: {
426+
walletID: string,
427+
outPoint: OutPoint,
428+
fee: string,
429+
feeRate: string,
430+
}) {
431+
if (!params) {
432+
throw new IsRequired('Parameters')
433+
}
434+
const walletsService = WalletsService.getInstance()
435+
const tx = await walletsService.startWithdrawFromDao(
436+
params.walletID,
437+
params.outPoint,
438+
params.fee,
439+
params.feeRate,
440+
)
441+
return {
442+
status: ResponseCode.Success,
443+
result: tx,
444+
}
445+
}
446+
447+
public static async withdrawFormDao(params: {
448+
walletID: string,
449+
depositOutPoint: OutPoint,
450+
withdrawingOutPoint: OutPoint,
451+
fee: string,
452+
feeRate: string,
453+
}) {
454+
if (!params) {
455+
throw new IsRequired('Parameters')
456+
}
457+
const walletsService = WalletsService.getInstance()
458+
const tx = await walletsService.withdrawFormDao(
459+
params.walletID,
460+
params.depositOutPoint,
461+
params.withdrawingOutPoint,
462+
params.fee,
463+
params.feeRate,
464+
)
465+
return {
466+
status: ResponseCode.Success,
467+
result: tx,
468+
}
469+
}
470+
403471
public static async computeCycles(params: { walletID: string; capacities: string }) {
404472
if (!params) {
405473
throw new IsRequired('Parameters')

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,13 +498,17 @@ export default class WalletService {
498498
return tx
499499
}
500500

501-
public startWithdrawFormDao = async (
501+
public startWithdrawFromDao = async (
502502
walletID: string,
503-
lockHashes: string[],
504503
outPoint: OutPoint,
505504
fee: string = '0',
506505
feeRate: string = '0'
507506
): Promise<TransactionWithoutHash> => {
507+
const wallet = await this.get(walletID)
508+
if (!wallet) {
509+
throw new WalletNotFound(walletID)
510+
}
511+
508512
const cellStatus = await core.rpc.getLiveCell(outPoint, false)
509513
if (cellStatus.status !== 'live') {
510514
throw new Error('Cell is not yet live!')
@@ -514,6 +518,12 @@ export default class WalletService {
514518
throw new Error('Transaction is not committed yet!')
515519
}
516520

521+
const addressInfos = await this.getAddressInfos(walletID)
522+
523+
const addresses: string[] = addressInfos.map(info => info.address)
524+
525+
const lockHashes: string[] = new LockUtils(await LockUtils.systemScript()).addressesToAllLockHashes(addresses)
526+
517527
const feeInt = BigInt(fee)
518528
const feeRateInt = BigInt(feeRate)
519529
const mode = new FeeMode(feeRateInt)

packages/neuron-wallet/src/types/controller.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ declare module Controller {
2222
pageSize: number
2323
addresses: string
2424
}
25+
26+
interface GetDaoCellsParams {
27+
walletID: string
28+
pageNo: number
29+
pageSize: number
30+
}
31+
2532
interface BackupWallet {
2633
id: string
2734
password: string

0 commit comments

Comments
 (0)