Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
yuga-cb committed May 17, 2024
1 parent 9cfb355 commit ca9d9ab
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 39 deletions.
57 changes: 25 additions & 32 deletions src/coinbase/tests/address_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,41 @@ import Decimal from "decimal.js";

const newEthAddress = ethers.Wallet.createRandom();

const VALID_ADDRESS_MODEL: AddressModel = {
export const VALID_ADDRESS_MODEL: AddressModel = {
address_id: newEthAddress.address,
network_id: Coinbase.networkList.BaseSepolia,
public_key: newEthAddress.publicKey,
wallet_id: randomUUID(),
};

const VALID_BALANCE_MODEL: BalanceModel = {
export const ETH_BALANCE_MODEL: BalanceModel = {
amount: "1000000000000000000",
asset: {
asset_id: Coinbase.assetList.Eth,
network_id: Coinbase.networkList.BaseSepolia,
},
};

const VALID_ADDRESS_BALANCE_LIST: AddressBalanceList = {
data: [
{
amount: "1000000000000000000",
asset: {
asset_id: Coinbase.assetList.Eth,
network_id: Coinbase.networkList.BaseSepolia,
decimals: 18,
},
},
{
amount: "5000000000",
asset: {
asset_id: "usdc",
network_id: Coinbase.networkList.BaseSepolia,
decimals: 6,
},
},
{
amount: "3000000000000000000",
asset: {
asset_id: "weth",
network_id: Coinbase.networkList.BaseSepolia,
decimals: 6,
},
},
],
export const USDC_BALANCE_MODEL: BalanceModel = {
amount: "5000000000",
asset: {
asset_id: "usdc",
network_id: Coinbase.networkList.BaseSepolia,
decimals: 6,
},
};

export const WETH_BALANCE_MODEL: BalanceModel = {
amount: "3000000000000000000",
asset: {
asset_id: "weth",
network_id: Coinbase.networkList.BaseSepolia,
decimals: 6,
},
};

export const VALID_ADDRESS_BALANCE_LIST: AddressBalanceList = {
data: [ETH_BALANCE_MODEL, USDC_BALANCE_MODEL, WETH_BALANCE_MODEL],
has_more: false,
next_page: "",
total_count: 3,
Expand Down Expand Up @@ -104,21 +97,21 @@ describe("Address", () => {
});

it("should return the correct ETH balance", async () => {
axiosMock.onGet().reply(200, VALID_BALANCE_MODEL);
axiosMock.onGet().reply(200, ETH_BALANCE_MODEL);
const ethBalance = await address.getBalance(Coinbase.assetList.Eth);
expect(ethBalance).toBeInstanceOf(Decimal);
expect(ethBalance).toEqual(new Decimal(1));
});

it("should return the correct Gwei balance", async () => {
axiosMock.onGet().reply(200, VALID_BALANCE_MODEL);
axiosMock.onGet().reply(200, ETH_BALANCE_MODEL);
const ethBalance = await address.getBalance("gwei");
expect(ethBalance).toBeInstanceOf(Decimal);
expect(ethBalance).toEqual(new Decimal(1000000000));
});

it("should return the correct Wei balance", async () => {
axiosMock.onGet().reply(200, VALID_BALANCE_MODEL);
axiosMock.onGet().reply(200, ETH_BALANCE_MODEL);
const ethBalance = await address.getBalance("wei");
expect(ethBalance).toBeInstanceOf(Decimal);
expect(ethBalance).toEqual(new Decimal(1000000000000000000));
Expand Down
58 changes: 54 additions & 4 deletions src/coinbase/tests/wallet_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { Coinbase } from "../coinbase";
import { ArgumentError } from "../errors";
import { Wallet } from "../wallet";
import { createAxiosMock } from "./utils";
import { ETH_BALANCE_MODEL, VALID_ADDRESS_BALANCE_LIST, VALID_ADDRESS_MODEL } from "./address_test";
import Decimal from "decimal.js";
import { APIError } from "../api_error";

const walletId = randomUUID();
export const VALID_WALLET_MODEL = {
Expand All @@ -20,7 +23,7 @@ export const VALID_WALLET_MODEL = {
};

describe("Wallet Class", () => {
let wallet, axiosMock;
let wallet: Wallet, axiosMock: MockAdapter;
const seed = bip39.generateMnemonic();

const [axiosInstance, configuration, BASE_PATH] = createAxiosMock();
Expand All @@ -31,15 +34,19 @@ describe("Wallet Class", () => {

beforeAll(async () => {
axiosMock = new MockAdapter(axiosInstance);
axiosMock.onPost().reply(200, VALID_WALLET_MODEL).onGet().reply(200, VALID_WALLET_MODEL);
axiosMock
.onPost(/v1\/wallets/)
.replyOnce(200, VALID_WALLET_MODEL)
.onGet(/v1\/wallets\/.+\/addresses\/.+/)
.reply(200, VALID_ADDRESS_MODEL);
wallet = await Wallet.init(VALID_WALLET_MODEL, client, seed, 2);
});

afterEach(() => {
axiosMock.reset();
});

describe("should initializes a new Wallet", () => {
describe("should initialize a new Wallet", () => {
it("should return a Wallet instance", async () => {
expect(wallet).toBeInstanceOf(Wallet);
});
Expand All @@ -54,7 +61,50 @@ describe("Wallet Class", () => {
});

it("should derive the correct number of addresses", async () => {
expect(wallet.addresses.length).toBe(2);
expect(wallet.listAddresses().length).toBe(2);
});
it("should return the correct address", () => {
const defaultAddress = wallet.defaultAddress();
expect(wallet.getAddress(defaultAddress!.getId())).toEqual(wallet.listAddresses()[0]);
});

it("should return a BalanceMap with ETH, USDC, and WETH balances", async () => {
axiosMock.onGet().reply(200, VALID_ADDRESS_BALANCE_LIST);
const balances = await wallet.listBalances();
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, ETH_BALANCE_MODEL);
const ethBalance = await wallet.getBalance(Coinbase.assetList.Eth);
expect(ethBalance).toBeInstanceOf(Decimal);
expect(ethBalance).toEqual(new Decimal(1));
});

it("should return the correct Gwei balance", async () => {
axiosMock.onGet().reply(200, ETH_BALANCE_MODEL);
const ethBalance = await wallet.getBalance("gwei");
expect(ethBalance).toBeInstanceOf(Decimal);
expect(ethBalance).toEqual(new Decimal(1000000000));
});

it("should return the correct Wei balance", async () => {
axiosMock.onGet().reply(200, ETH_BALANCE_MODEL);
const ethBalance = await wallet.getBalance("wei");
expect(ethBalance).toBeInstanceOf(Decimal);
expect(ethBalance).toEqual(new Decimal(1000000000000000000));
});

it("should return an error for an unsupported asset", async () => {
axiosMock.onGet().reply(404, null);
try {
await wallet.getBalance("unsupported-asset");
fail("Expect 404 to be thrown");
} catch (error) {
expect(error).toBeInstanceOf(APIError);
}
});

it("should return the correct string representation", async () => {
Expand Down
25 changes: 22 additions & 3 deletions src/coinbase/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,28 @@ export class Wallet {
* @returns The default address
*/
public defaultAddress(): Address | undefined {
return this.model.default_address
? new Address(this.model.default_address, this.client.address!)
: undefined;
if (this.model.default_address?.address_id !== undefined) {
return this.getAddress(this.model.default_address?.address_id);
}
}

/**
* Lists the addresses in the Wallet.
*
* @returns {Address[]} The list of addresses in the wallet
*/
public listAddresses(): Address[] {
return this.addresses;
}

/**
* Gets the address with the specified ID in the Wallet.
*
* @param {string} addressId - The ID of the address to fetch.
* @returns {Address} The address with the specified ID.
*/
public getAddress(addressId: string): Address | undefined {
return this.addresses.find(address => address.getId() === addressId);
}

/**
Expand Down

0 comments on commit ca9d9ab

Please sign in to comment.