forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[sdk] Fix nested type parsing in TypeTag (MystenLabs#7055)
I've fixed the parsing of nested types in TypeTag ([this comment](https://github.com/MystenLabs/sui/blob/main/sdk/typescript/src/signers/txn-data-serializers/type-tag-serializer.ts#L62-L63)). Also implemented a `tagToString` function which does the oposite of `parseFromStr` (converts `TagType` -> `string`) NOTE: I've removed `normalizeSuiAddress` from address field parsing in `parseFromString` because I don't think this is the right place to use it (`0x2::sui::SUI` becomes `{ address: 0x000000...00002::sui::SUI, ...}`). Especially because the RPC should support both MystenLabs#6777: > For RPC requests, people should be able to pass either way as long as it's supported by parse_sui_struct_tag. Changes: - fixed parsing of nested struct types - added `parseFromString` function - made `TypeTagParser` methods static - exported TypeTagParser in `index.ts` - removed `normalizeSuiAddress` from parsing struct type address field Test plan -- added unit tests.
- Loading branch information
Showing
4 changed files
with
201 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
sdk/typescript/test/unit/signers/txn-data-serializers/type-tag-serializer.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { it, describe, expect } from 'vitest'; | ||
import { TypeTagSerializer } from '../../../../src/signers/txn-data-serializers/type-tag-serializer'; | ||
|
||
describe('parseFromStr', () => { | ||
it('parses nested struct type from a string', () => { | ||
const typeStr = | ||
'0x2::balance::Supply<0x72de5feb63c0ab6ed1cda7e5b367f3d0a999add7::amm::LP<0x2::sui::SUI, 0xfee024a3c0c03ada5cdbda7d0e8b68802e6dec80::example_coin::EXAMPLE_COIN>>'; | ||
const act = TypeTagSerializer.parseFromStr(typeStr); | ||
const exp = { | ||
struct: { | ||
address: '0x2', | ||
module: 'balance', | ||
name: 'Supply', | ||
typeParams: [ | ||
{ | ||
struct: { | ||
address: '0x72de5feb63c0ab6ed1cda7e5b367f3d0a999add7', | ||
module: 'amm', | ||
name: 'LP', | ||
typeParams: [ | ||
{ | ||
struct: { | ||
address: '0x2', | ||
module: 'sui', | ||
name: 'SUI', | ||
typeParams: [], | ||
}, | ||
}, | ||
{ | ||
struct: { | ||
address: '0xfee024a3c0c03ada5cdbda7d0e8b68802e6dec80', | ||
module: 'example_coin', | ||
name: 'EXAMPLE_COIN', | ||
typeParams: [], | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
}, | ||
}; | ||
expect(act).toEqual(exp); | ||
}); | ||
|
||
it('parses non parametrized struct type from a string', () => { | ||
const typeStr = '0x72de5feb63c0ab6ed1cda7e5b367f3d0a999add7::foo::FOO'; | ||
const act = TypeTagSerializer.parseFromStr(typeStr); | ||
const exp = { | ||
struct: { | ||
address: '0x72de5feb63c0ab6ed1cda7e5b367f3d0a999add7', | ||
module: 'foo', | ||
name: 'FOO', | ||
typeParams: [], | ||
}, | ||
}; | ||
expect(act).toEqual(exp); | ||
}); | ||
}); | ||
|
||
describe('tagToString', () => { | ||
it('converts nested struct type to a string', () => { | ||
const type = { | ||
struct: { | ||
address: '0x2', | ||
module: 'balance', | ||
name: 'Supply', | ||
typeParams: [ | ||
{ | ||
struct: { | ||
address: '0x72de5feb63c0ab6ed1cda7e5b367f3d0a999add7', | ||
module: 'amm', | ||
name: 'LP', | ||
typeParams: [ | ||
{ | ||
struct: { | ||
address: '0x2', | ||
module: 'sui', | ||
name: 'SUI', | ||
typeParams: [], | ||
}, | ||
}, | ||
{ | ||
struct: { | ||
address: '0xfee024a3c0c03ada5cdbda7d0e8b68802e6dec80', | ||
module: 'example_coin', | ||
name: 'EXAMPLE_COIN', | ||
typeParams: [], | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
], | ||
}, | ||
}; | ||
const act = TypeTagSerializer.tagToString(type); | ||
const exp = | ||
'0x2::balance::Supply<0x72de5feb63c0ab6ed1cda7e5b367f3d0a999add7::amm::LP<0x2::sui::SUI, 0xfee024a3c0c03ada5cdbda7d0e8b68802e6dec80::example_coin::EXAMPLE_COIN>>'; | ||
expect(act).toEqual(exp); | ||
}); | ||
}); |