diff --git a/__tests__/fixtures.ts b/__tests__/fixtures.ts index 74fa9daf2..295f18be8 100644 --- a/__tests__/fixtures.ts +++ b/__tests__/fixtures.ts @@ -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) => { @@ -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); diff --git a/__tests__/provider.test.ts b/__tests__/provider.test.ts index 2c11efd30..a28f35898 100644 --- a/__tests__/provider.test.ts +++ b/__tests__/provider.test.ts @@ -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; @@ -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'); @@ -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"` + ); } }); diff --git a/src/provider/default.ts b/src/provider/default.ts index df3eab6c9..4a1c0d870 100644 --- a/src/provider/default.ts +++ b/src/provider/default.ts @@ -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') { diff --git a/www/docs/API/utils.md b/www/docs/API/utils.md index 1643d02aa..963b0120d 100644 --- a/www/docs/API/utils.md +++ b/www/docs/API/utils.md @@ -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` @@ -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. @@ -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); { @@ -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`. @@ -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. \ No newline at end of file +Function that calculates contract address from hash. Returns a string.