-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathonchain_market.vif
62 lines (57 loc) · 2.41 KB
/
onchain_market.vif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
units: {
currency_value: "a currency"
}
# total_eth_qty: public(wei_value)
total_eth_qty:IFL(wei_value,this)
#total_token_qty: public(uint256(currency_value))
total_token_qty:IFL(uint256(currency_value),this)
# Constant set in `initiate` that's used to calculate
# the amount of ether/tokens that are exchanged
# invariant: public(uint256(wei * currency_value))
invariant:IFL(uint256(wei * currency_value),this)
# token_address: address(ERC20)
token_address:IFL(address(ERC20),this)
#owner: public(address)
owner:IFL(address,this)
# Sets the on chain market maker with its owner, intial token quantity,
# and initial ether quantity
@public
@payable
def initiate(token_addr: address, token_quantity: uint256(currency_value)):
assert self.invariant == 0
self.token_address = token_addr
self.token_address.transferFrom(msg.sender, self, as_unitless_number(token_quantity))
self.owner = msg.sender
self.total_eth_qty = msg.value
self.total_token_qty = token_quantity
self.invariant = msg.value * token_quantity
assert self.invariant > 0
# Sells ether to the contract in exchange for tokens (minus a fee)
@public
@IFL_this
@payable
def eth_to_tokens():
fee: wei_value = msg.value / 500
eth_in_purchase: wei_value = msg.value - fee
new_total_eth: wei_value = self.total_eth_qty + eth_in_purchase
new_total_tokens: uint256(currency_value) = self.invariant / new_total_eth
self.token_address.transfer(msg.sender, as_unitless_number(self.total_token_qty - new_total_tokens))
self.total_eth_qty = new_total_eth
self.total_token_qty = new_total_tokens
# Sells tokens to the contract in exchange for ether
@public
@IFL_this
def tokens_to_eth(sell_quantity:IFL(uint256(currency_value),this)):
self.token_address.transferFrom(msg.sender, self, as_unitless_number(sell_quantity))
new_total_tokens: uint256(currency_value) = self.total_token_qty + sell_quantity
new_total_eth: wei_value = self.invariant / new_total_tokens
eth_to_send: wei_value = self.total_eth_qty - new_total_eth
send(endorse(msg.sender,bot,this), eth_to_send)
self.total_eth_qty = new_total_eth
self.total_token_qty = new_total_tokens
# Owner can withdraw their funds and destroy the market maker
@public
def owner_withdraw():
assert self.owner == msg.sender
self.token_address.transfer(endorse(self.owner,bot,this), as_unitless_number(self.total_token_qty))
selfdestruct(endorse(self.owner,bot,this))