Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Commit

Permalink
revamp
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickAlphaC committed Aug 2, 2021
1 parent a2a95bf commit e82f89d
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 305 deletions.
9 changes: 7 additions & 2 deletions brownie-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@ reports:
exclude_contracts:
- SafeMath
dependencies:
- smartcontractkit/chainlink-brownie-contracts@1.0.2
- smartcontractkit/chainlink-brownie-contracts@1.1.1
- OpenZeppelin/[email protected]
compiler:
solc:
remappings:
- '@chainlink=smartcontractkit/chainlink-brownie-contracts@1.0.2'
- '@chainlink=smartcontractkit/chainlink-brownie-contracts@1.1.1'
- '@openzeppelin=OpenZeppelin/[email protected]'
# automatically fetch contract sources from Etherscan
autofetch_sources: True
dotenv: .env
# set a custom mnemonic for the development network
networks:
default: development
development:
keyhash: '0x6c3699283bda56ad74f6b855546325b68d482e983852a7a82979cc4807b641f4'
fee: 100000000000000000
jobId: '29fa9aa13bf1468788b7cc4a500a45b8'
verify: False
ganache:
verify: False
mainnet-fork:
Expand Down
81 changes: 52 additions & 29 deletions contracts/Lottery.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import "@openzeppelin/contracts/access/Ownable.sol";
contract Lottery is VRFConsumerBase, Ownable {
using SafeMathChainlink for uint256;
AggregatorV3Interface internal ethUsdPriceFeed;
enum LOTTERY_STATE { OPEN, CLOSED, CALCULATING_WINNER }
enum LOTTERY_STATE {
OPEN,
CLOSED,
CALCULATING_WINNER
}
LOTTERY_STATE public lottery_state;
uint256 public lotteryId;
address payable[] public players;
Expand All @@ -20,64 +24,83 @@ contract Lottery is VRFConsumerBase, Ownable {
uint256 public randomness;
event RequestedRandomness(bytes32 requestId);

constructor(address ethUsdPriceFeedAddress, address _vrfCoordinator, address _link, bytes32 _keyHash)
constructor(
address ethUsdPriceFeedAddress,
address _vrfCoordinator,
address _link,
bytes32 _keyHash
)
public
VRFConsumerBase(
_vrfCoordinator, // VRF Coordinator
_link // LINK Token
) public
{
_link // LINK Token
)
{
ethUsdPriceFeed = AggregatorV3Interface(ethUsdPriceFeedAddress);
lotteryId = 1;
lottery_state = LOTTERY_STATE.CLOSED;
keyHash = _keyHash;
usdEntryFee = 50;
usdEntryFee = 50 * (10**18);
}

function enter() public payable {
require(msg.value >= getEntranceFee(), "Not enough ETH to enter!");
require(lottery_state == LOTTERY_STATE.OPEN);
players.push(msg.sender);
}
}

function getEntranceFee() public view returns(uint256){
uint256 precision = 1 * 10 ** 36;
function getEntranceFee() public view returns (uint256) {
uint256 precision = 1 * 10**18;
uint256 price = getLatestEthUsdPrice(); // 2000_000000000000000000
// uint256 costToEnter = usdEntryFee * 10 ** 18 / price; This would be 0.25
uint256 costToEnter = usdEntryFee * precision / price;
uint256 costToEnter = (usdEntryFee * precision) / price;
return costToEnter;
}

function getLatestEthUsdPrice() public view returns (uint256) {
(,int price,,,) = ethUsdPriceFeed.latestRoundData();
(, int256 price, , , ) = ethUsdPriceFeed.latestRoundData();
price = price * 10000000000;
return uint256(price);
}

function startLottery() public onlyOwner {
require(lottery_state == LOTTERY_STATE.CLOSED, "can't start a new lottery yet");
require(
lottery_state == LOTTERY_STATE.CLOSED,
"can't start a new lottery yet"
);
lottery_state = LOTTERY_STATE.OPEN;
randomness = 0;
}

function endLottery(uint256 userProvidedSeed) public onlyOwner {
require(lottery_state == LOTTERY_STATE.OPEN, "Can't end a lottery that hasnt started!");
lottery_state = LOTTERY_STATE.CALCULATING_WINNER;
pickWinner(userProvidedSeed);
function endLottery() public onlyOwner {
require(
lottery_state == LOTTERY_STATE.OPEN,
"Can't end a lottery that hasnt started!"
);
lottery_state = LOTTERY_STATE.CALCULATING_WINNER;
pickWinner();
}


function pickWinner(uint256 userProvidedSeed) private returns (bytes32){
function pickWinner() private returns (bytes32) {
// DONT do something like this:
// uint(keccak256(abi.encodePacked(nonce, msg.sender, block.difficulty, block.timestamp))) % players.length;
require(lottery_state == LOTTERY_STATE.CALCULATING_WINNER, "You aren't at that stage yet!");
bytes32 requestId = requestRandomness(keyHash, fee, userProvidedSeed);
require(
lottery_state == LOTTERY_STATE.CALCULATING_WINNER,
"You aren't at that stage yet!"
);
bytes32 requestId = requestRandomness(keyHash, fee);
emit RequestedRandomness(requestId);
}

function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
require(lottery_state == LOTTERY_STATE.CALCULATING_WINNER, "You aren't at that stage yet!");
require(randomness > 0, "random-not-found");
uint256 index = randomness % players.length;

function fulfillRandomness(bytes32 _requestId, uint256 _randomness)
internal
override
{
require(
lottery_state == LOTTERY_STATE.CALCULATING_WINNER,
"You aren't at that stage yet!"
);
require(_randomness > 0, "random-not-found");
uint256 index = _randomness % players.length;
recentWinner = players[index];
players[index].transfer(address(this).balance);
players = new address payable[](0);
Expand All @@ -88,8 +111,8 @@ contract Lottery is VRFConsumerBase, Ownable {
function get_players() public view returns (address payable[] memory) {
return players;
}
function get_pot() public view returns(uint256){

function get_pot() public view returns (uint256) {
return address(this).balance;
}
}
37 changes: 0 additions & 37 deletions scripts/0_deploy_mocks.py

This file was deleted.

20 changes: 0 additions & 20 deletions scripts/1_deploy_lottery.py

This file was deleted.

15 changes: 0 additions & 15 deletions scripts/2_start_lottery.py

This file was deleted.

11 changes: 0 additions & 11 deletions scripts/3_enter_lottery.py

This file was deleted.

15 changes: 0 additions & 15 deletions scripts/4_end_lottery.py

This file was deleted.

56 changes: 56 additions & 0 deletions scripts/deploy_lottery.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/python3
from brownie import Lottery, accounts, config, network
from scripts.helpful_scripts import get_contract, get_account, fund_with_link
import time

lottery_enum = {0: "OPEN", 1: "CLOSED", 2: "CALCULATING_WINNER"}


def deploy_lottery():
account = get_account()
# level up get_account for all 3 though
print(network.show_active())
return Lottery.deploy(
get_contract("eth_usd_price_feed").address,
get_contract("vrf_coordinator").address,
get_contract("link_token").address,
config["networks"][network.show_active()]["keyhash"],
{"from": account},
publish_source=config["networks"][network.show_active()].get("verify", False),
)


def start_lottery():
account = get_account()
lottery = Lottery[-1]
transaction = fund_with_link(lottery.address)
transaction.wait(1)
transaction_2 = lottery.startLottery({"from": account})
transaction_2.wait(1)
print(f"The lottery is currently {lottery_enum[lottery.lottery_state()]}")


def enter_lottery():
account = get_account()
lottery = Lottery[-1]
value = lottery.getEntranceFee() + 100000000
transaction_2 = lottery.enter({"from": account, "value": value})
transaction_2.wait(1)
print(f"{account} has entered the lottery!")


def end_lottery():
account = get_account()
lottery = Lottery[-1]
print(f"The previous winner is {lottery.recentWinner()}, let's get a new one!")
transaction = lottery.endLottery({"from": account})
transaction.wait(1)
time.sleep(60)
print(f"{lottery.recentWinner()} is the new winner!")


def main():
deploy_lottery()
# start_lottery()
# enter_lottery()
# end_lottery()
5 changes: 5 additions & 0 deletions scripts/deploy_mocks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from scripts.helpful_scripts import deploy_mocks


def main():
deploy_mocks()
Loading

0 comments on commit e82f89d

Please sign in to comment.