Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
xinyu-li-cb committed Aug 9, 2024
1 parent 5edb455 commit c113325
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
16 changes: 11 additions & 5 deletions src/coinbase/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Balance } from "./balance";
import { BalanceMap } from "./balance_map";
import { FaucetTransaction } from "./faucet_transaction";
import { HistoricalBalance } from "./historical_balance";
import { Amount, StakeOptionsMode } from "./types";
import { Amount, StakeOptionsMode, ListHistoricalBalancesResult } from "./types";
import { formatDate, getWeekBackDate } from "./utils";
import { StakingRewardFormat } from "../client";
import { StakingReward } from "./staking_reward";
Expand Down Expand Up @@ -94,7 +94,7 @@ export class Address {
assetId: string,
limit?: number,
page?: string,
): Promise<[HistoricalBalance[], string]> {
): Promise<ListHistoricalBalancesResult> {
const historyList: HistoricalBalance[] = [];

if (limit !== undefined) {
Expand All @@ -105,16 +105,19 @@ export class Address {
limit,
page ? page : undefined,
);

response.data.data.forEach(historicalBalanceModel => {
const historicalBalance = HistoricalBalance.fromModel(historicalBalanceModel);
historyList.push(historicalBalance);
});

return [historyList, response.data.next_page];
return {
historicalBalances: historyList,
nextPageToken: response.data.next_page,
};
}

const queue: string[] = [""];

while (queue.length > 0 && historyList.length < Address.MAX_HISTORICAL_BALANCE) {
const page = queue.shift();
const response = await Coinbase.apiClients.externalAddress!.listAddressHistoricalBalance(
Expand All @@ -137,7 +140,10 @@ export class Address {
}
}

return [historyList, ""];
return {
historicalBalances: historyList,
nextPageToken: "",
};
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/coinbase/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
} from "./../client/api";
import { Address } from "./address";
import { Wallet } from "./wallet";
import { HistoricalBalance } from "./historical_balance"

Check failure on line 38 in src/coinbase/types.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `;`

export type AssetAPIClient = {
/**
Expand Down Expand Up @@ -780,3 +781,11 @@ export type CreateTradeOptions = {
timeoutSeconds?: number;
intervalSeconds?: number;
};

/**
* Result of ListHistoricalBalances.
*/
export type ListHistoricalBalancesResult = {
historicalBalances: HistoricalBalance[],

Check failure on line 789 in src/coinbase/types.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `,` with `;`
nextPageToken: string,

Check failure on line 790 in src/coinbase/types.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `,` with `;`
}

Check failure on line 791 in src/coinbase/types.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `;⏎`
37 changes: 24 additions & 13 deletions src/tests/address_test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { Coinbase } from "../coinbase/coinbase";
import { Address } from "../index";
import { VALID_ADDRESS_MODEL } from "./utils";
import {
AddressHistoricalBalanceList,
} from "../client";
import {
VALID_ADDRESS_MODEL,
mockReturnValue,
newAddressModel,
externalAddressApiMock,
} from "./utils";
import Decimal from "decimal.js";

describe("Address", () => {
const newAddress = newAddressModel("", randomUUID(), Coinbase.networks.EthereumHolesky);

Check failure on line 15 in src/tests/address_test.ts

View workflow job for this annotation

GitHub Actions / test

Cannot find name 'randomUUID'.

const address = new Address(newAddress.network_id, newAddress.address_id);

describe(".getNetworkId", () => {
it("should get the network ID", () => {
const address = new Address(VALID_ADDRESS_MODEL.network_id, VALID_ADDRESS_MODEL.address_id);
Expand Down Expand Up @@ -60,10 +71,10 @@ describe("Address", () => {
});

it("should return results with USDC historical balance", async () => {
const [history, nextPage] = await address.listHistoricalBalances(Coinbase.assets.Usdc);
expect(history.length).toEqual(2);
expect(history[0].amount).toEqual(new Decimal(1));
expect(history[1].amount).toEqual(new Decimal(5));
const historicalBalancesResult = await address.listHistoricalBalances(Coinbase.assets.Usdc);
expect(historicalBalancesResult.historicalBalances.length).toEqual(2);
expect(historicalBalancesResult.historicalBalances[0].amount).toEqual(new Decimal(1));
expect(historicalBalancesResult.historicalBalances[1].amount).toEqual(new Decimal(5));
expect(
Coinbase.apiClients.externalAddress!.listAddressHistoricalBalance,
).toHaveBeenCalledTimes(1);
Expand All @@ -74,7 +85,7 @@ describe("Address", () => {
100,
undefined,
);
expect(nextPage).toEqual("")
expect(historicalBalancesResult.nextPageToken).toEqual("")
});

it("should return empty if no historical balance found", async () => {
Expand All @@ -83,8 +94,8 @@ describe("Address", () => {
has_more: false,
next_page: "",
});
const [history, nextPage] = await address.listHistoricalBalances(Coinbase.assets.Usdc);
expect(history.length).toEqual(0);
const historicalBalancesResult = await address.listHistoricalBalances(Coinbase.assets.Usdc);
expect(historicalBalancesResult.historicalBalances.length).toEqual(0);
expect(
Coinbase.apiClients.externalAddress!.listAddressHistoricalBalance,
).toHaveBeenCalledTimes(1);
Expand All @@ -95,7 +106,7 @@ describe("Address", () => {
100,
undefined,
);
expect(nextPage).toEqual("")
expect(historicalBalancesResult.nextPageToken).toEqual("")
});

it("should return results with USDC historical balance and next page", async () => {
Expand All @@ -116,9 +127,9 @@ describe("Address", () => {
next_page: "next page",
});

const [history, nextPage] = await address.listHistoricalBalances(Coinbase.assets.Usdc, 1);
expect(history.length).toEqual(1);
expect(history[0].amount).toEqual(new Decimal(5));
const historicalBalancesResult = await address.listHistoricalBalances(Coinbase.assets.Usdc, 1);
expect(historicalBalancesResult.historicalBalances.length).toEqual(1);
expect(historicalBalancesResult.historicalBalances[0].amount).toEqual(new Decimal(5));
expect(
Coinbase.apiClients.externalAddress!.listAddressHistoricalBalance,
).toHaveBeenCalledTimes(1);
Expand All @@ -129,7 +140,7 @@ describe("Address", () => {
1,
undefined,
);
expect(nextPage).toEqual("next page")
expect(historicalBalancesResult.nextPageToken).toEqual("next page")
});
});

Expand Down

0 comments on commit c113325

Please sign in to comment.