# Alchemy第八周教程

By [NicWong nicwwd.lens(📦,💫)(🌸, 🌿)](https://paragraph.com/@nicwong-nicwwd-lens) · 2022-08-20

---

Step1 建立测试网络

1.到[Alchemy](https://dashboard.alchemyapi.io/)新建一个Optimism Goerli测试网络

![](https://storage.googleapis.com/papyrus_images/eaa1c808984696cf5e398dded97fcd2b5737ce7850c094de555e993a68bec94e.png)

2.这个网页不要关闭，等会要填url到.env(不过 这里有个问题，因为我用的是eth的Goerli测试网，实际上这一步不知道对不对，大家自己试试。不行就换过来，嘿嘿）

![](https://storage.googleapis.com/papyrus_images/d873d2bcedbb341948ec9a4e30d39e83a49dc043953720a0510ed3ad79aa6237.png)

Step 2 Clone代码

1.先到代码库用为自己建立一个分叉

![](https://storage.googleapis.com/papyrus_images/95232f4df8c13f6c4b14253a35f433a20c900cec31b4df613618b69a98c3fdc7.png)

1.在项目文件夹点击右键，打开git bash

![](https://storage.googleapis.com/papyrus_images/fe39dffc575446c308895d7bafd4e89eb8255aa6c9a7084df5c199a9dcec57b6.png)

2.在MINGW64输入

git clone https://[github.com/nicwwd/ROAD3\_WEEK08.git](http://github.com/nicwwd/ROAD3_WEEK08.git)

![](https://storage.googleapis.com/papyrus_images/dda41c559bff85a97cb7208ac258c690f8447e67f826cc56aecfa7cb21e8dfd8.png)

Step3 修改配置文件

1.用vscode打开ROAD3\_WEEK08文件夹

2.新建一个.env文件，填写上一步的网址和开发钱包的私钥

    REACT_APP_ALCHEMY_API_URL=
    REACT_APP_PRIVATE_KEY=
    

![](https://storage.googleapis.com/papyrus_images/1ccde331dddb2117437905a47354ff297093dd4bb6eef808fcb2e3df91ff0a84.png)

3.编辑hardhat.config.js

    第一行改为
    require("@nomicfoundation/hardhat-toolbox");
    

![](https://storage.googleapis.com/papyrus_images/fbb89cc6b89f1cd254688186543aefbe5d1ca2902271d5c56614df31885adb61.png)

4.打开CMD配置环境

按顺序执行一下命令

cd ROAD3\_WEEK08

npm update

![](https://storage.googleapis.com/papyrus_images/3870623f940396a398a621385e219d919b94d7556a74369239f4d11dbb528f0c.png)

npm install dotenv

npm install yarn

![](https://storage.googleapis.com/papyrus_images/d99a1a52ab596d76e827bd3bf2a51c667da8e8f57e0fcdc70ce0f7fbef8bc7c4.png)

npm install hardhat

![](https://storage.googleapis.com/papyrus_images/3e240c8b296a045a247993438213abd734175c94627f9035078f9e90c508610e.png)

npm install @nomicfoundation/hardhat-toolbox

![](https://storage.googleapis.com/papyrus_images/524e04a2de8e4b15ecc514d649918cd37d5b6b402439e0925f2bc96f585dc54f.png)

5.部署合约

    yarn  hardhat console --network optimism
    因为添加了console，cmd就进入了控制台
    

![](https://storage.googleapis.com/papyrus_images/96c6492a9d1afb0485802e49da519d3b412add8558464d87f10cb00e2046c239.png)

    6.查看一下是不是自己的钱包
    signer = await ethers.getSigner();
    

![](https://storage.googleapis.com/papyrus_images/378f3798448e7ad92ea164a23675aad86ecfe0623d3a0a712e1ac4cfadb33ca0.png)

    7.查询钱包余额
    balance0 = await ethers.provider.getBalance((await ethers.getSigner()).address)
    
    返回钱包余额
    

![](https://storage.googleapis.com/papyrus_images/bad273a5335cd7a1a1546248e0f761ffc281617d022fa6034aadbeaf4240ae55.png)

    8.依次执行三行命令
    factory = ethers.getContractFactory("Casino")
    factory = await factory
    casino = await factory.deploy()
    这样就开始部署合约了。
    最后出来合约地址
    

![](https://storage.googleapis.com/papyrus_images/64d4c86a68a5ecf36b55a1706b8c618c5c0c0c055174f15868f2159c2fb19877.png)

到[Goerli区块浏览器](https://goerli.etherscan.io/)输入地址查询合约，目前Optimism Goerli是没有区块浏览器的，正好我也没有用Optimism Goerli的测试网，所以。。。当然大家也可以用Optimism主网来做。

![](https://storage.googleapis.com/papyrus_images/90db4edfa53b9954c03be10f720bbd355b61f2c9db9649f493cc704bd1544cb0.png)

Step4 测试游戏

    const valA = ethers.utils.keccak256(0xBAD060A7)
    const hashA = ethers.utils.keccak256(valA)
    const valBwin = ethers.utils.keccak256(0x600D60A7)
    tx1 = await casino.proposeBet(hashA,{ value: 1e5})
    

![](https://storage.googleapis.com/papyrus_images/1d23dc9165a0d4c2b4ee12f33c45e701d5ff6fc103d1a9099d532bcfc34e4bb9.png)

然后执行第二个

    tx2 = await casino.acceptBet(hashA, valBwin, {value: 1e5})
    

![](https://storage.googleapis.com/papyrus_images/2425728184d4d13f07e4c10872a216ae340009475b5dc9d5b8454fbb5635522a.png)

![](https://storage.googleapis.com/papyrus_images/5683482bd193a2dee4737373f64847b95e0b178b4dd16b0e8b628fb0b937a58f.png)

到链上看看就可以了。

最后可以上传到github，找找前面的教程

然后提交[第八周表格](https://alchemyapi.typeform.com/roadtoweek8)

如果错了不要找我维权！！！！！！

---

*Originally published on [NicWong nicwwd.lens(📦,💫)(🌸, 🌿)](https://paragraph.com/@nicwong-nicwwd-lens/alchemy)*
