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

feat: supporting getAllDelegates in rpc #2975

Merged
merged 2 commits into from
May 28, 2024
Merged
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
2 changes: 2 additions & 0 deletions docs/rpc_nodes_integration_test.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Remove `./rpc-nodes.spec.ts` from `"testPathIgnorePatterns"` in the package.json
✓ Verify that rpcClient.getManagerKey for known baker returns the manager key of the contract (30 ms)
✓ Verify that rpcClient.getDelegate for known baker returns the delegate of the contract (30 ms)
✓ Verify that rpcClient.getBigMapExpr for encoded expression returns the value associated with a key in a big map (151 ms)
✓ Verify that rpcClient.getAllDelegates returns all delegates from RPC (132 ms)
✓ Verify that rpcClient.getDelegates for known baker returns information about a delegate from RPC (32 ms)
✓ Verify that rpc.getVotingInfo for known baker returns voting information about a delegate from RPC (30 ms)
✓ Verify that rpcClient.getConstants returns all constants from RPC (33 ms)
Expand Down Expand Up @@ -86,6 +87,7 @@ Remove `./rpc-nodes.spec.ts` from `"testPathIgnorePatterns"` in the package.json
✓ Verify that rpcClient.getManagerKey for known baker returns the manager key of the contract (11 ms)
✓ Verify that rpcClient.getDelegate for known baker returns the delegate of the contract (8 ms)
✓ Verify that rpcClient.getBigMapExpr for encoded expression returns the value associated with a key in a big map (93 ms)
✓ Verify that rpcClient.getAllDelegates returns all delegates from RPC (132 ms)
✓ Verify that rpcClient.getDelegates for known baker returns information about a delegate from RPC (10 ms)
✓ Verify that rpc.getVotingInfo for known baker returns voting information about a delegate from RPC (7 ms)
✓ Verify that rpcClient.getConstants returns all constants from RPC (10 ms)
Expand Down
10 changes: 10 additions & 0 deletions integration-tests/__tests__/rpc/nodes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,16 @@ CONFIGS().forEach(
expect(bigMapValue).toBeDefined();
});

it(`Verify that rpcClient.getAllDelegates returns all delegates from RPC`, async () => {
const allDelegates = await rpcClient.getAllDelegates();
expect(allDelegates).toBeDefined();

const allViableDelegates = await rpcClient.getAllDelegates({active: true, with_minimal_stake: true});
expect(allViableDelegates).toBeDefined();

expect(allViableDelegates.length).toBeLessThanOrEqual(allDelegates.length);
});

