简体   繁体   English

如何使用 Go 连接到已部署的智能合约?

[英]How can I connect to already deployed smart contracts with Go?

I'm a budding blockchain dev, and I've been using Go to dive into things as that's the language I'm most comfortable with.我是一名崭露头角的区块链开发人员,我一直在使用 Go 来深入研究,因为这是我最熟悉的语言。

I've at an impasse now, and hope I can get some help.我现在陷入僵局,希望能得到一些帮助。

Pretty much all the tutorials I've seen on working with smart contracts with Go involve using geth to compile the Solidity source code to its ABI and binary forms then generating a.go file with bindings to the contract for deployment and other actions.我看到的几乎所有关于使用 Go 使用智能合约的教程都涉及使用 geth 将 Solidity 源代码编译为其 ABI 和二进制 forms,然后生成一个 .go 文件,其中包含部署和其他操作的合约绑定。

This is fine when working with my own created contracts, but how would I go around interfacing with already deployed third-party contracts such as Cryptokitties for instance?这在使用我自己创建的合约时很好,但是我 go 如何与已经部署的第三方合约(例如 Cryptokitties)交互? Basically, in the cases where I may not have the luxury of having access to the contract source code to generate bindings.基本上,在我可能无法访问合约源代码来生成绑定的情况下。

Going through the Cryptozombies course, with Solidity I just had to define the interfaces of the desired contract, but Go doesn't seem to be as straightforward.通过 Cryptozombies 课程,我只需要使用 Solidity 定义所需合约的接口,但 Go 似乎并不那么简单。

You could manually save the ABI files from Etherscan to your local workspace to generate the binding.您可以手动将 ABI 文件从 Etherscan 保存到本地工作区以生成绑定。

For example, you can copy ABI from this CryptoKitties contract .例如,您可以从这个CryptoKitties 合约复制 ABI。 As long as the contracts are verified, you can always get the ABIs.只要合约经过验证,你就可以随时获得 ABI。

Note:笔记:

  • Solidity is the only language that doesn't need ABI because it is the native language of the contract development on the EVM (Ethereum Virtual Machine). Solidity 是唯一不需要 ABI 的语言,因为它是 EVM(以太坊虚拟机)上合约开发的母语。
  • You could also use Etherscan API to programmatically retrieve the ABI.您还可以使用Etherscan API以编程方式检索 ABI。 But I don't think we need to go that far in most of the cases.但我认为在大多数情况下我们不需要那么远的 go 。

Read more: https://geth.ethereum.org/docs/dapp/native-bindings阅读更多: https://geth.ethereum.org/docs/dapp/native-bindings

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM