Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
1125 lines (948 sloc) 25.8 KB

Exchange API

setup

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

// Can only be called by factory contract during createExchange()

setup(token_addr: address):

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

// Can only be called by factory contract during createExchange()

exchangeContract.methods.setup(token: String).send()

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Description
token_addr Ethereum address of an ERC20 Token

addLiquidity

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

@payable
addLiquidity(
    min_liquidity: uint256, 
    max_tokens: uint256, 
    deadline: uint256
): uint256

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

ethValue: String        // big integer, msg.value

exchangeContract.methods.addLiquidity(
    min_liquidity: String,        // big integer
    max_tokens: String,           // big integer
    deadline: Integer
).send({value: ethValue})

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
msg.value uint256 Amount of ETH added
min_liquidity uint256 Minimum minted liquidity
max_tokens uint256 Maximum ERC20 tokens added
deadline uint256 Transaction deadline
Returns
uint256 Amount of liquidity tokens minted

removeLiquidity

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

removeLiquidity(
    amount: uint256;
    min_eth: uint256, 
    min_tokens: uint256, 
    deadline: uint256
): (uint256, uint256)

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.removeLiquidity(
    amount: String,        // big integer
    min_eth: String,       // big integer
    max_tokens: String     // big integer
    deadline: Integer    
).send()

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
amount uint256 Amount of liquidity burned
min_eth uint256 Minimum ETH removed
min_tokens uint256 Minimum ERC20 tokens removed
deadline uint256 Transaction deadline
Returns
uint256 Amount of ETH removed
uint256 Amount of ERC20 tokens removed.

default

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

// Default function in Vyper replaces the "fallback" function in Solidity

@payable
__default__():

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

ethAmount: String        // big integer, msg.value
web3.eth.sendTransaction({value: ethAmount})

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
msg.value uint256 Amount of ETH sold

ethToTokenSwapInput

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

@payable
ethToTokenSwapInput(
    min_tokens: uint256, 
    deadline: uint256
): uint256

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

ethValue: String        // big integer, msg.value

exchangeContract.methods.ethToTokenSwapInput(
    min_liquidity: String,        // big integer
    max_tokens: String,           // big integer
    deadline: Integer
).send({value: ethValue})

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
msg.value uint256 Amount of ETH sold
min_tokens uint256 Minimum ERC20 tokens bought
deadline uint256 Transaction deadline
Returns
uint256 Amount of ERC20 tokens bought

ethToTokenTransferInput

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

@payable
ethToTokenTransferInput(
    min_tokens: uint256, 
    deadline: uint256,
    recipient: address
): uint256

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

ethValue: String        // big integer, msg.value

exchangeContract.methods.ethToTokenTransferInput(
    min_liquidity: String,        // big integer
    max_tokens: String,           // big integer
    deadline: Integer,
    recipient: String
).send({value: ethValue})

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
msg.value uint256 Amount of ETH sold
min_tokens uint256 Minimum ERC20 tokens bought
deadline uint256 Transaction deadline
recipient address Address that receives ERC20 tokens
Returns
uint256 Amount of ERC20 tokens bought

ethToTokenSwapOutput

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

@payable
ethToTokenSwapOutput(
    tokens_bought: uint256, 
    deadline: uint256
): uint256

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

ethValue: String        // big integer, msg.value

exchangeContract.methods.ethToTokenSwapOutput(
    tokens_bought: String,        // big integer
    deadline: Integer
).send({value: ethValue})

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
msg.value uint256 Maximum ETH sold
tokens_bought uint256 Amount of ERC20 tokens bought
deadline uint256 Transaction deadline
Returns
uint256 Amount of ETH sold

ethToTokenTransferOutput

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

@payable
ethToTokenTransferOutput(
    tokens_bought: uint256, 
    deadline: uint256,
    recipient: address
): uint256

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

ethValue: String        // big integer, msg.value

exchangeContract.methods.ethToTokenTransferOutput(
    tokens_bought: String,        // big integer
    deadline: Integer,
    recipient: String
).send({value: ethValue})

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
msg.value uint256 Maximum ETH sold
tokens_bought uint256 Amount of ERC20 tokens bought
deadline uint256 Transaction deadline
recipient address Address that receives ERC20 tokens
Returns
uint256 Amount of ETH sold

****

tokenToEthSwapInput

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

