Danai
February 14, 2023, 9:33am
1
This is a question from the Suinami Riders group (Telegram).
I’m having trouble with rpc.getDynamicFieldObject(parentId, fieldName)
because I don’t know how to serialize dynamic field names.
Say I have a parent object which contains two dynamic field objects:
The name for the 1st one is “Early”, as a vector<u8>
The name for the 2nd one is “Early”, as a sui::string::String
They look like this on-chain:
sui>-$ dynamic-field --json PARENT_OBJECT_ID
{
"data": [
{ "name": "vector[69u8, 97u8, 114u8, 108u8, 121u8]", ... },
{ "name": "0x1::string::String {bytes: vector[69u8, 97u8, 114u8, 108u8, 121u8]}", ... }
], ...
}
Now I want to fetch these dynamic objects from JS:
// Doesn't work:
rpc.getDynamicFieldObject(PARENT_OBJECT_ID, 'Early');
// Works (1st object):
rpc.getDynamicFieldObject(PARENT_OBJECT_ID, 'vector[69u8, 97u8, 114u8, 108u8, 121u8]');
// Works (2nd object):
rpc.getDynamicFieldObject(PARENT_OBJECT_ID, '0x1::string::String {bytes: vector[69u8, 97u8, 114u8, 108u8, 121u8]}');
My question is, how can I transform the JS string “Early” into the correct format for dynamic field names so that rpc.getDynamicFieldObject()
finds the objects?
3 Likes
Thanks for posting my question here. Just to be clear, rpc
in the example above is an instance of JsonRpcProvider
from @mysten/sui.js
.
1 Like
Looks like this PR will solve it:
MystenLabs:main
← MystenLabs:pat/dynamic_field_parse_name
opened 04:31PM - 11 Jan 23 UTC
Current implementation of RPC dynamic field query use the `MoveValue::to_string`… as name, which is quite ugly and hard to construct on client side to be used in `sui_getDynamicFieldObject`.
Example name struct in move:
```
struct Name has copy, drop, store {
name_str: std::string::String
}
```
`sui_getDynamicFields` response:
```
{
"jsonrpc": "2.0",
"result": {
"data": [
{
"name": "0xc03bf9390eb6849760890f4dabc53e64505c0159::object_basics::Name {name_str: 0x1::string::String {bytes: vector[84u8, 101u8, 115u8, 116u8, 32u8, 78u8, 97u8, 109u8, 101u8]}}",
"type": "DynamicField",
"objectType": "0xc03bf9390eb6849760890f4dabc53e64505c0159::object_basics::Object",
"objectId": "0x3f8af3b8158af4b25d51c7d39c7c514cea94b1d0",
"version": 10727,
"digest": "AeSblVNBG3z6hTjwzwYE5rbUpEcjCEXzOUEt5rObkHs="
}
],
"nextCursor": null
},
"id": 1
}
```
This PR change the String name to `DynamicFieldName`, which is a struct of type and json value.
```
pub struct DynamicFieldName {
pub type_: TypeTag,
pub value: Value,
}
```
The `name` field become more readable and easier to create on client side.
```
{
"jsonrpc": "2.0",
"result": {
"data": [
{
"name": {
"type": "0x1a43c696e91e24e8128c6764e713d38d24ffe4e1::object_basics::Name",
"value": {
"name_str": "Test Name"
}
},
"type": "DynamicField",
"objectType": "0x1a43c696e91e24e8128c6764e713d38d24ffe4e1::object_basics::Object",
"objectId": "0x7fe687114a592a8ae5baecf5954a3d0d2a74c60b",
"version": 5,
"digest": "fOmZ+NrNl2BP5MT1aGJ+YUk1rpiAmF5Bg6qGGbcgaIw="
}
],
"nextCursor": null
},
"id": 1
}
```
Example response for primitive type name:
```
{
"jsonrpc": "2.0",
"result": {
"data": [
{
"name": {
"type": "bool",
"value": true
},
"type": "DynamicField",
"objectType": "0x1a43c696e91e24e8128c6764e713d38d24ffe4e1::object_basics::Object",
"objectId": "0x33274b734d787c19fc19871fb7b8c51a048b58b0",
"version": 8,
"digest": "56q6L9uzK4OswbZ9VvQak4CdOrhwA5DiweXrcA44JWA="
}
],
"nextCursor": null
},
"id": 1
}
```
2 Likes