Skip to content

Adding contract #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions SmartContractAakash/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
node_modules
.env
coverage
coverage.json
typechain
typechain-types

# Hardhat files
cache
artifacts

13 changes: 13 additions & 0 deletions SmartContractAakash/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Sample Hardhat Project

This project demonstrates a basic Hardhat use case. It comes with a sample contract, a test for that contract, and a script that deploys that contract.

Try running some of the following tasks:

```shell
npx hardhat help
npx hardhat test
REPORT_GAS=true npx hardhat test
npx hardhat node
npx hardhat run scripts/deploy.js
```
74 changes: 74 additions & 0 deletions SmartContractAakash/contracts/ipl.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;

// Uncomment this line to use console.log
// import "hardhat/console.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract IPLBetting {
struct Bet {
address bettor;
uint amount;
uint team;
bool paidOut;
}

Bet[] public bets;
uint public winningTeam;
bool public gameSettled;
address public owner;
constructor() {
owner = msg.sender;
}

function placeBet(uint _team) public payable {
require(msg.value > 0, "Bet amount must be greater than 0.");
require(gameSettled == false, "Betting is closed.");
require(_team <= 2, "Invalid team selection.");

bets.push(Bet(msg.sender, msg.value, _team, false));
}

function settleBet(uint _winningTeam) public {
require(msg.sender == owner, "Only the owner can settle the bet.");
require(gameSettled == false, "The game has already been settled.");
require(_winningTeam <= 2, "Invalid team selection.");

winningTeam = _winningTeam;
gameSettled = true;

uint totalWinningAmount = 0;
uint totalLosingAmount = 0;

for (uint i = 0; i < bets.length; i++) {
if (bets[i].team == winningTeam) {
totalWinningAmount += bets[i].amount;
bets[i].paidOut = true;
} else {
totalLosingAmount += bets[i].amount;
}
}

for (uint i = 0; i < bets.length; i++) {
if (bets[i].paidOut == false) {
uint payout = (bets[i].amount * totalWinningAmount) / (totalWinningAmount + totalLosingAmount);
payable(bets[i].bettor).transfer(payout);
bets[i].paidOut = true;
}
}
}

function getNumBets() public view returns (uint) {
return bets.length;
}

function getBet(uint _index) public view returns (address, uint, uint, bool) {
require(_index < bets.length, "Invalid index.");
return (bets[_index].bettor, bets[_index].amount, bets[_index].team, bets[_index].paidOut);
}

function getWinningTeam() public view returns (uint) {
require(gameSettled == true, "The game has not been settled yet.");
return winningTeam;
}
}

101 changes: 101 additions & 0 deletions SmartContractAakash/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
require('@nomiclabs/hardhat-ethers');
require('@nomiclabs/hardhat-etherscan');

// Change private keys accordingly - ONLY FOR DEMOSTRATION PURPOSES - PLEASE STORE PRIVATE KEYS IN A SAFE PLACE
// Export your private key as
// export PRIVKEY=0x.....


module.exports = {
defaultNetwork: 'buildbear',

networks: {
hardhat: {},
buildbear: {
url: "https://rpc.buildbear.io/Curious_Jocasta_Nu_7265a8d5",
},

},
solidity: {
compilers: [
{
version: '0.8.16',
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
{
version: '0.8.4',
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
{
version: '0.8.9',
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
{
version: '0.5.0',
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
{
version: '0.8.13',
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
{
version: '0.5.5',
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
],
},

etherscan: {
apiKey: {
buildbear: "test1",
},
customChains: [
{
network: "buildbear",
chainId: 8646,
urls: {
apiURL: "https://rpc.buildbear.io/verify/etherscan/Curious_Jocasta_Nu_7265a8d5",
browserURL: "https://explorer.buildbear.io/Curious_Jocasta_Nu_7265a8d5",
},
},
],
},
paths: {
sources: './contracts',
cache: './cache',
artifacts: './artifacts',
},
mocha: {
timeout: 20000000000,
},
};
24 changes: 24 additions & 0 deletions SmartContractAakash/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "smartcontractaakash",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@openzeppelin/contracts": "^4.8.2",
"npx": "^10.2.2"
},
"devDependencies": {
"@nomicfoundation/hardhat-chai-matchers": "^1.0.6",
"@nomicfoundation/hardhat-network-helpers": "^1.0.8",
"@nomiclabs/hardhat-ethers": "^2.2.2",
"@nomiclabs/hardhat-etherscan": "^3.1.7",
"chai": "^4.3.7",
"chai-as-promised": "^7.1.1",
"hardhat": "^2.13.0"
}
}
32 changes: 32 additions & 0 deletions SmartContractAakash/scripts/deploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// We require the Hardhat Runtime Environment explicitly here. This is optional
// but useful for running the script in a standalone fashion through `node <script>`.
//
// You can also run a script with `npx hardhat run <script>`. If you do that, Hardhat
// will compile your contracts, add the Hardhat Runtime Environment's members to the
// global scope, and execute the script.
const hre = require("hardhat");

async function main() {
const currentTimestampInSeconds = Math.round(Date.now() / 1000);
const unlockTime = currentTimestampInSeconds + 60;

const lockedAmount = hre.ethers.utils.parseEther("0.001");

const Lock = await hre.ethers.getContractFactory("Lock");
const lock = await Lock.deploy(unlockTime, { value: lockedAmount });

await lock.deployed();

console.log(
`Lock with ${ethers.utils.formatEther(
lockedAmount
)}ETH and unlock timestamp ${unlockTime} deployed to ${lock.address}`
);
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
41 changes: 41 additions & 0 deletions SmartContractAakash/test/Lock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { expect } = require("chai");
const chai = require("chai");

const chaiAsPromised = require("chai-as-promised");

describe("IPLBetting", function () {
let IPLBetting;
let iplBetting;

beforeEach(async function () {
IPLBetting = await ethers.getContractFactory("IPLBetting");
iplBetting = await IPLBetting.deploy();
await iplBetting.deployed();
});

it("should allow a player to place a bet", async function () {
const betAmount = 1;
const team = 1;

const bettorAddress = await (await ethers.getSigners())[0].getAddress();

await iplBetting.placeBet(team, { value: betAmount });

const bet = await iplBetting.getBet(0);

expect(bet.bettor).to.equal(bettorAddress);
expect(bet.amount).to.equal(betAmount);
expect(bet.team).to.equal(team);
expect(bet.paidOut).to.equal(false);
});

it("should not allow a bet of 0 ether", async function () {
const team = 1;

await expect(iplBetting.placeBet(team, { value: 0 })).to.be.rejectedWith(
"Bet amount must be greater than 0."
);
});
});

chai.use(chaiAsPromised);