Skip to content

Commit

Permalink
[TypeScript SDK] Fix balance field (MystenLabs#1999)
Browse files Browse the repository at this point in the history
  • Loading branch information
666lcz authored May 17, 2022
1 parent 9a76747 commit 16c8173
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
9 changes: 4 additions & 5 deletions explorer/client/src/components/ownedobjects/OwnedObjects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions sdk/typescript/src/types/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
15 changes: 15 additions & 0 deletions sdk/typescript/test/types/framework.test.ts
Original file line number Diff line number Diff line change
@@ -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'));
});
});

0 comments on commit 16c8173

Please sign in to comment.