Skip to content

Commit

Permalink
feat: support new operation UpdateConsensusKey for Lima
Browse files Browse the repository at this point in the history
  • Loading branch information
hui-an-yang committed Nov 5, 2022
1 parent 33489db commit 029efa1
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 1 deletion.
20 changes: 20 additions & 0 deletions packages/taquito/src/batch/rpc-batch-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
createTxRollupBatchOperation,
createTransferTicketOperation,
createIncreasePaidStorageOperation,
createUpdateConsensusKeyOperation,
} from '../contract/prepare';
import { BatchOperation } from '../operations/batch-operation';
import { OperationEmitter } from '../operations/operation-emitter';
Expand All @@ -31,6 +32,7 @@ import {
TxRollupBatchParams,
TransferTicketParams,
IncreasePaidStorageParams,
UpdateConsensusKeyParams,
} from '../operations/types';
import { OpKind } from '@taquito/rpc';
import { ContractMethodObject } from '../contract/contract-methods/contract-method-object-param';
Expand Down Expand Up @@ -173,6 +175,17 @@ export class OperationBatch extends OperationEmitter {
return this;
}

/**
*
* @description Add an operation to update consensus key to the batch
*
* @param params UpdateConsensusKey operation parameter
*/
withUpdateConsensusKey(params: UpdateConsensusKeyParams) {
this.operations.push({ kind: OpKind.UPDATE_CONSENSUS_KEY, ...params });
return this;
}

/**
*
* @description Add an operation to originate a rollup to the batch
Expand Down Expand Up @@ -223,6 +236,10 @@ export class OperationBatch extends OperationEmitter {
return createIncreasePaidStorageOperation({
...param,
});
case OpKind.UPDATE_CONSENSUS_KEY:
return createUpdateConsensusKeyOperation({
...param,
});
case OpKind.TX_ROLLUP_ORIGINATION:
return createTxRollupOriginationOperation({
...param,
Expand Down Expand Up @@ -267,6 +284,9 @@ export class OperationBatch extends OperationEmitter {
case OpKind.INCREASE_PAID_STORAGE:
this.withIncreasePaidStorage(param);
break;
case OpKind.UPDATE_CONSENSUS_KEY:
this.withUpdateConsensusKey(param);
break;
case OpKind.TX_ROLLUP_ORIGINATION:
this.withTxRollupOrigination(param);
break;
Expand Down
12 changes: 12 additions & 0 deletions packages/taquito/src/contract/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ import {
TxRollupOriginateParams,
TxRollupBatchParams,
IncreasePaidStorageParams,
UpdateConsensusKeyParams,
TransferTicketParams,
} from '../operations/types';
import { ContractAbstraction, ContractStorageType, DefaultContractType } from './contract';
import { TxRollupBatchOperation } from '../operations/tx-rollup-batch-operation';
import { IncreasePaidStorageOperation } from '../operations/increase-paid-storage-operation';
import { UpdateConsensusKeyOperation } from '../operations/update-consensus-key-operation';
import { TransferTicketOperation } from '../operations/transfer-ticket-operation';

export type ContractSchema = Schema | unknown;
Expand Down Expand Up @@ -195,6 +197,16 @@ export interface ContractProvider extends StorageProvider {
*/
increasePaidStorage(params: IncreasePaidStorageParams): Promise<IncreasePaidStorageOperation>;

/**
*
* @description Update the consensus key of the signing delegate to pk
*
* @returns An operation handle with the result from the rpc node
*
* @param params UpdateConsensusKey operation parameter
*/
updateConsensusKey(params: UpdateConsensusKeyParams): Promise<UpdateConsensusKeyOperation>;

