Skip to content

Instantly share code, notes, and snippets.

@jmerestain
Created April 11, 2020 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmerestain/1fedae27097e174af00ab84af25d0d40 to your computer and use it in GitHub Desktop.
Save jmerestain/1fedae27097e174af00ab84af25d0d40 to your computer and use it in GitHub Desktop.
token generation through purchase using oracle
contract UsdContract:
def read() -> bytes32: constant
contract Ophir:
def mint(_to : address, _value : uint256): modifying
ChangePrice: event({_value: uint256})
TokenPurchase: event({_to: indexed(address), _value: uint256})
TokenUpdate: event({_to: indexed(address)})
usdPrice : uint256
ophirContract : Ophir
usdSource : UsdContract
locked : bool
operator : address
@public
def __init__(_sourceAddress : address, _tokenAddress : address, _salePrice: uint256, _saleDecimal : uint256):
"""
@dev connect crowdsale contract to eth/usd price and token
@param _sourceAddress ETH/USD feed source
@param _tokenAddress token contract address
@param _salePrice initial sales price in USD
"""
assert _saleDecimal <= 18
self.usdPrice = _salePrice * 10 ** (18 - _saleDecimal)
self.usdSource = UsdContract(_sourceAddress)
self.ophirContract = Ophir(_tokenAddress)
self.locked = False
self.operator = msg.sender
@public
def displayPrice() -> uint256:
"""
@dev returns USD sale price, 18 decimals
"""
return self.usdPrice
@private
def _priceDivide(_x : uint256, _y : uint256) -> uint256:
numerator : uint256 = _x * 10 ** 3
result : uint256 = (numerator / _y)
assert result > 1
result = result
return result
@private
def _priceMultiply(_x : uint256, _y : uint256) -> uint256:
factorX : uint256 = _x
factorY : uint256 = _y
result : uint256 = factorX * factorY * (10 ** -3)
return result
@public
def isLocked() -> bool:
"""
@dev displays if smart contract is locked or otherwise
"""
return self.locked
@public
def setPrice(_price : uint256, _decimals : uint256):
"""
@dev sets new USD sale price, logs event
@param _price price in USD, from values $1-$999999
@param _decimals decimals in the _price param
"""
assert msg.sender == self.operator
assert _decimals <= 18
amtDecimal : uint256 = 18 - _decimals
self.usdPrice = _price * 10 ** amtDecimal
log.ChangePrice(self.usdPrice)
@public
def lockToggle() -> bool:
"""
@dev locks or unlocks the smart contract from new purchases
"""
assert msg.sender == self.operator
if(self.locked):
self.locked = False
return False
if(self.locked == False):
self.locked = True
return False
@public
def modifyToken(_tokenAddress : address):
"""
@dev changes token contract address in an event of contract modification
@param _tokenAddress token contract address
"""
assert msg.sender == self.operator
self.ophirSource = Ophir(_tokenAddress)
log.TokenUpdate(_tokenaddress)
@public
def modifyFeed(_sourceAddress : address):
"""
@dev changes feed contract address in an event of contract modification
@param _sourceAddress feed contract address
"""
assert msg.sender == self.operator
self.usdSource = UsdContract(_sourceAddress)
@payable
@public
def tokenPurchase():
"""
@dev executes the purchase and generation of new ophir tokens
1. Reads ETHUSD Oracle
2. Parse into uint256
3. Calculate rate for OPR/ETH
4. Calculate token amount rate * amount
5. Send/Mint Tokens to Buyer
"""
assert msg.value > 0
recv : bytes32 = self.usdSource.read()
ethusd : uint256 = convert(recv, uint256)
opreth : uint256 = self._priceDivide(ethusd, self.usdPrice)
amtTok : uint256 = self._priceMultiply(opreth, msg.value)
self.ophirContract.mint(msg.sender, amtTok)
log.TokenPurchase(msg.sender, amtTok)
@public
def destroyContract():
assert msg.sender == self.operator
selfdestruct(self.operator)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment