@taquito/taquito
Version:
High level functionality that builds upon the other packages in the Tezos Typescript Library Suite.
267 lines • 11.1 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RPCBatchProvider = exports.OperationBatch = exports.BATCH_KINDS = void 0;
const prepare_1 = require("../contract/prepare");
const batch_operation_1 = require("../operations/batch-operation");
const rpc_1 = require("@taquito/rpc");
const utils_1 = require("@taquito/utils");
const core_1 = require("@taquito/core");
const provider_1 = require("../provider");
const prepare_2 = require("../prepare");
exports.BATCH_KINDS = [
rpc_1.OpKind.ACTIVATION,
rpc_1.OpKind.ORIGINATION,
rpc_1.OpKind.TRANSACTION,
rpc_1.OpKind.DELEGATION,
];
class OperationBatch extends provider_1.Provider {
constructor(context, estimator) {
super(context);
this.estimator = estimator;
this.operations = [];
this.prepare = new prepare_2.PrepareProvider(this.context);
}
/**
*
* @description Add a transaction operation to the batch
*
* @param params Transfer operation parameter
*/
withTransfer(params) {
const toValidation = utils_1.validateAddress(params.to);
if (params.amount < 0) {
throw new core_1.InvalidAmountError(params.amount.toString());
}
if (toValidation !== utils_1.ValidationResult.VALID) {
throw new core_1.InvalidAddressError(params.to, utils_1.invalidDetail(toValidation));
}
this.operations.push(Object.assign({ kind: rpc_1.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) {
const destinationValidation = utils_1.validateAddress(params.destination);
if (destinationValidation !== utils_1.ValidationResult.VALID) {
throw new core_1.InvalidAddressError(params.destination, utils_1.invalidDetail(destinationValidation));
}
this.operations.push(Object.assign({ kind: rpc_1.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, options = {}) {
return this.withTransfer(params.toTransferParams(options));
}
/**
*
* @description Add a delegation operation to the batch
*
* @param params Delegation operation parameter
*/
withDelegation(params) {
var _a;
const sourceValidation = utils_1.validateAddress(params.source);
if (params.source && sourceValidation !== utils_1.ValidationResult.VALID) {
throw new core_1.InvalidAddressError(params.source, utils_1.invalidDetail(sourceValidation));
}
const delegateValidation = utils_1.validateAddress((_a = params.delegate) !== null && _a !== void 0 ? _a : '');
if (params.delegate && delegateValidation !== utils_1.ValidationResult.VALID) {
throw new core_1.InvalidAddressError(params.delegate, utils_1.invalidDetail(delegateValidation));
}
this.operations.push(Object.assign({ kind: rpc_1.OpKind.DELEGATION }, params));
return this;
}
/**
*
* @description Add an activation operation to the batch
*
* @param params Activation operation parameter
* @throws {@link InvalidKeyHashError}
*/
withActivation({ pkh, secret }) {
const pkhValidation = utils_1.validateKeyHash(pkh);
if (pkhValidation !== utils_1.ValidationResult.VALID) {
throw new core_1.InvalidKeyHashError(pkh, utils_1.invalidDetail(pkhValidation));
}
this.operations.push({ kind: rpc_1.OpKind.ACTIVATION, pkh, secret });
return this;
}
/**
*
* @description Add an origination operation to the batch
*
* @param params Origination operation parameter
*/
withOrigination(params) {
this.operations.push(Object.assign({ kind: rpc_1.OpKind.ORIGINATION }, params));
return this;
}
/**
*
* @description Add a register a global constant operation to the batch
*
* @param params RegisterGlobalConstant operation parameter
*/
withRegisterGlobalConstant(params) {
this.operations.push(Object.assign({ kind: rpc_1.OpKind.REGISTER_GLOBAL_CONSTANT }, params));
return this;
}
/**
*
* @description Add an increase paid storage operation to the batch
*
* @param params IncreasePaidStorage operation parameter
*/
withIncreasePaidStorage(params) {
this.operations.push(Object.assign({ kind: rpc_1.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) {
this.operations.push(Object.assign({ kind: rpc_1.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) {
this.operations.push(Object.assign({ kind: rpc_1.OpKind.SMART_ROLLUP_ORIGINATE }, params));
return this;
}
getRPCOp(param) {
return __awaiter(this, void 0, void 0, function* () {
switch (param.kind) {
case rpc_1.OpKind.TRANSACTION:
return prepare_1.createTransferOperation(Object.assign({}, param));
case rpc_1.OpKind.ORIGINATION:
return prepare_1.createOriginationOperation(yield this.context.parser.prepareCodeOrigination(Object.assign({}, param)));
case rpc_1.OpKind.DELEGATION:
return prepare_1.createSetDelegateOperation(Object.assign({}, param));
case rpc_1.OpKind.REGISTER_GLOBAL_CONSTANT:
return prepare_1.createRegisterGlobalConstantOperation(Object.assign({}, param));
case rpc_1.OpKind.INCREASE_PAID_STORAGE:
return prepare_1.createIncreasePaidStorageOperation(Object.assign({}, param));
case rpc_1.OpKind.TRANSFER_TICKET:
return prepare_1.createTransferTicketOperation(Object.assign({}, param));
case rpc_1.OpKind.SMART_ROLLUP_ADD_MESSAGES:
return prepare_1.createSmartRollupAddMessagesOperation(Object.assign({}, param));
case rpc_1.OpKind.SMART_ROLLUP_ORIGINATE:
return prepare_1.createSmartRollupOriginateOperation(Object.assign({}, param));
default:
throw new core_1.InvalidOperationKindError(JSON.stringify(param.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) {
for (const param of params) {
switch (param.kind) {
case rpc_1.OpKind.TRANSACTION:
this.withTransfer(param);
break;
case rpc_1.OpKind.ORIGINATION:
this.withOrigination(param);
break;
case rpc_1.OpKind.DELEGATION:
this.withDelegation(param);
break;
case rpc_1.OpKind.ACTIVATION:
this.withActivation(param);
break;
case rpc_1.OpKind.REGISTER_GLOBAL_CONSTANT:
this.withRegisterGlobalConstant(param);
break;
case rpc_1.OpKind.INCREASE_PAID_STORAGE:
this.withIncreasePaidStorage(param);
break;
case rpc_1.OpKind.TRANSFER_TICKET:
this.withTransferTicket(param);
break;
case rpc_1.OpKind.SMART_ROLLUP_ADD_MESSAGES:
this.withSmartRollupAddMessages(param);
break;
case rpc_1.OpKind.SMART_ROLLUP_ORIGINATE:
this.withSmartRollupOriginate(param);
break;
default:
throw new core_1.InvalidOperationKindError(JSON.stringify(param.kind));
}
}
return this;
}
/**
*
* @description Forge and Inject the operation batch
*
* @param params Optionally specify the source of the operation
*/
send(params) {
return __awaiter(this, void 0, void 0, function* () {
const publicKeyHash = yield this.signer.publicKeyHash();
const source = (params && params.source) || publicKeyHash;
const estimates = yield this.estimator.batch(this.operations);
if (estimates.length !== this.operations.length) {
estimates.shift();
}
const preparedOp = yield this.prepare.batch(this.operations, estimates);
const opBytes = yield this.forge(preparedOp);
const { hash, context, forgedBytes, opResponse } = yield this.signAndInject(opBytes);
return new batch_operation_1.BatchOperation(hash, preparedOp.opOb.contents, source, forgedBytes, opResponse, context);
});
}
}
exports.OperationBatch = OperationBatch;
class RPCBatchProvider {
constructor(context, estimator) {
this.context = context;
this.estimator = estimator;
}
/***
*
* @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) {
const batch = new OperationBatch(this.context, this.estimator);
if (Array.isArray(params)) {
batch.with(params);
}
return batch;
}
}
exports.RPCBatchProvider = RPCBatchProvider;
//# sourceMappingURL=rpc-batch-provider.js.map