Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adding FaucetTransaction class #9

Merged
merged 3 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`);
});
});
Loading