Taesoo Kim
Taesoo Kim
Ref. EI §2
Ref. The Block Research
Ref. Coinglass
Ref. Coin Gecko
Ref. DefiLlama
Ref. DefiLlama
Ref. Timothy Lin
→ Who can be the liquidity provider? Anyone!
Ref. Dan Robinson
Ref. Dan Robinson
Ref. Dan Robinson
Ref. Timothy Lin
and → So,
and → So,
# Pricing function for converting between ETH and Tokens.
#
#@param input_amount Amount of ETH or Tokens being sold.
#@param input_reserve Amount of ETH or Tokens (input type) in exchange reserves.
#@param output_reserve Amount of ETH or Tokens (output type) in exchange reserves.
#@return Amount of ETH or Tokens bought.
@private
@constant
def getInputPrice(input_amount: uint256, input_reserve: uint256,
output_reserve: uint256) => uint256:
assert input_reserve > 0 and output_reserve > 0
input_amount_with_fee: uint256 = input_amount * 997
numerator: uint256 = input_amount_with_fee * output_reserve
denominator: uint256 = (input_reserve * 1000) + input_amount_with_fee
return numerator / denominator
>>> token = ERCToken.deploy(1000, "A")
<ERCToken Contract '0xcFf3e49D8947D75Fa04115cFD3f9343c95956eb7'>
>>> factory.createExchange(token.address)
>>> factory.getExchange(token.address)
'0x0bEf07F1fF27538ee5C7D64e7582A42867069f4e'
# or simply
>>> exchange = new_exchange(factory, token)
<Exchange Contract '0x0bEf07F1fF27538ee5C7D64e7582A42867069f4e'>
# @notice Public price function for Token to ETH trades with an exact input.
# @param tokens_sold Amount of Tokens sold.
# @return Amount of ETH that can be bought with input Tokens.
@public
@constant
def getTokenToEthInputPrice(tokens_sold: uint256) -> uint256(wei):
assert tokens_sold > 0
token_reserve: uint256 = self.token.balanceOf(self)
eth_bought: uint256 = self.getInputPrice(tokens_sold, token_reserve, as_unitless_number(self.balance))
return as_wei_value(eth_bought, 'wei')
>>> token.approve(exchange.address, 1)
>>> exchange.tokenToEthSwapInput(1, ethPerToken, int(time.time())+1000)
├── General ERC20 Token (0xe0aA552A10d7EC8760Fc6c246D391E698a82dDf9)
│ ├── Approval
│ │ ├── owner: 0x66aB6D9362d4F35596279692F0251Db635165871
│ │ ├── spender: 0x87e23022F31814f835C052A9e280608bdE7aE3f9
│ │ └── value: 0
│ └── Transfer
│ ├── from: 0x66aB6D9362d4F35596279692F0251Db635165871
│ ├── to: 0x87e23022F31814f835C052A9e280608bdE7aE3f9
│ └── value: 1
│
└── 0x87e23022F31814f835C052A9e280608bdE7aE3f9
└── EthPurchase
├── buyer: 0x66aB6D9362d4F35596279692F0251Db635165871
├── tokens_sold: 1
└── eth_bought: 9871580
(WARNING. lots of things to digest!)