diff --git a/src/coinbase/faucet_transaction.ts b/src/coinbase/faucet_transaction.ts new file mode 100644 index 00000000..a0da023b --- /dev/null +++ b/src/coinbase/faucet_transaction.ts @@ -0,0 +1,48 @@ +import { FaucetTransaction as FaucetTransactionModel } from "../client"; +import { InternalError } from "./errors"; + +/** + * Represents a transaction from a faucet. + */ +export class FaucetTransaction { + private model: FaucetTransactionModel; + + /** + * Creates a new FaucetTransaction instance. + * Do not use this method directly - instead, use Address.faucet(). + * @constructor + * @param {FaucetTransactionModel} model - The FaucetTransaction model. + * @throws {InternalError} If the model does not exist. + */ + constructor(model: FaucetTransactionModel) { + if (!model) { + throw new InternalError("FaucetTransaction model cannot be empty"); + } + this.model = model; + } + + /** + * Returns the transaction hash. + * @returns {string} The transaction hash. + */ + public getTransactionHash(): string { + return this.model.transaction_hash; + } + + /** + * Returns the link to the transaction on the blockchain explorer. + * @returns {string} The link to the transaction on the blockchain explorer + */ + public getTransactionLink(): string { + // TODO: Parameterize this by Network. + return `https://sepolia.basescan.org/tx/${this.getTransactionHash()}`; + } + + /** + * Returns a string representation of the FaucetTransaction. + * @returns {string} A string representation of the FaucetTransaction. + */ + public toString(): string { + return `Coinbase::FaucetTransaction{transaction_hash: '${this.getTransactionHash()}', transaction_link: '${this.getTransactionLink()}'}`; + } +} diff --git a/src/coinbase/tests/faucetTransaction_test.ts b/src/coinbase/tests/faucetTransaction_test.ts new file mode 100644 index 00000000..0dc9ad64 --- /dev/null +++ b/src/coinbase/tests/faucetTransaction_test.ts @@ -0,0 +1,21 @@ +import { InternalError } from "./../errors"; +import { FaucetTransaction } from "../faucet_transaction"; + +describe("FaucetTransaction tests", () => { + it("should create a new FaucetTransaction instance and return the transaction hash", () => { + const faucetTransaction = new FaucetTransaction({ + transaction_hash: "abc", + }); + + expect(faucetTransaction).toBeInstanceOf(FaucetTransaction); + expect(faucetTransaction.getTransactionHash()).toBe("abc"); + expect(faucetTransaction.getTransactionLink()).toBe("https://sepolia.basescan.org/tx/abc"); + expect(faucetTransaction.toString()).toBe( + "Coinbase::FaucetTransaction{transaction_hash: 'abc', transaction_link: 'https://sepolia.basescan.org/tx/abc'}", + ); + }); + + it("should throw an InternalError if model is not provided", () => { + expect(() => new FaucetTransaction(null!)).toThrow(`FaucetTransaction model cannot be empty`); + }); +});