欢迎阅读又一篇关于以太坊编程基础的文章。在之前的文章中,我们已经知道了开发去中心化应用程序的流程和基本工具,在这篇文章中,我们将讨论怎样在公共测试网上部署 dapp,我也会为大家介绍一些有帮助的以太坊开发者资源。
Infura 的主要目的是提供对以太坊和 IPFS 网络即时访问,而无需在以太坊或者 IPFS 节点上做那些繁琐的设置。
Infura 基于最新的 microservice-driven 架构,可以动态扩容以支持 API 。开发者可以通过 HTTPs 和 WebSocket 链接到以太坊和 IPFS ,请求响应时间比其他服务和自托管解决方案要快 20 倍。我们的 API 套件会始终保持最新的更新,并在所有网络更新期间保持服务可用。——来自 Infura 的产品描述[4]
这一节,我们介绍把智能合约部署到 kovan 测试网的步骤。在 Ethereum stackexchange[5] 可以找到不同以太坊测试网之间的详细对比。
.secrets.json
,然后添加下面的内容:{
"mnemonic": <your mnemonic>,
"projectId": <your project Id>
}
HDWalletProvider
:要部署智能合约,我们必须发送一笔带有合约字节码的交易到网络上,并且,为了给这笔交易签名,我们还需要 truffle 框架的hdwallet-provider
包:npm install @truffle/hdwallet-provider
.secrets.json
文件。(更多配置细节请看truffle 文档[7])const HDWalletProvider = require("@truffle/hdwallet-provider");
const fs = require("fs");
const secrets = JSON.parse(fs.readFileSync(".secrets.json").toString().trim());
在网络部分添加一个新网络:
kovan: {
networkCheckTimeout: 10000,
provider: () => {
return new HDWalletProvider(
secrets.mnemonic,
`wss://kovan.infura.io/ws/v3/${secrets.projectId}`
);
},
network_id: "42",
},
现在可以用终端来将项目部署到 Kovan 网络了:
truffle migrate --network kovan
现在你已经具备写智能合约,开发 dapp 的基础知识了,你已经准备好下一步了。
这一节,我会推荐一些帮助你提升以太坊开发能力的资料:
电子书:精通以太坊-中文版[8]
Hands-On Smart Contract Development with Solidity and Ethereum: From Fundamentals to Deployment[9]
Mastering Blockchain Programming with Solidity: Write production-ready smart contracts for Ethereum…[10]
以太坊智能合约最佳实践[13]
ConsenSys/ethereum-developer-tools-list[14]
Solidity 最新中文文档[15]
address(this).balance
基于合约余额写任何逻辑,因为以太币可以使用selfdestrct(recipient)
强制发送给合约。tx.origin
授权。这是以太坊编程基础系列最后一篇文章了。如果你需要更高级的主题,可以从这些内容开始:Oracles[19], Gasless transactions[20]。
你可能还想看看一些 github 上的工具:scaffold-eth[21] solidity-template[22]
原文:https://medium.com/nerd-for-tech/deploy-your-smart-contracts-to-a-public-testnet-7f9aef3f6039
[1]
登链翻译计划: https://github.com/lbc-team/Pioneer
[2]
翻译小组: https://learnblockchain.cn/people/412
[3]
Tiny 熊: https://learnblockchain.cn/people/15
[4]
产品描述: https://infura.io/product
[5]
Ethereum stackexchange: https://ethereum.stackexchange.com/questions/27048/comparison-of-the-different-testnets
[6]
这里: https://blog.infura.io/getting-started-with-infura-28e41844cc89/
[7]
truffle文档: https://www.trufflesuite.com/docs/truffle/reference/configuration#providers
[8]
电子书:精通以太坊-中文版: https://learnblockchain.cn/goods/6
[9]
Hands-On Smart Contract Development with Solidity and Ethereum: From Fundamentals to Deployment: https://www.amazon.com/Hands-Contract-Development-Solidity-Ethereum/dp/1492045268/ref=sr_1_1?crid=1BEZJDSK8C00C&dchild=1&keywords=hands+on+smart+contract+development&qid=1614801020&sprefix=hands+on+smart+contract+%2Cdigital-text%2C661&sr=8-1
[10]
Mastering Blockchain Programming with Solidity: Write production-ready smart contracts for Ethereum…: https://www.amazon.com/Mastering-Blockchain-Programming-Solidity-production-ready-ebook/dp/B07W5F8S1L/ref=sr_1_1?dchild=1&keywords=Mastering+Blockchain+Programming+with+Solidity&qid=1614801145&sr=8-1
[11]
eattheblock-pro: https://eattheblocks-pro.teachable.com/p/6-figure-blockchain-developer
[12]
Consensys academy courses: https://learn.consensys.net/catalog
[13]
以太坊智能合约最佳实践: https://consensys.github.io/smart-contract-best-practices/
[14]
ConsenSys/ethereum-developer-tools-list: https://github.com/ConsenSys/ethereum-developer-tools-list
[15]
Solidity最新中文文档: https://learnblockchain.cn/docs/solidity/
[16]
这里: https://docs.soliditylang.org/en/v0.8.2/security-considerations.html#use-the-checks-effects-interactions-pattern
[17]
撤回(取款)模式: https://github.com/wissalHaji/solidity-coding-advices/tree/master/design-patterns/security#withdrawal-pattern
[18]
这里: https://github.com/wissalHaji/solidity-coding-advices/blob/master/best-practices/be-careful-with-loops.md
[19]
Oracles: https://ethereum.org/en/developers/docs/oracles/
[20]
Gasless transactions: https://docs.openzeppelin.com/learn/sending-gasless-transactions
[21]
scaffold-eth: https://github.com/austintgriffith/scaffold-eth
[22]
solidity-template: https://github.com/paulrberg/solidity-template
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有