# 第三週作業
---
基礎:
1. 部一個ERC721的合約,寫一個typescript/javascript,運用ethers,完成:
- A錢包Mint並Approve 該NFT 給B地址。
- B地址轉移該NFT到C地址。
2. 用ethers實作一個掏空某錢包地址的ETH的程式
3. 部署一個ERC20,用ethers實作一個掏空該ERC20的程式
進階:
1. 研究Flashbot,並試著將兩筆交易打包在同一個區塊中。
- A錢包部署NFT合約,實作setBaseURI (onlyOwner)
- 將以下兩個交易打包成同一個區塊上鏈:
- B錢包打錢到A錢包
- A錢包設定setBaseURI
---
## 基礎1
我用網站的形式實現,雖然還有很多漏洞
不過能實現 基礎1的要求(Mint, Approve and TransferFrom)
URL如下:
NetWork : SepoliaTestnet
https://dragon-mint-nine.vercel.app/
---
## 基礎2
NetWork : SepoliaTestnet
Development Environment : HardHat
```javascript
const { ethers } = require("hardhat")
module.exports = async () => {
const infuraUrl = "yourInfuraUrl"
const address = "targetAddress"
const privateKey = "targetPrivateKey"
const provider = new ethers.providers.JsonRpcProvider(infuraUrl)
const signer = new ethers.Wallet(privateKey, provider)
const sendEthers = async (recipientAddress, amountInWei) => {
return new Promise(async (resolve, reject) => {
const transaction = {
to: recipientAddress,
value: amountInWei,
gasPrice,
gasLimit: ethers.utils.hexlify(100000),
nonce,
}
const tx = await signer
.sendTransaction(transaction)
.then((transactionHash) => resolve(transactionHash))
.catch((error) => reject(error))
})
}
const balanceInWei = await provider.getBalance(address)
console.log(`balanceInWei : ${balanceInWei}`)
const gasPrice = await provider.getGasPrice()
console.log(`gasPrice : ${gasPrice}`)
const nonce = await provider.getTransactionCount(signer.address, "latest")
console.log(`nonce : ${nonce}`)
let amountInWei = balanceInWei - gasPrice * 200000
amountInWei = amountInWei + ""
console.log(`amountInWei : ${amountInWei}`)
const amount = ethers.utils.formatEther(amountInWei)
console.log(`amount : ${amount}`)
await sendEthers("yourAddress", ethers.utils.parseEther(amount))
}
module.exports.tags = ["all", "ethersjs"]
```
---
## 基礎3
NetWork : SepoliaTestnet
Development Environment : HardHat
```javascript
const { ethers } = require("hardhat")
module.exports = async () => {
const infuraUrl = "yourInfuraUrl"
const address = "targetAddress"
const privateKey = "targetPrivateKey"
const provider = new ethers.providers.JsonRpcProvider(infuraUrl)
const signer = new ethers.Wallet(privateKey, provider)
console.log(`Link Token Transfer`)
const LinkContractAbi_PartOf = [
"function balanceOf(address _account) public view returns (uint256)",
"function transfer(address _recipient, uint256 _amount) public returns (bool success)",
]
const LinkContract_SepoliaTestnet = new ethers.Contract(
"0x779877A7B0D9E8603169DdbD7836e478b4624789",
LinkContractAbi_PartOf,
signer
)
const balanceOf = await LinkContract_SepoliaTestnet.balanceOf(address)
const balanceOf_readable = ethers.utils.formatUnits(balanceOf, 18)
console.log(`balanceOf : ${balanceOf}`)
console.log(`balanceOf_readable : ${balanceOf_readable}`)
const transfer = await LinkContract_SepoliaTestnet.transfer(
"yourAddress",
ethers.utils.parseEther(balanceOf_readable)
)
}
module.exports.tags = ["all", "ethersjs"]
```
---
## 進階1
未定
---