tokenToEthSwapInput(
    tokens_sold: uint256,
    min_eth: uint256, 
    deadline: uint256
): uint256

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.tokenToEthSwapInput(
    tokens_sold: String,        // big integer
    min_eth: String,            // big integer
    deadline: Integer
).send()

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
tokens_sold uint256 Amount of ERC20 tokens sold
min_eth uint256 Minimum ETH bought
deadline uint256 Transaction deadline
Returns
uint256 Amount of ETH bought

tokenToEthTransferInput

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

tokenToEthTransferInput(
    tokens_sold: uint256,
    min_eth: uint256, 
    deadline: uint256,
    recipient: address
): uint256

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.tokenToEthTransferInput(
    tokens_sold: String,        // big integer
    min_eth: String,            // big integer
    deadline: Integer,
    recipient: String
).send()

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
tokens_sold uint256 Amount of ERC20 tokens sold
min_eth uint256 Minimum ETH bought
deadline uint256 Transaction deadline
recipient address Address that receives ETH
Returns
uint256 Amount of ETH bought

tokenToEthSwapOutput

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

tokenToEthSwapOutput(
    eth_bought: uint256,
    max_tokens: uint256, 
    deadline: uint256
): uint256

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.tokenToEthSwapOutput(
    eth_bought: String,        // big integer
    max_tokens: String,        // big integer
    deadline: Integer
).send()

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
eth_bought uint256 Amount of ETH bought
max_tokens uint256 Maximum ERC20 tokens sold
deadline uint256 Transaction deadline
Returns
uint256 Amount of ERC20 tokens sold

****

tokenToEthTransferOutput

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

tokenToEthTransferOutput(
    eth_bought: uint256,
    max_tokens: uint256, 
    deadline: uint256,
    recipient: address
): uint256

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.tokenToEthTransferOutput(
    eth_bought: String,        // big integer
    max_tokens: String,        // big integer
    deadline: Integer,
    recipient: String
).send()

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
eth_bought uint256 Amount of ETH bought
max_tokens uint256 Maximum ERC20 tokens sold
deadline uint256 Transaction deadline
recipient address Address that receives ETH
Returns
uint256 Amount of ERC20 tokens sold

tokenToTokenSwapInput

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

tokenToTokenSwapInput(
    tokens_sold: uint256,
    min_tokens_bought: uint256,
    min_eth_bought: uint256,
    deadline: uint256,
    token_addr: address
): uint256

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.tokenToTokenSwapInput(
    tokens_sold: String,            // big integer
    min_tokens_bought: String,      // big integer
    min_eth_bought: String,         // big integer
    deadline: Integer,
    token_addr: String
).send()

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
tokens_sold uint256 Amount of input ERC20 tokens sold
min_tokens_bought uint256 Minimum output ERC20 tokens bought
min_eth_bought uint256 Minimum ETH bought as intermediary
deadline uint256 Transaction deadline
token_addr address Address of output ERC20 token
Returns
uint256 Amount of output ERC20 tokens bought

tokenToTokenTransferInput

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

tokenToTokenTransferInput(
    tokens_sold: uint256,
    min_tokens_bought: uint256,
    min_eth_bought: uint256,
    deadline: uint256,
    recipient: address
    token_addr: address
): uint256

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.tokenToTokenTransferInput(
    tokens_sold: String,            // big integer
    min_tokens_bought: String,      // big integer
    min_eth_bought: String,         // big integer
    deadline: Integer,
    recipient: String
    token_addr: String
).send()

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
tokens_sold uint256 Amount of input ERC20 tokens sold
min_tokens_bought uint256 Minimum output ERC20 tokens bought
min_eth_bought uint256 Minimum ETH bought as intermediary
deadline uint256 Transaction deadline
recipient address Address that receives output ERC20 tokens
token_addr address Address of output ERC20 token
Returns
uint256 Amount of output ERC20 tokens bought

tokenToTokenSwapOutput

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

tokenToTokenSwapOutput(
    tokens_bought: uint256,
    max_tokens_sold: uint256,
    max_eth_sold: uint256,
    deadline: uint256,
    token_addr: address
): uint256

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.tokenToTokenSwapOutput(
    tokens_bought: String,            // big integer
    max_tokens_sold: String,      // big integer
    max_eth_sold: String,         // big integer
    deadline: Integer,
    token_addr: String
).send()

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
tokens_bought uint256 Amount of output ERC20 tokens bought
max_tokens_sold uint256 Maximum input ERC20 tokens bought
max_eth_sold uint256 Maximum ETH bought as intermediary
deadline uint256 Transaction deadline
token_addr address Address of output ERC20 token
Returns
uint256 Amount of input ERC20 tokens sold

