Skip to content

Commit

Permalink
Merge branch 'wallet' of github.com:jasonandjay/Dapp-Learning into wa…
Browse files Browse the repository at this point in the history
…llet
  • Loading branch information
jasonandjay committed Mar 30, 2024
2 parents 58e26c5 + 85963cf commit 3788ad2
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 75 deletions.
6 changes: 6 additions & 0 deletions basic/05-ethersjs-erc20/README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@
node index.js
```

4. 查看 mempool 中 pending 的交易

```sh
node mempool.js
```

## 参考文档

官方文档:
Expand Down
6 changes: 6 additions & 0 deletions basic/05-ethersjs-erc20/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ Difference between `web3.js` and `ethers.js` can be seen [here](./web3-vs-ethers
node index.js
```

4. check the pending transactions in mempool

```sh
node mempool.js
```

## References

Official documentation:
Expand Down
39 changes: 39 additions & 0 deletions basic/05-ethersjs-erc20/mempool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { ethers } = require('ethers');
require('dotenv').config();

async function main() {
const provider = new ethers.WebSocketProvider(`wss://sepolia.infura.io/ws/v3/${process.env.INFURA_ID}`);

function limitRPCRequest(fn, delay) {
let timer;
return function(){
if(!timer) {
fn.call(this, ...arguments)
timer = setTimeout(()=>{
clearTimeout(timer)
timer = null
},delay)
}
}
}

console.log("begin to listen to pending transactions")
let j = 0
var finishFlag = false
provider.on("pending", limitRPCRequest(async (txHash) => {
if (txHash && j <= 20) {
// get the detail of transactions
let tx = await provider.getTransaction(txHash);
console.log(`[${(new Date).toLocaleTimeString()}] : ${txHash}`);
console.log(tx);
j++
}else{
finishFlag = true
}
}, 1000));

}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main()
6 changes: 3 additions & 3 deletions basic/15-nft-blindbox-chainlink-vrf/README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ INFURA_ID=yyyyyyyy

- 部署测试合约
```
npx hardhat run scripts/deploy.js --network goerli
npx hardhat run scripts/deploy.js --network sepolia
```

- 获取随机数
```
npx hardhat run scripts/random-number-vrf.js --network goerli
npx hardhat run scripts/random-number-vrf.js --network sepolia
```

- 生成随机 Character
```
npx hardhat run scripts/transaction.js --network goerli
npx hardhat run scripts/transaction.js --network sepolia
```

## 参考链接
Expand Down
6 changes: 3 additions & 3 deletions basic/15-nft-blindbox-chainlink-vrf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ INFURA_ID=yyyyyyyy

- Deployment test contract
```
npx hardhat run scripts/deploy.js --network goerli
npx hardhat run scripts/deploy.js --network sepolia
```

- Get random number
```
npx hardhat run scripts/random-number-vrf.js --network goerli
npx hardhat run scripts/random-number-vrf.js --network sepolia
```

- Generate random Character
```
npx hardhat run scripts/transaction.js --network goerli
npx hardhat run scripts/transaction.js --network sepolia
```

## Refer to the link
Expand Down
33 changes: 5 additions & 28 deletions basic/15-nft-blindbox-chainlink-vrf/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
require("@nomiclabs/hardhat-waffle");
const fs = require("fs");
require("@nomicfoundation/hardhat-toolbox");
require('dotenv').config();
require('hardhat-contract-sizer');
require("@nomiclabs/hardhat-etherscan");

// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async () => {
const accounts = await ethers.getSigners();

for (const account of accounts) {
console.log(account.address);
}
});

// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more

