-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adding FaucetTransaction class (#9)
* feat: Adding FaucetTransaction class
- Loading branch information
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}'}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | ||
}); | ||
}); |