tokenToTokenTransferOutput

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

tokenToTokenTransferOutput(
    tokens_bought: uint256,
    max_tokens_sold: uint256,
    max_eth_sold: uint256,
    deadline: uint256,
    recipient: address,
    token_addr: address
): uint256

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.tokenToTokenTransferOutput(
    tokens_bought: String,         // big integer
    max_tokens_sold: String,       // big integer
    max_eth_sold: String,          // big integer
    deadline: Integer,
    recipient: String,
    token_addr: String
).send()

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
tokens_bought uint256 Amount of output ERC20 tokens bought
max_tokens_sold uint256 Maximum input ERC20 tokens bought
max_eth_sold uint256 Maximum ETH bought as intermediary
deadline uint256 Transaction deadline
recipient address Address that receives output ERC20 tokens
token_addr address Address of output ERC20 token
Returns
uint256 Amount of input ERC20 tokens sold

tokenToExchangeSwapInput

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

tokenToTokenSwapInput(
    tokens_sold: uint256,
    min_tokens_bought: uint256,
    min_eth_bought: uint256,
    deadline: uint256,
    exchange_addr: address
): uint256

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.tokenToTokenSwapInput(
    tokens_sold: String,            // big integer
    min_tokens_bought: String,      // big integer
    min_eth_bought: String,         // big integer
    deadline: Integer,
    exchange_addr: String
).send()

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
tokens_sold uint256 Amount of input ERC20 tokens sold
min_tokens_bought uint256 Minimum output ERC20 tokens bought
min_eth_bought uint256 Minimum ETH bought as intermediary
deadline uint256 Transaction deadline
exchange_addr address Address of output ERC20 token exchange
Returns
uint256 Amount of output ERC20 tokens bought

tokenToExchangeTransferInput

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

tokenToExchangeTransferInput(
    tokens_sold: uint256,
    min_tokens_bought: uint256,
    min_eth_bought: uint256,
    deadline: uint256,
    recipient: address
    exchange_addr: address
): uint256

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.tokenToExchangeTransferInput(
    tokens_sold: String,            // big integer
    min_tokens_bought: String,      // big integer
    min_eth_bought: String,         // big integer
    deadline: Integer,
    recipient: String
    exchange_addr: String
).send()

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
tokens_sold uint256 Amount of input ERC20 tokens sold
min_tokens_bought uint256 Minimum output ERC20 tokens bought
min_eth_bought uint256 Minimum ETH bought as intermediary
deadline uint256 Transaction deadline
recipient address Address that receives output ERC20 tokens
exchange_addr address Address of output ERC20 token exchange
Returns
uint256 Amount of output ERC20 tokens bought

tokenToExchangeSwapOutput

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

tokenToExchangeSwapOutput(
    tokens_bought: uint256,
    max_tokens_sold: uint256,
    max_eth_sold: uint256,
    deadline: uint256,
    exchange_addr: address
): uint256

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.tokenToExchangeSwapOutput(
    tokens_bought: String,            // big integer
    max_tokens_sold: String,      // big integer
    max_eth_sold: String,         // big integer
    deadline: Integer,
    exchange_addr: String
).send()

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
tokens_bought uint256 Amount of output ERC20 tokens bought
max_tokens_sold uint256 Maximum input ERC20 tokens bought
max_eth_sold uint256 Maximum ETH bought as intermediary
deadline uint256 Transaction deadline
exchange_addr address Address of output ERC20 token exchange
Returns
uint256 Amount of input ERC20 tokens sold

tokenToExchangeTransferOutput

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

tokenToExchangeTransferOutput(
    tokens_bought: uint256,
    max_tokens_sold: uint256,
    max_eth_sold: uint256,
    deadline: uint256,
    recipient: address,
    exchange_addr: address
): uint256

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.tokenToExchangeTransferOutput(
    tokens_bought: String,         // big integer
    max_tokens_sold: String,       // big integer
    max_eth_sold: String,          // big integer
    deadline: Integer,
    recipient: String,
    exchange_addr: String
).send()

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
tokens_bought uint256 Amount of output ERC20 tokens bought
max_tokens_sold uint256 Maximum input ERC20 tokens bought
max_eth_sold uint256 Maximum ETH bought as intermediary
deadline uint256 Transaction deadline
recipient address Address that receives output ERC20 tokens
exchange_addr address Address of output ERC20 token exchange
Returns
uint256 Amount of input ERC20 tokens sold

