如何将“类型”传递给Flow CLI中的交易和脚本
给定一个脚本:
pub fun main(type: Type): Type {
return type
}
如何使用 Flow CLI 将类型参数传递给脚本?
使用此命令:
流程脚本执行 ./cadence/scripts/test.cdc --args-json '[{"type":"Type","value":{"staticType":"Int"}}] '
该命令失败并出现以下错误:
Command Error: failed to submit executable script: client: rpc error: code = Unknown desc = [Error Code: 1101] cadence runtime error Execution failed:
error: invalid argument at index 0: cannot import value of type cadence.TypeValue
--> 6136ed6c98f85c642aca23dc50ce10eec81e48ccb5556ccf21f002b861ecf371
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
上面问题中的命令使用文档中指定的json-cadence对象:
https://docs.onflow.org/cadence/json-cadence/json-cadence-specece-pece-spec/ /#类型
当前(2022/3/31)文档不正确,如下所示:
https://github.com/onflow/cadence/issues/1511
解决方案是在JSON对象中包含一个键/值,如上述问题所示:
{ type“:“ type”,“ value”:{“ altatictype”:{“ ank”:“ int”}}}}
注意添加
{“ angn”:“ ank otch”:“ int”}
如果您需要将更复杂的类型传递给Flow CLI,则请参阅此链接,其中突出显示资源类型:
https://github.com/onflow/cadence/blob/314f5fa8a5da8c452d23a42148a1d7927915d613/encoding/json/encoding_test.go#L1180-L1193
You将会看到,通过更复杂的类型流动CLI迅速变得旷日持久,除了最简单的类型以外的任何事物都容易出错。当前,没有简单的解决方案用于传递复杂类型的流动CLI。
如果您的脚本/TX可以容纳体内硬编码的值,则有一个解决方案。
假设您的合同具有称为
nft
的资源,并且该合同称为mynft
,并且mynft
已部署到0x1234567890
在TestNet上,然后:The command in the question above uses the JSON-Cadence object specified in the docs:
https://docs.onflow.org/cadence/json-cadence-spec/#type
Currently (2022/3/31) the docs are incorrect, as indicated by this issue here:
https://github.com/onflow/cadence/issues/1511
The solution is to include an key/value in the JSON object, as demonstrated by the above issue:
{ "type" : "Type", "value" : { "staticType" : { "kind" : "Int" } } }
Notice the addition of
{ "kind": "Int" }
If you need to pass in more complex types to the Flow CLI, then refer to this link, where the resource type is highlighted:
https://github.com/onflow/cadence/blob/314f5fa8a5da8c452d23a42148a1d7927915d613/encoding/json/encoding_test.go#L1180-L1193
You will see that passing in more complex types to Flow CLI quickly becomes protracted and error prone for anything but the simplest types. There is currently no simple solution for passing in complex types to Flow CLI.
If your script/tx can accommodate values hardcoded in the body, then there is a solution.
Assuming you have a contract that has a resource called
NFT
, and the contract is calledMyNFT
, andMyNFT
has been deployed to0x1234567890
on Testnet, then:您可以解决问题的另一种方法是将类型标识符发送到脚本中,然后使用运行时类型创建Cadence中的实际类型,
然后您将使用CLI中的正常字符串调用脚本。
Another way you can solve your problem is to send in a type identifier to your script and then use the functions describe in runtime types to create the actual type in cadence
Then you would call the script with a normal string in the cli.