Skip to content

Commit

Permalink
feat: slipt sendCapacity to generateTx and sendTx
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Oct 22, 2019
1 parent ea1a93d commit f956088
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 57 deletions.
25 changes: 21 additions & 4 deletions packages/neuron-wallet/src/controllers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,36 @@ export default class ApiController {
}

@CatchControllerError
public static async sendCapacity(params: {
public static async sendTx(params: {
id: string
walletID: string
tx: string,
password: string
description?: string
}) {
return WalletsController.sendTx(params)
}

@CatchControllerError
public static async generateTx(params: {
id: string
walletID: string
items: {
address: string
capacity: string
}[]
password: string
fee: string
feeRate: string
description?: string
}) {
return WalletsController.sendCapacity(params)
return WalletsController.generateTx(params)
}

@CatchControllerError
public static async calculateFee(params: {
id: string
tx: string
}) {
return WalletsController.calculateFee(params)
}

@CatchControllerError
Expand Down
48 changes: 35 additions & 13 deletions packages/neuron-wallet/src/controllers/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import i18n from 'utils/i18n'
import AddressService from 'services/addresses'
import WalletCreatedSubject from 'models/subjects/wallet-created-subject'
import logger from 'utils/logger'
import { TransactionWithoutHash } from 'types/cell-types';

export default class WalletsController {
@CatchControllerError
Expand Down Expand Up @@ -348,29 +349,23 @@ export default class WalletsController {
}

@CatchControllerError
public static async sendCapacity(params: {
public static async sendTx(params: {
id: string
walletID: string
items: {
address: string
capacity: string
}[]
tx: string,
password: string
fee: string
feeRate: string
description?: string
}) {
if (!params) {
throw new IsRequired('Parameters')
}
const transaction: TransactionWithoutHash = JSON.parse(params.tx)
try {
const walletsService = WalletsService.getInstance()
const hash = await walletsService.sendCapacity(
const hash = await walletsService.sendTx(
params.walletID,
params.items,
transaction,
params.password,
params.fee,
params.feeRate,
params.description
)
return {
Expand All @@ -387,25 +382,52 @@ export default class WalletsController {
}

@CatchControllerError
public static async calculateFee(params: {
public static async generateTx(params: {
id: string
walletID: string
items: {
address: string
capacity: string
}[]
fee: string
feeRate: string
}) {
if (!params) {
throw new IsRequired('Parameters')
}
try {
const walletsService = WalletsService.getInstance()
const fee = await walletsService.calculateFee(
const tx = await walletsService.generateTx(
params.walletID,
params.items,
params.fee,
params.feeRate,
)
return {
status: ResponseCode.Success,
result: JSON.stringify(tx),
}
} catch (err) {
logger.error(`generateTx:`, err)
return {
status: err.code || ResponseCode.Fail,
message: `Error: "${err.message}"`,
}
}
}

@CatchControllerError
public static async calculateFee(params: {
id: string
tx: string
}) {
if (!params) {
throw new IsRequired('Parameters')
}
const transaction: TransactionWithoutHash = JSON.parse(params.tx)
try {
const walletsService = WalletsService.getInstance()
const fee = await walletsService.calculateFee(transaction)
return {
status: ResponseCode.Success,
result: fee,
Expand Down
55 changes: 19 additions & 36 deletions packages/neuron-wallet/src/services/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,10 @@ export default class WalletService {
this.listStore.clear()
}

public sendCapacity = async (
public sendTx = async (
walletID: string = '',
items: {
address: string
capacity: string
}[] = [],
tx: TransactionWithoutHash,
password: string = '',
fee: string = '0',
feeRate: string = '0',
description: string = ''
) => {
const wallet = await this.get(walletID)
Expand All @@ -350,25 +345,6 @@ export default class WalletService {

const addressInfos = await this.getAddressInfos(walletID)

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

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

const targetOutputs = items.map(item => ({
...item,
capacity: BigInt(item.capacity).toString(),
}))

const changeAddress: string = await this.getChangeAddress()

const tx: TransactionWithoutHash = await TransactionGenerator.generateTx(
lockHashes,
targetOutputs,
changeAddress,
fee,
feeRate
)

let txHash: string = core.utils.rawTransactionToHash(ConvertTo.toSdkTxWithoutHash(tx))
if (!txHash.startsWith('0x')) {
txHash = `0x${txHash}`
Expand Down Expand Up @@ -412,13 +388,27 @@ export default class WalletService {
}

public calculateFee = async (
tx: TransactionWithoutHash
) => {
const inputCapacities = tx.inputs!
.map(input => BigInt(input.capacity!))
.reduce((result, c) => result + c, BigInt(0))
const outputCapacities = tx.outputs!
.map(output => BigInt(output.capacity!))
.reduce((result, c) => result + c, BigInt(0))

return (inputCapacities - outputCapacities).toString()
}

public generateTx = async (
walletID: string = '',
items: {
address: string
capacity: string
}[] = [],
fee: string = '0',
feeRate: string = '0',
) => {
): Promise<TransactionWithoutHash> => {
const wallet = await this.get(walletID)
if (!wallet) {
throw new WalletNotFound(walletID)
Expand All @@ -441,18 +431,11 @@ export default class WalletService {
lockHashes,
targetOutputs,
changeAddress,
'0',
fee,
feeRate
)

const inputCapacities = tx.inputs!
.map(input => BigInt(input.capacity))
.reduce((result, c) => result + c, BigInt(0))
const outputCapacities = tx.outputs!
.map(output => BigInt(output.capacity))
.reduce((result, c) => result + c, BigInt(0))

return (inputCapacities - outputCapacities).toString()
return tx
}

public computeCycles = async (walletID: string = '', capacities: string): Promise<string> => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { getConnection } from 'typeorm'
import { initConnection } from '../../../src/database/chain/ormconfig'
import { ScriptHashType, Script, TransactionWithoutHash } from '../../../src/types/cell-types';
import { ScriptHashType, Script, TransactionWithoutHash } from '../../../src/types/cell-types'
import { OutputStatus } from '../../../src/services/tx/params'
import OutputEntity from '../../../src/database/chain/entities/output'
import SkipDataAndType from '../../../src/services/settings/skip-data-and-type';
import TransactionGenerator from '../../../src/services/tx/transaction-generator';
import SkipDataAndType from '../../../src/services/settings/skip-data-and-type'
import TransactionGenerator from '../../../src/services/tx/transaction-generator'
import LockUtils from '../../../src/models/lock-utils'
import CellsService from '../../../src/services/cells';
import CellsService from '../../../src/services/cells'

const systemScript = {
outPoint: {
Expand Down

0 comments on commit f956088

Please sign in to comment.