-
Notifications
You must be signed in to change notification settings - Fork 42
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 Address Class with tests #10
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fc1202e
Adding Address Class with tests and updating faucet_transaction test …
erdimaden 19563da
Updating JSDocs
erdimaden d52fc66
Updating error type in types file
erdimaden 4cc5c7b
Updating Address JSDoc
erdimaden d7af21d
Updating mock Address Model object
erdimaden 6598071
Updating mock value
erdimaden 7cd9c7d
- Adding APIErrors Class to handle HTTP errors globally, - Creating c…
erdimaden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,84 @@ | ||
import { Address as AddressModel } from "../client"; | ||
import { InternalError } from "./errors"; | ||
import { FaucetTransaction } from "./faucet_transaction"; | ||
import { AddressAPIClient } from "./types"; | ||
|
||
/** | ||
* A representation of a blockchain address, which is a user-controlled account on a network. | ||
*/ | ||
export class Address { | ||
private model: AddressModel; | ||
private client: AddressAPIClient; | ||
|
||
/** | ||
* Initializes a new Address instance. | ||
* @param {AddressModel} model - The address model data. | ||
* @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: AddressAPIClient) { | ||
if (!model) { | ||
throw new InternalError("Address model cannot be empty"); | ||
} | ||
if (!client) { | ||
throw new InternalError("Address client cannot be empty"); | ||
} | ||
this.model = model; | ||
this.client = client; | ||
} | ||
|
||
/** | ||
* Requests faucet funds for the address. | ||
* Only supported on testnet networks. | ||
* @returns {Promise<FaucetTransaction>} The faucet transaction object. | ||
* @throws {InternalError} If the request does not return a transaction hash. | ||
* @throws {Error} If the request fails. | ||
*/ | ||
async faucet(): Promise<FaucetTransaction> { | ||
const response = await this.client.requestFaucetFunds( | ||
this.model.wallet_id, | ||
this.model.address_id, | ||
); | ||
return new FaucetTransaction(response.data); | ||
} | ||
|
||
/** | ||
* Returns the address ID. | ||
* @returns {string} The address ID. | ||
*/ | ||
public getId(): string { | ||
return this.model.address_id; | ||
} | ||
|
||
/** | ||
* Returns the network ID. | ||
* @returns {string} The network ID. | ||
*/ | ||
public getNetworkId(): string { | ||
return this.model.network_id; | ||
} | ||
|
||
/** | ||
* Returns the public key. | ||
* @returns {string} The public key. | ||
*/ | ||
public getPublicKey(): string { | ||
return this.model.public_key; | ||
} | ||
|
||
/** | ||
* Returns the wallet ID. | ||
* @returns {string} The wallet ID. | ||
*/ | ||
public getWalletId(): string { | ||
return this.model.wallet_id; | ||
} | ||
|
||
/** | ||
* Returns a string representation of the address. | ||
* @returns {string} A string representing the address. | ||
*/ | ||
public toString(): string { | ||
return `Coinbase:Address{addressId: '${this.model.address_id}', networkId: '${this.model.network_id}', walletId: '${this.model.wallet_id}'}`; | ||
} | ||
} |
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,130 @@ | ||
import { AxiosError } from "axios"; | ||
import { InternalError } from "./errors"; | ||
|
||
/** | ||
* The API error response type. | ||
*/ | ||
type APIErrorResponseType = { | ||
code: string; | ||
message: string; | ||
}; | ||
|
||
/** | ||
* A wrapper for API errors to provide more context. | ||
*/ | ||
export class APIError extends AxiosError { | ||
httpCode: number | null; | ||
apiCode: string | null; | ||
apiMessage: string | null; | ||
|
||
/** | ||
* Initializes a new APIError object. | ||
* @constructor | ||
* @param {AxiosError} error - The Axios error. | ||
*/ | ||
constructor(error) { | ||
super(); | ||
this.name = this.constructor.name; | ||
this.httpCode = error.response ? error.response.status : null; | ||
this.apiCode = null; | ||
this.apiMessage = null; | ||
|
||
if (error.response && error.response.data) { | ||
const body = error.response.data; | ||
this.apiCode = body.code; | ||
this.apiMessage = body.message; | ||
} | ||
} | ||
|
||
/** | ||
* Creates a specific APIError based on the API error code. | ||
* @param {AxiosError} error - The underlying error object. | ||
* @returns {APIError} A specific APIError instance. | ||
*/ | ||
static fromError(error: AxiosError) { | ||
const apiError = new APIError(error); | ||
if (!error.response || !error.response.data) { | ||
return apiError; | ||
} | ||
|
||
const body = error?.response?.data as APIErrorResponseType; | ||
switch (body?.code) { | ||
case "unimplemented": | ||
return new UnimplementedError(error); | ||
case "unauthorized": | ||
return new UnauthorizedError(error); | ||
case "internal": | ||
return new InternalError(error.message); | ||
case "not_found": | ||
return new NotFoundError(error); | ||
case "invalid_wallet_id": | ||
return new InvalidWalletIDError(error); | ||
case "invalid_address_id": | ||
return new InvalidAddressIDError(error); | ||
case "invalid_wallet": | ||
return new InvalidWalletError(error); | ||
case "invalid_address": | ||
return new InvalidAddressError(error); | ||
case "invalid_amount": | ||
return new InvalidAmountError(error); | ||
case "invalid_transfer_id": | ||
return new InvalidTransferIDError(error); | ||
case "invalid_page_token": | ||
return new InvalidPageError(error); | ||
case "invalid_page_limit": | ||
return new InvalidLimitError(error); | ||
case "already_exists": | ||
return new AlreadyExistsError(error); | ||
case "malformed_request": | ||
return new MalformedRequestError(error); | ||
case "unsupported_asset": | ||
return new UnsupportedAssetError(error); | ||
case "invalid_asset_id": | ||
return new InvalidAssetIDError(error); | ||
case "invalid_destination": | ||
return new InvalidDestinationError(error); | ||
case "invalid_network_id": | ||
return new InvalidNetworkIDError(error); | ||
case "resource_exhausted": | ||
return new ResourceExhaustedError(error); | ||
case "faucet_limit_reached": | ||
return new FaucetLimitReachedError(error); | ||
case "invalid_signed_payload": | ||
return new InvalidSignedPayloadError(error); | ||
case "invalid_transfer_status": | ||
return new InvalidTransferStatusError(error); | ||
default: | ||
return apiError; | ||
} | ||
} | ||
|
||
/** | ||
* Returns a String representation of the APIError. | ||
* @returns {string} a String representation of the APIError | ||
*/ | ||
toString() { | ||
return `APIError{httpCode: ${this.httpCode}, apiCode: ${this.apiCode}, apiMessage: ${this.apiMessage}}`; | ||
} | ||
} | ||
|
||
export class UnimplementedError extends APIError {} | ||
export class UnauthorizedError extends APIError {} | ||
export class NotFoundError extends APIError {} | ||
export class InvalidWalletIDError extends APIError {} | ||
export class InvalidAddressIDError extends APIError {} | ||
export class InvalidWalletError extends APIError {} | ||
export class InvalidAddressError extends APIError {} | ||
export class InvalidAmountError extends APIError {} | ||
export class InvalidTransferIDError extends APIError {} | ||
export class InvalidPageError extends APIError {} | ||
export class InvalidLimitError extends APIError {} | ||
export class AlreadyExistsError extends APIError {} | ||
export class MalformedRequestError extends APIError {} | ||
export class UnsupportedAssetError extends APIError {} | ||
export class InvalidAssetIDError extends APIError {} | ||
export class InvalidDestinationError extends APIError {} | ||
export class InvalidNetworkIDError extends APIError {} | ||
export class ResourceExhaustedError extends APIError {} | ||
export class FaucetLimitReachedError extends APIError {} | ||
export class InvalidSignedPayloadError extends APIError {} | ||
export class InvalidTransferStatusError extends APIError {} |
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
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
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,103 @@ | ||
import { ethers } from "ethers"; | ||
import { AddressesApiFactory, Address as AddressModel } from "../../client"; | ||
import { Address } from "./../address"; | ||
import { FaucetTransaction } from "./../faucet_transaction"; | ||
|
||
import MockAdapter from "axios-mock-adapter"; | ||
import { randomUUID } from "crypto"; | ||
import { APIError, FaucetLimitReachedError } from "../api_error"; | ||
import { createAxiosMock } from "./utils"; | ||
import { InternalError } from "../errors"; | ||
|
||
const newEthAddress = ethers.Wallet.createRandom(); | ||
|
||
const VALID_ADDRESS_MODEL: AddressModel = { | ||
address_id: newEthAddress.address, | ||
network_id: "base-sepolia", | ||
public_key: newEthAddress.publicKey, | ||
wallet_id: randomUUID(), | ||
}; | ||
|
||
// Test suite for Address class | ||
describe("Address", () => { | ||
const [axiosInstance, configuration, BASE_PATH] = createAxiosMock(); | ||
const client = AddressesApiFactory(configuration, BASE_PATH, axiosInstance); | ||
let address, axiosMock; | ||
|
||
beforeAll(() => { | ||
axiosMock = new MockAdapter(axiosInstance); | ||
}); | ||
|
||
beforeEach(() => { | ||
address = new Address(VALID_ADDRESS_MODEL, client); | ||
}); | ||
|
||
afterEach(() => { | ||
axiosMock.reset(); | ||
}); | ||
|
||
it("should initialize a new Address", () => { | ||
expect(address).toBeInstanceOf(Address); | ||
}); | ||
|
||
it("should return the network ID", () => { | ||
expect(address.getId()).toBe(newEthAddress.address); | ||
}); | ||
|
||
it("should return the address ID", () => { | ||
expect(address.getNetworkId()).toBe(VALID_ADDRESS_MODEL.network_id); | ||
}); | ||
|
||
it("should return the public key", () => { | ||
expect(address.getPublicKey()).toBe(newEthAddress.publicKey); | ||
}); | ||
|
||
it("should return the wallet ID", () => { | ||
expect(address.getWalletId()).toBe(VALID_ADDRESS_MODEL.wallet_id); | ||
}); | ||
|
||
it("should throw an InternalError when model is not provided", () => { | ||
expect(() => new Address(null!, client)).toThrow(`Address model cannot be empty`); | ||
}); | ||
|
||
it("should throw an InternalError when client is not provided", () => { | ||
expect(() => new Address(VALID_ADDRESS_MODEL, null!)).toThrow(`Address client cannot be empty`); | ||
}); | ||
|
||
it("should request funds from the faucet and returns the faucet transaction", async () => { | ||
const transactionHash = "0xdeadbeef"; | ||
axiosMock.onPost().reply(200, { | ||
transaction_hash: transactionHash, | ||
}); | ||
const faucetTransaction = await address.faucet(); | ||
expect(faucetTransaction).toBeInstanceOf(FaucetTransaction); | ||
expect(faucetTransaction.getTransactionHash()).toBe(transactionHash); | ||
}); | ||
|
||
it("should throw an APIError when the request is unsuccesful", async () => { | ||
axiosMock.onPost().reply(400); | ||
await expect(address.faucet()).rejects.toThrow(APIError); | ||
}); | ||
|
||
it("should throw a FaucetLimitReachedError when the faucet limit is reached", async () => { | ||
axiosMock.onPost().reply(429, { | ||
code: "faucet_limit_reached", | ||
message: "Faucet limit reached", | ||
}); | ||
await expect(address.faucet()).rejects.toThrow(FaucetLimitReachedError); | ||
}); | ||
|
||
it("should throw an InternalError when the request fails unexpectedly", async () => { | ||
axiosMock.onPost().reply(500, { | ||
code: "internal", | ||
message: "unexpected error occurred while requesting faucet funds", | ||
}); | ||
await expect(address.faucet()).rejects.toThrow(InternalError); | ||
}); | ||
|
||
it("should return the correct string representation", () => { | ||
expect(address.toString()).toBe( | ||
`Coinbase:Address{addressId: '${VALID_ADDRESS_MODEL.address_id}', networkId: '${VALID_ADDRESS_MODEL.network_id}', walletId: '${VALID_ADDRESS_MODEL.wallet_id}'}`, | ||
); | ||
}); | ||
}); |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: only supported on testnet networks