Skip to content

Commit

Permalink
test: gateway error
Browse files Browse the repository at this point in the history
  • Loading branch information
janek26 committed Jun 20, 2022
1 parent 405402a commit c3f3f28
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
13 changes: 9 additions & 4 deletions __tests__/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ const DEFAULT_TEST_ACCOUNT_ADDRESS = // run `starknet-devnet --seed 0` and this
'0x65d53c8ec4178096167b35a08e16e548d8075cb08ad7bc63d07966ca13569dc';
const DEFAULT_TEST_ACCOUNT_PRIVATE_KEY = '0xe3e70682c2094cac629f6fbed82c07cd';

export const getTestProvider = () => {
const baseUrl = process.env.TEST_PROVIDER_BASE_URL || DEFAULT_TEST_PROVIDER_BASE_URL;
const BASE_URL = process.env.TEST_PROVIDER_BASE_URL || DEFAULT_TEST_PROVIDER_BASE_URL;
export const IS_DEVNET = !BASE_URL.includes('starknet.io');

const provider = new Provider({ baseUrl });
export const getTestProvider = () => {
const provider = new Provider({ baseUrl: BASE_URL });

if (baseUrl.includes('localhost') || baseUrl.includes('127.0.0.1')) {
if (IS_DEVNET) {
// accelerate the tests when running locally
const originalWaitForTransaction = provider.waitForTransaction.bind(provider);
provider.waitForTransaction = (txHash, retryInterval) => {
Expand All @@ -43,3 +44,7 @@ export const getTestAccount = () => {

return new Account(provider, testAccountAddress, ec.getKeyPair(testAccountPrivateKey));
};

export const testIf = (condition: boolean) => (condition ? test : test.skip);
export const testIfDevnet = testIf(IS_DEVNET);
export const testIfNotDevnet = testIf(!IS_DEVNET);
37 changes: 24 additions & 13 deletions __tests__/provider.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { BlockNumber, stark } from '../src';
import { toBN } from '../src/utils/number';
import { compiledErc20, compiledOpenZeppelinAccount, getTestProvider } from './fixtures';
import {
IS_DEVNET,
compiledErc20,
compiledOpenZeppelinAccount,
getTestProvider,
testIfNotDevnet,
} from './fixtures';

const { compileCalldata } = stark;

const provider = getTestProvider();

const testIf = (condition: boolean) => (condition ? test : test.skip);

describe('defaultProvider', () => {
let exampleTransactionHash: string;
let exampleContractAddress: string;
Expand All @@ -30,7 +34,7 @@ describe('defaultProvider', () => {
});

describe('feeder gateway endpoints', () => {
testIf(provider.baseUrl.includes('starknet.io'))('getContractAddresses()', async () => {
testIfNotDevnet('getContractAddresses()', async () => {
// not supported in starknet-devnet
const { GpsStatementVerifier, Starknet } = await provider.getContractAddresses();
expect(typeof GpsStatementVerifier).toBe('string');
Expand Down Expand Up @@ -104,17 +108,24 @@ describe('defaultProvider', () => {
});

test('callContract() - gateway error', async () => {
const promise = provider.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 provider.callContract({
contractAddress: exampleContractAddress,
entrypoint: 'non_existent_entrypoint',
calldata: compileCalldata({
user: '0xdeadbeef',
}),
});
await promise;
} catch (e) {
expect(e).toHaveProperty('message');
expect(e).toHaveProperty('errorCode');
expect(e.errorCode).toMatchInlineSnapshot(
IS_DEVNET ? `500n` : `"StarknetErrorCode.ENTRY_POINT_NOT_FOUND_IN_CONTRACT"`
);
}
});

Expand Down
4 changes: 3 additions & 1 deletion src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ export class Provider implements ProviderInterface {
if (!res.ok) {
// This will allow user to handle contract errors
const responseBody = parse(textResponse);
throw new GatewayError(responseBody.message, responseBody.code); // Caught locally, and re-thrown for the user

const errorCode = responseBody.code || ((responseBody as any)?.status_code as string); // starknet-devnet uses status_code instead of code; They need to fix that
throw new GatewayError(responseBody.message, errorCode); // Caught locally, and re-thrown for the user
}

if (endpoint === 'estimate_fee') {
Expand Down
10 changes: 5 additions & 5 deletions www/docs/API/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Function that generates a random contract address.

### `makeAddress(input: string): string`

Function that turns an incompatible address string into stark address format. Returns a string.
Function that turns an incompatible address string into stark address format. Returns a string.

Example: `0xdFD0F27FCe99b50909de0bDD328Aed6eAbe76BC5` -> `0xdfd0f27fce99b50909de0bdd328aed6eabe76bc5`

Expand Down Expand Up @@ -90,7 +90,7 @@ await this.callContract({

### `estimatedFeeToMaxFee(estimatedFee: BigNumberish, overhead: number = 0.5): BN`

Function that calculates and returns maximum fee based on the previously estimated one.
Function that calculates and returns maximum fee based on the previously estimated one.

Returns a BN.

Expand Down Expand Up @@ -150,7 +150,7 @@ Asserts input is equal to or greater then `lowerBound` and lower then `upperBoun

Convert BigNumberish array to decimal array. Used for signature conversion.

``` js
```js
const signature = await this.signer.signTransaction(transactions, signerDetails);

{
Expand Down Expand Up @@ -229,7 +229,7 @@ Function to compute a Pedersen hash on a array of elements. Returns a string.

### `calculateTransactionHashCommon(txHashPrefix: TransactionHashPrefix, version: BigNumberish,contractAddress: BigNumberish, entryPointSelector: BigNumberish, calldata: BigNumberish[], maxFee: BigNumberish, chainId: StarknetChainId, additionalData: BigNumberish[] = []): string`

Calculates the transaction hash in the StarkNet network - a unique identifier of the transaction.
Calculates the transaction hash in the StarkNet network - a unique identifier of the transaction.

Called internally in `calculateDeployTransactionHash` and `calculcateTransactionHash`.

Expand Down Expand Up @@ -262,4 +262,4 @@ const hashMsg = calculcateTransactionHash(

### `calculateContractAddressFromHash(salt: BigNumberish, classHash: BigNumberish, constructorCalldata: RawCalldata, deployerAddress: BigNumberish)`

Function that calculates contract address from hash. Returns a string.
Function that calculates contract address from hash. Returns a string.

0 comments on commit c3f3f28

Please sign in to comment.