Skip to content

Commit

Permalink
feat: support drain_delegate operation in contract api
Browse files Browse the repository at this point in the history
  • Loading branch information
hui-an-yang committed Dec 1, 2022
1 parent 8be5b27 commit efc0272
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 1 deletion.
12 changes: 12 additions & 0 deletions packages/taquito/src/contract/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import {
TxRollupBatchParams,
IncreasePaidStorageParams,
TransferTicketParams,
DrainDelegateParams,
BallotParams,
ProposalsParams,
} 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 { TransferTicketOperation } from '../operations/transfer-ticket-operation';
import { DrainDelegateOperation } from '../operations';
import { BallotOperation } from '../operations';
import { ProposalsOperation } from '../operations/proposals-operation';

Expand Down Expand Up @@ -219,6 +221,16 @@ export interface ContractProvider extends StorageProvider {
*/
txRollupSubmitBatch(params: TxRollupBatchParams): Promise<TxRollupBatchOperation>;

/**
*
* @description Submit drain delegate for an ongoing proposal
*
* @returns An operation handle with the result from the RPC node
*
* @param DrainDelegateParams DrainDelegate operation parameter
*/
drainDelegate(params: DrainDelegateParams): Promise<DrainDelegateOperation>;

/**
*
* @description Submit ballot for an ongoing proposal
Expand Down
15 changes: 15 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,
DrainDelegateParams,
RPCDrainDelegateOperation,
BallotParams,
RPCBallotOperation,
ProposalsParams,
Expand Down Expand Up @@ -269,6 +271,19 @@ export const createIncreasePaidStorageOperation = async ({
} as RPCIncreasePaidStorageOperation;
};

export const createDrainDelegateOperation = async ({
consensus_key,
delegate,
destination,
}: DrainDelegateParams) => {
return {
kind: OpKind.DRAIN_DELEGATE,
consensus_key,
delegate,
destination,
} as RPCDrainDelegateOperation;
};

export const createBallotOperation = async ({ source, proposal, ballot }: BallotParams) => {
return {
kind: OpKind.BALLOT,
Expand Down
30 changes: 29 additions & 1 deletion 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,
DrainDelegateParams,
BallotParams,
ProposalsParams,
} from '../operations/types';
Expand All @@ -42,6 +43,7 @@ import {
createTxRollupBatchOperation,
createTransferTicketOperation,
createIncreasePaidStorageOperation,
createDrainDelegateOperation,
createBallotOperation,
createProposalsOperation,
} from './prepare';
Expand All @@ -58,7 +60,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 { BallotOperation } from '../operations';
import { BallotOperation, DrainDelegateOperation } from '../operations';
import { ProposalsOperation } from '../operations/proposals-operation';

export class RpcContractProvider
Expand Down Expand Up @@ -527,6 +529,32 @@ export class RpcContractProvider
);
}

/**
*
* @description Transfers the spendable balance of the delegate to destination when consensus_key is the active consensus key of delegate
*
* @returns An operation handle with the result from the rpc node
*
* @param params drainDelegate operation parameter
*/
async drainDelegate(params: DrainDelegateParams) {
const publicKeyHash = await this.signer.publicKeyHash();
const operation = await createDrainDelegateOperation({
...params,
});
const prepared = await this.prepareOperation({ operation: operation, source: publicKeyHash });
const opBytes = await this.forge(prepared);
const { hash, context, forgedBytes, opResponse } = await this.signAndInject(opBytes);
return new DrainDelegateOperation(
hash,
operation,
publicKeyHash,
forgedBytes,
opResponse,
context
);
}

/**
*
* @description Originate a new tx rollup. Will sign and inject an operation using the current context
Expand Down
46 changes: 46 additions & 0 deletions packages/taquito/src/operations/drain-delegate-operation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { OperationContentsAndResult, OperationContentsAndResultDrainDelegate } from '@taquito/rpc';
import { Context } from '../context';
import { Operation } from './operations';
import { ForgedBytes, RPCDrainDelegateOperation } from './types';

/**
*
* @description DrainDelegateOperation provides utility functions to fetch a new operation of kind drain_delegate
*
*/

export class DrainDelegateOperation extends Operation {
constructor(
hash: string,
private readonly params: RPCDrainDelegateOperation,
public readonly source: string,
raw: ForgedBytes,
results: OperationContentsAndResult[],
context: Context
) {
super(hash, raw, results, context);
}

get operationResults() {
const DrainDelegateOp =
Array.isArray(this.results) &&
(this.results.find(
(op) => op.kind === 'drain_delegate'
) as OperationContentsAndResultDrainDelegate);
const result =
DrainDelegateOp && DrainDelegateOp.metadata && DrainDelegateOp.metadata.balance_updates;
return result ? result : undefined;
}

get consensusKey() {
return this.params.consensus_key;
}

get delegate() {
return this.params.delegate;
}

get detination() {
return this.params.destination;
}
}
3 changes: 3 additions & 0 deletions packages/taquito/src/operations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export {
RPCActivateOperation,
RPCOperation,
PrepareOperationParams,
DrainDelegateParams,
RPCDrainDelegateOperation,
BallotParams,
RPCBallotOperation,
} from './types';
Expand All @@ -35,4 +37,5 @@ export { DelegateOperation } from './delegate-operation';
export { OriginationOperation } from './origination-operation';
export { TransactionOperation } from './transaction-operation';
export { BallotOperation } from './ballot-operation';
export { DrainDelegateOperation } from './drain-delegate-operation';
export { Operation } from './operations';
1 change: 1 addition & 0 deletions packages/taquito/src/operations/operation-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export abstract class OperationEmitter {
cOps.map((op: RPCOperation) => {
switch (op.kind) {
case OpKind.ACTIVATION:
case OpKind.DRAIN_DELEGATE:
return {
...op,
};
Expand Down
20 changes: 20 additions & 0 deletions packages/taquito/src/operations/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,25 @@ export interface RPCIncreasePaidStorageOperation {
destination: string;
}

/**
* @description Parameters for the DrainDelegate method
*/
export interface DrainDelegateParams {
consensus_key: string;
delegate: string;
destination: string;
}

/**
* @description RPC DrainDelegate operation
*/
export interface RPCDrainDelegateOperation {
kind: OpKind.DRAIN_DELEGATE;
consensus_key: string;
delegate: string;
destination: string;
}

/**
* @description Ballot operation params
*/
Expand Down Expand Up @@ -466,6 +485,7 @@ export type RPCOperation =
| RPCTxRollupBatchOperation
| RPCTransferTicketOperation
| RPCIncreasePaidStorageOperation
| RPCDrainDelegateOperation
| RPCBallotOperation
| RPCProposalsOperation;

Expand Down

0 comments on commit efc0272

Please sign in to comment.