如何使用javascript从智能合约中检索数据?
2 个回答
- 投票数
-
- 2020-01-25
从当前的巴比伦协议和即将到来的迦太基开始,RPC仅使您能够检索提供给RPC的已知密钥的值.但是即使在这种情况下,您也不需要提供存储的可读密钥,而应提供该密钥的哈希,因为这是它在内部的表示方式. 此答案描述了如何调用RPC
RPC不提供"获取所有键"或"获取所有值"的方法.为此,您必须进行不切实际的解析,因此建议您使用索引器或库来为您完成大部分工作.
对于单键查找,Taquito库允许您执行以下操作:
const contract = await Tezos.contract.at("KT1...") const storage = await contract.storage() const bigMapDetail = await storage.namedbigmap.get("readable lookup key")
请访问 https:/,详细了解塔基托和大地图/medium.com/tezoscommons/new-taquito-release-now-with-bigmaps-7d7352351af4
要获取所有值,tzStats索引器提供了一个易于使用的API(很快其他索引器也可能会添加它).示例TzStats调用从一张大地图的合同中获取所有值:
curl "https://api.tzstats.com/explorer/bigmap/17/values"
在 https://tzstats.com/上详细了解tzStats大地图支持.博客/tezos-smart-contract-apis/
如果您不想依靠索引器,也可以使用PyTezos"big_map_get"为您完成一些繁重的工作. https://baking-bad.github.io/pytezos/
As of the current Babylon protocol and the upcoming Carthage, the RPC only provides you the ability to retrieve the value for a known key that you provide to the RPC. But even in this scenario you need to provide not the readable key you stored, but a hash of that key as that is how it is represented internally. This answer describes how to call the RPC
The RPC does not provide a means to "get all keys" or to "get all values". To accomplish this you must do an impractical amount of parsing so it is recommended that you use either an indexer or a library to do most of the work for you.
For single key lookup, Taquito library allows you to do the following:
const contract = await Tezos.contract.at("KT1...") const storage = await contract.storage() const bigMapDetail = await storage.namedbigmap.get("readable lookup key")
Read more on Taquito and big maps at https://medium.com/tezoscommons/new-taquito-release-now-with-bigmaps-7d7352351af4
To get all the values, the tzStats indexer provides an easy to use API (soon other indexers are likely to add this as well). Example TzStats call to get all the values out of a contract with a big map:
curl "https://api.tzstats.com/explorer/bigmap/17/values"
Read more on tzStats big map support at https://tzstats.com/blog/tezos-smart-contract-apis/
You can also use PyTezos "big_map_get" to do some of the heavy lifting for you if you don't want to rely on an indexer. https://baking-bad.github.io/pytezos/
-
谢谢你使用tzstats适合我的情况,但是当我尝试检索json时会抛出CORS错误.有没有办法实现$ .getJSON(https://api.babylonnet.tzstats.com/explorer/contract/{kt}/storage..`函数?附加`?callback=?`似乎没有实现让它工作.Thanks for this. Using tzstats suits my case but it is throwing a CORS error when I try to retrieve the json. Is there a way to implement the `$.getJSON(https://api.babylonnet.tzstats.com/explorer/contract/{kt}/storage..` function? Appending `?callback=?` doesn't seem to get this to work.
- 0
- 2020-01-28
- macourtney7
-
如果要从基于浏览器的脚本中调用它,则需要使用的服务器端点提供程序将您的调用域列入CORS白名单.在[email protected]上给Alex留言if you are going to call it from a browser based script you will need to get your calling domain whitelisted for CORS by the provider of the server endpoint you are using. Message Alex at [email protected]
- 0
- 2020-01-28
- cousinit
-
我相信亚历克斯提供了第二个答案,希望他会尽快回应.我正在用[cors-anywhere](https://github.com/Rob--W/cors-anywhere/)代理请求以完成原型.I believe Alex provided the second answer, hopefully he will respond soon. I am proxying requests with [cors-anywhere](https://github.com/Rob--W/cors-anywhere/) to complete the prototype.
- 0
- 2020-01-28
- macourtney7
-
我们没有实现JSONP的计划.您可以开箱即用地运行我们自己的支持CORS标头的索引器副本,或向我发送请求以将其列入公共API白名单.We have no plans to implement JSONP. You can run your own copy of our indexer which supports CORS headers out of the box or send me a request to get whitelisted on our public API.
- 0
- 2020-01-29
- Alexander Eichhorn
-
- 2020-01-25
我们在TzStats维护所有历史大图数据的索引.在bigmap调用中获取所有当前键和值
https://api.tzstats.com/explorer/bigmap/:id/values
如果您的大图很大,则可以使用
limit
和offset
参数(默认为100个条目,最大为500个)对结果进行分页.有关更多详细信息和示例,请参见我们的大地图文档.如果您喜欢使用Tezos RPC,请阅读此答案.现在,RPC仅在每次调用时获取一个bigmap密钥,您需要知道密钥的脚本表达式哈希.
We maintain an index of all historic bigmap data at TzStats. To fetch all current keys and values in a bigmap call
https://api.tzstats.com/explorer/bigmap/:id/values
If your bigmap is very large you can page through the result with
limit
andoffset
parameters (default is 100 entries, max is 500). See our bigmap docu for more details and examples.If you prefer using the Tezos RPC read this answer. Right now the RPC fetches a single bigmap key per call only and you need to know the script expression hash of your key.
-
嗨,Alex,我希望在一个请求中返回所有bigmap值.合同部署后,完整的bigmap使用`../explorer/contract/{hash}/storage`显示,但更新后仅显示bigmap ID.我尝试了`../explorer/bigmap/{id}/values`,但这会产生20个条目.我将如何获取"所有**"键/值(仅32个)?非常感谢.Hi Alex, I wish to return all bigmap values in a single request. After contract deployment the full bigmap is shown using `../explorer/contract/{hash}/storage` but after an update it shows only the bigmap id. I tried `../explorer/bigmap/{id}/values` but this produces 20 entries. How would I fetch **all** key/values (only 32)? Many thanks.
- 0
- 2020-01-30
- macourtney7
-
我将" limit"参数从默认值(最多20个条目)追加到了.I appended the `limit` parameter to up from default (20 entries).
- 0
- 2020-01-30
- macourtney7
-
您自己找到了,很好.与../explorer下的所有其他列表终结点一样,../explorer/bigmap/{id}/values终结点支持使用offset和limit进行分页.最大限制为100.一个重要的问题是,在两次呼叫之间,链可以前进或重组.对于稳定的分页(不受新块的影响),请使用附加参数"block={hash| height}",该参数可将您的呼叫有效地锁定到历史记录中的特定点.好处是,如果两次调用之间的链重组,则会出现409错误,因此您知道所有先前的页面都是陈旧的.You found it yourself, good. The `../explorer/bigmap/{id}/values` endpoint like all other listing endpoints under `../explorer` support paging using `offset` and `limit`. Max limit is 100. One important issue is that between two paged calls the chain can advance or reorganize. For stable paging (not-influenced by new blocks) use an additional parameter `block={hash|height}` which effectively locks your call to a specific point in history. Benefit is that if the chain reorged between calls you get a 409 error, so you know all previous pages are stale.
- 1
- 2020-02-02
- Alexander Eichhorn
我已存储要从已部署的智能合约中检索的数据.
存储结构是使用SmartPy定义的:
此帖子的答案似乎正在沿用右行,但不再有效.如果可能的话,解决方案还应该返回JSON对象,这是所希望的.
我想检索存储在
sp.big_map
和三个sp.address
容器中的数据.bmap
中有固定的32个项目,可通过诸如better- call.dev非常感谢您的帮助!