Skip to content

Commit

Permalink
add test on receipt with new version #100
Browse files Browse the repository at this point in the history
  • Loading branch information
KlonD90 committed Jul 18, 2024
1 parent 6945f6c commit a8891c5
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 10 deletions.
27 changes: 24 additions & 3 deletions src/clients/PublicClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { type Hex, assertIsValidShardId } from "../index.js";
import type { IAddress } from "../signers/types/IAddress.js";
import type { Block, BlockTag } from "../types/Block.js";
import type { CallArgs } from "../types/CallArgs.js";
import type { IReceipt } from "../types/IReceipt.js";
import type { IReceipt, ProcessedReceipt } from "../types/IReceipt.js";
import type { ProcessedMessage } from "../types/ProcessedMessage.js";
import type { RPCMessage } from "../types/RPCMessage.js";
import { addHexPrefix } from "../utils/hex.js";
Expand Down Expand Up @@ -295,8 +295,25 @@ class PublicClient extends BaseClient {
*
* const receipt = await client.getMessageReceiptByHash(1, Uint8Array.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]));
*/
public async getMessageReceiptByHash(hash: Hex, shardId = this.shardId) {
public async getMessageReceiptByHash(
hash: Hex,
shardId = this.shardId,
): Promise<ProcessedReceipt | null> {
assertIsValidShardId(shardId);
const mapReceipt = (receipt: IReceipt): ProcessedReceipt => {
return {
...receipt,
gasUsed: BigInt(receipt.gasUsed),
gasPrice: BigInt(receipt.gasPrice),
outputReceipts:
receipt.outputReceipts?.map((x) => {
if (x === null) {
return null;
}
return mapReceipt(x);
}) ?? null,
};
};

const res = await this.request<IReceipt | null>({
method: "eth_getInMessageReceipt",
Expand All @@ -308,7 +325,11 @@ class PublicClient extends BaseClient {
],
});

return res;
if (res === null) {
return null;
}

return mapReceipt(res);
}

/**
Expand Down
53 changes: 53 additions & 0 deletions src/integrations/receipt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { testEnv } from "../../test/testEnv.js";
import { Faucet, WalletV1 } from "../contracts/index.js";
import {
HttpTransport,
PublicClient,
convertEthToWei,
waitTillCompleted,
} from "../index.js";
import { LocalECDSAKeySigner } from "../signers/LocalECDSAKeySigner.js";
import { generateRandomPrivateKey } from "../signers/privateKey.js";

const client = new PublicClient({
transport: new HttpTransport({
endpoint: testEnv.endpoint,
}),
shardId: 1,
});

test("Receipt test", async ({ expect }) => {
const faucet = new Faucet(client);

const signer = new LocalECDSAKeySigner({
privateKey: generateRandomPrivateKey(),
});

const pubkey = await signer.getPublicKey();

const wallet = new WalletV1({
pubkey: pubkey,
salt: 100n,
shardId: 1,
client,
signer,
});
const walletAddress = await wallet.getAddressHex();

expect(walletAddress).toBeDefined();

const faucetHash = await faucet.withdrawToWithRetry(
walletAddress,
convertEthToWei(0.1),
);

const receipts = await waitTillCompleted(client, 1, faucetHash);

expect(receipts).toBeDefined();
for (const receipt of receipts) {
expect(receipt).toBeDefined();
expect(receipt.gasPrice).toBeDefined();
expect(receipt.gasUsed).toBeDefined();
expect(receipt.gasPrice).toBeTypeOf("bigint");
}
});
20 changes: 15 additions & 5 deletions src/types/IReceipt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,27 @@ import type { ILog } from "./ILog.js";
*/
type IReceipt = {
success: boolean;
gasUsed: number;
gasUsed: string;
gasPrice: string;
bloom: string;
logs: ILog[];
messageHash: Hex;
contractAddress: string;
blockHash: string;
blockNumber: bigint;
msgIndex: bigint;
blockNumber: number;
msgIndex: number;
outMessages: Hex[] | null;
outputReceipts: IReceipt[] | null;
outputReceipts: (IReceipt | null)[] | null;
shardId: number;
};

export type { IReceipt };
type ProcessedReceipt = Omit<
IReceipt,
"gasUsed" | "gasPrice" | "outputReceipts"
> & {
gasUsed: bigint;
gasPrice: bigint;
outputReceipts: (ProcessedReceipt | null)[] | null;
};

export type { IReceipt, ProcessedReceipt };
4 changes: 2 additions & 2 deletions src/utils/receipt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { PublicClient } from "../clients/PublicClient.js";
import type { Hex } from "../types/Hex.js";
import type { IReceipt } from "../types/IReceipt.js";
import type { ProcessedReceipt } from "../types/IReceipt.js";

/**
* Makes it so that the client waits until the processing of the message whose hash is passed.
Expand All @@ -18,7 +18,7 @@ export const waitTillCompleted = async (
shardId: number,
hash: Hex,
) => {
const receipts: IReceipt[] = [];
const receipts: ProcessedReceipt[] = [];
const hashes: [number, Hex][] = [[shardId, hash]];
let cur = 0;
while (cur !== hashes.length) {
Expand Down

0 comments on commit a8891c5

Please sign in to comment.