From 99935c50c9652a3b4e25dacc8b3eef7819ddf374 Mon Sep 17 00:00:00 2001 From: rohan-agarwal-coinbase Date: Thu, 21 Nov 2024 18:38:23 -0500 Subject: [PATCH] Fix Asset fromModel bug to use passed in asset_id when initializing (#317) --- CHANGELOG.md | 3 +++ src/coinbase/asset.ts | 7 ++++++- src/tests/asset_test.ts | 12 ++++++++---- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db63823e..fc64bc56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +### Fixed +- Fixed a bug where the asset ID was not being set correctly for Gwei and Wei + ## [0.10.0] - 2024-10-31 ### Added diff --git a/src/coinbase/asset.ts b/src/coinbase/asset.ts index 18b41abb..7ceb2ad5 100644 --- a/src/coinbase/asset.ts +++ b/src/coinbase/asset.ts @@ -64,7 +64,12 @@ export class Asset { throw new ArgumentError(`Invalid asset ID: ${assetId}`); } } - return new Asset(model.network_id, model.asset_id, model.contract_address!, decimals); + return new Asset( + model.network_id, + assetId ?? model.asset_id, + model.contract_address!, + decimals, + ); } /** diff --git a/src/tests/asset_test.ts b/src/tests/asset_test.ts index 9eac3622..477c250e 100644 --- a/src/tests/asset_test.ts +++ b/src/tests/asset_test.ts @@ -24,25 +24,29 @@ describe("Asset", () => { }); describe("when the asset_id is gwei", () => { - it("should set the decimals to 9", () => { + it("should set the decimals to 9 and assetId to gwei", () => { const model = { asset_id: "eth", network_id: Coinbase.networks.BaseSepolia, contract_address: "0x", decimals: 18, }; - expect(Asset.fromModel(model, Coinbase.assets.Gwei).decimals).toEqual(GWEI_DECIMALS); + const asset = Asset.fromModel(model, Coinbase.assets.Gwei); + expect(asset.decimals).toEqual(GWEI_DECIMALS); + expect(asset.getAssetId()).toEqual("gwei"); }); }); describe("when the asset_id is wei", () => { - it("should set the decimals to 0", () => { + it("should set the decimals to 0 and assetId to wei", () => { const model = { asset_id: "eth", network_id: Coinbase.networks.BaseSepolia, contract_address: "0x", decimals: 18, }; - expect(Asset.fromModel(model, Coinbase.assets.Wei).decimals).toEqual(0); + const asset = Asset.fromModel(model, Coinbase.assets.Wei); + expect(asset.decimals).toEqual(0); + expect(asset.getAssetId()).toEqual("wei"); }); }); });