Skip to content

Commit

Permalink
feat: implement stake, unstake, and finalize_unstake
Browse files Browse the repository at this point in the history
  • Loading branch information
ac10n committed Sep 13, 2023
1 parent edbd33a commit 5a0689e
Show file tree
Hide file tree
Showing 7 changed files with 465 additions and 2 deletions.
4 changes: 3 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@
"tzip",
"Umami",
"unopt",
"UNPAIR"
"UNPAIR",
"unstake",
"zarith"
],
"files": [
"**/*.ts",
Expand Down
84 changes: 84 additions & 0 deletions packages/taquito/src/contract/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ import {
SmartRollupOriginateParamsWithProof,
ActivationParams,
RPCActivateOperation,
RPCStakeOperation,
RPCUnstakeOperation,
RPCFinalizeUnstakeOperation,
StakeParams,
UnstakeParams,
FinalizeUnstakeParams,
} from '../operations/types';
import {
DEFAULT_FEE,
Expand Down Expand Up @@ -339,3 +345,81 @@ export const createSmartRollupOriginateOperation = async ({
parameters_ty: parametersType,
} as RPCSmartRollupOriginateOperation;
};

export const createStakeOperation = async ({
baker,
amount,
fee = DEFAULT_FEE.TRANSFER,
gasLimit = DEFAULT_GAS_LIMIT.TRANSFER,
storageLimit = DEFAULT_STORAGE_LIMIT.TRANSFER,
mutez = false,
}: StakeParams) => {
const operation: RPCStakeOperation = {
kind: OpKind.TRANSACTION,
fee,
gas_limit: gasLimit,
storage_limit: storageLimit,
amount: mutez ? amount.toString() : format('tz', 'mutez', amount).toString(),
source: baker,
destination: baker,
parameters: {
entrypoint: 'stake',
value: {
prim: 'Unit',
},
},
};
return operation;
};

export const createUnstakeOperation = async ({
baker,
amount,
fee = DEFAULT_FEE.TRANSFER,
gasLimit = DEFAULT_GAS_LIMIT.TRANSFER,
storageLimit = DEFAULT_STORAGE_LIMIT.TRANSFER,
mutez = false,
}: UnstakeParams) => {
const operation: RPCUnstakeOperation = {
kind: OpKind.TRANSACTION,
fee,
gas_limit: gasLimit,
storage_limit: storageLimit,
amount: mutez ? amount.toString() : format('tz', 'mutez', amount).toString(),
source: baker,
destination: baker,
parameters: {
entrypoint: 'unstake',
value: {
prim: 'Unit',
},
},
};
return operation;
};

export const createFinalizeUnstakeOperation = async ({
baker,
amount,
fee = DEFAULT_FEE.TRANSFER,
gasLimit = DEFAULT_GAS_LIMIT.TRANSFER,
storageLimit = DEFAULT_STORAGE_LIMIT.TRANSFER,
mutez = false,
}: FinalizeUnstakeParams) => {
const operation: RPCFinalizeUnstakeOperation = {
kind: OpKind.TRANSACTION,
fee,
gas_limit: gasLimit,
storage_limit: storageLimit,
amount: mutez ? amount.toString() : format('tz', 'mutez', amount).toString(),
source: baker,
destination: baker,
parameters: {
entrypoint: 'finalize_unstake',
value: {
prim: 'Unit',
},
},
};
return operation;
};
71 changes: 71 additions & 0 deletions packages/taquito/src/contract/rpc-contract-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ import {
SmartRollupAddMessagesParams,
SmartRollupOriginateParams,
FailingNoopParams,
StakeParams,
UnstakeParams,
} from '../operations/types';
import { DefaultContractType, ContractStorageType, ContractAbstraction } from './contract';
import { InvalidDelegationSource, RevealOperationError } from './errors';
Expand Down Expand Up @@ -764,6 +766,75 @@ export class RpcContractProvider extends Provider implements ContractProvider, S

return batch;
}

/**
*
* @description Stake founds. Will sign and inject an operation using the current context
*
* @returns An operation handle with the result from the rpc node
*
* @param stake operation parameter
*/
async stake(params: StakeParams) {
const estimate = await this.estimate(params, this.estimator.stake.bind(this.estimator));
const source = await this.signer.publicKeyHash();

const prepared = await this.prepare.stake({ ...params, ...estimate });
const content = prepared.opOb.contents.find(
(op) => op.kind === OpKind.TRANSACTION
) as OperationContentsTransaction;

const opBytes = await this.forge(prepared);
const { hash, context, forgedBytes, opResponse } = await this.signAndInject(opBytes);
return new TransactionOperation(hash, content, source, forgedBytes, opResponse, context);
}

/**
*
* @description Unstake founds. Will sign and inject an operation using the current context
*
* @returns An operation handle with the result from the rpc node
*
* @param unstake operation parameter
*/
async unstake(params: UnstakeParams) {
const estimate = await this.estimate(params, this.estimator.stake.bind(this.estimator));
const source = await this.signer.publicKeyHash();

const prepared = await this.prepare.unstake({ ...params, ...estimate });
const content = prepared.opOb.contents.find(
(op) => op.kind === OpKind.TRANSACTION
) as OperationContentsTransaction;

const opBytes = await this.forge(prepared);
const { hash, context, forgedBytes, opResponse } = await this.signAndInject(opBytes);
return new TransactionOperation(hash, content, source, forgedBytes, opResponse, context);
}

/**
*
* @description Unstake founds. Will sign and inject an operation using the current context
*
* @returns An operation handle with the result from the rpc node
*
* @param unstake operation parameter
*/
async finalizeUnstake(params: UnstakeParams) {
const estimate = await this.estimate(
params,
this.estimator.finalizeUnstake.bind(this.estimator)
);
const source = await this.signer.publicKeyHash();

const prepared = await this.prepare.finalizeUnstake({ ...params, ...estimate });
const content = prepared.opOb.contents.find(
(op) => op.kind === OpKind.TRANSACTION
) as OperationContentsTransaction;

const opBytes = await this.forge(prepared);
const { hash, context, forgedBytes, opResponse } = await this.signAndInject(opBytes);
return new TransactionOperation(hash, content, source, forgedBytes, opResponse, context);
}
}

