Skip to content

Commit

Permalink
create mock transaction sender
Browse files Browse the repository at this point in the history
  • Loading branch information
boudra committed Jan 11, 2024
1 parent 8202486 commit 641c4c4
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions packages/data-layer/src/allo/transaction-sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,44 @@ export function createViemTransactionSender(
};
}

/**
* @dev This is a mock transaction sender that does not actually send transactions. It is useful for testing.
*
* @example
* const sender = createInMemoryTransactionSender();
* const txHash = await sender.send({
* to: "0x1234",
* data: "0x1234",
* value: 0n,
* });
* const receipt = await sender.wait(txHash);
* console.log(receipt);
* // { transactionHash: '0x...', blockHash: '0x...', blockNumber: 1n }
*/
export function createMockTransactionSender(): TransactionSender & {
transactions: Record<Hex, TransactionReceipt>;
} {
const transactions: Record<Hex, TransactionReceipt> = {};

return {
transactions,

async send(_tx: TransactionData): Promise<Hex> {
const txHash = `0x${Math.random().toString(16).slice(2)}` as Hex;
transactions[txHash] = {
transactionHash: txHash,
blockHash: `0x${Math.random().toString(16).slice(2)}` as Hex,
blockNumber: 1n,
};
return txHash;
},

async wait(txHash: Hex): Promise<TransactionReceipt> {
return transactions[txHash];
},
};
}

export async function sendTransaction(
sender: TransactionSender,
args: Parameters<typeof encodeFunctionData>[0] & { address: Address },
Expand Down

0 comments on commit 641c4c4

Please sign in to comment.