Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

staking in oxford #2656

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 27 additions & 0 deletions integration-tests/contract-staking.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { CONFIGS } from "./config";

CONFIGS().forEach(({ lib, rpc, setup, knownBaker }) => {
const Tezos = lib;
describe(`Test staking through contract API using: ${rpc}`, () => {
beforeEach(async (done) => {
await setup(true)
done()
});
it('Should be able to stake', async (done) => {
const delegate = knownBaker
const pkh = await Tezos.signer.publicKeyHash();
const op = await Tezos.contract.stake({
baker: delegate,
amount: 0.1,
parameter: {
entrypoint: "stake",
value: { "prim": "Unit" }
}
});
await op.confirmation()
expect(op.hash).toBeDefined();
console.log(op);
done();
});
});
});
33 changes: 33 additions & 0 deletions packages/taquito/src/contract/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import {
SmartRollupAddMessagesParams,
SmartRollupOriginateParams,
FailingNoopParams,
StakeParams,
UnstakeParams,
FinalizeUnstakeParams,
} from '../operations/types';
import { ContractAbstraction, ContractStorageType, DefaultContractType } from './contract';
import { IncreasePaidStorageOperation } from '../operations/increase-paid-storage-operation';
Expand Down Expand Up @@ -283,4 +286,34 @@ export interface ContractProvider extends StorageProvider {
* @param params FailingNoop operation parameter
*/
failingNoop(params: FailingNoopParams): Promise<FailingNoopOperation>;

/**
*
* @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
*/
stake(params: StakeParams): Promise<TransactionOperation>;

/**
*
* @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
*/
unstake(params: UnstakeParams): Promise<TransactionOperation>;

/**
*
* @description Finalize unstake founds. Will sign and inject an operation using the current context
*
* @returns An operation handle with the result from the rpc node
*
* @param finalizeUnstake operation parameter
*/
unstake(params: FinalizeUnstakeParams): Promise<TransactionOperation>;
}
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 Finalize 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
Loading