****

getEthToTokenInputPrice

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

@constant
getEthToTokenInputPrice(eth_sold: uint256): uint256

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.getEthToTokenInputPrice(
    eth_sold: String,        // big integer
).call()

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
eth_sold uint256 Amount of ETH sold
Returns
uint256 Amount of ERC20 tokens that can be bought

getEthToTokenOutputPrice

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

@constant
getEthToTokenOutputPrice(tokens_bought: uint256): uint256

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.getEthToTokenOutputPrice(
    tokens_bought: String,        // big integer
).call()

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
tokens_bought uint256 Amount of ERC20 tokens bought
Returns
uint256 Amount of ETH that must be sold

****

getTokenToEthInputPrice

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

@constant
getTokenToEthInputPrice(tokens_sold: uint256): uint256

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.getTokenToEthInputPrice(
    tokens_sold: String,        // big integer
).call()

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
tokens_sold uint256 Amount of ERC20 tokens sold
Returns
uint256 Amount of ETH that can be bought

getTokenToEthOutputPrice

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

@constant
getTokenToEthOutputPrice(eth_bought: uint256): uint256

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.getTokenToEthOutputPrice(
    eth_bought: String,        // big integer
).call()

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
eth_bought uint256 Amount of ETH bought
Returns
uint256 Amount of ERC20 tokens that must be sold

tokenAddress

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

@constant
tokenAddress(): address

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.tokenAddress().call()

{% endcode-tabs-item %} {% endcode-tabs %}

Returns
address Address of ERC20 token sold on exchange

factoryAddress

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

@constant
factoryAddress(): address

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.factoryAddress().call()

{% endcode-tabs-item %} {% endcode-tabs %}

Returns
address Address of factory that created exchange

name

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

// All exchange contracts have the same name
@constant
name(): bytes32        // Uniswap V1

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.tokenAddress().call()

{% endcode-tabs-item %} {% endcode-tabs %}

Returns
bytes32 Name of liquidity token

symbol

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

// All exchange contracts have the same symbol
@constant
symbol(): bytes32        // UNI-V1

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.tokenAddress().call()

{% endcode-tabs-item %} {% endcode-tabs %}

Returns
bytes32 Symbol of liquidity token

decimals

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

// All exchange contracts have the same decimals
@constant
decimals(): uint256        // 18

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.decimals().call()

{% endcode-tabs-item %} {% endcode-tabs %}

Returns
uint256 Decimals of liquidity token

balanceOf

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

@constant
balanceOf(_owner: address): uint256

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.balanceOf(_owner: String).call()

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
_owner address Ethereum address
Returns
uint256 Liquidity token balance of address

transfer

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

transfer(
    _to: address,
    _value : uint256
): bool

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.transfer(
    _to: String,
    _value : String        // big integer
).send()

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
_to address Recipient address
_value uint256 Amount transferred
Returns
bool True if successful. Reverts or false on failure

transferFrom

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

transferFrom(
    _from: address,
    _to: address,
    _value : uint256
): bool

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.transferFrom(
    _from: String,
    _to: String,
    _value : String        // big integer
).send()

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
_from address Sender address
_to address Recipient address
_value uint256 Amount transferred
Returns
bool True if successful. Reverts or false on failure

approve

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

approve(
    _spender: address,
    _value: uint256
): bool

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.approve(
    _spender: String,    
    _value: String        // big integer
).send()

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
_spender address Address of approved spender
_value uint256 Spender allowance
Returns
bool True if successful. Reverts or false on failure

allowance

{% code-tabs %} {% code-tabs-item title="Smart Contract" %}

allowance(
    _owner: address,
    _spender: address
): uint256

{% endcode-tabs-item %}

{% code-tabs-item title="Web3" %}

exchangeContract.methods.allowance(
    _owner: String,    
    _spender: String        
).call()

{% endcode-tabs-item %} {% endcode-tabs %}

Parameter Type Description
_owner address Address of liquidity token owner
_spender uint256 Address of approved spender
Returns
uint256 Spender allowance