@taquito/taquito
Version:
High level functionality that builds upon the other packages in the Tezos Typescript Library Suite.
320 lines (319 loc) • 13.7 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");
var constants_1 = require("./constants");
Object.defineProperty(exports, "BATCH_KINDS", { enumerable: true, get: function () { return constants_1.BATCH_KINDS; } });
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 = (0, 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, 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 = (0, utils_1.validateAddress)(params.destination);
if (destinationValidation !== utils_1.ValidationResult.VALID) {
throw new core_1.InvalidAddressError(params.destination, 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, _b;
const sourceValidation = (0, utils_1.validateAddress)((_a = params.source) !== null && _a !== void 0 ? _a : '');
if (params.source && sourceValidation !== utils_1.ValidationResult.VALID) {
throw new core_1.InvalidAddressError(params.source, sourceValidation);
}
const delegateValidation = (0, utils_1.validateAddress)((_b = params.delegate) !== null && _b !== void 0 ? _b : '');
if (params.delegate && delegateValidation !== utils_1.ValidationResult.VALID) {
throw new core_1.InvalidAddressError(params.delegate, 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 = (0, utils_1.validateKeyHash)(pkh);
if (pkhValidation !== utils_1.ValidationResult.VALID) {
throw new core_1.InvalidKeyHashError(pkh, 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 update consensus key operation to the batch
*
* @param params UpdateConsensusKey operation parameter
*/
withUpdateConsensusKey(params) {
const [, pkPrefix] = (0, utils_1.b58DecodeAndCheckPrefix)(params.pk, utils_1.publicKeyPrefixes);
if (pkPrefix === utils_1.PrefixV2.BLS12_381PublicKey) {
if (!params.proof) {
throw new core_1.InvalidProofError('Proof is required to set a bls account as consensus key ');
}
}
else {
if (params.proof) {
throw new core_1.ProhibitedActionError('Proof field is only allowed for a bls account as consensus key');
}
}
this.operations.push(Object.assign({ kind: rpc_1.OpKind.UPDATE_CONSENSUS_KEY }, params));
return this;
}
/**
*
* @description Add a update companion key operation to the batch
*
* @param params UpdateCompanionKey operation parameter
*/
withUpdateCompanionKey(params) {
const [, pkPrefix] = (0, utils_1.b58DecodeAndCheckPrefix)(params.pk, utils_1.publicKeyPrefixes);
if (pkPrefix !== utils_1.PrefixV2.BLS12_381PublicKey) {
throw new core_1.ProhibitedActionError('companion key must be a bls account');
}
if (!params.proof) {
throw new core_1.InvalidProofError('Proof is required to set a bls account as companion key ');
}
this.operations.push(Object.assign({ kind: rpc_1.OpKind.UPDATE_COMPANION_KEY }, 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;
}
/**
*
* @description Add a smart rollup execute outbox message to the batch
*
* @param params Smart Rollup Execute Outbox Message operation parameter
*/
withSmartRollupExecuteOutboxMessage(params) {
this.operations.push(Object.assign({ kind: rpc_1.OpKind.SMART_ROLLUP_EXECUTE_OUTBOX_MESSAGE }, params));
return this;
}
getRPCOp(param) {
return __awaiter(this, void 0, void 0, function* () {
switch (param.kind) {
case rpc_1.OpKind.TRANSACTION:
return (0, prepare_1.createTransferOperation)(Object.assign({}, param));
case rpc_1.OpKind.ORIGINATION:
return (0, prepare_1.createOriginationOperation)(yield this.context.parser.prepareCodeOrigination(Object.assign({}, param)));
case rpc_1.OpKind.DELEGATION:
return (0, prepare_1.createSetDelegateOperation)(Object.assign({}, param));
case rpc_1.OpKind.REGISTER_GLOBAL_CONSTANT:
return (0, prepare_1.createRegisterGlobalConstantOperation)(Object.assign({}, param));
case rpc_1.OpKind.INCREASE_PAID_STORAGE:
return (0, prepare_1.createIncreasePaidStorageOperation)(Object.assign({}, param));
case rpc_1.OpKind.UPDATE_CONSENSUS_KEY:
return (0, prepare_1.createUpdateConsensusKeyOperation)(Object.assign({}, param));
case rpc_1.OpKind.UPDATE_COMPANION_KEY:
return (0, prepare_1.createUpdateCompanionKeyOperation)(Object.assign({}, param));
case rpc_1.OpKind.TRANSFER_TICKET:
return (0, prepare_1.createTransferTicketOperation)(Object.assign({}, param));
case rpc_1.OpKind.SMART_ROLLUP_ADD_MESSAGES:
return (0, prepare_1.createSmartRollupAddMessagesOperation)(Object.assign({}, param));
case rpc_1.OpKind.SMART_ROLLUP_ORIGINATE:
return (0, prepare_1.createSmartRollupOriginateOperation)(Object.assign({}, param));
case rpc_1.OpKind.SMART_ROLLUP_EXECUTE_OUTBOX_MESSAGE:
return (0, prepare_1.createSmartRollupExecuteOutboxMessageOperation)(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;
case rpc_1.OpKind.SMART_ROLLUP_EXECUTE_OUTBOX_MESSAGE:
this.withSmartRollupExecuteOutboxMessage(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;