Skip to content

Commit

Permalink
Implement PR feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuga-cb committed May 17, 2024
1 parent 31d1c41 commit 70c10ce
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/coinbase/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class Address {
/**
* Returns the list of balances for the address.
*
* @returns {BalanceMap} The map from asset ID to balance.
* @returns {BalanceMap} - The map from asset ID to balance.
*/
async listBalances(): Promise<BalanceMap> {
const response = await this.client.listAddressBalances(
Expand Down
6 changes: 3 additions & 3 deletions src/coinbase/asset.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Decimal from "decimal.js";
import { ATOMIC_UNITS_PER_USDC, WEI_PER_ETHER, WEI_PER_GWEI } from "./constants";

/** # A representation of an Asset. */
/** A representation of an Asset. */
export class Asset {
/**
* Converts an amount from the atomic value of the primary denomination of the provided Asset ID
* to whole units of the specified asset ID.
*
* @param atomicAmount - The amount in atomic units.
* @param assetId - The assset ID.
* @param {Decimal} atomicAmount - The amount in atomic units.
* @param {string} assetId - The assset ID.
* @returns The amount in whole units of the asset with the specified ID.
*/
static fromAtomicAmount(atomicAmount: Decimal, assetId: string): Decimal {
Expand Down
14 changes: 7 additions & 7 deletions src/coinbase/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export class Balance {
* Private constructor to prevent direct instantiation outside of the factory methods.
*
* @ignore
* @param amount - The amount of the balance.
* @param assetId - The asset ID.
* @param {Decimal} amount - The amount of the balance.
* @param {string} assetId - The asset ID.
* @hideconstructor
*/
private constructor(amount: Decimal, assetId: string) {
Expand All @@ -23,8 +23,8 @@ export class Balance {
/**
* Converts a BalanceModel into a Balance object.
*
* @param model - The balance model object.
* @returns The Balance object.
* @param {BalanceModel} model - The balance model object.
* @returns {Balance} The Balance object.
*/
public static fromModel(model: BalanceModel): Balance {
return this.fromModelAndAssetId(model, model.asset.asset_id);
Expand All @@ -33,9 +33,9 @@ export class Balance {
/**
* Converts a BalanceModel and asset ID into a Balance object.
*
* @param model - The balance model object.
* @param assetId - The asset ID.
* @returns The Balance object.
* @param {BalanceModel} model - The balance model object.
* @param {string} assetId - The asset ID.
* @returns {Balance} The Balance object.
*/
public static fromModelAndAssetId(model: BalanceModel, assetId: string): Balance {
return new Balance(Asset.fromAtomicAmount(new Decimal(model.amount), assetId), assetId);
Expand Down
6 changes: 3 additions & 3 deletions src/coinbase/balance_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export class BalanceMap extends Map<string, Decimal> {
/**
* Converts a list of Balance models to a BalanceMap.
*
* @param balances - The list of balances fetched from the API.
* @returns The converted BalanceMap object.
* @param {BalanceModel[]} balances - The list of balances fetched from the API.
* @returns {BalanceMap} The converted BalanceMap object.
*/
public static fromBalances(balances: BalanceModel[]): BalanceMap {
const balanceMap = new BalanceMap();
Expand All @@ -24,7 +24,7 @@ export class BalanceMap extends Map<string, Decimal> {
/**
* Adds a balance to the map.
*
* @param balance - The balance to add to the map.
* @param {Balance} balance - The balance to add to the map.
*/
public add(balance: Balance): void {
if (!(balance instanceof Balance)) {
Expand Down
16 changes: 8 additions & 8 deletions src/coinbase/tests/address_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const VALID_ADDRESS_MODEL: AddressModel = {
const VALID_BALANCE_MODEL: BalanceModel = {
amount: "1000000000000000000",
asset: {
asset_id: "eth",
network_id: "base-sepolia",
asset_id: Coinbase.assetList.Eth,
network_id: Coinbase.networkList.BaseSepolia,
},
};

Expand All @@ -38,24 +38,24 @@ const VALID_ADDRESS_BALANCE_LIST: AddressBalanceList = {
{
amount: "1000000000000000000",
asset: {
asset_id: "eth",
network_id: "base-sepolia",
asset_id: Coinbase.assetList.Eth,
network_id: Coinbase.networkList.BaseSepolia,
decimals: 18,
},
},
{
amount: "5000000000",
asset: {
asset_id: "usdc",
network_id: "base-sepolia",
network_id: Coinbase.networkList.BaseSepolia,
decimals: 6,
},
},
{
amount: "3000000000000000000",
asset: {
asset_id: "weth",
network_id: "base-sepolia",
network_id: Coinbase.networkList.BaseSepolia,
decimals: 6,
},
},
Expand Down Expand Up @@ -98,14 +98,14 @@ describe("Address", () => {
it("should return the correct list of balances", async () => {
axiosMock.onGet().reply(200, VALID_ADDRESS_BALANCE_LIST);
const balances = await address.listBalances();
expect(balances.get("eth")).toEqual(new Decimal(1));
expect(balances.get(Coinbase.assetList.Eth)).toEqual(new Decimal(1));
expect(balances.get("usdc")).toEqual(new Decimal(5000));
expect(balances.get("weth")).toEqual(new Decimal(3));
});

it("should return the correct ETH balance", async () => {
axiosMock.onGet().reply(200, VALID_BALANCE_MODEL);
const ethBalance = await address.getBalance("eth");
const ethBalance = await address.getBalance(Coinbase.assetList.Eth);
expect(ethBalance).toBeInstanceOf(Decimal);
expect(ethBalance).toEqual(new Decimal(1));
});
Expand Down
27 changes: 17 additions & 10 deletions src/coinbase/tests/balance_map_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BalanceMap } from "../balance_map";
import { Balance as BalanceModel } from "../../client";
import { Balance } from "../balance";
import { Decimal } from "decimal.js";
import { Coinbase } from "../coinbase";

describe("BalanceMap", () => {
const ethAmount = new Decimal(1);
Expand All @@ -14,24 +15,24 @@ describe("BalanceMap", () => {
describe(".fromBalances", () => {
const ethBalanceModel: BalanceModel = {
asset: {
asset_id: "eth",
network_id: "base-sepolia",
asset_id: Coinbase.assetList.Eth,
network_id: Coinbase.networkList.BaseSepolia,
},
amount: ethAtomicAmount,
};

const usdcBalanceModel: BalanceModel = {
asset: {
asset_id: "usdc",
network_id: "base-sepolia",
network_id: Coinbase.networkList.BaseSepolia,
},
amount: usdcAtomicAmount,
};

const wethBalanceModel: BalanceModel = {
asset: {
asset_id: "weth",
network_id: "base-sepolia",
network_id: Coinbase.networkList.BaseSepolia,
},
amount: wethAtomicAmount,
};
Expand All @@ -41,16 +42,19 @@ describe("BalanceMap", () => {
const balanceMap = BalanceMap.fromBalances(balances);

it("returns a new BalanceMap object with the correct balances", () => {
expect(balanceMap.get("eth")).toEqual(ethAmount);
expect(balanceMap.get(Coinbase.assetList.Eth)).toEqual(ethAmount);
expect(balanceMap.get("usdc")).toEqual(usdcAmount);
expect(balanceMap.get("weth")).toEqual(wethAmount);
});
});

describe("#add", () => {
const assetId = "eth";
const assetId = Coinbase.assetList.Eth;
const balance = Balance.fromModelAndAssetId(
{ amount: ethAtomicAmount, asset: { asset_id: assetId, network_id: "base-sepolia" } },
{
amount: ethAtomicAmount,
asset: { asset_id: assetId, network_id: Coinbase.networkList.BaseSepolia },
},
assetId,
);

Expand All @@ -63,17 +67,20 @@ describe("BalanceMap", () => {
});

describe("#toString", () => {
const assetId = "eth";
const assetId = Coinbase.assetList.Eth;
const balance = Balance.fromModelAndAssetId(
{ amount: ethAtomicAmount, asset: { asset_id: assetId, network_id: "base-sepolia" } },
{
amount: ethAtomicAmount,
asset: { asset_id: assetId, network_id: Coinbase.networkList.BaseSepolia },
},
assetId,
);

const balanceMap = new BalanceMap();
balanceMap.add(balance);

it("returns a string representation of asset_id to floating-point number", () => {
expect(balanceMap.toString()).toBe(`{\"${assetId}\":\"${ethAmount}\"}`);
expect(balanceMap.toString()).toBe(`{"${assetId}":"${ethAmount}"}`);
});
});
});
19 changes: 10 additions & 9 deletions src/coinbase/tests/balance_test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Balance } from "../balance";
import { Balance as BalanceModel } from "../../client";
import { Decimal } from "decimal.js";
import { Coinbase } from "../coinbase";

describe("Balance", () => {
describe(".fromModel", () => {
const amount = new Decimal(1);
const balanceModel: BalanceModel = {
amount: "1000000000000000000",
asset: {
asset_id: "eth",
network_id: "base-sepolia",
asset_id: Coinbase.assetList.Eth,
network_id: Coinbase.networkList.BaseSepolia,
},
};

Expand All @@ -20,25 +21,25 @@ describe("Balance", () => {
});

it("returns a new Balance object with the correct asset_id", () => {
expect(balance.assetId).toBe("eth");
expect(balance.assetId).toBe(Coinbase.assetList.Eth);
});
});

describe(".fromModelAndAssetId", () => {
const amount = new Decimal(123);
const amount = new Decimal(1);
const balanceModel: BalanceModel = {
asset: { asset_id: "eth", network_id: "base-sepolia" },
amount: amount.toString(),
asset: { asset_id: Coinbase.assetList.Eth, network_id: Coinbase.networkList.BaseSepolia },
amount: "1000000000000000000",
};

const balance = Balance.fromModelAndAssetId(balanceModel, "ETH");
const balance = Balance.fromModelAndAssetId(balanceModel, Coinbase.assetList.Eth);

it("returns a new Balance object with the correct amount", () => {
expect(balance.amount.equals(amount)).toBeTruthy();
expect(balance.amount).toEqual(amount);
});

it("returns a new Balance object with the correct asset_id", () => {
expect(balance.assetId).toBe("ETH");
expect(balance.assetId).toBe(Coinbase.assetList.Eth);
});
});
});

0 comments on commit 70c10ce

Please sign in to comment.