/** @type import('hardhat/config').HardhatUserConfig */
function mnemonic() {

return process.env.PRIVATE_KEY
Expand Down Expand Up @@ -48,24 +33,16 @@ module.exports = {
(you can put in a mnemonic here to set the deployer locally)
*/
},
goerli: {
url: "https://goerli.infura.io/v3/" + process.env.INFURA_ID, //<---- YOUR INFURA ID! (or it won't work)
accounts: [
mnemonic()
],
sepolia: {
url: 'https://sepolia.infura.io/v3/' + process.env.INFURA_ID, //<---- CONFIG YOUR INFURA ID IN .ENV! (or it won't work)
accounts: [mnemonic()],
},
mainnet: {
url: "https://mainnet.infura.io/v3/" + process.env.INFURA_ID, //<---- YOUR INFURA ID! (or it won't work)
accounts: [
mnemonic()
],
},
ropsten: {
url: "https://ropsten.infura.io/v3/" + process.env.INFURA_ID, //<---- YOUR INFURA ID! (or it won't work)
accounts: [
mnemonic()
],
},
},
etherscan: {
apiKey: "1324"
Expand Down
27 changes: 14 additions & 13 deletions basic/15-nft-blindbox-chainlink-vrf/package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
{
"name": "hardhat-project",
"devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.0.2",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"@openzeppelin/test-helpers": "^0.5.6",
"chai": "^4.3.4",
"ethereum-waffle": "^3.3.0",
"ethers": "^5.3.1",
"hardhat": "^2.4.1",
"hardhat-contract-sizer": "^2.5.0"
"name": "15-nft-blindbox-chainlink-vrf",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@chainlink/contracts": "^0.4.0",
"@nomiclabs/hardhat-etherscan": "^3.0.1",
"@nomiclabs/hardhat-web3": "^2.0.0",
"@openzeppelin/contracts": "^4.5.0",
"dotenv": "^10.0.0"
"dotenv": "^16.4.5",
"hardhat": "^2.22.2"
},
"devDependencies": {
"@nomicfoundation/hardhat-toolbox": "^5.0.0"
}
}
10 changes: 5 additions & 5 deletions basic/15-nft-blindbox-chainlink-vrf/scripts/deploy.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@

const hre = require("hardhat");
const { ethers } = require('hardhat');
require('dotenv').config();
const { saveDeployment } = require('./utils');

async function main() {
// We get the contract to deploy
const Dnd = await hre.ethers.getContractFactory("DungeonsAndDragonsCharacter");
const Dnd = await ethers.getContractFactory("DungeonsAndDragonsCharacter");

const dnd = await Dnd.deploy(process.env.SubscriptionId, "http://81.69.8.95/WaterMarginJson/");

await dnd.deployed();
await dnd.waitForDeployment()

console.log("dnd deployed to:", dnd.address);
console.log("dnd deployed to:", dnd.target);

// save contract address to file
saveDeployment({
dndAddress: dnd.address,
dndAddress: dnd.target,
});

}
Expand Down
26 changes: 5 additions & 21 deletions basic/15-nft-blindbox-chainlink-vrf/scripts/random-number-vrf.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const hre = require('hardhat');
require('@nomiclabs/hardhat-web3');
const { BigNumber } = require('ethers');
const { ethers } = require('hardhat');
require('dotenv').config();
const { readDeployment,saveDeployment } = require('./utils');

async function main() {
const provider = new ethers.providers.WebSocketProvider(`wss://goerli.infura.io/ws/v3/${process.env.INFURA_ID}`);
const provider = new ethers.WebSocketProvider(`wss://sepolia.infura.io/ws/v3/${process.env.INFURA_ID}`);
const { abi: DungeonsAndDragonsCharacterABI } = require('../artifacts/contracts/DungeonsAndDragonsCharacter.sol/DungeonsAndDragonsCharacter.json');

const deployment = readDeployment();
Expand All @@ -23,31 +21,17 @@ async function main() {

let random0ID, random0Res;

// 监听randomNumberConsumer 的请求随机数事件
const filterCall = {
address: addr,
topics: [ethers.utils.id('RequestId(address,uint256)')],
};

// 监听chainlink VRF Coordinator 的随机数回写事件
const filterRes = {
address: addr,
topics: [
ethers.utils.id('FulfillRandomness(uint256,uint256[])')
],
};

console.log(`Listen on random number call...`);
dndCharacter.on(filterCall, (sender, requestID) => {
dndCharacter.on("RequestId", (sender, requestID) => {
console.log('event RequestId(address,uint256)');
console.log(`Sender: ${sender} requestID: ${requestID}`)
random0ID = requestID;
});

console.log(`Listen on random number result...`);
dndCharacter.on(filterRes, (requestID, randomNumbers) => {
dndCharacter.on("FulfillRandomness", (requestID, randomNumbers) => {
console.log('event FulfillRandomness(uint256,uint256[])');
if (BigNumber.from(requestID).eq(random0ID)) {
if (BigInt(requestID).eq(random0ID)) {
random0Res = randomNumbers[0];
console.log('random0Res: ', random0Res.toString());
} else {
Expand Down
4 changes: 2 additions & 2 deletions basic/15-nft-blindbox-chainlink-vrf/scripts/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
//
// When running the script with `hardhat run <script>` you'll find the Hardhat
// Runtime Environment's members available in the global scope.
const hre = require("hardhat");
const { ethers } = require('hardhat');
const { readDeployment } = require('./utils');

async function main() {
const deployment = readDeployment();
const addr = deployment.dndAddress;
const requestID = deployment.requestID;

const dnd = await hre.ethers.getContractAt("DungeonsAndDragonsCharacter",addr);
const dnd = await ethers.getContractAt("DungeonsAndDragonsCharacter",addr);

// Do the blindCharacter
console.log("Going to do blindCharacter");
Expand Down

0 comments on commit 3788ad2

Please sign in to comment.