Last active
October 7, 2021 12:49
-
-
Save rpaskin/3e1d515d3c9ace058a7c7ed698435cc9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.7.0 <0.9.0; | |
contract StoreWeather { | |
enum Weather { sunny, cloudy, rainy, snow } | |
address public owner = msg.sender; | |
Weather currentWeather; | |
function store(Weather _setWeatherTo) public { | |
require(msg.sender == owner, "Only the owner can do this!"); | |
currentWeather = _setWeatherTo; | |
} | |
function retrieve() public view returns (Weather){ | |
return currentWeather; | |
} | |
} | |
contract SnowInsurance { | |
address payable public insurer; | |
address payable public insured; | |
address public oracle; | |
uint public premium; | |
constructor(address _oracleAddress) { | |
require(_oracleAddress != address(0)); | |
insurer = payable(msg.sender); | |
oracle = _oracleAddress; | |
} | |
function openContract(uint _premium) public payable { | |
require (msg.sender == insurer, "Only insurer can open contract"); | |
require (msg.value > 0, "Must send ether to open contract"); | |
require (_premium > 0, "Must define premium greater than 0"); | |
premium = _premium; | |
} | |
function insureMe() public payable { | |
require (address(this).balance > 0, "Contract not open yet"); | |
require (insured == address(0), "Contract already in use"); | |
require (msg.value >= premium, "Must pay premium"); | |
insured = payable(msg.sender); | |
// check if there's change | |
if ((msg.value - premium) > 0){ | |
insurer.transfer(msg.value - premium); | |
} | |
} | |
function cancel() public { | |
require (msg.sender == insurer, "Only insurer can cancel"); | |
insured.transfer(premium); | |
insurer.transfer(address(this).balance); | |
} | |
function claim() public { | |
require (msg.sender == insured, "Only insured can claim"); | |
StoreWeather sw = StoreWeather(oracle); | |
require (sw.retrieve() == StoreWeather.Weather.snow); | |
insurer.transfer(premium); | |
insured.transfer(address(this).balance); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
improved a bit so it's easier to do TDD