Skip to content

Instantly share code, notes, and snippets.

@fbslo
Created March 14, 2022 01:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fbslo/4c23186e267827d7ab83d3fb15d2eda8 to your computer and use it in GitHub Desktop.
Save fbslo/4c23186e267827d7ab83d3fb15d2eda8 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
// @fbsloXBT
pragma solidity 0.8.12;
interface IChainLink {
function latestAnswer() external returns (uint256);
}
interface IUSDC {
function transfer(address to, uint256 amount) external;
function transferFrom(address from, address to, uint256 amount) external;
function balanceOf(address user) external returns (uint256);
}
contract LunaBet {
uint256 public start;
uint256 public startPrice;
address public lower;
address public higher;
function enter(bool side) external {
//Transfer 1,000,000 USDC from sender
IUSDC(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48).transferFrom(msg.sender, address(this), 1000000000000);
//if side = false, user is betting that luna price will be lower
if (side == false){
require(lower == address(0), "Side already occupied");
lower = msg.sender;
} else {
require(higher == address(0), "Side already occupied");
higher = msg.sender;
}
if (lower != address(0) && higher != address(0)){
start = block.timestamp;
startPrice = getLunaPrice();
}
}
function redeem() external {
require(block.timestamp - start > 31556926, "1 year has not yet passed");
uint256 balance = IUSDC(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48).balanceOf(address(this));
if (getLunaPrice() > startPrice){
IUSDC(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48).transfer(higher, balance);
} else if (getLunaPrice() < startPrice) {
IUSDC(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48).transfer(lower, balance);
} else {
IUSDC(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48).transfer(higher, balance / 2);
IUSDC(0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48).transfer(lower, balance / 2);
}
}
//Returns USD value using ChainLink on Ethereum
function getLunaPrice() public returns (uint256) {
//get LUNA-ETH price
uint256 ethLuna = IChainLink(0x91E9331556ED76C9393055719986409e11b56f73).latestAnswer(); //18 decimals
//get ETH-USD
uint256 usdEth = IChainLink(0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419).latestAnswer(); //8 decimals
//LUNA-USD value
return ethLuna * usdEth; //26 decimal places
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment