# Getting started

Stablecoinswap is an Ethereum DApp which allows anyone to swap stablecoins without slippage. Stablecoin prices are set by the oracle.

## Addresses

* Stablecoinswap: [0x087239350b3b9bcdad0e7e318ef9c4eaff5eaed3](https://etherscan.io/address/0x087239350b3b9bcdad0e7e318ef9c4eaff5eaed3)
* PriceOracle: [0xefe076de83ccf27bc893002439dec13f93564e84](https://etherscan.io/address/0xefe076de83ccf27bc893002439dec13f93564e84)

## Resources

* [Website](https://stablecoinswap.io)
* [Github](https://github.com/stablecoinswap)
* [Twitter](https://twitter.com/stableswapdex)
* [Reddit](https://reddit.com/r/stablecoinswap)
* [Discord](http://discord.gg/hTmxYUJ)
* [Email](mailto:info@stablecoinswap.io)

## Trade without slippage

Token prices are set by the oracle, which allows all trades to be executed with a fixed price without any slippage. No matter what the order value is, be it just $1 or $100,000, the price will stay the same. As long as the contract has enough liquidity, the order will be fulfilled instantly.

## Earn fees by providing liquidity

Anyone can deposit funds into the smart contract and earn trading fees.

When you provide liquidity, you receive STL tokens which represent your stake in the pool (in percentage). Over time, as more and more trades are executed, the pool value and the value of your STL tokens grows.

You can redeem your STL tokens back into the stablecoins at any time. Note that the redemption (withdrawal) also bears the same fee as the normal swap. If this fee wasn't present, anyone could deposit one token into the contract and immediately withdraw it in another token for a swap without fees.

## How to use it

Head over to [Stablecoinswap.io](http://stablecoinswap.io) to use the frontend or interact with the smart contracts directly.


# Stablecoinswap API

Contract is written in Vyper (0.1.0b9)

## addLiquidity

{% hint style="info" %}
Adding liquidity is free. However, removing liquidity bears the same fee as the swap operation. This is to prevent users from bypassing the trading fee by depositing liquidity in one token and instantly withdrawing it in another token.
{% endhint %}

{% tabs %}
{% tab title="Smart Contract" %}

```python
def addLiquidity(
    token_address: address, 
    erc20_token_amount: uint256, 
    deadline: timestamp
) -> bool
```

{% endtab %}

{% tab title="Web3" %}

```javascript
contract.methods.addLiquidity(token_address, amount, deadline).send({ from: user_address }, callback)
```

{% endtab %}
{% endtabs %}

| Parameter            | Type    |                   Description |
| -------------------- | ------- | ----------------------------: |
| token\_address       | address | Address of ERC20 token to add |
| erc20\_token\_amount | uint256 |  How many ERC20 tokens to add |
| deadline             | uint256 |          Transaction deadline |

| Returns |                                                  |
| ------- | -----------------------------------------------: |
| uint256 | The amount of STL tokens received by the sender. |

## removeLiquidity

{% tabs %}
{% tab title="Smart Contract" %}

```python
def removeLiquidity(
    token_address: address, 
    stableswap_token_amount: uint256, 
    erc20_min_output_amount: uint256, 
    deadline: timestamp
) -> bool
```

{% endtab %}

{% tab title="Web3" %}

```
 contract.methods.removeLiquidity(token_address, amount, min_output_amount, deadline).send({ from: user_address }, callback)
```

{% endtab %}
{% endtabs %}

| Parameter                  | Type    |                        Description |
| -------------------------- | ------- | ---------------------------------: |
| token\_address             | address |   Address of ERC20 token to remove |
| stableswap\_token\_amount  | uint256 |     Amount of STL tokens to redeem |
| erc20\_min\_output\_amount | uint256 | Minimum ERC20 tokens output amount |
| deadline                   | uint256 |               Transaction deadline |

| Returns |                                                    |
| ------- | -------------------------------------------------: |
| uint256 | The amount of erc20 tokens received by the sender. |

## swapTokens

{% tabs %}
{% tab title="Smart Contract" %}

```python
def swapTokens(
    input_token: address,
    output_token: address, 
    erc20_input_amount: uint256, 
    erc20_min_output_amount: uint256, 
    deadline: timestamp
) -> bool
```

{% endtab %}

{% tab title="Web3" %}

```
contract.methods.swapTokens(input_token_address, output_token_address, input_amount, min_output_amount, deadline).send({ from: this.currentAddress() }, callback)
```

{% endtab %}
{% endtabs %}

| Parameter                  | Type    |                        Description |
| -------------------------- | ------- | ---------------------------------: |
| input\_token               | address |      Address of input  ERC20 token |
| output\_token              | address |      Address of output ERC20 token |
| erc20\_input\_amount       | uint256 |  Amount of ERC20 input tokens sold |
| erc20\_min\_output\_amount | uint256 | Minimum ERC20 output tokens to buy |
| deadline                   | uint256 |               Transaction deadline |

| Returns |                                                    |
| ------- | -------------------------------------------------: |
| uint256 | The amount of erc20 tokens received by the sender. |

## tokenExchangeRateAfterFees

{% tabs %}
{% tab title="Smart Contract" %}

```python
@constant
def tokenExchangeRateAfterFees(
    input_token_address: address, 
    output_token_address: address
) -> uint256
```

{% endtab %}

{% tab title="Web3" %}

```javascript
contract.methods.tokenExchangeRateAfterFees(input_token_address, output_token_address).call()
```

{% endtab %}
{% endtabs %}

| Parameter              | Type    |                    Description |
| ---------------------- | ------- | -----------------------------: |
| input\_token\_address  | address | Address of ERC20 token to sell |
| output\_token\_address | address |  Address of ERC20 token to buy |

| Returns |                                                                                                                                                                                                              |
| ------- | -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| uint265 | <p>Exchange rate multiplied by 10^22.</p><p></p><p> Note that due to rounding, the fees could be slightly higher for the tokens with smaller decimal precision and/or if the exchange amount is too low.</p> |

## tokenOutputAmountAfterFees

{% tabs %}
{% tab title="Smart Contract" %}

```python
@constant
def tokenOutputAmountAfterFees(
    input_token_amount: uint256, 
    input_token_address: address, 
    output_token_address: address
) -> uint256
```

{% endtab %}

{% tab title="Web3" %}

```javascript
contract.methods.tokenOutputAmountAfterFees(amount, input_token_address, output_token_address).call()
```

{% endtab %}
{% endtabs %}

| Parameter              | Type    |                    Description |
| ---------------------- | ------- | -----------------------------: |
| input\_token\_amount   | uint256 |  Amount of ERC20 token to sell |
| input\_token\_address  | address | Address of ERC20 token to sell |
| output\_token\_address | address |  Address of ERC20 token to buy |

| Returns |                                                                  |
| ------- | ---------------------------------------------------------------: |
| uint256 | How many tokens can be bought for a given amount of input tokens |

## poolOwnership

{% tabs %}
{% tab title="Smart Contract" %}

```python
@constant
def poolOwnership(user_address: address) -> decimal
```

{% endtab %}

{% tab title="Web3" %}

```javascript
contract.methods.poolOwnership(address).call()
```

{% endtab %}
{% endtabs %}

| Parameter     | Type    |       Description |
| ------------- | ------- | ----------------: |
| user\_address | address | Address of a user |

| Returns               |                                                |
| --------------------- | ---------------------------------------------: |
| fixed168x10 (uint168) | Share of total liquidity belonging to the user |

## fees

{% tabs %}
{% tab title="Smart Contract" %}

```python
@constant
def fees(fee_name: string[32]) -> decimal
```

{% endtab %}

{% tab title="Web3" %}

```javascript
contract.methods.fees(fee_name).call()
```

{% endtab %}
{% endtabs %}

| Parameter | Type   |              Description |
| --------- | ------ | -----------------------: |
| fee\_name | string | 'ownerFee' or 'tradeFee' |

| Returns               |                                   |
| --------------------- | --------------------------------: |
| fixed168x10 (uint168) | Fee value (i.e. 0.001 means 0.1%) |

## inputTokens

{% tabs %}
{% tab title="Smart Contract" %}

```python
inputTokens: public(map(address, bool))
```

{% endtab %}

{% tab title="Web3" %}

```javascript
contract.methods.inputTokens(address).call()
```

{% endtab %}
{% endtabs %}

| Parameter | Type    |            Description |
| --------- | ------- | ---------------------: |
| address   | address | Address of ERC20 token |

| Returns |                                                             |
| ------- | ----------------------------------------------------------: |
| bool    | True if an address is in the list of available input tokens |

## outputTokens

{% tabs %}
{% tab title="Smart Contract" %}

```python
outputTokens: public(map(address, bool))
```

{% endtab %}

{% tab title="Web3" %}

```javascript
contract.methods.outputTokens(address).call()
```

{% endtab %}
{% endtabs %}

| Parameter | Type    |            Description |
| --------- | ------- | ---------------------: |
| address   | address | Address of ERC20 token |

| Returns |                                                              |
| ------- | -----------------------------------------------------------: |
| bool    | True if an address is in the list of available output tokens |

## priceOracleAddress

{% tabs %}
{% tab title="Smart Contract" %}

```python
priceOracleAddress: public(address)
```

{% endtab %}

{% tab title="Web3" %}

```javascript
contract.methods.priceOracleAddress().call()
```

{% endtab %}
{% endtabs %}

| Returns |                                   |
| ------- | --------------------------------: |
| address | Address of a priceoracle contract |


# Priceoracle API

Contract is written in Vyper (0.1.0b9)

## poolSize

{% tabs %}
{% tab title="Smart Contract" %}

```python
@constant
def poolSize(contract_address: address) -> uint256
```

{% endtab %}

{% tab title="Web3" %}

```javascript
contract.methods.poolSize(stablecoinswap_address).call()
```

{% endtab %}
{% endtabs %}

| Parameter         | Type    |                          Description |
| ----------------- | ------- | -----------------------------------: |
| contract\_address | address | Address of a stablecoinswap contract |

| Returns |                                                                          |
| ------- | -----------------------------------------------------------------------: |
| uint256 | Total pool size of stablecoinswap contract in USD multiplied by 10\*\*18 |

## supportedTokens

{% tabs %}
{% tab title="Smart Contract" %}

```python
supportedTokens: public(address[5])
```

{% endtab %}

{% tab title="Web3" %}

```
contract.methods.supported_tokens(index).call()
```

{% endtab %}
{% endtabs %}

| Parameter | Type |                         Description |
| --------- | ---- | ----------------------------------: |
| index     | uint | Index of a supported token (0 to 4) |

| Returns |                        |
| ------- | ---------------------: |
| address | Address of ERC20 token |

## normalized\_token\_prices

{% tabs %}
{% tab title="Smart Contract" %}

```python
normalized_token_prices: public(map(address, uint256))
```

{% endtab %}

{% tab title="Web3" %}

```javascript
contract.methods.normalized_token_prices(token_address).call()
```

{% endtab %}
{% endtabs %}

| Parameter | Type    |                     Description |
| --------- | ------- | ------------------------------: |
| address   | address | Address of ERC20 token contract |

| Returns |                                                               |
| ------- | ------------------------------------------------------------: |
| uint256 | Returns normalized price in USD for a token at given address. |

Where:

`normalized_usd_price = usd_price * 10**8 * 10**(18 - token.decimals)`

Example: USD price for USDC = $1.0, token has 6 decimals:

`normalized_usd_price = 100000000000000000000`

USD price for DAI token is the price according to [https://etherscan.io/address/0x787f552bdc17332c98aa360748884513e3cb401a](https://etherscan.io/address/0x787f552bdc17332c98aa360748884513e3cb401a#readContract) and USD price for any other token is always $1.0


