From f6b0d3cf4345e77404ab26fe142c8f7f7e02095b Mon Sep 17 00:00:00 2001 From: huianyang Date: Fri, 20 Jan 2023 09:54:34 -0800 Subject: [PATCH] feat: support new rpc endpoint ticket_balance re #2270 --- .../src/rpc-wrapper.ts | 8 ++++++ .../taquito-rpc/src/rpc-client-interface.ts | 7 +++++ .../src/rpc-client-modules/rpc-cache.ts | 19 ++++++++++++++ packages/taquito-rpc/src/taquito-rpc.ts | 26 +++++++++++++++++++ packages/taquito-rpc/src/types.ts | 6 +++++ 5 files changed, 66 insertions(+) diff --git a/packages/taquito-contracts-library/src/rpc-wrapper.ts b/packages/taquito-contracts-library/src/rpc-wrapper.ts index fa2a7ff411..7789378a2e 100644 --- a/packages/taquito-contracts-library/src/rpc-wrapper.ts +++ b/packages/taquito-contracts-library/src/rpc-wrapper.ts @@ -44,6 +44,7 @@ import { UnparsingMode, VotesListingsResponse, VotingPeriodBlockResult, + ticketTokenParams, } from '@taquito/rpc'; import { ContractsLibrary } from './taquito-contracts-library'; @@ -306,4 +307,11 @@ export class RpcWrapperContractsLibrary implements RpcClientInterface { ): Promise { return this.rpc.getStoragePaidSpace(contract, { block }); } + async getTicketBalance( + contract: string, + ticket: ticketTokenParams, + { block }: RPCOptions = defaultRPCOptions + ): Promise { + return this.rpc.getTicketBalance(contract, ticket, { block }); + } } diff --git a/packages/taquito-rpc/src/rpc-client-interface.ts b/packages/taquito-rpc/src/rpc-client-interface.ts index 03efa80fe0..1a1d54b878 100644 --- a/packages/taquito-rpc/src/rpc-client-interface.ts +++ b/packages/taquito-rpc/src/rpc-client-interface.ts @@ -44,6 +44,7 @@ import { VotesListingsResponse, VotingInfoResponse, VotingPeriodBlockResult, + ticketTokenParams, } from './types'; export interface RPCOptions { @@ -119,6 +120,11 @@ export interface RpcClientInterface { ): Promise; getStorageUsedSpace(contract: string, options?: RPCOptions): Promise; getStoragePaidSpace(contract: string, options?: RPCOptions): Promise; + getTicketBalance( + contract: string, + ticket: ticketTokenParams, + options?: RPCOptions + ): Promise; } export enum RPCMethodName { @@ -159,4 +165,5 @@ export enum RPCMethodName { PACK_DATA = 'packData', GET_STORAGE_USED_SPACE = 'getStorageUsedSpace', GET_STORAGE_PAID_SPACE = 'getStoragePaidSpace', + GET_TICKET_BALANCE = 'getTicketBalance', } diff --git a/packages/taquito-rpc/src/rpc-client-modules/rpc-cache.ts b/packages/taquito-rpc/src/rpc-client-modules/rpc-cache.ts index 964051c7d7..5bd788d0ae 100644 --- a/packages/taquito-rpc/src/rpc-client-modules/rpc-cache.ts +++ b/packages/taquito-rpc/src/rpc-client-modules/rpc-cache.ts @@ -46,6 +46,7 @@ import { UnparsingMode, VotesListingsResponse, VotingPeriodBlockResult, + ticketTokenParams, } from '../types'; import { @@ -1165,4 +1166,22 @@ export class RpcClientCache implements RpcClientInterface { return response; } } + + async getTicketBalance( + contract: string, + ticket: ticketTokenParams, + { block }: { block: string } = defaultRPCOptions + ): Promise { + const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_TICKET_BALANCE, [ + block, + contract, + ]); + if (this.has(key)) { + return this.get(key); + } else { + const response = this.rpcClient.getTicketBalance(contract, ticket, { block }); + this.put(key, response); + return response; + } + } } diff --git a/packages/taquito-rpc/src/taquito-rpc.ts b/packages/taquito-rpc/src/taquito-rpc.ts index 7c697ab764..befb6db408 100644 --- a/packages/taquito-rpc/src/taquito-rpc.ts +++ b/packages/taquito-rpc/src/taquito-rpc.ts @@ -57,6 +57,7 @@ import { VotingPeriodBlockResult, TxRollupStateResponse, TxRollupInboxResponse, + ticketTokenParams, } from './types'; import { castToBigNumber } from './utils/utils'; import { @@ -1126,4 +1127,29 @@ export class RpcClient implements RpcClientInterface { method: 'GET', }); } + + /** + * + * @param contract address of the contract we want to retrieve ticket balance of + * @param options contains generic configuration for rpc calls + * + * @description Access the contract's balance of ticket with specified ticketer, content type, and content. + * + * @see https://tezos.gitlab.io/protocols/016_mumbai.html#rpc-changes + */ + async getTicketBalance( + contract: string, + ticket: ticketTokenParams, + { block }: { block: string } = defaultRPCOptions + ): Promise { + return this.httpBackend.createRequest( + { + url: this.createURL( + `/chains/${this.chain}/blocks/${block}/context/contracts/${contract}/ticket_balance` + ), + method: 'POST', + }, + ticket + ); + } } diff --git a/packages/taquito-rpc/src/types.ts b/packages/taquito-rpc/src/types.ts index 6de47f3098..c4c8da9580 100644 --- a/packages/taquito-rpc/src/types.ts +++ b/packages/taquito-rpc/src/types.ts @@ -1089,6 +1089,12 @@ export interface PackDataParams { gas?: BigNumber; } +export interface ticketTokenParams { + ticketer: 'string'; + content_type: MichelsonV1Expression; + content: MichelsonV1Expression; +} + export type HexString = string; export interface PackDataResponse {