type ContractAbstractionComposer<T> = (
Expand Down
27 changes: 27 additions & 0 deletions packages/taquito/src/estimate/estimate-provider-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import {
UpdateConsensusKeyParams,
SmartRollupAddMessagesParams,
SmartRollupOriginateParams,
StakeParams,
UnstakeParams,
FinalizeUnstakeParams,
} from '../operations/types';
import { Estimate } from './estimate';
import { ContractMethod, ContractMethodObject, ContractProvider } from '../contract';
Expand Down Expand Up @@ -140,4 +143,28 @@ export interface EstimationProvider {
* @param SmartRollupOrigianteParams
*/
smartRollupOriginate(params: SmartRollupOriginateParams): Promise<Estimate>;

/**
*
* @description Estimate gasLimit, storageLimit and fees for stake operation
*
* @returns An estimation of gasLimit, storageLimit and fees for the stake operation
*/
stake(params: StakeParams): Promise<Estimate>;

/**
*
* @description Estimate gasLimit, storageLimit and fees for unstake operation
*
* @returns An estimation of gasLimit, storageLimit and fees for the unstake operation
*/
unstake(params: UnstakeParams): Promise<Estimate>;

/**
*
* @description Estimate gasLimit, storageLimit and fees for finalize_unstake operation
*
* @returns An estimation of gasLimit, storageLimit and fees for the finalize_unstake operation
*/
finalizeUnstake(params: FinalizeUnstakeParams): Promise<Estimate>;
}
63 changes: 63 additions & 0 deletions packages/taquito/src/estimate/rpc-estimate-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import {
UpdateConsensusKeyParams,
SmartRollupAddMessagesParams,
SmartRollupOriginateParams,
StakeParams,
UnstakeParams,
FinalizeUnstakeParams,
} from '../operations/types';
import { Estimate, EstimateProperties } from './estimate';
import { EstimationProvider } from '../estimate/estimate-provider-interface';
Expand Down Expand Up @@ -457,4 +460,64 @@ export class RPCEstimateProvider extends Provider implements EstimationProvider
}
return Estimate.createEstimateInstanceFromProperties(estimateProperties);
}

/**
*
* @description Estimate gasLimit, storageLimit and fees for stake operation
*
* @returns An estimation of gasLimit, storageLimit and fees for the stake operation
*
* @param Estimate
*/
async stake(params: StakeParams) {
const protocolConstants = await this.context.readProvider.getProtocolConstants('head');
const preparedOperation = await this.prepare.stake(params);

const estimateProperties = await this.calculateEstimates(preparedOperation, protocolConstants);

if (preparedOperation.opOb.contents[0].kind === 'reveal') {
estimateProperties.shift();
}
return Estimate.createEstimateInstanceFromProperties(estimateProperties);
}

/**
*
* @description Estimate gasLimit, storageLimit and fees for unstake operation
*
* @returns An estimation of gasLimit, storageLimit and fees for the unstake operation
*
* @param Estimate
*/
async unstake(params: UnstakeParams) {
const protocolConstants = await this.context.readProvider.getProtocolConstants('head');
const preparedOperation = await this.prepare.unstake(params);

const estimateProperties = await this.calculateEstimates(preparedOperation, protocolConstants);

if (preparedOperation.opOb.contents[0].kind === 'reveal') {
estimateProperties.shift();
}
return Estimate.createEstimateInstanceFromProperties(estimateProperties);
}

/**
*
* @description Estimate gasLimit, storageLimit and fees for finalize_unstake operation
*
* @returns An estimation of gasLimit, storageLimit and fees for the finalize_unstake operation
*
* @param Estimate
*/
async finalizeUnstake(params: FinalizeUnstakeParams) {
const protocolConstants = await this.context.readProvider.getProtocolConstants('head');
const preparedOperation = await this.prepare.finalizeUnstake(params);

const estimateProperties = await this.calculateEstimates(preparedOperation, protocolConstants);

if (preparedOperation.opOb.contents[0].kind === 'reveal') {
estimateProperties.shift();
}
return Estimate.createEstimateInstanceFromProperties(estimateProperties);
}
}
Loading

0 comments on commit 5a0689e

Please sign in to comment.