概论

Uniswap V1版本在2018年11月启动,提供ETH和ERC-20代币之间进行兑换的功能。

Uniswap V1本质上是一个自动的交易所,能够自动地和用户交互并兑换ETH和ERC-20代币,兑换比例的确定(即价格)采用恒定乘积自动做市系统(constant-product automated market maker),也就是说,交易所资金池内的ETH和ERC-20代币数量的乘积总体是恒定的。

V1 Features

源码

Factor

// uniswap_factory.vy
contract Exchange():
    def setup(token_addr: address): modifying

NewExchange: event({token: indexed(address), exchange: indexed(address)})

exchangeTemplate: public(address)
tokenCount: public(uint256)
token_to_exchange: address[address]
exchange_to_token: address[address]
id_to_token: address[uint256]

@public
def initializeFactory(template: address):
    assert self.exchangeTemplate == ZERO_ADDRESS
    assert template != ZERO_ADDRESS
    self.exchangeTemplate = template

@public
def createExchange(token: address) -> address:
    assert token != ZERO_ADDRESS
    assert self.exchangeTemplate != ZERO_ADDRESS
    assert self.token_to_exchange[token] == ZERO_ADDRESS
		// 通过 DELEGATECALL 复制合约代码并将其部署为新实例
    exchange: address = create_with_code_of(self.exchangeTemplate)
    Exchange(exchange).setup(token)
		// Token合约地址->Token交易地址
    self.token_to_exchange[token] = exchange
		// Token交易地址->Token合约地址
    self.exchange_to_token[exchange] = token
    token_id: uint256 = self.tokenCount + 1
    self.tokenCount = token_id
		// TokenId->Token合约地址
    self.id_to_token[token_id] = token
    log.NewExchange(token, exchange)
    return exchange

@public
@constant
def getExchange(token: address) -> address:
    return self.token_to_exchange[token]

@public
@constant
def getToken(exchange: address) -> address:
    return self.exchange_to_token[exchange]

@public
@constant
def getTokenWithId(token_id: uint256) -> address:
    return self.id_to_token[token_id]