OSDN Git Service

update the api return format and addup the error handle.
[bytom/bytom-dashboard.git] / src / sdk / api / transactions.js
1 const shared = require('../shared')
2 const errors = require('../errors')
3
4 // TODO: replace with default handler in requestSingle/requestBatch variants
5 function checkForError(resp) {
6   if ('code' in resp) {
7     throw errors.create(
8       errors.types.BAD_REQUEST,
9       errors.formatErrMsg(resp, ''),
10       {body: resp}
11     )
12   }else if (resp.status === 'fail') {
13     throw new Error(resp.msg)
14   }
15   return resp
16 }
17
18 class TransactionBuilder {
19   constructor() {
20     this.actions = []
21
22
23     this.allowAdditionalActions = false
24
25     this.baseTransaction = null
26   }
27
28   issue(params) {
29     this.actions.push(Object.assign({}, params, {type: 'issue'}))
30   }
31
32   controlWithReceiver(params) {
33     this.actions.push(Object.assign({}, params, {type: 'control_receiver'}))
34   }
35
36   spendFromAccount(params) {
37     this.actions.push(Object.assign({}, params, {type: 'spend_account'}))
38   }
39
40   spendUnspentOutput(params) {
41     this.actions.push(Object.assign({}, params, {type: 'spend_account_unspent_output'}))
42   }
43
44   retire(params) {
45     this.actions.push(Object.assign({}, params, {type: 'retire'}))
46   }
47
48   transactionReferenceData(referenceData) {
49     this.actions.push({
50       type: 'set_transaction_reference_data',
51       referenceData
52     })
53   }
54 }
55
56 const transactionsAPI = (client) => {
57   // TODO: implement finalize
58   const finalize = (template, cb) => shared.tryCallback(
59     Promise.resolve(template),
60     cb
61   )
62
63   // TODO: implement finalizeBatch
64   const finalizeBatch = (templates, cb) => shared.tryCallback(
65     Promise.resolve(new shared.BatchResponse(templates)),
66     cb
67   )
68
69   return {
70     query: (params, cb) => shared.query(client, 'transactions', '/list-transactions',
71       Object.assign({}, params, {unconfirmed:true, detail: true}),
72       {cb}),
73
74     queryAll: (params, processor, cb) => shared.queryAll(client, 'transactions', params, processor, cb),
75
76     build: (builderBlock, cb) => {
77       const builder = new TransactionBuilder()
78
79       try {
80         builderBlock(builder)
81       } catch (err) {
82         return Promise.reject(err)
83       }
84
85       return shared.tryCallback(
86         client.request('/build-transaction', builder),
87         cb
88       ).then(resp => checkForError(resp))
89     },
90
91     buildBatch: (builderBlocks, cb) => {
92       const builders = []
93       for (let i in builderBlocks) {
94         const b = new TransactionBuilder()
95         try {
96           builderBlocks[i](b)
97         } catch (err) {
98           return Promise.reject(err)
99         }
100         builders.push(b)
101       }
102
103       return shared.createBatch(client, '/build-transaction', builders, {cb})
104     },
105
106     decodeTransaction: (raw_transaction, cb) => shared.tryCallback(
107       client.request('/decode-raw-transaction', {'raw_transaction': raw_transaction}).then(resp => checkForError(resp)),
108       cb
109     ),
110
111     sign: (template, cb) => finalize(template)
112       .then(finalized => client.request('/sign-transaction', finalized ).then(resp => checkForError(resp)),
113         cb
114       ),
115
116     signBatch: (templates, cb) => finalizeBatch(templates)
117       // TODO: merge batch errors from finalizeBatch
118       .then(finalized => client.signer.signBatch(finalized.successes, cb)),
119
120     submit: (signed, cb) => shared.tryCallback(
121       client.request('/submit-transaction', {'raw_transaction': signed}).then(resp => checkForError(resp)),
122       cb
123     ),
124
125     submitBatch: (signed, cb) => shared.tryCallback(
126       client.request('/submit-transaction', {transactions: signed})
127             .then(resp => new shared.BatchResponse(resp)),
128       cb
129     ),
130
131     estimateGas: (template, cb) => shared.tryCallback(
132       client.request('/estimate-transaction-gas', {'transactionTemplate': template}).then(resp => checkForError(resp)),
133       cb
134     )
135   }
136 }
137
138 module.exports = transactionsAPI