Skip to content

Instantly share code, notes, and snippets.

@davidhq
Created February 22, 2022 14:25
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 davidhq/779f863c11cc8fbfb654172ad1049942 to your computer and use it in GitHub Desktop.
Save davidhq/779f863c11cc8fbfb654172ad1049942 to your computer and use it in GitHub Desktop.
from vyper.interfaces import ERC20
totalEthQty: public(uint256)
totalTokenQty: public(uint256)
# Constant set in `initiate` that's used to calculate
# the amount of ether/tokens that are exchanged
invariant: public(uint256)
token_address: ERC20
owner: public(address)
# Sets the on chain market maker with its owner, intial token quantity,
# and initial ether quantity
@external
@payable
def initiate(token_addr: address, token_quantity: uint256):
assert self.invariant == 0
self.token_address = ERC20(token_addr)
self.token_address.transferFrom(msg.sender, self, token_quantity)
self.owner = msg.sender
self.totalEthQty = msg.value
self.totalTokenQty = token_quantity
self.invariant = msg.value * token_quantity
assert self.invariant > 0
# Sells ether to the contract in exchange for tokens (minus a fee)
@external
@payable
def ethToTokens():
fee: uint256 = msg.value / 500
eth_in_purchase: uint256 = msg.value - fee
new_total_eth: uint256 = self.totalEthQty + eth_in_purchase
new_total_tokens: uint256 = self.invariant / new_total_eth
self.token_address.transfer(msg.sender, self.totalTokenQty - new_total_tokens)
self.totalEthQty = new_total_eth
self.totalTokenQty = new_total_tokens
# Sells tokens to the contract in exchange for ether
@external
def tokensToEth(sell_quantity: uint256):
self.token_address.transferFrom(msg.sender, self, sell_quantity)
new_total_tokens: uint256 = self.totalTokenQty + sell_quantity
new_total_eth: uint256 = self.invariant / new_total_tokens
eth_to_send: uint256 = self.totalEthQty - new_total_eth
send(msg.sender, eth_to_send)
self.totalEthQty = new_total_eth
self.totalTokenQty = new_total_tokens
# Owner can withdraw their funds and destroy the market maker
@external
def ownerWithdraw():
assert self.owner == msg.sender
self.token_address.transfer(self.owner, self.totalTokenQty)
selfdestruct(self.owner)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment