如何将“类型”传递给Flow CLI中的交易和脚本

发布于 2025-01-18 11:12:57 字数 606 浏览 2 评论 0 原文

给定一个脚本:

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

Given a script:

pub fun main(type: Type): Type {
    return type
}

How do you pass in a Type param to the script using Flow CLI?

Using this command:

flow scripts execute ./cadence/scripts/test.cdc --args-json '[{"type":"Type","value":{"staticType":"Int"}}]'

The command fails with the following error:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

執念 2025-01-25 11:12:57

上面问题中的命令使用文档中指定的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上,然后:

import MyNFT from 0x1234567890

pub fun main(): Type {
    let type = Type<@MyNFT.NFT>() 
    return type
}

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 called MyNFT, and MyNFT has been deployed to 0x1234567890 on Testnet, then:

import MyNFT from 0x1234567890

pub fun main(): Type {
    let type = Type<@MyNFT.NFT>() 
    return type
}
花期渐远 2025-01-25 11:12:57

您可以解决问题的另一种方法是将类型标识符发送到脚本中,然后使用运行时类型创建Cadence中的实际类型,

pub fun main(type: String): Type {
    return CompositeType(type)
}

然后您将使用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

pub fun main(type: String): Type {
    return CompositeType(type)
}

Then you would call the script with a normal string in the cli.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文