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

added new rpc endpoints and updated tests #2148

Merged
merged 4 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { CONFIGS } from './config';
import { OpKind, Protocols } from '@taquito/taquito';
import { ligoSample } from './data/ligo-simple-contract';
import { RpcClient } from '@taquito/rpc';

CONFIGS().forEach(({ lib, rpc, setup, protocol, knownContract }) => {
const kathmandunetAndAlpha = (protocol === Protocols.PtKathman || protocol === Protocols.ProtoALpha) ? test : test.skip;
dsawali marked this conversation as resolved.
Show resolved Hide resolved
const limanetAndAlpha = (protocol === Protocols.PtLimaPtL || protocol === Protocols.ProtoALpha) ? test : test.skip;

const Tezos = lib;
const client = new RpcClient(rpc);

describe(`Test Increase Paid Storage using: ${rpc}`, () => {
beforeEach(async (done) => {
Expand All @@ -23,7 +27,25 @@ CONFIGS().forEach(({ lib, rpc, setup, protocol, knownContract }) => {
expect(op.status).toEqual('applied');
done();
});

limanetAndAlpha('should be able to increase the paid storage of a contract successfully', async (done) => {
const paidSpaceBefore = await client.getStoragePaidSpace(knownContract);
dsawali marked this conversation as resolved.
Show resolved Hide resolved

const op = await Tezos.contract.increasePaidStorage({
amount: 1,
destination: knownContract
});

await op.confirmation();
expect(op.hash).toBeDefined();
expect(op.status).toEqual('applied');

const paidSpaceAfter = await client.getStoragePaidSpace(knownContract);

expect(parseInt(paidSpaceAfter)).toEqual(parseInt(paidSpaceBefore) + 1);
done();
});

kathmandunetAndAlpha('should be able to include increasePaidStorage operation in a batch', async (done) => {
const op = await Tezos.contract
.batch()
Expand All @@ -48,6 +70,36 @@ CONFIGS().forEach(({ lib, rpc, setup, protocol, knownContract }) => {
done();
});

limanetAndAlpha('should be able to include increasePaidStorage operation in a batch', async (done) => {
const paidSpaceBefore = await client.getStoragePaidSpace(knownContract);

const op = await Tezos.contract
.batch()
.withOrigination({
balance: "1",
code: `parameter string;
storage string;
code {CAR;
PUSH string "Hello ";
CONCAT;
NIL operation; PAIR};
`,
init: `"test"`
})
.withIncreasePaidStorage({
amount: 1,
destination: knownContract
})
.send();
await op.confirmation();
expect(op.status).toEqual('applied');

const paidSpaceAfter = await client.getStoragePaidSpace(knownContract);

expect(parseInt(paidSpaceAfter)).toEqual(parseInt(paidSpaceBefore) + 1);
done();
});

kathmandunetAndAlpha('should be able to include increasePaidStorage operation in a batch (different batch syntax)', async (done) => {
const op = await Tezos.contract.batch([
{
Expand All @@ -69,7 +121,34 @@ CONFIGS().forEach(({ lib, rpc, setup, protocol, knownContract }) => {
done();
});

kathmandunetAndAlpha('should return error when destination contract address is invalid', async (done) => {
limanetAndAlpha('should be able to include increasePaidStorage operation in a batch (different batch syntax)', async (done) => {
const paidSpaceBefore = await client.getStoragePaidSpace(knownContract);

const op = await Tezos.contract.batch([
{
kind: OpKind.ORIGINATION,
balance: '1',
code: ligoSample,
storage: 0
},
{
kind: OpKind.INCREASE_PAID_STORAGE,
amount: 1,
destination: knownContract
}
])
.send();

await op.confirmation();
expect(op.status).toEqual('applied');

const paidSpaceAfter = await client.getStoragePaidSpace(knownContract);

expect(parseInt(paidSpaceAfter)).toEqual(parseInt(paidSpaceBefore) + 1);
done();
});

it('should return error when destination contract address is invalid', async (done) => {
expect(async () => {
const op = await Tezos.contract.increasePaidStorage({
amount: 1,
Expand Down
13 changes: 12 additions & 1 deletion integration-tests/rpc-nodes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ CONFIGS().forEach(
}) => {
const Tezos = lib;

const limanet = protocol === Protocols.PtLimaPtL ? test : test.skip;
const limanetAndAlpha = protocol === Protocols.PtLimaPtL || protocol === Protocols.ProtoALpha ? test : test.skip;
const kathmandunetAndAlpha = protocol === Protocols.PtKathman || protocol === Protocols.ProtoALpha ? test : test.skip;

beforeAll(async (done) => {
Expand Down Expand Up @@ -433,6 +433,17 @@ CONFIGS().forEach(
done();
});

limanetAndAlpha('Verify that rpcClient.getStorageUsedSpace will retrieve the used space of a contract storage', async (done) => {
const usedSpace = await rpcClient.getStorageUsedSpace(knownContract);
expect(usedSpace).toBeDefined();
done();
});

limanetAndAlpha('Verify that rpcClient.getStoragePaidSpace will retrieve the paid space of a contract storage', async (done) => {
const paidSpace = await rpcClient.getStoragePaidSpace(knownContract);
expect(paidSpace).toBeDefined();
done();
});
});
});
}
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 @@ -294,4 +294,16 @@ export class RpcWrapperContractsLibrary implements RpcClientInterface {
): Promise<TxRollupInboxResponse | null> {
return this.rpc.getTxRollupInbox(txRollupId, blockLevel, { block });
}
async getStorageUsedSpace(
contract: string,
{ block }: RPCOptions = defaultRPCOptions
): Promise<string> {
return this.rpc.getStorageUsedSpace(contract, { block });
}
async getStoragePaidSpace(
contract: string,
{ block }: RPCOptions = defaultRPCOptions
): Promise<string> {
return this.rpc.getStoragePaidSpace(contract, { block });
}
}
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 @@ -117,6 +117,8 @@ export interface RpcClientInterface {
blockLevel: string,
options?: RPCOptions
): Promise<TxRollupInboxResponse | null>;
getStorageUsedSpace(contract: string, options?: RPCOptions): Promise<string>;
getStoragePaidSpace(contract: string, options?: RPCOptions): Promise<string>;
}

export enum RPCMethodName {
Expand Down Expand Up @@ -155,4 +157,6 @@ export enum RPCMethodName {
GET_TX_ROLLUP_STATE = 'getTxRollupState',
GET_VOTES_LISTINGS = 'getVotesListings',
PACK_DATA = 'packData',
GET_STORAGE_USED_SPACE = 'getStorageUsedSpace',
GET_STORAGE_PAID_SPACE = 'getStoragePaidSpace',
}
54 changes: 54 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 @@ -1111,4 +1111,58 @@ export class RpcClientCache implements RpcClientInterface {
return response;
}
}

/**
*
* @param contract address of the contract we want to retrieve storage information of
* @param options contains generic configuration for rpc calls
*
* @description Access the amount of used space used in a contract's storage
*
* @see https://tezos.gitlab.io/lima/rpc.html#get-block-id-context-contracts-contract-id-storage
*/
async getStorageUsedSpace(
contract: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<string> {
const key = this.formatCacheKey(
this.rpcClient.getRpcUrl(),
RPCMethodName.GET_STORAGE_USED_SPACE,
[block, contract]
);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getStorageUsedSpace(contract, { block });
this.put(key, response);
return response;
}
}

/**
*
* @param contract address of the contract we want to retrieve storage information of
* @param options contains generic configuration for rpc calls
*
* @description Access the amount of paid space in a contract's storage
*
* @see https://tezos.gitlab.io/lima/rpc.html#get-block-id-context-contracts-contract-id-storage
*/
async getStoragePaidSpace(
contract: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<string> {
const key = this.formatCacheKey(
this.rpcClient.getRpcUrl(),
RPCMethodName.GET_STORAGE_PAID_SPACE,
[block, contract]
);
if (this.has(key)) {
return this.get(key);
} else {
const response = this.rpcClient.getStoragePaidSpace(contract, { block });
this.put(key, response);
return response;
}
}
}
42 changes: 42 additions & 0 deletions packages/taquito-rpc/src/taquito-rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1083,4 +1083,46 @@ export class RpcClient implements RpcClientInterface {
method: 'GET',
});
}

/**
*
* @param contract address of the contract we want to retrieve storage information of
* @param options contains generic configuration for rpc calls
*
* @description Access the amount of used space used in a contract's storage
dsawali marked this conversation as resolved.
Show resolved Hide resolved
*
* @see https://tezos.gitlab.io/lima/rpc.html#get-block-id-context-contracts-contract-id-storage
*/
async getStorageUsedSpace(
contract: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<string> {
return this.httpBackend.createRequest<string>({
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${contract}/storage/used_space`
),
method: 'GET',
});
}

/**
*
* @param contract address of the contract we want to retrieve storage information of
* @param options contains generic configuration for rpc calls
*
* @description Access the amount of paid space in a contract's storage
*
* @see https://tezos.gitlab.io/lima/rpc.html#get-block-id-context-contracts-contract-id-storage
*/
async getStoragePaidSpace(
contract: string,
{ block }: { block: string } = defaultRPCOptions
): Promise<string> {
return this.httpBackend.createRequest<string>({
url: this.createURL(
`/chains/${this.chain}/blocks/${block}/context/contracts/${contract}/storage/paid_space`
),
method: 'GET',
});
}
}
25 changes: 24 additions & 1 deletion packages/taquito-rpc/test/rpc-cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ describe('RpcClientCache test', () => {
getLiveBlocks: jest.fn(),
getBalance: jest.fn(),
getStorage: jest.fn(),
getStorageUsedSpace: jest.fn(),
getStoragePaidSpace: jest.fn(),
getScript: jest.fn(),
getNormalizedScript: jest.fn(),
getContract: jest.fn(),
Expand Down Expand Up @@ -90,6 +92,8 @@ describe('RpcClientCache test', () => {
mockRpcClient.getLiveBlocks.mockReturnValue(liveBlocks);
mockRpcClient.getBalance.mockReturnValue(balance);
mockRpcClient.getStorage.mockReturnValue(storage);
mockRpcClient.getStorageUsedSpace.mockReturnValue('100');
mockRpcClient.getStoragePaidSpace.mockReturnValue('120');
mockRpcClient.getScript.mockReturnValue(script);
mockRpcClient.getNormalizedScript.mockReturnValue(script);
mockRpcClient.getContract.mockReturnValue(contract);
Expand Down Expand Up @@ -117,7 +121,6 @@ describe('RpcClientCache test', () => {
mockRpcClient.getProtocols.mockReturnValue(protocols);
mockRpcClient.getTxRollupInbox.mockReturnValue(txRollupInbox);
mockRpcClient.getTxRollupState.mockReturnValue(txRollupState);

rpcCache = new RpcClientCache(mockRpcClient);
});

Expand All @@ -132,6 +135,8 @@ describe('RpcClientCache test', () => {
await rpcCache.getLiveBlocks();
await rpcCache.getBalance(address);
await rpcCache.getStorage(contractAddress);
await rpcCache.getStoragePaidSpace(contractAddress);
await rpcCache.getStorageUsedSpace(contractAddress);
await rpcCache.getScript(contractAddress);
await rpcCache.getNormalizedScript(contractAddress);
await rpcCache.getContract(contractAddress);
Expand Down Expand Up @@ -172,6 +177,12 @@ describe('RpcClientCache test', () => {
expect(
rpcCache.getAllCachedData()[`rpcTest/getStorage/head/${contractAddress}/`].response
).toEqual(storage);
expect(
rpcCache.getAllCachedData()[`rpcTest/getStoragePaidSpace/head/${contractAddress}/`].response
).toEqual('120');
expect(
rpcCache.getAllCachedData()[`rpcTest/getStorageUsedSpace/head/${contractAddress}/`].response
).toEqual('100');
expect(
rpcCache.getAllCachedData()[`rpcTest/getScript/head/${contractAddress}/`].response
).toEqual(script);
Expand Down Expand Up @@ -256,6 +267,8 @@ describe('RpcClientCache test', () => {
await rpcCache.getLiveBlocks(block);
await rpcCache.getBalance(address, block);
await rpcCache.getStorage(contractAddress, block);
await rpcCache.getStoragePaidSpace(contractAddress, block);
await rpcCache.getStorageUsedSpace(contractAddress, block);
await rpcCache.getScript(contractAddress, block);
await rpcCache.getNormalizedScript(contractAddress, { unparsing_mode: 'Readable' }, block);
await rpcCache.getContract(contractAddress, block);
Expand Down Expand Up @@ -306,6 +319,14 @@ describe('RpcClientCache test', () => {
expect(
rpcCache.getAllCachedData()[`rpcTest/getStorage/${block.block}/${contractAddress}/`].response
).toEqual(storage);
expect(
rpcCache.getAllCachedData()[`rpcTest/getStoragePaidSpace/${block.block}/${contractAddress}/`]
.response
).toEqual('120');
expect(
rpcCache.getAllCachedData()[`rpcTest/getStorageUsedSpace/${block.block}/${contractAddress}/`]
.response
).toEqual('100');
expect(
rpcCache.getAllCachedData()[`rpcTest/getScript/${block.block}/${contractAddress}/`].response
).toEqual(script);
Expand Down Expand Up @@ -402,6 +423,8 @@ describe('RpcClientCache test', () => {
await rpcCache.getLiveBlocks();
await rpcCache.getBalance(address);
await rpcCache.getStorage(contractAddress);
await rpcCache.getStoragePaidSpace(contractAddress);
await rpcCache.getStorageUsedSpace(contractAddress);
await rpcCache.getScript(contractAddress);
await rpcCache.getNormalizedScript(contractAddress);
await rpcCache.getContract(contractAddress);
Expand Down
22 changes: 22 additions & 0 deletions packages/taquito-rpc/test/taquito-rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,28 @@ describe('RpcClient test', () => {

done();
});

it('should query used_space url correctly', async (done) => {
await client.getStorageUsedSpace(contractAddress);

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

done();
});

it('should query used_paid url correctly', async (done) => {
await client.getStoragePaidSpace(contractAddress);

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

done();
});
});

describe('getScript', () => {
Expand Down