Skip to content

Commit

Permalink
added new rpc endpoints and updated tests (#2148)
Browse files Browse the repository at this point in the history
* added new rpc endpoints and updated tests

* updated unit tests

* updated unit test for rpc-cache

* updated typedoc and updated test to originate own contract
  • Loading branch information
dsawali authored Nov 24, 2022
1 parent bececca commit 5652389
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 13 deletions.
119 changes: 108 additions & 11 deletions integration-tests/contract-increase-paid-storage-operation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,95 @@ import { CONFIGS } from './config';
import { OpKind, Protocols } from '@taquito/taquito';
import { ligoSample } from './data/ligo-simple-contract';

CONFIGS().forEach(({ lib, rpc, setup, protocol, knownContract }) => {
const kathmandunetAndAlpha = (protocol === Protocols.PtKathman || protocol === Protocols.ProtoALpha) ? test : test.skip;
CONFIGS().forEach(({ lib, rpc, setup, protocol }) => {
const kathmandunet = (protocol === Protocols.PtKathman) ? test : test.skip;
const limanetAndAlpha = (protocol === Protocols.PtLimaPtL || protocol === Protocols.ProtoALpha) ? test : test.skip;

const Tezos = lib;

let simpleContractAddress: string;
describe(`Test Increase Paid Storage using: ${rpc}`, () => {
beforeEach(async (done) => {
beforeAll(async (done) => {
await setup(true);

try {
const op = await Tezos.contract.originate({
balance: "1",
code: `parameter string;
storage string;
code {CAR;
PUSH string "Hello ";
CONCAT;
NIL operation; PAIR};
`,
init: `"test"`
});

await op.confirmation();

simpleContractAddress = op.contractAddress!;
} catch(e) {
console.log(JSON.stringify(e));
}
done();
});

kathmandunetAndAlpha('should be able to increase the paid storage of a contract successfully', async (done) => {
kathmandunet(`should be able to increase the paid storage of a contract successfully: ${rpc}`, async (done) => {
const op = await Tezos.contract.increasePaidStorage({
amount: 1,
destination: knownContract
destination: simpleContractAddress
});

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

limanetAndAlpha(`should be able to increase the paid storage of a contract successfully: ${rpc}`, async (done) => {
const paidSpaceBefore = await Tezos.rpc.getStoragePaidSpace(simpleContractAddress);

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

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

const paidSpaceAfter = await Tezos.rpc.getStoragePaidSpace(simpleContractAddress);

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

kathmandunet(`should be able to include increasePaidStorage operation in a batch: ${rpc}`, async (done) => {
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: simpleContractAddress
})
.send();
await op.confirmation();
expect(op.status).toEqual('applied');
done();
});

kathmandunetAndAlpha('should be able to include increasePaidStorage operation in a batch', async (done) => {
limanetAndAlpha(`should be able to include increasePaidStorage operation in a batch: ${rpc}`, async (done) => {
const paidSpaceBefore = await Tezos.rpc.getStoragePaidSpace(simpleContractAddress);

const op = await Tezos.contract
.batch()
.withOrigination({
Expand All @@ -40,15 +106,42 @@ CONFIGS().forEach(({ lib, rpc, setup, protocol, knownContract }) => {
})
.withIncreasePaidStorage({
amount: 1,
destination: knownContract
destination: simpleContractAddress
})
.send();
await op.confirmation();
expect(op.status).toEqual('applied');

const paidSpaceAfter = await Tezos.rpc.getStoragePaidSpace(simpleContractAddress);

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

kathmandunet(`should be able to include increasePaidStorage operation in a batch (different batch syntax): ${rpc}`, async (done) => {
const op = await Tezos.contract.batch([
{
kind: OpKind.ORIGINATION,
balance: '1',
code: ligoSample,
storage: 0
},
{
kind: OpKind.INCREASE_PAID_STORAGE,
amount: 1,
destination: simpleContractAddress
}
])
.send();

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

kathmandunetAndAlpha('should be able to include increasePaidStorage operation in a batch (different batch syntax)', async (done) => {
limanetAndAlpha(`should be able to include increasePaidStorage operation in a batch (different batch syntax): ${rpc}`, async (done) => {
const paidSpaceBefore = await Tezos.rpc.getStoragePaidSpace(simpleContractAddress);

const op = await Tezos.contract.batch([
{
kind: OpKind.ORIGINATION,
Expand All @@ -59,17 +152,21 @@ CONFIGS().forEach(({ lib, rpc, setup, protocol, knownContract }) => {
{
kind: OpKind.INCREASE_PAID_STORAGE,
amount: 1,
destination: knownContract
destination: simpleContractAddress
}
])
.send();

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

const paidSpaceAfter = await Tezos.rpc.getStoragePaidSpace(simpleContractAddress);

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

kathmandunetAndAlpha('should return error when destination contract address is invalid', async (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 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> {
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',
});
}
}
Loading

0 comments on commit 5652389

Please sign in to comment.