Skip to content

Commit

Permalink
feat: support new rpc endpoint ticket_balance
Browse files Browse the repository at this point in the history
  • Loading branch information
hui-an-yang committed Jan 20, 2023
1 parent 2e182a3 commit f6b0d3c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/taquito-contracts-library/src/rpc-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
UnparsingMode,
VotesListingsResponse,
VotingPeriodBlockResult,
ticketTokenParams,
} from '@taquito/rpc';
import { ContractsLibrary } from './taquito-contracts-library';

Expand Down Expand Up @@ -306,4 +307,11 @@ export class RpcWrapperContractsLibrary implements RpcClientInterface {
): Promise<string> {
return this.rpc.getStoragePaidSpace(contract, { block });
}
async getTicketBalance(
contract: string,
ticket: ticketTokenParams,
{ block }: RPCOptions = defaultRPCOptions
): Promise<string> {
return this.rpc.getTicketBalance(contract, ticket, { block });
}
}
7 changes: 7 additions & 0 deletions packages/taquito-rpc/src/rpc-client-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
VotesListingsResponse,
VotingInfoResponse,
VotingPeriodBlockResult,
ticketTokenParams,
} from './types';

export interface RPCOptions {
Expand Down Expand Up @@ -119,6 +120,11 @@ export interface RpcClientInterface {
): Promise<TxRollupInboxResponse | null>;
getStorageUsedSpace(contract: string, options?: RPCOptions): Promise<string>;
getStoragePaidSpace(contract: string, options?: RPCOptions): Promise<string>;
getTicketBalance(
contract: string,
ticket: ticketTokenParams,
options?: RPCOptions
): Promise<string>;
}

export enum RPCMethodName {
Expand Down Expand Up @@ -159,4 +165,5 @@ export enum RPCMethodName {
PACK_DATA = 'packData',
GET_STORAGE_USED_SPACE = 'getStorageUsedSpace',
GET_STORAGE_PAID_SPACE = 'getStoragePaidSpace',
GET_TICKET_BALANCE = 'getTicketBalance',
}
19 changes: 19 additions & 0 deletions packages/taquito-rpc/src/rpc-client-modules/rpc-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
UnparsingMode,
VotesListingsResponse,
VotingPeriodBlockResult,
ticketTokenParams,
} from '../types';

import {
Expand Down Expand Up @@ -1165,4 +1166,22 @@ export class RpcClientCache implements RpcClientInterface {
return response;
}
}

async getTicketBalance(
contract: string,
ticket: ticketTokenParams,
{ block }: { block: string } = defaultRPCOptions
): Promise<string> {
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;
}
}
}
26 changes: 26 additions & 0 deletions packages/taquito-rpc/src/taquito-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
VotingPeriodBlockResult,
TxRollupStateResponse,
TxRollupInboxResponse,
ticketTokenParams,
} from './types';
import { castToBigNumber } from './utils/utils';
import {
Expand Down Expand Up @@ -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<string> {
return this.httpBackend.createRequest<string>(
{
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${contract}/ticket_balance`
),
method: 'POST',
},
ticket
);
}
}
6 changes: 6 additions & 0 deletions packages/taquito-rpc/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f6b0d3c

Please sign in to comment.