Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/coinbase/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,15 @@ export class Asset {
throw new Error("Invalid asset model");
}


let decimals = model.decimals!;
// TODO: Push this logic down to the backend.
if (
assetId &&
model.asset_id &&
Coinbase.toAssetId(model.asset_id) !== Coinbase.toAssetId(assetId)
) {
switch (assetId) {
switch (Coinbase.toAssetId(assetId)) {
case "gwei":
decimals = GWEI_DECIMALS;
break;
Expand Down Expand Up @@ -92,9 +93,11 @@ export class Asset {
* @returns The primary denomination for the Asset ID.
*/
public static primaryDenomination(assetId: string): string {
return [Coinbase.assets.Gwei, Coinbase.assets.Wei].includes(assetId)
const normalizedAssetId = Coinbase.toAssetId(assetId);

return [Coinbase.assets.Gwei, Coinbase.assets.Wei].includes(normalizedAssetId)
? Coinbase.assets.Eth
: assetId;
: normalizedAssetId;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/coinbase/coinbase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class Coinbase {
Weth: "weth",
Sol: "sol",
Lamport: "lamport",
Pol: "pol",
};

static apiClients: ApiClients = {};
Expand Down Expand Up @@ -253,11 +254,12 @@ export class Coinbase {

/**
* Converts a string to a symbol, replacing hyphens with underscores.
* This also converts the string to lowercase.
*
* @param asset - The string to convert
* @returns the converted symbol
*/
static toAssetId(asset: string): string {
return asset.replace(/-/g, "_");
return asset.replace(/-/g, "_").toLowerCase();
}
}
17 changes: 16 additions & 1 deletion src/tests/asset_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ describe("Asset", () => {
expect(asset.getAssetId()).toEqual(Coinbase.assets.Eth);
});

describe("when the specified Asset ID is not normalized", () => {
it("should normalize the Asset ID", () => {
const model = {
asset_id: "pol",
network_id: Coinbase.networks.PolygonMainnet,
decimals: 18,
};
const asset = Asset.fromModel(model, "POL");
expect(asset).toBeInstanceOf(Asset);
expect(asset.getAssetId()).toEqual(Coinbase.assets.Pol);

});
});

describe("when the model is invalid", () => {
it("should throw an error", () => {
expect(() => Asset.fromModel(null!)).toThrow("Invalid asset model");
Expand All @@ -34,6 +48,7 @@ describe("Asset", () => {
expect(Asset.fromModel(model, Coinbase.assets.Gwei).decimals).toEqual(GWEI_DECIMALS);
});
});

describe("when the asset_id is wei", () => {
it("should set the decimals to 0", () => {
const model = {
Expand Down Expand Up @@ -62,7 +77,7 @@ describe("Asset", () => {
});

describe(".primaryDenomination", () => {
["wei", "gwei"].forEach(assetId => {
["wei", "gwei", "WEI", "GWEI"].forEach(assetId => {
describe(`when the assetId is ${assetId}`, () => {
it("should return 'eth'", () => {
expect(Asset.primaryDenomination(assetId)).toEqual("eth");
Expand Down
2 changes: 1 addition & 1 deletion src/tests/wallet_address_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ describe("WalletAddress", () => {

it("should return an error for an unsupported asset", async () => {
const getAddressBalance = mockReturnRejectedValue(new APIError(""));
const assetId = "unsupported-asset";
const assetId = "unsupportedasset";
Coinbase.apiClients.externalAddress!.getExternalAddressBalance = getAddressBalance;
await expect(address.getBalance(assetId)).rejects.toThrow(APIError);
expect(getAddressBalance).toHaveBeenCalledWith(
Expand Down