Skip to content

Commit

Permalink
fix: rpc provider tests
Browse files Browse the repository at this point in the history
  • Loading branch information
badurinantun committed Jul 18, 2022
1 parent d89bf30 commit 12db930
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 91 deletions.
38 changes: 2 additions & 36 deletions __tests__/account.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { isBN } from 'bn.js';

import typedDataExample from '../__mocks__/typedDataExample.json';
import { Account, Contract, Provider, ec, number, stark } from '../src';
import { Account, Contract, Provider, number, stark } from '../src';
import { toBN } from '../src/utils/number';
import {
compiledErc20,
compiledOpenZeppelinAccount,
compiledTestDapp,
getTestAccount,
getTestProvider,
} from './fixtures';
import { compiledErc20, compiledTestDapp, getTestAccount, getTestProvider } from './fixtures';

describe('deploy and test Wallet', () => {
const provider = getTestProvider();
Expand Down Expand Up @@ -132,34 +126,6 @@ describe('deploy and test Wallet', () => {
expect(await account.verifyMessage(typedDataExample, signature)).toBe(true);
});

describe('new deployed account', () => {
let newAccount: Account;

beforeAll(async () => {
const starkKeyPair = ec.genKeyPair();
const starkKeyPub = ec.getStarkKey(starkKeyPair);

const accountResponse = await provider.deployContract({
contract: compiledOpenZeppelinAccount,
constructorCalldata: [starkKeyPub],
});

await provider.waitForTransaction(accountResponse.transaction_hash);

newAccount = new Account(provider, accountResponse.contract_address!, starkKeyPair);
});

test('read nonce', async () => {
const { result } = await account.callContract({
contractAddress: newAccount.address,
entrypoint: 'get_nonce',
});
const nonce = result[0];

expect(number.toBN(nonce).toString()).toStrictEqual(number.toBN(0).toString());
});
});

describe('Contract interaction with Account', () => {
const wallet = stark.randomAddress();

Expand Down
61 changes: 17 additions & 44 deletions __tests__/defaultProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from '../src';
import { toBN } from '../src/utils/number';
import {
IS_DEVNET,
compiledErc20,
compiledOpenZeppelinAccount,
describeIfNotDevnet,
Expand Down Expand Up @@ -51,9 +50,11 @@ describe('defaultProvider', () => {
test('getBlock(blockHash=undefined, blockNumber=null)', async () => {
expect(exampleBlock).not.toBeNull();

// pending block doesnt have a block number value
expect(exampleBlock).toHaveProperty('block_number');
expect(exampleBlock).toHaveProperty('accepted_time');
const { block_number, accepted_time } = exampleBlock;

expect(typeof block_number).toEqual('number');

return expect(typeof accepted_time).toEqual('number');
});

test('getBlock() -> { blockNumber }', async () => {
Expand All @@ -63,38 +64,20 @@ describe('defaultProvider', () => {

describe('getStorageAt', () => {
test('with "key" type of number', () => {
return expect(
testProvider.getStorageAt(
exampleContractAddress,
0,
'0x1b77403cfce4a31f6919cf6c64c5bca7f9bba841ec6492d2cda8cf4486a58e1'
)
).resolves.not.toThrow();
return expect(testProvider.getStorageAt(exampleContractAddress, 0)).resolves.not.toThrow();
});

test('"key" type of string', () => {
return expect(
testProvider.getStorageAt(
exampleContractAddress,
'0x0',
'0x1b77403cfce4a31f6919cf6c64c5bca7f9bba841ec6492d2cda8cf4486a58e1'
)
testProvider.getStorageAt(exampleContractAddress, '0x0')
).resolves.not.toThrow();
});

test('with "key" type of BN', () => {
return expect(
testProvider.getStorageAt(
exampleContractAddress,
toBN('0x0'),
'0x1b77403cfce4a31f6919cf6c64c5bca7f9bba841ec6492d2cda8cf4486a58e1'
)
testProvider.getStorageAt(exampleContractAddress, toBN('0x0'))
).resolves.not.toThrow();
});

test('(blockHash=undefined, blockNumber=null)', () => {
return expect(testProvider.getStorageAt(exampleContractAddress, 0)).resolves.not.toThrow();
});
});

test('getTransaction() - successful transaction', async () => {
Expand Down Expand Up @@ -122,25 +105,15 @@ describe('defaultProvider', () => {
});

test('callContract() - gateway error', async () => {
const promise = testProvider.callContract({
contractAddress: exampleContractAddress,
entrypoint: 'non_existent_entrypoint',
calldata: compileCalldata({
user: '0xdeadbeef',
}),
});
expect(promise).rejects.toHaveProperty('errorCode');
expect(promise).rejects.toThrowErrorMatchingInlineSnapshot(
`"Entry point 0x23b0c8b3d98aa73d8a35f5303fe77d132c6d04279e63f6e1d6aac5946e04612 not found in contract with class hash 0x2864c45bd4ba3e66d8f7855adcadf07205c88f43806ffca664f1f624765207e."`
);

try {
await promise;
} catch (e) {
expect(e.errorCode).toMatchInlineSnapshot(
IS_DEVNET ? `500` : `"StarknetErrorCode.ENTRY_POINT_NOT_FOUND_IN_CONTRACT"`
);
}
return expect(
testProvider.callContract({
contractAddress: exampleContractAddress,
entrypoint: 'non_existent_entrypoint',
calldata: compileCalldata({
user: '0xdeadbeef',
}),
})
).rejects.toThrowError();
});
});

Expand Down
8 changes: 4 additions & 4 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ export class Provider implements ProviderInterface {
return this.provider.chainId;
}

public async getBlock(blockIdentifier: BlockIdentifier = 'pending'): Promise<GetBlockResponse> {
public async getBlock(blockIdentifier: BlockIdentifier = 'latest'): Promise<GetBlockResponse> {
return this.provider.getBlock(blockIdentifier);
}

public async getClassAt(
contractAddress: string,
blockIdentifier: BlockIdentifier = 'pending'
blockIdentifier: BlockIdentifier = 'latest'
): Promise<any> {
return this.provider.getClassAt(contractAddress, blockIdentifier);
}
Expand All @@ -67,9 +67,9 @@ export class Provider implements ProviderInterface {
public async getStorageAt(
contractAddress: string,
key: BigNumberish,
blockIdentifier: BlockTag | BigNumberish = 'latest'
blockTagOrHash: BlockTag | BigNumberish = 'latest'
): Promise<BigNumberish> {
return this.provider.getStorageAt(contractAddress, key, blockIdentifier);
return this.provider.getStorageAt(contractAddress, key, blockTagOrHash);
}

public async getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse> {
Expand Down
8 changes: 4 additions & 4 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class RpcProvider implements ProviderInterface {
return this.fetchEndpoint('starknet_chainId');
}

public async getBlock(blockIdentifier: BlockIdentifier = 'pending'): Promise<GetBlockResponse> {
public async getBlock(blockIdentifier: BlockIdentifier = 'latest'): Promise<GetBlockResponse> {
const method =
typeof blockIdentifier === 'string' && isHex(blockIdentifier)
? 'starknet_getBlockByHash'
Expand All @@ -104,7 +104,7 @@ export class RpcProvider implements ProviderInterface {
public async getStorageAt(
contractAddress: string,
key: BigNumberish,
blockHashOrTag: BlockTag | BigNumberish = 'pending'
blockHashOrTag: BlockTag | BigNumberish = 'latest'
): Promise<BigNumberish> {
const parsedKey = toHex(toBN(key));
return this.fetchEndpoint('starknet_getStorageAt', [
Expand All @@ -128,7 +128,7 @@ export class RpcProvider implements ProviderInterface {

public async getClassAt(
contractAddress: string,
_blockIdentifier: BlockIdentifier = 'pending'
_blockIdentifier: BlockIdentifier = 'latest'
): Promise<any> {
return this.fetchEndpoint('starknet_getClassAt', [contractAddress]);
}
Expand Down Expand Up @@ -219,7 +219,7 @@ export class RpcProvider implements ProviderInterface {
let retries = 100;

while (!onchain) {
const successStates = ['ACCEPTED_ON_L1', 'ACCEPTED_ON_L2', 'PENDING'];
const successStates = ['ACCEPTED_ON_L1', 'ACCEPTED_ON_L2'];
const errorStates = ['REJECTED', 'NOT_RECEIVED'];

// eslint-disable-next-line no-await-in-loop
Expand Down
6 changes: 3 additions & 3 deletions src/provider/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export class SequencerProvider implements ProviderInterface {
).then(this.responseParser.parseCallContractResponse);
}

public async getBlock(blockIdentifier: BlockIdentifier = 'pending'): Promise<GetBlockResponse> {
public async getBlock(blockIdentifier: BlockIdentifier = 'latest'): Promise<GetBlockResponse> {
return this.fetchEndpoint('get_block', { blockIdentifier }).then(
this.responseParser.parseGetBlockResponse
);
Expand Down Expand Up @@ -253,7 +253,7 @@ export class SequencerProvider implements ProviderInterface {

public async getClassAt(
contractAddress: string,
blockIdentifier: BlockIdentifier = 'pending'
blockIdentifier: BlockIdentifier = 'latest'
): Promise<ContractClass> {
return this.fetchEndpoint('get_full_contract', { blockIdentifier, contractAddress }).then(
parseContract
Expand Down Expand Up @@ -306,7 +306,7 @@ export class SequencerProvider implements ProviderInterface {

public async getEstimateFee(
invocation: Invocation,
blockIdentifier: BlockIdentifier = 'pending',
blockIdentifier: BlockIdentifier = 'latest',
invocationDetails: InvocationsDetails = {}
): Promise<EstimateFeeResponse> {
return this.fetchEndpoint(
Expand Down

0 comments on commit 12db930

Please sign in to comment.