Skip to content

Commit

Permalink
feat: Adding FaucetTransaction class (#9)
Browse files Browse the repository at this point in the history
* feat: Adding FaucetTransaction class
  • Loading branch information
erdimaden authored May 14, 2024
1 parent 5b50e78 commit 3efbe1e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/coinbase/faucet_transaction.ts
Original file line number Diff line number Diff line change
@@ -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()}'}`;
}
}
21 changes: 21 additions & 0 deletions src/coinbase/tests/faucetTransaction_test.ts
Original file line number Diff line number Diff line change
@@ -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`);
});
});

0 comments on commit 3efbe1e

Please sign in to comment.