/
rpc-batch-provider.ts
361 lines (336 loc) · 10.8 KB
/
rpc-batch-provider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import { Context } from '../context';
import { ContractStorageType, DefaultContractType } from '../contract/contract';
import { ContractMethod } from '../contract/contract-methods/contract-method-flat-param';
import { SendParams } from '../contract/contract-methods/contract-method-interface';
import { ContractProvider } from '../contract/interface';
import {
createOriginationOperation,
createRegisterGlobalConstantOperation,
createSetDelegateOperation,
createTransferOperation,
createTransferTicketOperation,
createIncreasePaidStorageOperation,
createSmartRollupAddMessagesOperation,
createSmartRollupOriginateOperation,
createSmartRollupExecuteOutboxMessageOperation,
} from '../contract/prepare';
import { BatchOperation } from '../operations/batch-operation';
import {
ActivationParams,
DelegateParams,
OriginateParams,
TransferParams,
ParamsWithKind,
RegisterGlobalConstantParams,
TransferTicketParams,
IncreasePaidStorageParams,
SmartRollupAddMessagesParams,
SmartRollupOriginateParams,
SmartRollupExecuteOutboxMessageParams,
} from '../operations/types';
import { OpKind } from '@taquito/rpc';
import { ContractMethodObject } from '../contract/contract-methods/contract-method-object-param';
import { validateAddress, validateKeyHash, ValidationResult, invalidDetail } from '@taquito/utils';
import { EstimationProvider } from '../estimate/estimate-provider-interface';
import {
InvalidAddressError,
InvalidKeyHashError,
InvalidOperationKindError,
InvalidAmountError,
} from '@taquito/core';
import { Provider } from '../provider';
import { PrepareProvider } from '../prepare';
export const BATCH_KINDS = [
OpKind.ACTIVATION,
OpKind.ORIGINATION,
OpKind.TRANSACTION,
OpKind.DELEGATION,
];
export type BatchKinds =
| OpKind.ACTIVATION
| OpKind.ORIGINATION
| OpKind.TRANSACTION
| OpKind.DELEGATION;
export class OperationBatch extends Provider {
private operations: ParamsWithKind[] = [];
constructor(
context: Context,
private estimator: EstimationProvider
) {
super(context);
}
private prepare = new PrepareProvider(this.context);
/**
*
* @description Add a transaction operation to the batch
*
* @param params Transfer operation parameter
*/
withTransfer(params: TransferParams) {
const toValidation = validateAddress(params.to);
if (params.amount < 0) {
throw new InvalidAmountError(params.amount.toString());
}
if (toValidation !== ValidationResult.VALID) {
throw new InvalidAddressError(params.to, invalidDetail(toValidation));
}
this.operations.push({ kind: OpKind.TRANSACTION, ...params });
return this;
}
/**
*
* @description Transfer tickets from a Tezos address (tz1,tz2 or tz3) to a smart contract address( KT1)
*
* @param params Transfer operation parameter
*/
withTransferTicket(params: TransferTicketParams) {
const destinationValidation = validateAddress(params.destination);
if (destinationValidation !== ValidationResult.VALID) {
throw new InvalidAddressError(params.destination, invalidDetail(destinationValidation));
}
this.operations.push({ kind: OpKind.TRANSFER_TICKET, ...params });
return this;
}
/**
*
* @description Add a contract call to the batch
*
* @param params Call a contract method
* @param options Generic operation parameters
*/
withContractCall(
params: ContractMethod<ContractProvider> | ContractMethodObject<ContractProvider>,
options: Partial<SendParams> = {}
) {
return this.withTransfer(params.toTransferParams(options));
}
/**
*
* @description Add a delegation operation to the batch
*
* @param params Delegation operation parameter
*/
withDelegation(params: DelegateParams) {
const sourceValidation = validateAddress(params.source);
if (params.source && sourceValidation !== ValidationResult.VALID) {
throw new InvalidAddressError(params.source, invalidDetail(sourceValidation));
}
const delegateValidation = validateAddress(params.delegate ?? '');
if (params.delegate && delegateValidation !== ValidationResult.VALID) {
throw new InvalidAddressError(params.delegate, invalidDetail(delegateValidation));
}
this.operations.push({ kind: OpKind.DELEGATION, ...params });
return this;
}
/**
*
* @description Add an activation operation to the batch
*
* @param params Activation operation parameter
* @throws {@link InvalidKeyHashError}
*/
withActivation({ pkh, secret }: ActivationParams) {
const pkhValidation = validateKeyHash(pkh);
if (pkhValidation !== ValidationResult.VALID) {
throw new InvalidKeyHashError(pkh, invalidDetail(pkhValidation));
}
this.operations.push({ kind: OpKind.ACTIVATION, pkh, secret });
return this;
}
/**
*
* @description Add an origination operation to the batch
*
* @param params Origination operation parameter
*/
withOrigination<TContract extends DefaultContractType = DefaultContractType>(
params: OriginateParams<ContractStorageType<TContract>>
) {
this.operations.push({ kind: OpKind.ORIGINATION, ...params });
return this;
}
/**
*
* @description Add a register a global constant operation to the batch
*
* @param params RegisterGlobalConstant operation parameter
*/
withRegisterGlobalConstant(params: RegisterGlobalConstantParams) {
this.operations.push({ kind: OpKind.REGISTER_GLOBAL_CONSTANT, ...params });
return this;
}
/**
*
* @description Add an increase paid storage operation to the batch
*
* @param params IncreasePaidStorage operation parameter
*/
withIncreasePaidStorage(params: IncreasePaidStorageParams) {
this.operations.push({ kind: OpKind.INCREASE_PAID_STORAGE, ...params });
return this;
}
/**
*
* @description Add a smart rollup add messages operation to the batch
*
* @param params Rollup origination operation parameter
*/
withSmartRollupAddMessages(params: SmartRollupAddMessagesParams) {
this.operations.push({ kind: OpKind.SMART_ROLLUP_ADD_MESSAGES, ...params });
return this;
}
/**
*
* @description Add a smart rollup originate operation to the batch
*
* @param params Smart Rollup Originate operation parameter
*/
withSmartRollupOriginate(params: SmartRollupOriginateParams) {
this.operations.push({ kind: OpKind.SMART_ROLLUP_ORIGINATE, ...params });
return this;
}
/**
*
* @description Add a smart rollup execute outbox message to the batch
*
* @param params Smart Rollup Execute Outbox Message operation parameter
*/
withSmartRollupExecuteOutboxMessage(params: SmartRollupExecuteOutboxMessageParams) {
this.operations.push({ kind: OpKind.SMART_ROLLUP_EXECUTE_OUTBOX_MESSAGE, ...params });
return this;
}
async getRPCOp(param: ParamsWithKind) {
switch (param.kind) {
case OpKind.TRANSACTION:
return createTransferOperation({
...param,
});
case OpKind.ORIGINATION:
return createOriginationOperation(
await this.context.parser.prepareCodeOrigination({
...param,
})
);
case OpKind.DELEGATION:
return createSetDelegateOperation({
...param,
});
case OpKind.REGISTER_GLOBAL_CONSTANT:
return createRegisterGlobalConstantOperation({
...param,
});
case OpKind.INCREASE_PAID_STORAGE:
return createIncreasePaidStorageOperation({
...param,
});
case OpKind.TRANSFER_TICKET:
return createTransferTicketOperation({
...param,
});
case OpKind.SMART_ROLLUP_ADD_MESSAGES:
return createSmartRollupAddMessagesOperation({
...param,
});
case OpKind.SMART_ROLLUP_ORIGINATE:
return createSmartRollupOriginateOperation({
...param,
});
case OpKind.SMART_ROLLUP_EXECUTE_OUTBOX_MESSAGE:
return createSmartRollupExecuteOutboxMessageOperation({
...param,
});
default:
throw new InvalidOperationKindError(JSON.stringify((param as any).kind));
}
}
/**
*
* @description Add a group operation to the batch. Operation will be applied in the order they are in the params array
*
* @param params Operations parameter
* @throws {@link InvalidOperationKindError}
*/
with(params: ParamsWithKind[]) {
for (const param of params) {
switch (param.kind) {
case OpKind.TRANSACTION:
this.withTransfer(param);
break;
case OpKind.ORIGINATION:
this.withOrigination(param);
break;
case OpKind.DELEGATION:
this.withDelegation(param);
break;
case OpKind.ACTIVATION:
this.withActivation(param);
break;
case OpKind.REGISTER_GLOBAL_CONSTANT:
this.withRegisterGlobalConstant(param);
break;
case OpKind.INCREASE_PAID_STORAGE:
this.withIncreasePaidStorage(param);
break;
case OpKind.TRANSFER_TICKET:
this.withTransferTicket(param);
break;
case OpKind.SMART_ROLLUP_ADD_MESSAGES:
this.withSmartRollupAddMessages(param);
break;
case OpKind.SMART_ROLLUP_ORIGINATE:
this.withSmartRollupOriginate(param);
break;
case OpKind.SMART_ROLLUP_EXECUTE_OUTBOX_MESSAGE:
this.withSmartRollupExecuteOutboxMessage(param);
break;
default:
throw new InvalidOperationKindError(JSON.stringify((param as any).kind));
}
}
return this;
}
/**
*
* @description Forge and Inject the operation batch
*
* @param params Optionally specify the source of the operation
*/
async send(params?: { source?: string }) {
const publicKeyHash = await this.signer.publicKeyHash();
const source = (params && params.source) || publicKeyHash;
const estimates = await this.estimator.batch(this.operations);
if (estimates.length !== this.operations.length) {
estimates.shift();
}
const preparedOp = await this.prepare.batch(this.operations, estimates);
const opBytes = await this.forge(preparedOp);
const { hash, context, forgedBytes, opResponse } = await this.signAndInject(opBytes);
return new BatchOperation(
hash,
preparedOp.opOb.contents,
source,
forgedBytes,
opResponse,
context
);
}
}
export class RPCBatchProvider {
constructor(
private context: Context,
private estimator: EstimationProvider
) {}
/***
*
* @description Batch a group of operation together. Operations will be applied in the order in which they are added to the batch
*
* @param params List of operation to batch together
*/
batch(params?: ParamsWithKind[]) {
const batch = new OperationBatch(this.context, this.estimator);
if (Array.isArray(params)) {
batch.with(params);
}
return batch;
}
}