1 const shared = require('../shared')
2 const errors = require('../errors')
4 // TODO: replace with default handler in requestSingle/requestBatch variants
5 function checkForError(resp) {
8 errors.types.BAD_REQUEST,
9 errors.formatErrMsg(resp, ''),
12 }else if (resp.status === 'fail') {
13 throw new Error(resp.msg)
18 class TransactionBuilder {
23 this.allowAdditionalActions = false
25 this.baseTransaction = null
29 this.actions.push(Object.assign({}, params, {type: 'issue'}))
32 controlWithReceiver(params) {
33 this.actions.push(Object.assign({}, params, {type: 'control_receiver'}))
36 spendFromAccount(params) {
37 this.actions.push(Object.assign({}, params, {type: 'spend_account'}))
40 spendUnspentOutput(params) {
41 this.actions.push(Object.assign({}, params, {type: 'spend_account_unspent_output'}))
45 this.actions.push(Object.assign({}, params, {type: 'retire'}))
48 transactionReferenceData(referenceData) {
50 type: 'set_transaction_reference_data',
56 const transactionsAPI = (client) => {
57 // TODO: implement finalize
58 const finalize = (template, cb) => shared.tryCallback(
59 Promise.resolve(template),
63 // TODO: implement finalizeBatch
64 const finalizeBatch = (templates, cb) => shared.tryCallback(
65 Promise.resolve(new shared.BatchResponse(templates)),
70 query: (params, cb) => shared.query(client, 'transactions', '/list-transactions',
71 Object.assign({}, params, {unconfirmed:true, detail: true}),
74 queryAll: (params, processor, cb) => shared.queryAll(client, 'transactions', params, processor, cb),
76 build: (builderBlock, cb) => {
77 const builder = new TransactionBuilder()
82 return Promise.reject(err)
85 return shared.tryCallback(
86 client.request('/build-transaction', builder),
88 ).then(resp => checkForError(resp))
91 buildBatch: (builderBlocks, cb) => {
93 for (let i in builderBlocks) {
94 const b = new TransactionBuilder()
98 return Promise.reject(err)
103 return shared.createBatch(client, '/build-transaction', builders, {cb})
106 decodeTransaction: (raw_transaction, cb) => shared.tryCallback(
107 client.request('/decode-raw-transaction', {'raw_transaction': raw_transaction}).then(resp => checkForError(resp)),
111 sign: (template, cb) => finalize(template)
112 .then(finalized => client.request('/sign-transaction', finalized ).then(resp => checkForError(resp)),
116 signBatch: (templates, cb) => finalizeBatch(templates)
117 // TODO: merge batch errors from finalizeBatch
118 .then(finalized => client.signer.signBatch(finalized.successes, cb)),
120 submit: (signed, cb) => shared.tryCallback(
121 client.request('/submit-transaction', {'raw_transaction': signed}).then(resp => checkForError(resp)),
125 submitBatch: (signed, cb) => shared.tryCallback(
126 client.request('/submit-transaction', {transactions: signed})
127 .then(resp => new shared.BatchResponse(resp)),
131 estimateGas: (template, cb) => shared.tryCallback(
132 client.request('/estimate-transaction-gas', {'transactionTemplate': template}).then(resp => checkForError(resp)),
138 module.exports = transactionsAPI