Last active
June 15, 2022 09:42
-
-
Save naik899/dadf11f0420845e88b0e4301aa82be5f to your computer and use it in GitHub Desktop.
Spock Token
This file contains 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
// contracts/SpockToken.sol | |
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | |
contract SpockToken is ERC20 { | |
address owner; | |
address multiSig; | |
modifier onlyOwner() { | |
require(msg.sender == owner, "Only owner can call this function"); | |
_; | |
} | |
constructor(uint256 initialSupply, address _multiSig) ERC20("SPOCK", "SPOCK") { | |
require(_multiSig != address(0), "Mutisig address cannot be zero"); | |
uint256 supply = initialSupply * (1 ether); | |
owner = msg.sender; | |
multiSig = _multiSig; | |
_mint(multiSig, supply); | |
} | |
function setMultiSig(address _multiSig) public onlyOwner { | |
require(_multiSig != address(0), "Mutisig address cannot be zero"); | |
multiSig = _multiSig; | |
} | |
function mintMore(uint256 supply) public onlyOwner { | |
require(multiSig != address(0), "Mutisig address cannot be zero"); | |
_mint(multiSig, supply); | |
} | |
} | |
//This contract should ideally be deployed by the multisig | |
contract SpockTrading { | |
//This address will contain the stablecoin/native token liquidity to use for sell calls | |
address multiSig; | |
address owner; | |
SpockToken token; | |
uint256 tokensPerSpock; | |
modifier onlyOwner() { | |
require(msg.sender == owner, "Only owner can call this function"); | |
_; | |
} | |
modifier onlyMultiSig(){ | |
require(msg.sender == multiSig, "Only multiSig can call this function"); | |
_; | |
} | |
constructor(address contractAddress, address _multiSig) { | |
require(_multiSig != address(0), "Mutisig address cannot be zero"); | |
require(contractAddress != address(0), "Spock token contract cannot be zero"); | |
token = SpockToken(contractAddress); | |
owner = msg.sender; | |
multiSig = owner; | |
} | |
//Set num of tokens per spock - set in correct decimals | |
function setPricePerSpock(uint _tokenPrice) public onlyOwner { | |
tokensPerSpock = _tokenPrice; | |
} | |
function getPricePerSpock()public view returns(uint) { | |
return tokensPerSpock; | |
} | |
// Function to receive $ONE. msg.data must be empty | |
receive() external payable {} | |
// Fallback function is called when msg.data is not empty | |
fallback() external payable {} | |
function checkLiquidity() public view returns(uint) { | |
return address(this).balance; | |
} | |
//Should be onlyMultiSig | |
function withDrawFunds(uint amount) public onlyOwner { | |
require(amount > 0, "You cannot withdraw zero amount"); | |
payable(msg.sender).transfer(amount); | |
} | |
function buySpock(uint256 _numSpock) public payable { | |
require(_numSpock >= 0, "You cannot buy 0 SPOCK"); | |
require(tokensPerSpock != 0, "SPOCK price cannot be zero"); | |
require(msg.value >= _numSpock*tokensPerSpock, "Not enough amount to buy SPOCK"); | |
require(multiSig != address(0), "Need to set a multisig address for liqidity"); | |
token.transferFrom(multiSig, msg.sender, _numSpock * 1 ether); | |
payable(address(this)).transfer(msg.value); | |
} | |
function sellSpock(uint256 _numSpock) public { | |
require(_numSpock >= 0, "You cannot sell 0 SPOCK"); | |
require(tokensPerSpock != 0, "SPOCK price cannot be zero"); | |
require(token.balanceOf(msg.sender) >= _numSpock * 1 ether, "User does not have enough spock"); | |
uint payoutAmount = _numSpock*tokensPerSpock; | |
require((address(this).balance) > payoutAmount, "Contract doesn't have enough liqidity"); | |
token.transferFrom(msg.sender, multiSig, _numSpock * 1 ether); | |
payable(msg.sender).transfer(payoutAmount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment