sui的nodejs前端如何遍历容器内容,vector,table,bag? #274
-
sui的前端如何遍历容器内容,vector,table,bag? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
首先你要将这几个容器存放在 Sui 对象中,才能被 Sui 区块链索引到。我以我在 foxgame-contracts 这个项目中的 FoCRegistry 这个对象为例(省略了其他非必要的字段): struct Global has key {
id: UID,
foc_registry: FoCRegistry,
}
struct FoCRegistry has key, store {
id: UID,
foc_hash: vector<u8>,
types: Table<ID, bool>,
trait_data: Table<u8, Table<u8, Trait>>,
} 其中包含了 {
"jsonrpc": "2.0",
"result": {
"status": "Exists",
"details": {
"data": {
"dataType": "moveObject",
"type": "0x314bcd9ed47e7d200432c6fb871e6f6a5868ebf8::fox::Global",
"has_public_transfer": false,
"fields": {
"foc_registry": {
"type": "0x314bcd9ed47e7d200432c6fb871e6f6a5868ebf8::token_helper::FoCRegistry",
"fields": {
"foc_hash": [
220,
77,
206,
226,
6,
3,
102,
233,
164,
244
],
"id": {
"id": "0x75b49d5a05f2038e9abee1d1ad7d2461b6700cdd"
},
"trait_data": {
"type": "0x2::table::Table<u8, 0x2::table::Table<u8, 0x314bcd9ed47e7d200432c6fb871e6f6a5868ebf8::token_helper::Trait>>",
"fields": {
"id": {
"id": "0x318745cbc36056e03d6761041553eac72637e9f3"
},
"size": "18"
}
},
"types": {
"type": "0x2::table::Table<0x2::object::ID, bool>",
"fields": {
"id": {
"id": "0x2b0ce0c56cab6a1e2f207ee5f72ef6cb04c63b4c"
},
"size": "3"
}
}
}
},
"id": {
"id": "0xc2461548818b1db84824dffffb8daebfb984e612"
}
}
},
"owner": {
"Shared": {
"initial_shared_version": 837
}
},
"previousTransaction": "BBhajvjGQgEB7GMoUKYyQ8zeXtKkKowYQQcAndDX6q2g",
"storageRebate": 95,
"reference": {
"objectId": "0xc2461548818b1db84824dffffb8daebfb984e612",
"version": 945,
"digest": "Tzp7AoUWayP0hIFS6pNc9GOlIBTxh3+pHQDylrqEL6I="
}
}
},
"id": 1
} 其中 vector 类型的 {
"jsonrpc": "2.0",
"result": [
{
"objectId": "0x247153b20340343f9258bc407010084289decefa",
"version": 865,
"digest": "5S3LHSN+pcf3pcIv2ORPtHw2K3bocw0AxxXsP3TjN1Q=",
"type": "0x2::dynamic_field::Field<0x2::object::ID, bool>",
"owner": {
"ObjectOwner": "0x2b0ce0c56cab6a1e2f207ee5f72ef6cb04c63b4c"
},
"previousTransaction": "7znSfXd1aTXmfYU6S3RMwi9Gv7srirRd322KxLeEZ5cg"
},
{
"objectId": "0x2a17433a1e9384c6d848e223b1cafe5400d86904",
"version": 840,
"digest": "JAxNZVVJ7DzjeFUMPEDlQ5Phx2D9FRedwytDURbAJpw=",
"type": "0x2::dynamic_field::Field<0x2::object::ID, bool>",
"owner": {
"ObjectOwner": "0x2b0ce0c56cab6a1e2f207ee5f72ef6cb04c63b4c"
},
"previousTransaction": "BgdXXet7mdJvPbH4qfKUNcAXATvSa7RzpeedB1n81YTG"
},
{
"objectId": "0xa7ab8f058011682807ce96820ddf9dedceb4a70e",
"version": 841,
"digest": "9+vks4xwjX4eRGwCM8ncFsdDBgzVFtz07L1/OPqwTMw=",
"type": "0x2::dynamic_field::Field<0x2::object::ID, bool>",
"owner": {
"ObjectOwner": "0x2b0ce0c56cab6a1e2f207ee5f72ef6cb04c63b4c"
},
"previousTransaction": "6ckScLzosh7tcepqVn2uEKxcr7XiUYZod23arbzwsVF1"
}
],
"id": 1
} 可以看到这个table 中包含3个数据,每个数据的类型为 {
"jsonrpc": "2.0",
"result": {
"status": "Exists",
"details": {
"data": {
"dataType": "moveObject",
"type": "0x2::dynamic_field::Field<0x2::object::ID, bool>",
"has_public_transfer": false,
"fields": {
"id": {
"id": "0x247153b20340343f9258bc407010084289decefa"
},
"name": "0x0ca9d40e1f67362762ccda9e2db8501391fb4459",
"value": true
}
},
"owner": {
"ObjectOwner": "0x2b0ce0c56cab6a1e2f207ee5f72ef6cb04c63b4c"
},
"previousTransaction": "7znSfXd1aTXmfYU6S3RMwi9Gv7srirRd322KxLeEZ5cg",
"storageRebate": 18,
"reference": {
"objectId": "0x247153b20340343f9258bc407010084289decefa",
"version": 865,
"digest": "5S3LHSN+pcf3pcIv2ORPtHw2K3bocw0AxxXsP3TjN1Q="
}
}
},
"id": 1
} 其键 对于 {
"jsonrpc": "2.0",
"result": {
"data": [
{
"name": "0x2::object::ID {bytes: 0xca9d40e1f67362762ccda9e2db8501391fb4459}",
"type": "DynamicField",
"objectType": "bool",
"objectId": "0x247153b20340343f9258bc407010084289decefa",
"version": 865,
"digest": "5S3LHSN+pcf3pcIv2ORPtHw2K3bocw0AxxXsP3TjN1Q="
},
{
"name": "0x2::object::ID {bytes: 0x72678bbacd43e251098fbef5bd37c41a1b4132bf}",
"type": "DynamicField",
"objectType": "bool",
"objectId": "0x2a17433a1e9384c6d848e223b1cafe5400d86904",
"version": 840,
"digest": "JAxNZVVJ7DzjeFUMPEDlQ5Phx2D9FRedwytDURbAJpw="
},
{
"name": "0x2::object::ID {bytes: 0xca39e2c861fbe443e5a12eccedb20ab0c3820690}",
"type": "DynamicField",
"objectType": "bool",
"objectId": "0xa7ab8f058011682807ce96820ddf9dedceb4a70e",
"version": 841,
"digest": "9+vks4xwjX4eRGwCM8ncFsdDBgzVFtz07L1/OPqwTMw="
}
],
"nextCursor": null
},
"id": 1
} bag 的查询方法应该是和 table 类似的,你可以试试。 Sui RPC 中还有 |
Beta Was this translation helpful? Give feedback.
首先你要将这几个容器存放在 Sui 对象中,才能被 Sui 区块链索引到。我以我在 foxgame-contracts 这个项目中的 FoCRegistry 这个对象为例(省略了其他非必要的字段):
其中包含了
vector
类型的foc_hash
,table
类型的types
,我们会用到 Sui RPC 中的sui_getObject
,sui_getObjectsOwnedByObject
,和sui_getDynamicFields
。合约部署之后,可以找到 Global 对象 ID 为0xc2461548818b1db84824dffffb8daebfb984e612
,调用sui_getObject
可以得到对象 Global 的信息: