Skip to content

Commit

Permalink
test: add tests for rpcProvider & update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
notV4l committed May 1, 2023
1 parent 6fb0583 commit 7868bcc
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 21 deletions.
2 changes: 1 addition & 1 deletion __tests__/defaultProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('defaultProvider', () => {
expect(classResponse).toMatchSchemaRef('LegacyContractClass');
});

test('GetClassByHash', async () => {
test('getClassByHash', async () => {
const classResponse = await testProvider.getClassByHash(erc20ClassHash);
expect(classResponse).toMatchSchemaRef('LegacyContractClass');
});
Expand Down
120 changes: 112 additions & 8 deletions __tests__/rpcProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { getStarkKey, utils } from 'micro-starknet';

import { Account, GetBlockResponse, RpcProvider } from '../src';
import { Account, Contract, GetBlockResponse, RpcProvider, stark } from '../src';
import { StarknetChainId } from '../src/constants';
import { CallData } from '../src/utils/calldata';
import { felt, uint256 } from '../src/utils/calldata/cairo';
// import { toBigInt } from '../src/utils/num';
import {
compiledErc20Echo,
compiledOpenZeppelinAccount,
describeIfDevnet,
describeIfNotDevnet,
describeIfRpc,
getTestAccount,
Expand Down Expand Up @@ -45,20 +50,28 @@ describeIfRpc('RPCProvider', () => {
expect(blockHashAndNumber).toHaveProperty('block_number');
});

test('getBlockNumber', async () => {
const blockNumber = await rpcProvider.getBlockNumber();
expect(typeof blockNumber).toBe('number');
});

test('getStateUpdate', async () => {
const stateUpdate = await rpcProvider.getStateUpdate('latest');
expect(stateUpdate).toMatchSchemaRef('StateUpdateResponse');
});

xtest('getProtocolVersion - pathfinder not implement', async () => {
xtest('getProtocolVersion - pathfinder not implemented', async () => {
await rpcProvider.getProtocolVersion();
});

describeIfNotDevnet('devnet not implement', () => {
test('getPendingTransactions', async () => {
const transactions = await rpcProvider.getPendingTransactions();
expect(Array.isArray(transactions)).toBe(true);
});
test('getProtocolVersion - not implemented', async () => {
expect(rpcProvider.getProtocolVersion()).rejects.toThrow();
});

test('getCode - not implemented', async () => {
expect(
rpcProvider.getCode('0x058d97f7d76e78f44905cc30cb65b91ea49a4b908a76703c54197bca90f81773')
).rejects.toThrow();
});

describe('RPC methods', () => {
Expand Down Expand Up @@ -90,6 +103,87 @@ describeIfRpc('RPCProvider', () => {
await rpcProvider.traceBlockTransactions(latestBlock.block_hash);
});

describeIfDevnet('devnet only', () => {
test('getPendingTransactions - not implemented', async () => {
expect(rpcProvider.getPendingTransactions()).rejects.toThrow();
});

test('getSyncingStats', async () => {
const syncingStats = await rpcProvider.getSyncingStats();
expect(syncingStats).toBe(false);
});

test('getEvents ', async () => {
const randomWallet = stark.randomAddress();
const classHash = '0x011ab8626b891bcb29f7cc36907af7670d6fb8a0528c7944330729d8f01e9ea3';
const transferSelector =
'271746229759260285552388728919865295615886751538523744128730118297934206697';

const { deploy } = await account.declareAndDeploy({
contract: compiledErc20Echo,
classHash,
constructorCalldata: CallData.compile({
name: felt('Token'),
symbol: felt('ERC20'),
decimals: felt('18'),
initial_supply: uint256('1000000000'),
recipient: felt(account.address),
signers: [],
threshold: 1,
}),
});

const erc20EchoContract = new Contract(
compiledErc20Echo.abi,
deploy.contract_address!,
account
);
await erc20EchoContract.transfer(randomWallet, uint256(1));
await erc20EchoContract.transfer(randomWallet, uint256(1));

const blockNumber = await rpcProvider.getBlockNumber();
const result = await rpcProvider.getEvents({
from_block: { block_number: 0 },
to_block: { block_number: blockNumber },
address: deploy.contract_address,
keys: [transferSelector],
chunk_size: 2,
});

expect(result).toHaveProperty('continuation_token');
expect(result).toHaveProperty('events');
expect(Array.isArray(result?.events)).toBe(true);
expect(result?.events?.length).toBe(2);
expect(result.events[0]).toMatchSchemaRef('StarknetEmittedEvent');

const result2 = await rpcProvider.getEvents({
from_block: { block_number: 0 },
to_block: { block_number: blockNumber },
address: deploy.contract_address,
keys: [transferSelector],
chunk_size: 2,
continuation_token: result.continuation_token,
});

expect(result2).not.toHaveProperty('continuation_token');
expect(result2).toHaveProperty('events');
expect(Array.isArray(result2?.events)).toBe(true);
expect(result2?.events?.length).toBe(1);
});
});

describeIfNotDevnet('testnet only', () => {
test('getPendingTransactions', async () => {
const transactions = await rpcProvider.getPendingTransactions();
expect(Array.isArray(transactions)).toBe(true);
});

test('getSyncingStats', async () => {
const syncingStats = await rpcProvider.getSyncingStats();
expect(syncingStats).toMatchSchemaRef('GetSyncingStatsResponse');
});
});

describe('deploy contract related tests', () => {
let contract_address: string;
let transaction_hash: string;
Expand All @@ -112,7 +206,12 @@ describeIfRpc('RPCProvider', () => {

test('getTransactionByHash', async () => {
const transaction = await rpcProvider.getTransactionByHash(transaction_hash);
expect(transaction).toHaveProperty('transaction_hash');
expect(transaction).toMatchSchemaRef('GetTransactionResponse');
});

test('getTransaction', async () => {
const transaction = await rpcProvider.getTransaction(transaction_hash);
expect(transaction).toMatchSchemaRef('GetTransactionResponse');
});

test('getClassHashAt', async () => {
Expand All @@ -123,6 +222,11 @@ describeIfRpc('RPCProvider', () => {
xtest('traceTransaction', async () => {
await rpcProvider.traceTransaction(transaction_hash);
});

test('getClassAt', async () => {
const classAt = await rpcProvider.getClassAt(contract_address);
expect(classAt).toMatchSchemaRef('LegacyContractClass');
});
});

test('getClass classHash 0x058d97f7d76e78f44905cc30cb65b91ea49a4b908a76703c54197bca90f81773', async () => {
Expand Down
4 changes: 3 additions & 1 deletion __tests__/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { matchersWithOptions } from 'jest-json-schema';
import accountSchemas from './schemas/account.json';
import libSchemas from './schemas/lib.json';
import providerSchemas from './schemas/provider.json';
import rpcSchemas from './schemas/rpc.json';
import sequencerSchemas from './schemas/sequencer.json';

const schemas = [accountSchemas, sequencerSchemas, providerSchemas, libSchemas];
const schemas = [accountSchemas, sequencerSchemas, providerSchemas, libSchemas, rpcSchemas];
const jestJsonMatchers = matchersWithOptions({ schemas }, (ajv: any) => {
// @ts-ignore
ajv.addKeyword({
Expand Down Expand Up @@ -34,6 +35,7 @@ export const initializeMatcher = (expect: jest.Expect) => {
expect(sequencerSchemas).toBeValidSchema();
expect(providerSchemas).toBeValidSchema();
expect(libSchemas).toBeValidSchema();
expect(rpcSchemas).toBeValidSchema();
};

declare global {
Expand Down
66 changes: 66 additions & 0 deletions __tests__/schemas/rpc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"$id": "rpcSchemas",
"definitions": {
"GetSyncingStatsResponse": {
"type": "object",
"properties": {
"current_block_hash": {
"type": "string"
},
"current_block_num": {
"type": "string"
},
"highest_block_hash": {
"type": "string"
},
"highest_block_num": {
"type": "string"
},
"starting_block_hash": {
"type": "string"
},
"starting_block_num": {
"type": "string"
}
},
"required": [
"current_block_hash",
"current_block_num",
"highest_block_hash",
"highest_block_num",
"starting_block_hash",
"starting_block_num"
]
},
"StarknetEmittedEvent": {
"type": "object",
"properties": {
"from_address": {
"type": "string"
},
"keys": {
"type": "array",
"items": {
"type": "string"
}
},
"data": {
"type": "array",
"items": {
"type": "string"
}
},
"block_hash": {
"type": "string"
},
"block_number": {
"type": "number"
},
"transaction_hash": {
"type": "string"
}
},
"required": ["from_address", "keys", "data", "block_hash", "block_number", "transaction_hash"]
}
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"info:version": "npm pkg get version | xargs",
"format": "prettier --loglevel warn --write \"**/*.{ts,js,md,yml,json}\"",
"lint": "eslint . --cache --fix --ext .ts",
"ts:check": "tsc --noEmit --resolveJsonModule --project tsconfig.eslint.json"
"ts:check": "tsc --noEmit --resolveJsonModule --project tsconfig.eslint.json",
"docker:run": "docker run -p 5050:5050 shardlabs/starknet-devnet:next --seed 0 --timeout 5000 --disable-rpc-request-validation"
},
"keywords": [
"starknet",
Expand Down
8 changes: 4 additions & 4 deletions src/types/api/openrpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,10 @@ type STATE_UPDATE = {
};
type STORAGE_KEY = string;
type EVENT_FILTER = {
from_block?: BLOCK_ID;
to_block?: BLOCK_ID;
address?: ADDRESS;
keys?: Array<FELT>;
from_block: BLOCK_ID;
to_block: BLOCK_ID;
address: ADDRESS;
keys: Array<FELT>;
};
type RESULT_PAGE_REQUEST = {
continuation_token?: string;
Expand Down
26 changes: 20 additions & 6 deletions www/docs/API/Provider/rpcProvider.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,12 @@ Gets all the events filtered

```typescript
type EventFilter = {
fromBlock: string;
toBlock: string;
from_block: string;
to_block: string;
address: string;
keys: string[];
page_size: number;
page_number: number;
continuation_token?: string;
chunk_size: number;
};
```

Expand All @@ -313,7 +313,21 @@ type EventFilter = {
```typescript
{
events: StarknetEmittedEvent[];
page_number: number;
is_last_page: number;
continuation_token?: string;
}
```

continuation_token is a pointer to the last element of the delivered page, use this token in a subsequent query to obtain the next page

##### _StarknetEmittedEvent_

```typescript
type StarknetEmittedEvent = {
from_address: string;
keys: string[];
data: string[];
block_hash: string;
block_number: number;
transaction_hash: string;
};
```

0 comments on commit 7868bcc

Please sign in to comment.