/**
*
* @description Originate a new tx rollup. Will sign and inject an operation using the current context
Expand Down
19 changes: 19 additions & 0 deletions packages/taquito/src/contract/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
RPCTransferTicketOperation,
IncreasePaidStorageParams,
RPCIncreasePaidStorageOperation,
UpdateConsensusKeyParams,
RPCUpdateConsensusKeyOperation,
} from '../operations/types';
import { DEFAULT_FEE, DEFAULT_GAS_LIMIT, DEFAULT_STORAGE_LIMIT } from '../constants';
import { format } from '@taquito/utils';
Expand Down Expand Up @@ -264,3 +266,20 @@ export const createIncreasePaidStorageOperation = async ({
destination,
} as RPCIncreasePaidStorageOperation;
};

export const createUpdateConsensusKeyOperation = async ({
source,
fee,
gasLimit,
storageLimit,
pk,
}: UpdateConsensusKeyParams) => {
return {
kind: OpKind.UPDATE_CONSENSUS_KEY,
source,
fee,
gas_limit: gasLimit,
storage_limit: storageLimit,
pk,
} as RPCUpdateConsensusKeyOperation;
};
35 changes: 35 additions & 0 deletions packages/taquito/src/contract/rpc-contract-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
TxRollupBatchParams,
TransferTicketParams,
IncreasePaidStorageParams,
UpdateConsensusKeyParams,
} from '../operations/types';
import { DefaultContractType, ContractStorageType, ContractAbstraction } from './contract';
import { InvalidDelegationSource, RevealOperationError } from './errors';
Expand All @@ -40,6 +41,7 @@ import {
createTxRollupBatchOperation,
createTransferTicketOperation,
createIncreasePaidStorageOperation,
createUpdateConsensusKeyOperation,
} from './prepare';
import { smartContractAbstractionSemantic } from './semantic';
import {
Expand All @@ -54,6 +56,7 @@ import { TxRollupOriginationOperation } from '../operations/tx-rollup-originatio
import { TxRollupBatchOperation } from '../operations/tx-rollup-batch-operation';
import { TransferTicketOperation } from '../operations/transfer-ticket-operation';
import { IncreasePaidStorageOperation } from '../operations/increase-paid-storage-operation';
import { UpdateConsensusKeyOperation } from '../operations/update-consensus-key-operation';

export class RpcContractProvider
extends OperationEmitter
Expand Down Expand Up @@ -521,6 +524,38 @@ export class RpcContractProvider
);
}

/**
*
* @description Update the consensus key of the signing delegate to pk
*
* @returns An operation handle with the result from the rpc node
*
* @param params updateConsensusKey operation parameter
*/
async updateConsensusKey(params: UpdateConsensusKeyParams) {
const publicKeyHash = await this.signer.publicKeyHash();
const estimate = await this.estimate(
params,
this.estimator.updateConsensusKey.bind(this.estimator)
);
const operation = await createUpdateConsensusKeyOperation({
...params,
...estimate,
});
const ops = await this.addRevealOperationIfNeeded(operation, publicKeyHash);
const prepared = await this.prepareOperation({ operation: ops, source: publicKeyHash });
const opBytes = await this.forge(prepared);
const { hash, context, forgedBytes, opResponse } = await this.signAndInject(opBytes);
return new UpdateConsensusKeyOperation(
hash,
operation,
publicKeyHash,
forgedBytes,
opResponse,
context
);
}

/**
*
* @description Originate a new tx rollup. Will sign and inject an operation using the current context
Expand Down
11 changes: 11 additions & 0 deletions packages/taquito/src/estimate/estimate-provider-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
TxRollupBatchParams,
TransferTicketParams,
IncreasePaidStorageParams,
UpdateConsensusKeyParams,
} from '../operations/types';
import { Estimate } from './estimate';

Expand Down Expand Up @@ -98,6 +99,16 @@ export interface EstimationProvider {
*/
increasePaidStorage(params: IncreasePaidStorageParams): Promise<Estimate>;

/**
*
* @description Estimate gasLimit, storageLimit and fees for an update consensus key operation
*
* @returns An estimation of gasLimit, storageLimit, and fees for the operation
*
* @param Estimate
*/
updateConsensusKey(params: UpdateConsensusKeyParams): Promise<Estimate>;

