mirror of
https://github.com/rjNemo/wave-portal
synced 2026-06-06 00:56:39 +00:00
backend done
This commit is contained in:
parent
48a1a2c795
commit
2eb51f55c6
9 changed files with 27085 additions and 0 deletions
9
.gitignore
vendored
Normal file
9
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
node_modules
|
||||
.env
|
||||
coverage
|
||||
coverage.json
|
||||
typechain
|
||||
|
||||
#Hardhat files
|
||||
cache
|
||||
artifacts
|
||||
24
README.md
Normal file
24
README.md
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
# Basic Sample Hardhat Project
|
||||
|
||||
This project demonstrates a basic Hardhat use case. It comes with a sample contract, a test for that contract, a sample script that deploys that contract, and an example of a task implementation, which simply lists the available accounts.
|
||||
|
||||
Try running some of the following tasks:
|
||||
|
||||
```shell
|
||||
npx hardhat accounts
|
||||
npx hardhat compile
|
||||
npx hardhat clean
|
||||
npx hardhat test
|
||||
npx hardhat node
|
||||
node scripts/run.js
|
||||
npx hardhat help
|
||||
```
|
||||
|
||||
## Rinkeby
|
||||
|
||||
Contract deployed at address:
|
||||
|
||||
- 0xBf73336a5603e4E01E7f3e9b4B9CbafEdC57aeb4
|
||||
- 0xF2c9E46328b204b2A7C79Af92C2d24184757877E
|
||||
- 0xc366E39Ad6D14Ad44475abcB2E0Cc6fDD1BD3637
|
||||
- 0x427749f45eC0E0B76D1fBE5A1a4aa7b3C8132bb6
|
||||
64
contracts/WavePortal.sol
Normal file
64
contracts/WavePortal.sol
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
// SPDX-License-Identifier: UNLICENSED
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "hardhat/console.sol";
|
||||
|
||||
contract WavePortal {
|
||||
constructor() payable {
|
||||
console.log("Yo yo, I am a contract and I am smart");
|
||||
seed = newSeed();
|
||||
}
|
||||
|
||||
uint256 totalWaves;
|
||||
mapping(address => uint256) peopleToWaves;
|
||||
mapping(address => uint256) lastWavedAt;
|
||||
uint256 private seed;
|
||||
|
||||
event NewWave(address indexed from, uint256 timestamp, string message);
|
||||
|
||||
struct Wave {
|
||||
address waver;
|
||||
string message;
|
||||
uint256 timestamp;
|
||||
}
|
||||
|
||||
Wave[] waves;
|
||||
|
||||
function wave(string memory _message) public {
|
||||
require(
|
||||
lastWavedAt[msg.sender] + 15 minutes <= block.timestamp,
|
||||
"Wait 15 minutes to wave again"
|
||||
);
|
||||
lastWavedAt[msg.sender] = block.timestamp;
|
||||
totalWaves++;
|
||||
peopleToWaves[msg.sender]++;
|
||||
console.log("%s has waved with message: %s", msg.sender, _message);
|
||||
|
||||
waves.push(Wave(msg.sender, _message, block.timestamp));
|
||||
|
||||
seed = newSeed();
|
||||
|
||||
if (seed <= 50) {
|
||||
console.log("%s won!", msg.sender);
|
||||
uint256 prizeAmount = 0.001 ether;
|
||||
require(prizeAmount <= address(this).balance);
|
||||
(bool success, ) = (msg.sender).call{value: prizeAmount}("");
|
||||
require(success, "Failed to withdraw from the contract"); // mark the transaction as an error if it failed
|
||||
}
|
||||
|
||||
emit NewWave(msg.sender, block.timestamp, _message);
|
||||
}
|
||||
|
||||
function getTotalWaves() public view returns (uint256) {
|
||||
console.log("We have %d waves!", totalWaves);
|
||||
return totalWaves;
|
||||
}
|
||||
|
||||
function getAllWaves() public view returns (Wave[] memory) {
|
||||
return waves;
|
||||
}
|
||||
|
||||
function newSeed() private view returns (uint256) {
|
||||
return (block.timestamp + block.difficulty) % 100;
|
||||
}
|
||||
}
|
||||
27
hardhat.config.js
Normal file
27
hardhat.config.js
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
require("@nomiclabs/hardhat-waffle");
|
||||
require("dotenv").config();
|
||||
|
||||
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
|
||||
const accounts = await hre.ethers.getSigners();
|
||||
|
||||
for (const account of accounts) {
|
||||
console.log(account.address);
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @type import('hardhat/config').HardhatUserConfig
|
||||
*/
|
||||
module.exports = {
|
||||
solidity: "0.8.4",
|
||||
networks: {
|
||||
rinkeby: {
|
||||
url: STAGING_ALCHEMY_URL,
|
||||
accounts: [PRIVATE_KEY],
|
||||
},
|
||||
mainnet: {
|
||||
url: PROD_ALCHEMY_URL,
|
||||
accounts: [PRIVATE_KEY],
|
||||
},
|
||||
},
|
||||
};
|
||||
26861
package-lock.json
generated
Normal file
26861
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
30
package.json
Normal file
30
package.json
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
{
|
||||
"name": "wave-portal",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"accounts": "npx hardhat accounts",
|
||||
"compile": "npx hardhat compile",
|
||||
"test": "npx hardhat test",
|
||||
"run": "npx hardhat run scripts/run.js",
|
||||
"server": "npx hardhat node",
|
||||
"deploy-local": "npx hardhat run scripts/deploy.js --network localhost",
|
||||
"deploy-testnet": "npx hardhat run scripts/deploy.js --network rinkeby",
|
||||
"deploy-mainnet": "npx hardhat run scripts/deploy.js --network mainnet"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@nomiclabs/hardhat-ethers": "^2.0.5",
|
||||
"@nomiclabs/hardhat-waffle": "^2.0.3",
|
||||
"chai": "^4.3.6",
|
||||
"ethereum-waffle": "^3.4.4",
|
||||
"ethers": "^5.6.2",
|
||||
"hardhat": "^2.9.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"dotenv": "^16.0.0"
|
||||
}
|
||||
}
|
||||
24
scripts/deploy.js
Normal file
24
scripts/deploy.js
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
const main = async () => {
|
||||
const [deployer] = await hre.ethers.getSigners();
|
||||
const accountBalance = await deployer.getBalance();
|
||||
|
||||
console.log(`Deploying contracts with address: ${deployer.address}`);
|
||||
console.log(`Account balance: ${accountBalance.toString()}`);
|
||||
|
||||
const Token = await hre.ethers.getContractFactory("WavePortal");
|
||||
const portal = await Token.deploy({ value: hre.ethers.utils.parseEther("0.01") });
|
||||
await portal.deployed();
|
||||
console.log("WavePortal address: ", portal.address);
|
||||
};
|
||||
|
||||
const runMain = async () => {
|
||||
try {
|
||||
await main();
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
runMain();
|
||||
46
scripts/run.js
Normal file
46
scripts/run.js
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
const main = async () => {
|
||||
const [owner, randomPerson] = await hre.ethers.getSigners();
|
||||
const waveContractFactory = await hre.ethers.getContractFactory("WavePortal");
|
||||
const waveContract = await waveContractFactory.deploy({
|
||||
value: hre.ethers.utils.parseEther("0.1"),
|
||||
});
|
||||
await waveContract.deployed();
|
||||
console.log(`Contract deployed to: ${waveContract.address}`);
|
||||
console.log(`Contract deployed by: ${owner.address}`);
|
||||
|
||||
let contractBalance = await hre.ethers.provider.getBalance(waveContract.address);
|
||||
console.log(`Contract balance: ${hre.ethers.utils.formatEther(contractBalance)}`);
|
||||
|
||||
let waveCount;
|
||||
waveCount = await waveContract.getTotalWaves();
|
||||
|
||||
let waveTxn = await waveContract.wave("A message");
|
||||
await waveTxn.wait();
|
||||
|
||||
waveTxn = await waveContract.wave("Hi!");
|
||||
await waveTxn.wait();
|
||||
|
||||
waveCount = await waveContract.getTotalWaves();
|
||||
|
||||
waveTxn = await waveContract.connect(randomPerson).wave("another one");
|
||||
await waveTxn.wait();
|
||||
|
||||
contractBalance = await hre.ethers.provider.getBalance(waveContract.address);
|
||||
console.log(`Contract balance: ${hre.ethers.utils.formatEther(contractBalance)}`);
|
||||
|
||||
let allWaves = await waveContract.getAllWaves();
|
||||
console.log(allWaves);
|
||||
waveCount = await waveContract.getTotalWaves();
|
||||
};
|
||||
|
||||
const runMain = async () => {
|
||||
try {
|
||||
await main();
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
runMain();
|
||||
Loading…
Reference in a new issue