Skip to content

Commit

Permalink
feat(provider): add getTransactionReceipt()
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkelawala committed Feb 4, 2022
1 parent abfb012 commit 4267a5f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 6 deletions.
9 changes: 9 additions & 0 deletions __tests__/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ describe('defaultProvider', () => {
)
).resolves.not.toThrow();
});

test.only('getTransactionReceipt', async () => {
return expect(
defaultProvider.getTransactionReceipt(
'0x7906b9cfe400e11dd1f977a128f73995029e67c2135cdef1552622e636ff499'
)
).resolves.not.toThrow();
});

test('callContract()', () => {
return expect(
defaultProvider.callContract({
Expand Down
24 changes: 23 additions & 1 deletion src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import {
GetTransactionStatusResponse,
Signature,
Transaction,
TransactionReceipt,
} from '../types';
import { parse, stringify } from '../utils/json';
import { BigNumberish, toBN, toHex } from '../utils/number';
import { compressProgram, formatSignature, randomAddress } from '../utils/stark';
import { ProviderInterface } from './interface';
import { getFormattedBlockIdentifier } from './utils';
import { getFormattedBlockIdentifier, txIdentifier } from './utils';

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

Expand Down Expand Up @@ -223,6 +224,27 @@ export class Provider implements ProviderInterface {
return data;
}

/**
* Gets the transaction receipt from a tx hash or tx id.
*
* [Reference] (https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L104-L111)
*
* @param txHash
* @param txId
* @returns the transaction receipt object
*/

public async getTransactionReceipt(
txHash?: BigNumberish,
txId?: BigNumberish
): Promise<TransactionReceipt> {
const { data } = await axios.get<TransactionReceipt>(
urljoin(this.feederGatewayUrl, 'get_transaction_receipt', `?${txIdentifier(txHash, txId)}`)
);

return data;
}

/**
* Invoke a function on the starknet contract
*
Expand Down
6 changes: 6 additions & 0 deletions src/provider/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
GetTransactionStatusResponse,
Signature,
Transaction,
TransactionReceipt,
} from '../types';
import type { BigNumberish } from '../utils/number';

Expand Down Expand Up @@ -114,6 +115,11 @@ export abstract class ProviderInterface {
*/
public abstract getTransaction(txHash: BigNumberish): Promise<GetTransactionResponse>;

public abstract getTransactionReceipt(
txHash?: BigNumberish,
txId?: BigNumberish
): Promise<TransactionReceipt>;

/**
* Invoke a function on the starknet contract
*
Expand Down
20 changes: 15 additions & 5 deletions src/provider/utils.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
import type { BlockNumber } from '../types';
import { BigNumberish } from '../utils/number';
import { BigNumberish, toBN, toHex } from '../utils/number';

/**
* TODO
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L148-L153)
*
* @param hashValue
* @param hashField
*/
export function formatHash() {}
export function formatHash(hashValue: BigNumberish): string {
if (typeof hashValue === 'string') return hashValue;
return toHex(toBN(hashValue));
}

/**
* TODO
*
* [Reference](https://github.com/starkware-libs/cairo-lang/blob/fc97bdd8322a7df043c87c371634b26c15ed6cee/src/starkware/starknet/services/api/feeder_gateway/feeder_gateway_client.py#L156-L161)
* @param txHash
* @param txId
*/
export function txIdentifier() {}
export function txIdentifier(txHash?: BigNumberish, txId?: BigNumberish): string {
if (!txHash) {
return `transactionId=${JSON.stringify(txId)}`;
}
const hashString = formatHash(txHash);

return `transactionHash=${hashString}`;
}

/**
* Gets the block identifier for API request
Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,13 @@ export type AddTransactionResponse = {
transaction_hash: string;
address?: string;
};

export type TransactionReceipt = {
status: Status;
transaction_hash: string;
transaction_index: number;
block_hash: string;
block_number: string;
l2_to_l1_messages: string[];
events: string[];
};

0 comments on commit 4267a5f

Please sign in to comment.