Skip to content

Commit

Permalink
fix: repair and unify transaction retrieval error
Browse files Browse the repository at this point in the history
  • Loading branch information
penovicp committed Jan 11, 2023
1 parent b7dc46c commit ea5e646
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
16 changes: 13 additions & 3 deletions __tests__/defaultProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { BlockNumber, GetBlockResponse, Provider, stark } from '../src';
import { BlockNumber, GetBlockResponse, LibraryError, Provider, stark } from '../src';
import { toBN } from '../src/utils/number';
import { encodeShortString } from '../src/utils/shortString';
import { compiledErc20, erc20ClassHash, getTestAccount, getTestProvider } from './fixtures';
import {
compiledErc20,
erc20ClassHash,
getTestAccount,
getTestProvider,
wrongClassHash,
} from './fixtures';

const { compileCalldata } = stark;

Expand All @@ -21,7 +27,7 @@ describe('defaultProvider', () => {

const { deploy } = await account.declareDeploy({
contract: compiledErc20,
classHash: '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a',
classHash: erc20ClassHash,
constructorCalldata: [encodeShortString('Token'), encodeShortString('ERC20'), wallet],
});

Expand Down Expand Up @@ -117,6 +123,10 @@ describe('defaultProvider', () => {
});
});

test('getTransaction() - failed retrieval', () => {
return expect(testProvider.getTransaction(wrongClassHash)).rejects.toThrow(LibraryError);
});

test('getTransaction() - successful deploy transaction', async () => {
const transaction = await testProvider.getTransaction(exampleTransactionHash);

Expand Down
1 change: 1 addition & 0 deletions __tests__/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,4 @@ export const describeIfDevnetRpc = describeIf(IS_DEVNET_RPC);
export const describeIfDevnetSequencer = describeIf(IS_DEVNET_SEQUENCER);

export const erc20ClassHash = '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a';
export const wrongClassHash = '0x000000000000000000000000000000000000000000000000000000000000000';
3 changes: 2 additions & 1 deletion src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from '../utils/number';
import { parseCalldata, wait } from '../utils/provider';
import { RPCResponseParser } from '../utils/responseParser/rpc';
import { LibraryError } from './errors';
import { ProviderInterface } from './interface';
import { Block, BlockIdentifier } from './utils';

Expand Down Expand Up @@ -82,7 +83,7 @@ export class RpcProvider implements ProviderInterface {
protected errorHandler(error: any) {
if (error) {
const { code, message } = error;
throw new Error(`${code}: ${message}`);
throw new LibraryError(`${code}: ${message}`);
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/provider/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { parseContract, wait } from '../utils/provider';
import { SequencerAPIResponseParser } from '../utils/responseParser/sequencer';
import { randomAddress } from '../utils/stark';
import { buildUrl } from '../utils/url';
import { GatewayError, HttpError } from './errors';
import { GatewayError, HttpError, LibraryError } from './errors';
import { ProviderInterface } from './interface';
import { Block, BlockIdentifier } from './utils';

Expand Down Expand Up @@ -296,9 +296,11 @@ export class SequencerProvider implements ProviderInterface {

public async getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse> {
const txHashHex = toHex(toBN(txHash));
return this.fetchEndpoint('get_transaction', { transactionHash: txHashHex }).then((value) =>
this.responseParser.parseGetTransactionResponse(value)
);
return this.fetchEndpoint('get_transaction', { transactionHash: txHashHex }).then((result) => {
// throw for no matching transaction to unify behavior with RPC and avoid parsing errors
if (Object.values(result).length === 1) throw new LibraryError(result.status);
return this.responseParser.parseGetTransactionResponse(result);
});
}

public async getTransactionReceipt(txHash: BigNumberish): Promise<GetTransactionReceiptResponse> {
Expand Down

0 comments on commit ea5e646

Please sign in to comment.