diff --git a/src/coinbase/address.ts b/src/coinbase/address.ts index e8a30b50..db70de1a 100644 --- a/src/coinbase/address.ts +++ b/src/coinbase/address.ts @@ -1,22 +1,26 @@ +import { AxiosError } from "axios"; import { Address as AddressModel } from "../client"; import { InternalError } from "./errors"; import { FaucetTransaction } from "./faucet_transaction"; -import { AddressClient } from "./types"; +import { AddressAPIClient } from "./types"; /** * Class representing an Address in the Coinbase SDK. + * A representation of a blockchain Address, which is a user-controlled account on a Network. Addresses are used to + * send and receive Assets, and should be created using Wallet#create_address. Addresses require an + * Eth::Key to sign transaction data. */ export class Address { private model: AddressModel; - private client: AddressClient; + private client: AddressAPIClient; /** - * Creates an instance of Address. + * Initializes a new Address instance. * @param {AddressModel} model - The address model data. - * @param {AddressClient} client - The API client to interact with address-related endpoints. + * @param {AddressAPIClient} client - The API client to interact with address-related endpoints. * @throws {InternalError} If the model or client is empty. */ - constructor(model: AddressModel, client: AddressClient) { + constructor(model: AddressModel, client: AddressAPIClient) { if (!model) { throw new InternalError("Address model cannot be empty"); } @@ -29,6 +33,7 @@ export class Address { /** * Requests faucet funds for the address. + * note: only supported on testnet networks * @returns {Promise} The faucet transaction object. * @throws {InternalError} If the request does not return a transaction hash. * @throws {Error} If the request fails. @@ -41,12 +46,15 @@ export class Address { ); return new FaucetTransaction(response.data); } catch (e) { - throw new Error(`Failed to request faucet funds`); + if (e instanceof AxiosError) { + throw e; + } + throw new Error(`Failed to complete faucet request`); } } /** - * Gets the address ID. + * Returns the address ID. * @returns {string} The address ID. */ public getId(): string { @@ -54,7 +62,7 @@ export class Address { } /** - * Gets the network ID. + * Returns the network ID. * @returns {string} The network ID. */ public getNetworkId(): string { @@ -62,7 +70,7 @@ export class Address { } /** - * Gets the public key. + * Returns the public key. * @returns {string} The public key. */ public getPublicKey(): string { @@ -70,7 +78,7 @@ export class Address { } /** - * Gets the wallet ID. + * Returns the wallet ID. * @returns {string} The wallet ID. */ public getWalletId(): string { diff --git a/src/coinbase/faucet_transaction.ts b/src/coinbase/faucet_transaction.ts index a0da023b..877162b3 100644 --- a/src/coinbase/faucet_transaction.ts +++ b/src/coinbase/faucet_transaction.ts @@ -15,7 +15,7 @@ export class FaucetTransaction { * @throws {InternalError} If the model does not exist. */ constructor(model: FaucetTransactionModel) { - if (!model) { + if (!model?.transaction_hash) { throw new InternalError("FaucetTransaction model cannot be empty"); } this.model = model; diff --git a/src/coinbase/tests/address_test.ts b/src/coinbase/tests/address_test.ts index f4a987f7..dfa0a379 100644 --- a/src/coinbase/tests/address_test.ts +++ b/src/coinbase/tests/address_test.ts @@ -18,6 +18,10 @@ const VALID_ADDRESS_MODEL: AddressModel = { describe("Address", () => { const client = AddressesApiFactory(); + afterEach(() => { + axiosMock.reset(); + }); + it("should create an Address instance", () => { const address = new Address(VALID_ADDRESS_MODEL, client); expect(address).toBeInstanceOf(Address); @@ -44,11 +48,16 @@ describe("Address", () => { expect(faucetTransaction).toBeInstanceOf(FaucetTransaction); expect(faucetTransaction.getTransactionHash()).toBe("mocked_transaction_hash"); }); + it("should request faucet funds and return a FaucetTransactionaaa", async () => { + axiosMock.onPost().reply(200, {}); + const address = new Address(VALID_ADDRESS_MODEL, client); + await expect(address.faucet()).rejects.toThrow("Failed to complete faucet request"); + }); - it("should throw an error if faucet request fails", async () => { + it("should throw an AxiosError if faucet request fails", async () => { axiosMock.onPost().reply(400); const address = new Address(VALID_ADDRESS_MODEL, client); - await expect(address.faucet()).rejects.toThrow("Failed to request faucet funds"); + await expect(address.faucet()).rejects.toThrow("Request failed with status code 400"); }); it("should return the correct string representation", () => { diff --git a/src/coinbase/types.ts b/src/coinbase/types.ts index 46395f7c..af89d59b 100644 --- a/src/coinbase/types.ts +++ b/src/coinbase/types.ts @@ -4,7 +4,7 @@ import { User as UserModel } from "./../client/api"; /** * AddressAPI client type definition. */ -export type AddressClient = { +export type AddressAPIClient = { /** * Requests faucet funds for the address. * @param {string} walletId - The wallet ID.