Skip to content

Commit

Permalink
feat: add dao methods to controller
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Nov 6, 2019
1 parent 796228c commit 41953bf
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 4 deletions.
43 changes: 42 additions & 1 deletion packages/neuron-wallet/src/controllers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import { ConnectionStatusSubject } from 'models/subjects/node'
import { SystemScriptSubject } from 'models/subjects/system-script'
import { MapApiResponse } from 'decorators'
import { ResponseCode } from 'utils/const'
import { TransactionWithoutHash } from 'types/cell-types'
import { TransactionWithoutHash, OutPoint } from 'types/cell-types'
import DaoController from './dao'

/**
* @class ApiController
Expand Down Expand Up @@ -202,6 +203,37 @@ export default class ApiController {
return WalletsController.generateTx(params)
}

@MapApiResponse
public static async generateDepositTx(params: {
walletID: string,
capacity: string,
fee: string,
feeRate: string,
}) {
return WalletsController.generateDepositTx(params)
}

@MapApiResponse
public static async startWithdrawFromDao(params: {
walletID: string,
outPoint: OutPoint,
fee: string,
feeRate: string,
}) {
return WalletsController.startWithdrawFromDao(params)
}

@MapApiResponse
public static async withdrawFormDao(params: {
walletID: string,
depositOutPoint: OutPoint,
withdrawingOutPoint: OutPoint,
fee: string,
feeRate: string,
}) {
return WalletsController.withdrawFormDao(params)
}

@MapApiResponse
public static async computeCycles(params: { walletID: string; capacities: string }) {
return WalletsController.computeCycles(params)
Expand Down Expand Up @@ -266,6 +298,15 @@ export default class ApiController {
showWindow(`${env.mainURL}#/transaction/${hash}`, i18n.t(`messageBox.transaction.title`, { hash }))
}

// Dao

@MapApiResponse
public static async getDaoCells(
params: Controller.Params.GetDaoCellsParams,
) {
return DaoController.getDaoCells(params)
}

// Misc

@MapApiResponse
Expand Down
28 changes: 28 additions & 0 deletions packages/neuron-wallet/src/controllers/dao.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { PaginationResult } from 'services/tx'
import { Cell } from 'types/cell-types'
import CellsService from '../services/cells'
import { ServiceHasNoResponse } from 'exceptions'
import { ResponseCode } from 'utils/const'
import AddressesService from 'services/addresses'
import LockUtils from 'models/lock-utils'

export default class DaoController {
public static async getDaoCells(
params: Controller.Params.GetDaoCellsParams
): Promise<Controller.Response<PaginationResult<Cell>>> {
const { walletID, pageNo, pageSize } = params
const addresses = (await AddressesService.allAddressesByWalletId(walletID)).map(addr => addr.address)
const lockHashes: string[] = new LockUtils(await LockUtils.systemScript())
.addressesToAllLockHashes(addresses)
const cells = await CellsService.getDaoCells(lockHashes, pageNo, pageSize)

if (!cells) {
throw new ServiceHasNoResponse('DaoCells')
}

return {
status: ResponseCode.Success,
result: { ...params, ...cells },
}
}
}
70 changes: 69 additions & 1 deletion packages/neuron-wallet/src/controllers/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import i18n from 'utils/i18n'
import AddressService from 'services/addresses'
import WalletCreatedSubject from 'models/subjects/wallet-created-subject'
import { TransactionWithoutHash } from 'types/cell-types'
import { TransactionWithoutHash, OutPoint } from 'types/cell-types'

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

public static async generateDepositTx(params: {
walletID: string,
capacity: string,
fee: string,
feeRate: string,
}) {
if (!params) {
throw new IsRequired('Parameters')
}
const walletsService = WalletsService.getInstance()
const tx = await walletsService.generateDepositTx(
params.walletID,
params.capacity,
params.fee,
params.feeRate,
)
return {
status: ResponseCode.Success,
result: tx,
}
}

public static async startWithdrawFromDao(params: {
walletID: string,
outPoint: OutPoint,
fee: string,
feeRate: string,
}) {
if (!params) {
throw new IsRequired('Parameters')
}
const walletsService = WalletsService.getInstance()
const tx = await walletsService.startWithdrawFromDao(
params.walletID,
params.outPoint,
params.fee,
params.feeRate,
)
return {
status: ResponseCode.Success,
result: tx,
}
}

public static async withdrawFormDao(params: {
walletID: string,
depositOutPoint: OutPoint,
withdrawingOutPoint: OutPoint,
fee: string,
feeRate: string,
}) {
if (!params) {
throw new IsRequired('Parameters')
}
const walletsService = WalletsService.getInstance()
const tx = await walletsService.withdrawFormDao(
params.walletID,
params.depositOutPoint,
params.withdrawingOutPoint,
params.fee,
params.feeRate,
)
return {
status: ResponseCode.Success,
result: tx,
}
}

public static async computeCycles(params: { walletID: string; capacities: string }) {
if (!params) {
throw new IsRequired('Parameters')
Expand Down
14 changes: 12 additions & 2 deletions packages/neuron-wallet/src/services/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,13 +498,17 @@ export default class WalletService {
return tx
}

public startWithdrawFormDao = async (
public startWithdrawFromDao = async (
walletID: string,
lockHashes: string[],
outPoint: OutPoint,
fee: string = '0',
feeRate: string = '0'
): Promise<TransactionWithoutHash> => {
const wallet = await this.get(walletID)
if (!wallet) {
throw new WalletNotFound(walletID)
}

const cellStatus = await core.rpc.getLiveCell(outPoint, false)
if (cellStatus.status !== 'live') {
throw new Error('Cell is not yet live!')
Expand All @@ -514,6 +518,12 @@ export default class WalletService {
throw new Error('Transaction is not committed yet!')
}

const addressInfos = await this.getAddressInfos(walletID)

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

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

const feeInt = BigInt(fee)
const feeRateInt = BigInt(feeRate)
const mode = new FeeMode(feeRateInt)
Expand Down
7 changes: 7 additions & 0 deletions packages/neuron-wallet/src/types/controller.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ declare module Controller {
pageSize: number
addresses: string
}

interface GetDaoCellsParams {
walletID: string
pageNo: number
pageSize: number
}

interface BackupWallet {
id: string
password: string
Expand Down

0 comments on commit 41953bf

Please sign in to comment.