Skip to content

Commit

Permalink
feat: add getBalanceAndFrozenBonds and getSpendableAndFrozenBonds rpc…
Browse files Browse the repository at this point in the history
… endpoint

re #3023
  • Loading branch information
hui-an-yang committed Sep 9, 2024
1 parent 1cd66b4 commit 750fe59
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 0 deletions.
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 @@ -71,6 +71,16 @@ CONFIGS().forEach(
expect(balance).toBeDefined();
});

it(`Verify that rpcClient.getBalanceAndFrozenBonds for knownBaker returns the full balance`, async () => {
const balance = await rpcClient.getBalanceAndFrozenBonds(knownBaker);
expect(balance).toBeDefined();
});

betanet(`Verify that rpcClient.getSpendableAndFrozenBonds for knownBaker returns the full balance`, async () => {
const balance = await rpcClient.getSpendableAndFrozenBonds(knownBaker);
expect(balance).toBeDefined();
});

it(`Verify that rpcClient.getFullBalance for knownBaker returns the full balance`, async () => {
const balance = await rpcClient.getFullBalance(knownBaker);
expect(balance).toBeDefined();
Expand Down
12 changes: 12 additions & 0 deletions packages/taquito-contracts-library/src/rpc-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ export class RpcWrapperContractsLibrary implements RpcClientInterface {
): Promise<BalanceResponse> {
return this.rpc.getSpendable(address, { block });
}
async getBalanceAndFrozenBonds(
address: string,
{ block }: RPCOptions = defaultRPCOptions
): Promise<BalanceResponse> {
return this.rpc.getBalanceAndFrozenBonds(address, { block });
}
async getSpendableAndFrozenBonds(
address: string,
{ block }: RPCOptions = defaultRPCOptions
): Promise<BalanceResponse> {
return this.rpc.getSpendableAndFrozenBonds(address, { block });
}
async getFullBalance(
address: string,
{ block }: RPCOptions = defaultRPCOptions
Expand Down
4 changes: 4 additions & 0 deletions packages/taquito-rpc/src/rpc-client-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export interface RpcClientInterface {
getLiveBlocks(options?: RPCOptions): Promise<string[]>;
getBalance(address: string, options?: RPCOptions): Promise<BalanceResponse>;
getSpendable(address: string, options?: RPCOptions): Promise<BalanceResponse>;
getBalanceAndFrozenBonds(address: string, options?: RPCOptions): Promise<BalanceResponse>;
getSpendableAndFrozenBonds(address: string, options?: RPCOptions): Promise<BalanceResponse>;
getFullBalance(address: string, options?: RPCOptions): Promise<BalanceResponse>;
getStakedBalance(address: string, options?: RPCOptions): Promise<BalanceResponse>;
getUnstakedFinalizableBalance(address: string, options?: RPCOptions): Promise<BalanceResponse>;
Expand Down Expand Up @@ -150,6 +152,8 @@ export enum RPCMethodName {
GET_BLOCK_METADATA = 'getBlockMetadata',
GET_BALANCE = 'getBalance',
GET_SPENDABLE = 'getSpendable',
GET_BALANCE_AND_FROZEN_BONDS = 'getBalanceAndFrozenBonds',
GET_SPENDABLE_AND_FROZEN_BONDS = 'getSpendableAndFrozenBonds',
GET_FULL_BALANCE = 'getFullBalance',
GET_STAKED_BALANCE = 'getStakedBalance',
GET_UNSTAKED_FINALIZABLE_BALANCE = 'getUnstakedFinalizableBalance',
Expand Down
48 changes: 48 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 @@ -246,6 +246,54 @@ export class RpcClientCache implements RpcClientInterface {
}
}

/**
* @param address address from which we want to retrieve balance and frozen bonds
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description The sum (in mutez) of the spendable balance and frozen bonds of a contract. Corresponds to the contract's full balance from which staked funds and unstake requests have been excluded. Identical to the 'spendable_and_frozen_bonds' RPC.
*/
async getBalanceAndFrozenBonds(
address: string,
{ block }: RPCOptions = defaultRPCOptions
): Promise<BalanceResponse> {
this.validateAddress(address);
const key = this.formatCacheKey(
this.rpcClient.getRpcUrl(),
RPCMethodName.GET_BALANCE_AND_FROZEN_BONDS,
[block, address]
);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getBalanceAndFrozenBonds(address, { block });
this.put(key, response);
return response;
}
}

/**
* @param address address from which we want to retrieve spendable and frozen bonds
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description The sum (in mutez) of the spendable balance and frozen bonds of a contract. Corresponds to the contract's full balance from which staked funds and unstake requests have been excluded. Identical to the 'balance_and_frozen_bonds' RPC.
*/
async getSpendableAndFrozenBonds(
address: string,
{ block }: RPCOptions = defaultRPCOptions
): Promise<BalanceResponse> {
this.validateAddress(address);
const key = this.formatCacheKey(
this.rpcClient.getRpcUrl(),
RPCMethodName.GET_SPENDABLE_AND_FROZEN_BONDS,
[block, address]
);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getSpendableAndFrozenBonds(address, { block });
this.put(key, response);
return response;
}
}

/**
* @param address address from which we want to retrieve the full balance
* @param options contains generic configuration for rpc calls to specified block (default to head)
Expand Down
40 changes: 40 additions & 0 deletions packages/taquito-rpc/src/taquito-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,46 @@ export class RpcClient implements RpcClientInterface {
return new BigNumber(balance);
}

/**
* @param address address from which we want to retrieve balance and frozen bonds
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description The sum (in mutez) of the spendable balance and frozen bonds of a contract. Corresponds to the contract's full balance from which staked funds and unstake requests have been excluded. Identical to the 'spendable_and_frozen_bonds' RPC.
* @see https://tezos.gitlab.io/active/rpc.html#get-block-id-context-contracts-contract-id-full-balance
*/
async getBalanceAndFrozenBonds(
address: string,
{ block }: RPCOptions = defaultRPCOptions
): Promise<BalanceResponse> {
this.validateAddress(address);
const balance = await this.httpBackend.createRequest<BalanceResponse>({
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${address}/balance_and_frozen_bonds`
),
method: 'GET',
});
return new BigNumber(balance);
}

/**
* @param address address from which we want to retrieve spendable and frozen bonds
* @param options contains generic configuration for rpc calls to specified block (default to head)
* @description The sum (in mutez) of the spendable balance and frozen bonds of a contract. Corresponds to the contract's full balance from which staked funds and unstake requests have been excluded. Identical to the 'balance_and_frozen_bonds' RPC.
* @see https://tezos.gitlab.io/active/rpc.html#get-block-id-context-contracts-contract-id-full-balance
*/
async getSpendableAndFrozenBonds(
address: string,
{ block }: RPCOptions = defaultRPCOptions
): Promise<BalanceResponse> {
this.validateAddress(address);
const balance = await this.httpBackend.createRequest<BalanceResponse>({
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${address}/spendable_and_frozen_bonds`
),
method: 'GET',
});
return new BigNumber(balance);
}

/**
* @param address address from which we want to retrieve the full balance
* @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/test/rpc-cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ describe('RpcClientCache test', () => {
getLiveBlocks: jest.fn(),
getBalance: jest.fn(),
getSpendable: jest.fn(),
getBalanceAndFrozenBonds: jest.fn(),
getSpendableAndFrozenBonds: jest.fn(),
getFullBalance: jest.fn(),
getStakedBalance: jest.fn(),
getUnstakedFinalizableBalance: jest.fn(),
Expand Down Expand Up @@ -106,6 +108,8 @@ describe('RpcClientCache test', () => {
mockRpcClient.getLiveBlocks.mockReturnValue(liveBlocks);
mockRpcClient.getBalance.mockReturnValue(balance);
mockRpcClient.getSpendable.mockReturnValue(balance);
mockRpcClient.getBalanceAndFrozenBonds.mockReturnValue(balance);
mockRpcClient.getSpendableAndFrozenBonds.mockReturnValue(balance);
mockRpcClient.getFullBalance.mockReturnValue(balance);
mockRpcClient.getStakedBalance.mockReturnValue(balance);
mockRpcClient.getUnstakedFinalizableBalance.mockReturnValue(balance);
Expand Down Expand Up @@ -158,6 +162,8 @@ describe('RpcClientCache test', () => {
await rpcCache.getLiveBlocks();
await rpcCache.getBalance(address);
await rpcCache.getSpendable(address);
await rpcCache.getBalanceAndFrozenBonds(address);
await rpcCache.getSpendableAndFrozenBonds(address);
await rpcCache.getFullBalance(address);
await rpcCache.getStakedBalance(address);
await rpcCache.getUnstakedFinalizableBalance(address);
Expand Down Expand Up @@ -330,6 +336,8 @@ describe('RpcClientCache test', () => {
await rpcCache.getLiveBlocks(block);
await rpcCache.getBalance(address, block);
await rpcCache.getSpendable(address, block);
await rpcCache.getBalanceAndFrozenBonds(address, block);
await rpcCache.getSpendableAndFrozenBonds(address, block);
await rpcCache.getFullBalance(address, block);
await rpcCache.getStakedBalance(address, block);
await rpcCache.getUnstakedFinalizableBalance(address, block);
Expand Down Expand Up @@ -398,6 +406,14 @@ describe('RpcClientCache test', () => {
expect(
rpcCache.getAllCachedData()[`rpcTest/getSpendable/${block.block}/${address}/`].response
).toEqual(balance);
expect(
rpcCache.getAllCachedData()[`rpcTest/getBalanceAndFrozenBonds/${block.block}/${address}/`]
.response
).toEqual(balance);
expect(
rpcCache.getAllCachedData()[`rpcTest/getSpendableAndFrozenBonds/${block.block}/${address}/`]
.response
).toEqual(balance);
expect(
rpcCache.getAllCachedData()[`rpcTest/getFullBalance/${block.block}/${address}/`].response
).toEqual(balance);
Expand Down Expand Up @@ -528,6 +544,8 @@ describe('RpcClientCache test', () => {
await rpcCache.getLiveBlocks();
await rpcCache.getBalance(address);
await rpcCache.getSpendable(address);
await rpcCache.getBalanceAndFrozenBonds(address);
await rpcCache.getSpendableAndFrozenBonds(address);
await rpcCache.getFullBalance(address);
await rpcCache.getStakedBalance(address);
await rpcCache.getUnstakedFinalizableBalance(address);
Expand Down
28 changes: 28 additions & 0 deletions packages/taquito-rpc/test/taquito-rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,34 @@ describe('RpcClient test', () => {
});
});

describe('getBalanceAndFrozenBonds', () => {
it('should query the right url and return a string', async () => {
httpBackend.createRequest.mockReturnValue(Promise.resolve('10000'));
const balance = await client.getBalanceAndFrozenBonds(contractAddress);

expect(httpBackend.createRequest.mock.calls[0][0]).toEqual({
method: 'GET',
url: `root/chains/test/blocks/head/context/contracts/${contractAddress}/balance_and_frozen_bonds`,
});
expect(balance).toBeInstanceOf(BigNumber);
expect(balance.toString()).toEqual('10000');
});
});

describe('getSpendableAndFrozenBonds', () => {
it('should query the right url and return a string', async () => {
httpBackend.createRequest.mockReturnValue(Promise.resolve('10000'));
const balance = await client.getSpendableAndFrozenBonds(contractAddress);

expect(httpBackend.createRequest.mock.calls[0][0]).toEqual({
method: 'GET',
url: `root/chains/test/blocks/head/context/contracts/${contractAddress}/spendable_and_frozen_bonds`,
});
expect(balance).toBeInstanceOf(BigNumber);
expect(balance.toString()).toEqual('10000');
});
});

describe('getFullBalance', () => {
it('should query the right url and return a string', async () => {
httpBackend.createRequest.mockReturnValue(Promise.resolve('10000'));
Expand Down

0 comments on commit 750fe59

Please sign in to comment.