-
Notifications
You must be signed in to change notification settings - Fork 31
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
Changes from 1 commit
fc1202e
19563da
d52fc66
4cc5c7b
d7af21d
6598071
7cd9c7d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,87 @@ | ||||||
import { Address as AddressModel } from "../client"; | ||||||
import { InternalError } from "./errors"; | ||||||
import { FaucetTransaction } from "./faucet_transaction"; | ||||||
import { AddressClient } from "./types"; | ||||||
|
||||||
/** | ||||||
* Class representing an Address in the Coinbase SDK. | ||||||
*/ | ||||||
export class Address { | ||||||
private model: AddressModel; | ||||||
private client: AddressClient; | ||||||
|
||||||
/** | ||||||
* Creates an instance of Address. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
To align with |
||||||
* @param {AddressModel} model - The address model data. | ||||||
* @param {AddressClient} client - The API client to interact with address-related endpoints. | ||||||
* @throws {InternalError} If the model or client is empty. | ||||||
*/ | ||||||
constructor(model: AddressModel, client: AddressClient) { | ||||||
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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: 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> { | ||||||
try { | ||||||
const response = await this.client.requestFaucetFunds( | ||||||
this.model.wallet_id, | ||||||
this.model.address_id, | ||||||
); | ||||||
return new FaucetTransaction(response.data); | ||||||
} catch (e) { | ||||||
throw new Error(`Failed to request faucet funds`); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should add the api response I think there is a ticket for this? Let's handle this soon so we don't have to retroactively replace the temporary thrown errors in many places. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense, I'm going to throw |
||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Gets the address ID. | ||||||
erdimaden marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
* @returns {string} The address ID. | ||||||
*/ | ||||||
public getId(): string { | ||||||
return this.model.address_id; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Gets the network ID. | ||||||
* @returns {string} The network ID. | ||||||
*/ | ||||||
public getNetworkId(): string { | ||||||
return this.model.network_id; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Gets the public key. | ||||||
* @returns {string} The public key. | ||||||
*/ | ||||||
public getPublicKey(): string { | ||||||
return this.model.public_key; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Gets 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}'}`; | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { AddressesApiFactory, Address as AddressModel } from "../../client"; | ||
import { Address } from "./../address"; | ||
import { FaucetTransaction } from "./../faucet_transaction"; | ||
|
||
import axios from "axios"; | ||
import MockAdapter from "axios-mock-adapter"; | ||
|
||
const axiosMock = new MockAdapter(axios); | ||
|
||
const VALID_ADDRESS_MODEL: AddressModel = { | ||
address_id: "mocked_address_id", | ||
network_id: "mocked_network_id", | ||
public_key: "mocked_public_key", | ||
wallet_id: "mocked_wallet_id", | ||
}; | ||
|
||
// Test suite for Address class | ||
describe("Address", () => { | ||
const client = AddressesApiFactory(); | ||
|
||
it("should create an Address instance", () => { | ||
const address = new Address(VALID_ADDRESS_MODEL, client); | ||
expect(address).toBeInstanceOf(Address); | ||
expect(address.getId()).toBe("mocked_address_id"); | ||
expect(address.getNetworkId()).toBe("mocked_network_id"); | ||
expect(address.getPublicKey()).toBe("mocked_public_key"); | ||
expect(address.getWalletId()).toBe("mocked_wallet_id"); | ||
}); | ||
|
||
it("should throw an InternalError if model is not provided", () => { | ||
expect(() => new Address(null!, client)).toThrow(`Address model cannot be empty`); | ||
}); | ||
|
||
it("should throw an InternalError if client is not provided", () => { | ||
expect(() => new Address(VALID_ADDRESS_MODEL, null!)).toThrow(`Address client cannot be empty`); | ||
}); | ||
|
||
it("should request faucet funds and return a FaucetTransaction", async () => { | ||
axiosMock.onPost().reply(200, { | ||
transaction_hash: "mocked_transaction_hash", | ||
}); | ||
const address = new Address(VALID_ADDRESS_MODEL, client); | ||
const faucetTransaction = await address.faucet(); | ||
expect(faucetTransaction).toBeInstanceOf(FaucetTransaction); | ||
expect(faucetTransaction.getTransactionHash()).toBe("mocked_transaction_hash"); | ||
}); | ||
|
||
it("should throw an error 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"); | ||
}); | ||
|
||
it("should return the correct string representation", () => { | ||
const address = new Address(VALID_ADDRESS_MODEL, client); | ||
expect(address.toString()).toBe( | ||
"Coinbase:Address{addressId: 'mocked_address_id', networkId: 'mocked_network_id', walletId: 'mocked_wallet_id'}", | ||
); | ||
}); | ||
}); |
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.
Lets be consistent on information in class docs across SDK's
Reference Ruby SDK - https://github.com/coinbase/coinbase-sdk-ruby/blob/eb7326c69a67bd8d4000d3169c01e3adfd40e97d/lib/coinbase/address.rb#L11