it(`Verify that rpcClient.getDelegates for known baker returns information about a delegate from RPC`, async () => {
const delegates = await rpcClient.getDelegates(knownBaker);
expect(delegates).toBeDefined();
Expand Down
7 changes: 7 additions & 0 deletions packages/taquito-contracts-library/src/rpc-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
PendingOperationsQueryArguments,
RPCSimulateOperationParam,
AILaunchCycleResponse,
AllDelegatesQueryArguments,
} from '@taquito/rpc';
import { ContractsLibrary } from './taquito-contracts-library';

Expand Down Expand Up @@ -174,6 +175,12 @@ export class RpcWrapperContractsLibrary implements RpcClientInterface {
): Promise<BigMapResponse> {
return this.rpc.getBigMapExpr(id, expr, { block });
}
async getAllDelegates(
args: AllDelegatesQueryArguments,
{ block }: RPCOptions = defaultRPCOptions
): Promise<string[]> {
return this.rpc.getAllDelegates(args, { block });
}
async getDelegates(
address: string,
{ block }: RPCOptions = defaultRPCOptions
Expand Down
3 changes: 3 additions & 0 deletions packages/taquito-rpc/src/rpc-client-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
PendingOperationsQueryArguments,
RPCSimulateOperationParam,
AILaunchCycleResponse,
AllDelegatesQueryArguments,
} from './types';

export interface RPCOptions {
Expand Down Expand Up @@ -81,6 +82,7 @@ export interface RpcClientInterface {
getDelegate(address: string, options?: RPCOptions): Promise<DelegateResponse>;
getBigMapKey(address: string, key: BigMapKey, options?: RPCOptions): Promise<BigMapGetResponse>;
getBigMapExpr(id: string, expr: string, options?: RPCOptions): Promise<BigMapResponse>;
getAllDelegates(args: AllDelegatesQueryArguments, options?: RPCOptions): Promise<string[]>;
getDelegates(address: string, options?: RPCOptions): Promise<DelegatesResponse>;
getVotingInfo(address: string, options?: RPCOptions): Promise<VotingInfoResponse>;
getConstants(options?: RPCOptions): Promise<ConstantsResponse>;
Expand Down Expand Up @@ -161,6 +163,7 @@ export enum RPCMethodName {
GET_CURRENT_PROPOSAL = 'getCurrentProposal',
GET_CURRENT_QUORUM = 'getCurrentQuorum',
GET_DELEGATE = 'getDelegate',
GET_ALL_DELEGATES = 'getAllDelegates',
GET_DELEGATES = 'getDelegates',
GET_VOTING_INFO = 'getVotingInfo',
GET_ATTESTATION_RIGHTS = 'getAttestationRights',
Expand Down
25 changes: 25 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 @@ -52,6 +52,7 @@ import {
PendingOperationsV2,
RPCSimulateOperationParam,
AILaunchCycleResponse,
AllDelegatesQueryArguments,
} from '../types';
import { InvalidAddressError, InvalidContractAddressError } from '@taquito/core';
import {
Expand All @@ -73,6 +74,7 @@ type RpcMethodParam =
| UnparsingMode
| BigMapKey
| BakingRightsQueryArguments
| AllDelegatesQueryArguments
| PendingOperationsQueryArguments
| AttestationRightsQueryArguments;

Expand Down Expand Up @@ -546,6 +548,29 @@ export class RpcClientCache implements RpcClientInterface {
}
}

/**
* @param args contains optional query arguments (active, inactive, with_minimal_stake, without_minimal_stake)
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Lists all registered delegates by default with query arguments to filter unneeded values.
* @see https://tezos.gitlab.io/active/rpc.html#get-block-id-context-delegates-pkh
*/
async getAllDelegates(
args: AllDelegatesQueryArguments = {},
{ block }: { block: string } = defaultRPCOptions
): Promise<string[]> {
const key = this.formatCacheKey(this.rpcClient.getRpcUrl(), RPCMethodName.GET_ALL_DELEGATES, [
block,
args,
]);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getAllDelegates(args, { block });
this.put(key, response);
return response;
}
}

/**
* @param address delegate address which we want to retrieve
* @param options contains generic configuration for rpc calls to specified block (default to head)
Expand Down
18 changes: 18 additions & 0 deletions packages/taquito-rpc/src/taquito-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import {
PendingOperationsV2,
RPCSimulateOperationParam,
AILaunchCycleResponse,
AllDelegatesQueryArguments,
} from './types';
import { castToBigNumber } from './utils/utils';
import {
Expand Down Expand Up @@ -477,6 +478,23 @@ export class RpcClient implements RpcClientInterface {
});
}

/**
* @param args contains optional query arguments (active, inactive, with_minimal_stake, without_minimal_stake)
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description Lists all registered delegates by default with query arguments to filter unneeded values.
* @see https://tezos.gitlab.io/active/rpc.html#get-block-id-context-delegates-pkh
*/
async getAllDelegates(
args: AllDelegatesQueryArguments = {},
{ block }: { block: string } = defaultRPCOptions
): Promise<string[]> {
return await this.httpBackend.createRequest<string[]>({
url: this.createURL(`/chains/${this.chain}/blocks/${block}/context/delegates`),
method: 'GET',
query: args,
});
}

/**
* @param address delegate address which we want to retrieve
* @param options contains generic configuration for rpc calls to specified block (default to head)
Expand Down
7 changes: 7 additions & 0 deletions packages/taquito-rpc/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,13 @@ export type BakingRightsArgumentsDelegate = string | string[];
export type BakingRightsArgumentsCycle = number | number[];
export type BakingRightsArgumentsLevel = number | number[];

export type AllDelegatesQueryArguments = {
active?: boolean;
inactive?: boolean;
with_minimal_stake?: boolean;
without_minimal_stake?: boolean;
};

export type BakingRightsQueryArguments = BakingRightsQueryArgumentsBase;

export interface BakingRightsQueryArgumentsBase {
Expand Down
12 changes: 12 additions & 0 deletions packages/taquito-rpc/test/rpc-cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ describe('RpcClientCache test', () => {
getManagerKey: jest.fn(),
getDelegate: jest.fn(),
getBigMapExpr: jest.fn(),
getAllDelegates: jest.fn(),
getDelegates: jest.fn(),
getVotingInfo: jest.fn(),
getConstants: jest.fn(),
Expand Down Expand Up @@ -117,6 +118,7 @@ describe('RpcClientCache test', () => {
mockRpcClient.getManagerKey.mockReturnValue(managerKey);
mockRpcClient.getDelegate.mockReturnValue(delegate);
mockRpcClient.getBigMapExpr.mockReturnValue(bigmapValue);
mockRpcClient.getAllDelegates.mockReturnValue([delegate]);
mockRpcClient.getDelegates.mockReturnValue(delegates);
mockRpcClient.getVotingInfo.mockReturnValue(votingInfo);
mockRpcClient.getConstants.mockReturnValue(constants);
Expand Down Expand Up @@ -167,6 +169,7 @@ describe('RpcClientCache test', () => {
await rpcCache.getManagerKey(contractAddress);
await rpcCache.getDelegate(address);
await rpcCache.getBigMapExpr('72', 'expruPtxxirR4BVqFH43VcmEFZqHaQHJhZQDRVTMgSYAGGgBhBRxfp');
await rpcCache.getAllDelegates();
await rpcCache.getDelegates(address);
await rpcCache.getVotingInfo(address);
await rpcCache.getConstants();
Expand Down Expand Up @@ -250,6 +253,9 @@ describe('RpcClientCache test', () => {
'rpcTest/getBigMapExpr/head/72/expruPtxxirR4BVqFH43VcmEFZqHaQHJhZQDRVTMgSYAGGgBhBRxfp/'
].response
).toEqual(bigmapValue);
expect(rpcCache.getAllCachedData()[`rpcTest/getAllDelegates/head/{}/`].response).toEqual([
delegate,
]);
expect(rpcCache.getAllCachedData()[`rpcTest/getDelegates/head/${address}/`].response).toEqual(
delegates
);
Expand Down Expand Up @@ -335,6 +341,7 @@ describe('RpcClientCache test', () => {
'expruPtxxirR4BVqFH43VcmEFZqHaQHJhZQDRVTMgSYAGGgBhBRxfp',
block
);
await rpcCache.getAllDelegates({}, block);
await rpcCache.getDelegates(address, block);
await rpcCache.getVotingInfo(address, block);
await rpcCache.getConstants(block);
Expand Down Expand Up @@ -433,6 +440,10 @@ describe('RpcClientCache test', () => {
`rpcTest/getBigMapExpr/${block.block}/72/expruPtxxirR4BVqFH43VcmEFZqHaQHJhZQDRVTMgSYAGGgBhBRxfp/`
].response
).toEqual(bigmapValue);
console.log(`rpcTest/getAllDelegates/${block.block}/{}`);
expect(
rpcCache.getAllCachedData()[`rpcTest/getAllDelegates/${block.block}/{}/`].response
).toEqual([delegate]);
expect(
rpcCache.getAllCachedData()[`rpcTest/getDelegates/${block.block}/${address}/`].response
).toEqual(delegates);
Expand Down Expand Up @@ -520,6 +531,7 @@ describe('RpcClientCache test', () => {
await rpcCache.getManagerKey(contractAddress);
await rpcCache.getDelegate(address);
await rpcCache.getBigMapExpr('72', 'expruPtxxirR4BVqFH43VcmEFZqHaQHJhZQDRVTMgSYAGGgBhBRxfp');
await rpcCache.getAllDelegates();
await rpcCache.getDelegates(address);
await rpcCache.getVotingInfo(address);
await rpcCache.getConstants();
Expand Down
15 changes: 15 additions & 0 deletions packages/taquito-rpc/test/taquito-rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,21 @@ describe('RpcClient test', () => {
});
});

describe('getAllDelegates', () => {
it('should query the right url and data', async () => {
httpBackend.createRequest.mockResolvedValue(['tz1cjyja1TU6fiyiFav3mFAdnDsCReJ12hPD']);
const result = await client.getAllDelegates({ active: true, with_minimal_stake: true });
await client.getAllDelegates({ active: true, with_minimal_stake: true });
expect(httpBackend.createRequest.mock.calls[0][0]).toEqual({
method: 'GET',
url: 'root/chains/test/blocks/head/context/delegates',
query: { active: true, with_minimal_stake: true },
});

expect(result).toEqual(['tz1cjyja1TU6fiyiFav3mFAdnDsCReJ12hPD']);
});
});

describe('getDelegates', () => {
const sampleResponse = {
full_balance: new BigNumber('10289576365'),
Expand Down
Loading