Skip to content

Commit fd0c9c6

Browse files
authored
Merge pull request #187 from 0xs34n/bugfix/handle-bignumber-fetch-response
2 parents 5a40cd6 + f535edb commit fd0c9c6

File tree

5 files changed

+36
-29
lines changed

5 files changed

+36
-29
lines changed

__tests__/account.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isBN } from 'bn.js';
2+
13
import typedDataExample from '../__mocks__/typedDataExample.json';
24
import { Account, Contract, defaultProvider, ec, number, stark } from '../src';
35
import { toBN } from '../src/utils/number';
@@ -50,7 +52,7 @@ describe('deploy and test Wallet', () => {
5052
entrypoint: 'transfer',
5153
calldata: [erc20.address, '10'],
5254
});
53-
expect(typeof amount).toBe('number');
55+
expect(isBN(amount)).toBe(true);
5456
expect(typeof unit).toBe('string');
5557
});
5658

__tests__/provider.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ describe('defaultProvider', () => {
7676

7777
test('getTransactionReceipt', async () => {
7878
return expect(
79-
defaultProvider.getTransactionReceipt({
80-
txHash: '0x37013e1cb9c133e6fe51b4b371b76b317a480f56d80576730754c1662582348',
81-
})
79+
defaultProvider.getTransactionReceipt(
80+
'0x37013e1cb9c133e6fe51b4b371b76b317a480f56d80576730754c1662582348'
81+
)
8282
).resolves.not.toThrow();
8383
});
8484

src/provider/default.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@ import {
1010
CompiledContract,
1111
DeployContractPayload,
1212
Endpoints,
13+
EstimateFeeResponse,
1314
GetBlockResponse,
1415
GetCodeResponse,
1516
GetContractAddressesResponse,
1617
GetTransactionResponse,
1718
GetTransactionStatusResponse,
1819
GetTransactionTraceResponse,
1920
Invocation,
20-
TransactionReceipt,
21+
TransactionReceiptResponse,
2122
} from '../types';
2223
import { getSelectorFromName } from '../utils/hash';
2324
import { parse, stringify } from '../utils/json';
2425
import { BigNumberish, bigNumberishArrayToDecimalStringArray, toBN, toHex } from '../utils/number';
2526
import { compressProgram, randomAddress } from '../utils/stark';
2627
import { ProviderInterface } from './interface';
27-
import { BlockIdentifier, getFormattedBlockIdentifier, txIdentifier } from './utils';
28+
import { BlockIdentifier, getFormattedBlockIdentifier } from './utils';
2829

2930
type NetworkName = 'mainnet-alpha' | 'goerli-alpha';
3031

@@ -153,6 +154,17 @@ export class Provider implements ProviderInterface {
153154
try {
154155
const { data } = await axios.request<Endpoints[T]['RESPONSE']>({
155156
method,
157+
transformResponse:
158+
endpoint === 'estimate_fee'
159+
? (res): EstimateFeeResponse => {
160+
return parse(res, (_, v) => {
161+
if (v && typeof v === 'bigint') {
162+
return toBN(v.toString());
163+
}
164+
return v;
165+
});
166+
}
167+
: axios.defaults.transformResponse,
156168
url: urljoin(baseUrl, endpoint, queryString),
157169
data: stringify(request),
158170
headers,
@@ -272,22 +284,12 @@ export class Provider implements ProviderInterface {
272284
* [Reference] (https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L104-L111)
273285
*
274286
* @param txHash
275-
* @param txId
276287
* @returns the transaction receipt object
277288
*/
278289

279-
public async getTransactionReceipt({
280-
txHash,
281-
txId,
282-
}: {
283-
txHash?: BigNumberish;
284-
txId?: BigNumberish;
285-
}): Promise<TransactionReceipt> {
286-
const { data } = await axios.get<TransactionReceipt>(
287-
urljoin(this.feederGatewayUrl, 'get_transaction_receipt', `?${txIdentifier(txHash, txId)}`)
288-
);
289-
290-
return data;
290+
public async getTransactionReceipt(txHash: BigNumberish): Promise<TransactionReceiptResponse> {
291+
const txHashHex = toHex(toBN(txHash));
292+
return this.fetchEndpoint('get_transaction_receipt', { transactionHash: txHashHex });
291293
}
292294

293295
/**

src/provider/interface.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {
1010
GetTransactionResponse,
1111
GetTransactionStatusResponse,
1212
Invocation,
13-
TransactionReceipt,
13+
TransactionReceiptResponse,
1414
} from '../types';
1515
import type { BigNumberish } from '../utils/number';
1616
import { BlockIdentifier } from './utils';
@@ -109,13 +109,7 @@ export abstract class ProviderInterface {
109109
*/
110110
public abstract getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse>;
111111

112-
public abstract getTransactionReceipt({
113-
txHash,
114-
txId,
115-
}: {
116-
txHash?: BigNumberish;
117-
txId?: BigNumberish;
118-
}): Promise<TransactionReceipt>;
112+
public abstract getTransactionReceipt(txHash: BigNumberish): Promise<TransactionReceiptResponse>;
119113

120114
/**
121115
* Deploys a given compiled contract (json) to starknet

src/types/api.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import BN from 'bn.js';
2+
13
import { BlockIdentifier } from '../provider/utils';
24
import { BigNumberish } from '../utils/number';
35
import {
@@ -43,6 +45,13 @@ export type Endpoints = {
4345
REQUEST: never;
4446
RESPONSE: GetTransactionTraceResponse;
4547
};
48+
get_transaction_receipt: {
49+
QUERY: {
50+
transactionHash: string;
51+
};
52+
REQUEST: never;
53+
RESPONSE: TransactionReceiptResponse;
54+
};
4655
get_storage_at: {
4756
QUERY: {
4857
contractAddress: string;
@@ -217,7 +226,7 @@ export type AddTransactionResponse = {
217226
address?: string;
218227
};
219228

220-
export type TransactionReceipt = {
229+
export type TransactionReceiptResponse = {
221230
status: Status;
222231
transaction_hash: string;
223232
transaction_index: number;
@@ -228,7 +237,7 @@ export type TransactionReceipt = {
228237
};
229238

230239
export type EstimateFeeResponse = {
231-
amount: number;
240+
amount: BN;
232241
unit: string;
233242
};
234243

0 commit comments

Comments
 (0)