Skip to content
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

TODO #18

Closed
wants to merge 17 commits into from
15 changes: 3 additions & 12 deletions src/coinbase/balance_map.ts
Original file line number Diff line number Diff line change
@@ -5,16 +5,7 @@ import { Decimal } from "decimal.js";
/**
* A convenience class for storing and manipulating Asset balances in a human-readable format.
*/
export class BalanceMap {
private map: Map<string, Decimal>;

/**
* Constructor to initialize the BalanceMap.
*/
constructor() {
this.map = new Map<string, Decimal>();
}

export class BalanceMap extends Map<string, Decimal> {
/**
* Converts a list of Balance models to a BalanceMap.
*
@@ -39,7 +30,7 @@ export class BalanceMap {
if (!(balance instanceof Balance)) {
throw new Error("balance must be a Balance");
}
this.map.set(balance.assetId, balance.amount);
this.set(balance.assetId, balance.amount);
}

/**
@@ -49,7 +40,7 @@ export class BalanceMap {
*/
public toString(): string {
const result: Record<string, string> = {};
this.map.forEach((value, key) => {
this.forEach((value, key) => {
let str = value.toString();
if (value.isInteger()) {
str = value.toNumber().toString();
41 changes: 41 additions & 0 deletions src/coinbase/tests/address_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ethers } from "ethers";
import {
AddressBalanceList,
AddressesApiFactory,
Address as AddressModel,
Balance as BalanceModel,
@@ -32,6 +33,38 @@ const VALID_BALANCE_MODEL: BalanceModel = {
},
};

const VALID_ADDRESS_BALANCE_LIST: AddressBalanceList = {
data: [
{
amount: "1000000000000000000",
asset: {
asset_id: "eth",
network_id: "base-sepolia",
decimals: 18,
},
},
{
amount: "5000000000",
asset: {
asset_id: "usdc",
network_id: "base-sepolia",
decimals: 6,
},
},
{
amount: "3000000000000000000",
asset: {
asset_id: "weth",
network_id: "base-sepolia",
decimals: 6,
},
},
],
has_more: false,
next_page: "",
total_count: 3,
};

// Test suite for Address class
describe("Address", () => {
const [axiosInstance, configuration, BASE_PATH] = createAxiosMock();
@@ -62,6 +95,14 @@ describe("Address", () => {
expect(address.getNetworkId()).toBe(VALID_ADDRESS_MODEL.network_id);
});

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("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");