Skip to content

Commit

Permalink
[TypeScript SDK] Parse balance field for coin (MystenLabs#1699)
Browse files Browse the repository at this point in the history
  • Loading branch information
666lcz authored May 2, 2022
1 parent 0351049 commit da1e65f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion explorer/client/src/utils/api/DefaultRpcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type JsonBytes = { bytes: number[] };

const useLocal = false;
const LOCAL = 'http://127.0.0.1:5001';
const DEVNET = 'http://34.105.36.61:9000';
const DEVNET = 'http://gateway.devnet.sui.io:9000';

const rpcUrl = tryGetRpcSetting() ?? useLocal ? LOCAL : DEVNET;

Expand Down
14 changes: 14 additions & 0 deletions sdk/typescript/src/types/framework/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function transformObjectContent(input: ObjectContent): ObjectContent {
return;
}
const parsers: typeof MoveObjectContentTransformer[] = [
BalanceTransformer,
StringTransformer,
UniqueIDTransformer,
];
Expand Down Expand Up @@ -100,6 +101,19 @@ class UniqueIDTransformer extends MoveObjectContentTransformer {
}
}

class BalanceTransformer extends MoveObjectContentTransformer {
static toFieldValue(input: ObjectContent): ObjectContentField {
if (BalanceTransformer.canTransform(input)) {
return input.fields['value'] as number;
}
return input;
}

static canTransform(input: ObjectContent): boolean {
return input.type.startsWith('0x2::Balance::Balance');
}
}

// from https://weblog.rogueamoeba.com/2017/02/27/javascript-correctly-converting-a-byte-array-to-a-utf-8-string/
function stringFromUTF8Array(data: Uint8Array): string | null {
const extraByteMap = [1, 1, 1, 1, 2, 2, 3, 0];
Expand Down
23 changes: 14 additions & 9 deletions sdk/typescript/test/mocks/data/objects.json
Original file line number Diff line number Diff line change
Expand Up @@ -180,39 +180,44 @@
"status": "Exists",
"details": {
"objectRef": {
"objectId": "07db46736b11cc9e46ea2bbcaf4b71bea706ea4e",
"version": 10,
"digest": "DfpEDDD2HEyLnSDsAw1q9t8+8Pxh557hu+m0x0a3RGM="
"objectId": "dbe11ea56da16992d8e68d07427fe74a85606d5e",
"version": 2,
"digest": "hobnw2DdC2quOLZf7D7QiRtAJGPP3GeAJA3iPmEoEaA="
},
"objectType": "moveObject",
"object": {
"contents": {
"fields": {
"balance": {
"fields": {
"value": 50000
},
"type": "0x2::Balance::Balance<0x2::SUI::SUI>"
},
"id": {
"fields": {
"id": {
"fields": {
"id": {
"fields": {
"bytes": "07db46736b11cc9e46ea2bbcaf4b71bea706ea4e"
"bytes": "dbe11ea56da16992d8e68d07427fe74a85606d5e"
},
"type": "0x2::ID::ID"
}
},
"type": "0x2::ID::UniqueID"
},
"version": 10
"version": 2
},
"type": "0x2::ID::VersionedID"
},
"value": 93122
}
},
"type": "0x2::Coin::Coin<0x2::SUI::SUI>"
},
"owner": {
"AddressOwner": "ccdd6ef735e9d2081da41e6c112e9b0c45bb1f0a"
"AddressOwner": "f16a5aedcdf9f2a9c2bd0f077279ec3d5ff0dfee"
},
"tx_digest": "xTvGzQA/4oKdq74Ny/r+Ms8vhIynA7LfuD7lf+dtJoU="
"tx_digest": "vU/KG88bjX8VgdNtflBjNUFNZAqYv2qMCwP9S0+tQRc="
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions sdk/typescript/test/types/transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ describe('Test simplify common Move structs', () => {
const coin = getContent('coin');
const expected = {
...coin,
fields: {
...coin.fields,
balance: 50000,
},
};
replaceField(expected, 'id', '07db46736b11cc9e46ea2bbcaf4b71bea706ea4e');
expect(transformObjectContent(coin)).toEqual(expected);
Expand Down

0 comments on commit da1e65f

Please sign in to comment.