/**
*
* @description Estimate gasLimit, storageLimit and fees for a tx rollup origination operation
Expand Down
41 changes: 41 additions & 0 deletions packages/taquito/src/estimate/rpc-estimate-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
TxRollupBatchParams,
TransferTicketParams,
IncreasePaidStorageParams,
UpdateConsensusKeyParams,
} from '../operations/types';
import { Estimate, EstimateProperties } from './estimate';
import { EstimationProvider } from '../estimate/estimate-provider-interface';
Expand All @@ -36,6 +37,7 @@ import {
createTxRollupBatchOperation,
createTransferTicketOperation,
createIncreasePaidStorageOperation,
createUpdateConsensusKeyOperation,
} from '../contract/prepare';
import {
validateAddress,
Expand Down Expand Up @@ -452,6 +454,14 @@ export class RPCEstimateProvider extends OperationEmitter implements EstimationP
})
);
break;
case OpKind.UPDATE_CONSENSUS_KEY:
operations.push(
await createUpdateConsensusKeyOperation({
...param,
...mergeLimits(param, DEFAULT_PARAMS),
})
);
break;
default:
throw new InvalidOperationKindError((params as any).kind);
}
Expand Down Expand Up @@ -591,6 +601,37 @@ export class RPCEstimateProvider extends OperationEmitter implements EstimationP
return Estimate.createEstimateInstanceFromProperties(estimateProperties);
}

/**
*
* @description Estimate gasLimit, storageLimit, and fees for an updateConsensusKey operation
*
* @returns An estimation of gasLimit, storageLimit, and fees for the operation
*
* @param params updateConsensusKey operation parameters
*/
async updateConsensusKey(params: UpdateConsensusKeyParams) {
const { fee, storageLimit, gasLimit, ...rest } = params;
const pkh = (await this.getKeys()).publicKeyHash;

const protocolConstants = await this.context.readProvider.getProtocolConstants('head');
const DEFAULT_PARAMS = await this.getAccountLimits(pkh, protocolConstants);
const op = await createUpdateConsensusKeyOperation({
...rest,
...mergeLimits({ fee, storageLimit, gasLimit }, DEFAULT_PARAMS),
});
const isRevealNeeded = await this.isRevealOpNeeded([op], pkh);
const ops = isRevealNeeded ? await this.addRevealOp([op], pkh) : op;
const estimateProperties = await this.prepareEstimate(
{ operation: ops, source: pkh },
protocolConstants,
pkh
);
if (isRevealNeeded) {
estimateProperties.shift();
}
return Estimate.createEstimateInstanceFromProperties(estimateProperties);
}

/**
*
* @description Estimate gasLimit, storageLimit and fees for a rollup origination operation
Expand Down
29 changes: 28 additions & 1 deletion packages/taquito/src/operations/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type ParamsWithKind =
| withKind<ActivationParams, OpKind.ACTIVATION>
| withKind<RegisterGlobalConstantParams, OpKind.REGISTER_GLOBAL_CONSTANT>
| withKind<IncreasePaidStorageParams, OpKind.INCREASE_PAID_STORAGE>
| withKind<UpdateConsensusKeyParams, OpKind.UPDATE_CONSENSUS_KEY>
| withKind<TxRollupOriginateParams, OpKind.TX_ROLLUP_ORIGINATION>
| withKind<TxRollupBatchParams, OpKind.TX_ROLLUP_SUBMIT_BATCH>
| withKind<TransferTicketParams, OpKind.TRANSFER_TICKET>;
Expand Down Expand Up @@ -54,6 +55,7 @@ export type RPCOpWithFee =
| RPCRevealOperation
| RPCRegisterGlobalConstantOperation
| RPCIncreasePaidStorageOperation
| RPCUpdateConsensusKeyOperation
| RPCTxRollupOriginationOperation
| RPCTxRollupBatchOperation
| RPCTransferTicketOperation;
Expand All @@ -64,6 +66,7 @@ export type RPCOpWithSource =
| RPCRevealOperation
| RPCRegisterGlobalConstantOperation
| RPCIncreasePaidStorageOperation
| RPCUpdateConsensusKeyOperation
| RPCTxRollupOriginationOperation
| RPCTxRollupBatchOperation
| RPCTransferTicketOperation;
Expand Down Expand Up @@ -425,6 +428,29 @@ export interface RPCIncreasePaidStorageOperation {
destination: string;
}

/**
* @description Parameters for the updateConsensusKey method
*/
export interface UpdateConsensusKeyParams {
source?: string;
fee?: number;
gasLimit?: number;
storageLimit?: number;
pk: string;
}

/**
* @description RPC updateConsensusKey operation
*/
export interface RPCUpdateConsensusKeyOperation {
kind: OpKind.UPDATE_CONSENSUS_KEY;
source: string;
fee: number;
gas_limit: number;
storage_limit: number;
pk: string;
}

export type RPCOperation =
| RPCOriginationOperation
| RPCTransferOperation
Expand All @@ -435,7 +461,8 @@ export type RPCOperation =
| RPCTxRollupOriginationOperation
| RPCTxRollupBatchOperation
| RPCTransferTicketOperation
| RPCIncreasePaidStorageOperation;
| RPCIncreasePaidStorageOperation
| RPCUpdateConsensusKeyOperation;

export type PrepareOperationParams = {
operation: RPCOperation | RPCOperation[];
Expand Down
Loading

0 comments on commit 029efa1

Please sign in to comment.