Skip to content

Commit e11f3bc

Browse files
committed
wallet: return errors array with from makeBatch and related methods.
1 parent c2f49cf commit e11f3bc

File tree

5 files changed

+298
-59
lines changed

5 files changed

+298
-59
lines changed

lib/wallet/rpc.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2717,7 +2717,7 @@ class RPC extends RPCBase {
27172717
async sendBatch(args, help) {
27182718
const [actions, options] = this._validateBatch(args, help, 'sendbatch');
27192719
const wallet = this.wallet;
2720-
const tx = await wallet.sendBatch(actions, options);
2720+
const {tx} = await wallet.sendBatch(actions, options);
27212721

27222722
return tx.getJSON(this.network);
27232723
}
@@ -2727,7 +2727,7 @@ class RPC extends RPCBase {
27272727
options.paths = true;
27282728

27292729
const wallet = this.wallet;
2730-
const mtx = await wallet.createBatch(actions, options);
2730+
const {mtx} = await wallet.createBatch(actions, options);
27312731

27322732
return mtx.getJSON(this.network);
27332733
}

lib/wallet/wallet.js

Lines changed: 84 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3863,18 +3863,44 @@ class Wallet extends EventEmitter {
38633863

38643864
/**
38653865
* @typedef {Object} BatchAction
3866-
* @property {String} type - Action type
3867-
* @property {Array} [args] - Arguments for the action
3866+
* @property {String} type - Action type.
3867+
* @property {String} [id] - Action ID.
3868+
* @property {Array} [args] - Arguments for the action.
3869+
*/
3870+
3871+
/**
3872+
* @typedef {Object} BatchError
3873+
* @property {String} message - Error message.
3874+
* @property {String} type - Type of the action that caused the error.
3875+
* @property {Error} error - Original error object.
3876+
* @property {String} [id] - Optional ID for the action.
3877+
*/
3878+
3879+
/**
3880+
* @typedef {Object} BatchResult
3881+
* @property {MTX} mtx - The resulting transaction
3882+
* @property {BatchError[]} errors - List of errors encountered during
3883+
* processing.
3884+
*/
3885+
3886+
/**
3887+
* @typedef {Object} BatchTXResult
3888+
* @property {TX} tx - The resulting transaction
3889+
* @property {BatchError[]} errors - List of errors encountered during
3890+
* processing.
38683891
*/
38693892

38703893
/**
38713894
* Make a batch transaction with multiple actions.
38723895
* @param {BatchAction[]} actions
38733896
* @param {Object} options
3874-
* @returns {Promise<MTX>}
3897+
* @param {Number|String} [options.account=0] - Account index or name.
3898+
* @param {Boolean} [options.partialFailure=false] - Allow partial failure.
3899+
* @throws {Error} - general validations.
3900+
* @returns {Promise<BatchResult>}
38753901
*/
38763902

3877-
async makeBatch(actions, options) {
3903+
async makeBatch(actions, options = {}) {
38783904
assert(Array.isArray(actions));
38793905
assert(actions.length, 'Batches require at least one action.');
38803906

@@ -3916,17 +3942,15 @@ class Wallet extends EventEmitter {
39163942
// We track that by bumping receiveIndex.
39173943
const account = await this.getAccount(acct);
39183944
let receiveIndex = account.receiveDepth - 1;
3945+
/** @type {BatchError[]} */
3946+
const errors = [];
39193947

3920-
// "actions" are arrays that start with a covenant type (or meta-type)
3921-
// followed by the arguments expected by the corresponding "make" function.
3922-
for (const action of actions) {
3923-
assert(action);
3924-
assert(typeof action.type === 'string');
3925-
3926-
const args = action.args || [];
3927-
3928-
assert(Array.isArray(args), 'Action args must be an array.');
3948+
/**
3949+
* @param {BatchAction} action
3950+
* @param {Array} args
3951+
*/
39293952

3953+
const handleAction = async (action, args) => {
39303954
switch (action.type) {
39313955
case 'NONE': {
39323956
assert(args.length === 2);
@@ -3944,8 +3968,10 @@ class Wallet extends EventEmitter {
39443968
break;
39453969
}
39463970
case 'BID': {
3971+
const bidIndex = receiveIndex++;
3972+
39473973
assert(args.length === 3, 'Bad arguments for BID.');
3948-
const address = account.deriveReceive(receiveIndex++).getAddress();
3974+
const address = account.deriveReceive(bidIndex).getAddress();
39493975
const name = args[0];
39503976
const value = args[1];
39513977
const lockup = args[2];
@@ -4025,6 +4051,34 @@ class Wallet extends EventEmitter {
40254051
default:
40264052
throw new Error(`Unknown action type: ${action.type}`);
40274053
}
4054+
};
4055+
4056+
// "actions" are arrays that start with a covenant type (or meta-type)
4057+
// followed by the arguments expected by the corresponding "make" function.
4058+
for (const action of actions) {
4059+
assert(action);
4060+
assert(typeof action.type === 'string');
4061+
4062+
const args = action.args || [];
4063+
4064+
assert(Array.isArray(args), 'Action args must be an array.');
4065+
4066+
try {
4067+
await handleAction(action, args);
4068+
} catch (err) {
4069+
if (!options.partialFailure)
4070+
throw err;
4071+
4072+
if (action.type === 'BID')
4073+
receiveIndex--;
4074+
4075+
errors.push({
4076+
message: err.message,
4077+
type: action.type,
4078+
error: err,
4079+
id: action.id || null
4080+
});
4081+
}
40284082

40294083
if (rules.countOpens(mtx) > consensus.MAX_BLOCK_OPENS)
40304084
throw new Error('Too many OPENs.');
@@ -4076,27 +4130,30 @@ class Wallet extends EventEmitter {
40764130
throw new Error('Batch output addresses would exceed lookahead.');
40774131
}
40784132

4079-
return mtx;
4133+
return {mtx, errors};
40804134
}
40814135

40824136
/**
40834137
* Make a batch transaction with multiple actions.
40844138
* @param {Array} actions
40854139
* @param {Object} options
4086-
* @returns {Promise<MTX>}
4140+
* @returns {Promise<BatchResult>}
40874141
*/
40884142

40894143
async _createBatch(actions, options) {
4090-
const mtx = await this.makeBatch(actions, options);
4144+
const {mtx, errors} = await this.makeBatch(actions, options);
40914145
await this.fill(mtx, options);
4092-
return this.finalize(mtx, options);
4146+
return {
4147+
mtx: await this.finalize(mtx, options),
4148+
errors
4149+
};
40934150
}
40944151

40954152
/**
40964153
* Make a batch transaction with multiple actions.
40974154
* @param {Array} actions
40984155
* @param {Object} options
4099-
* @returns {Promise<MTX>}
4156+
* @returns {Promise<BatchResult>}
41004157
*/
41014158

41024159
async createBatch(actions, options) {
@@ -4112,20 +4169,23 @@ class Wallet extends EventEmitter {
41124169
* Create and send a batch transaction with multiple actions.
41134170
* @param {Array} actions
41144171
* @param {Object} options
4115-
* @returns {Promise<TX>}
4172+
* @returns {Promise<BatchTXResult>}
41164173
*/
41174174

41184175
async _sendBatch(actions, options) {
41194176
const passphrase = options ? options.passphrase : null;
4120-
const mtx = await this._createBatch(actions, options);
4121-
return this.sendMTX(mtx, passphrase);
4177+
const {mtx, errors} = await this._createBatch(actions, options);
4178+
return {
4179+
tx: await this.sendMTX(mtx, passphrase),
4180+
errors
4181+
};
41224182
}
41234183

41244184
/**
41254185
* Create and send a batch transaction with multiple actions.
41264186
* @param {Array} actions
41274187
* @param {Object} options
4128-
* @returns {Promise<TX>}
4188+
* @returns {Promise<BatchTXResult>}
41294189
*/
41304190

41314191
async sendBatch(actions, options) {
@@ -5470,7 +5530,7 @@ class Wallet extends EventEmitter {
54705530

54715531
/**
54725532
* Get current change address.
5473-
* @param {Number} [acct=0]
5533+
* @param {Number|String} [acct=0]
54745534
* @returns {Promise<Address>}
54755535
*/
54765536

0 commit comments

Comments
 (0)