diff --git a/explorer/client/src/components/ownedobjects/OwnedObjects.tsx b/explorer/client/src/components/ownedobjects/OwnedObjects.tsx index 8a7ecc922ace3..45ab01a50c31f 100644 --- a/explorer/client/src/components/ownedobjects/OwnedObjects.tsx +++ b/explorer/client/src/components/ownedobjects/OwnedObjects.tsx @@ -91,11 +91,10 @@ function OwnedObjectAPI({ id }: { id: string }) { const contents = getObjectFields(resp); const url = parseImageURL(contents); const objType = parseObjectType(resp); - // TODO: handle big number - const rawBalance = Coin.getBalance(resp); - const balanceValue = rawBalance - ? parseInt(rawBalance) - : undefined; + // TODO: handle big number by making the balance field + // in resultType a string + const balanceValue = + Coin.getBalance(resp)?.toNumber(); return { id: getObjectId(resp), Type: objType, diff --git a/sdk/typescript/src/types/framework.ts b/sdk/typescript/src/types/framework.ts index 257af580766ec..4631ecf8b6279 100644 --- a/sdk/typescript/src/types/framework.ts +++ b/sdk/typescript/src/types/framework.ts @@ -5,6 +5,8 @@ import { getObjectFields, GetObjectInfoResponse } from './objects'; import { getMoveObjectType } from './objects'; +import BN from 'bn.js'; + /** * Utility class for 0x2::Coin * as defined in https://github.com/MystenLabs/sui/blob/ca9046fd8b1a9e8634a4b74b0e7dabdc7ea54475/sui_programmability/framework/sources/Coin.move#L4 @@ -14,11 +16,11 @@ export class Coin { return getMoveObjectType(data)?.startsWith('0x2::Coin::Coin') ?? false; } - static getBalance(data: GetObjectInfoResponse): string | undefined { + static getBalance(data: GetObjectInfoResponse): BN | undefined { if (!Coin.isCoin(data)) { return undefined; } - const balance = getObjectFields(data)?.value; - return typeof balance === 'string' ? balance : balance?.toString(); + const balance = getObjectFields(data)?.balance; + return new BN.BN(balance, 10); } } diff --git a/sdk/typescript/test/types/framework.test.ts b/sdk/typescript/test/types/framework.test.ts new file mode 100644 index 0000000000000..278b2a96b9c40 --- /dev/null +++ b/sdk/typescript/test/types/framework.test.ts @@ -0,0 +1,15 @@ +// Copyright (c) 2022, Mysten Labs, Inc. +// SPDX-License-Identifier: Apache-2.0 + +import mockObjectData from '../../../../sui/open_rpc/samples/objects.json'; +import { Coin, GetObjectInfoResponse } from '../../src'; + +import BN from 'bn.js'; + +describe('Test framework classes', () => { + it('Test coin utils', () => { + const data = mockObjectData['coin'] as GetObjectInfoResponse; + expect(Coin.isCoin(data)).toBeTruthy(); + expect(Coin.getBalance(data)).toEqual(new BN.BN('100000')); + }); +});