Skip to content

Commit

Permalink
fix: Normalize asset IDs
Browse files Browse the repository at this point in the history
This handles normalizing asset IDs when checking responses from
the backend.

This will allow us to use either `POL` or the normalized `pol` when
interacting with Assets / Asset IDs.

Previously if you passed a non-normalized asset ID, e.g. `POL`,
then when we check the asset ID v.s. the returned model it would
not equal `pol`.
  • Loading branch information
alex-stone committed Oct 28, 2024
1 parent 2471020 commit cc631bb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
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");
}


Check failure on line 47 in src/coinbase/asset.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`
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

0 comments on commit cc631bb

Please sign in to comment.