# GnosisRouter

Router implementation with functions to interact with xDAI on Gnosis Chain.

## Variables

### sDAI

```solidity
contract IERC20 sDAI
```

*sDAI address.*

### savingsXDaiAdapter

```solidity
contract SavingsXDaiAdapter savingsXDaiAdapter
```

*SavingsXDaiAdapter address.*

## Functions

### constructor

```solidity
constructor(contract IConditionalTokens _conditionalTokens, contract IWrapped1155Factory _wrapped1155Factory) public
```

*Constructor.*

**Parameters**

| Name                 | Type                         | Description                  |
| -------------------- | ---------------------------- | ---------------------------- |
| \_conditionalTokens  | contract IConditionalTokens  | Conditional Tokens contract. |
| \_wrapped1155Factory | contract IWrapped1155Factory | Wrapped1155Factory contract. |

### splitFromBase

```solidity
function splitFromBase(contract Market market) external payable
```

Splits a position using xDAI and sends the ERC20 outcome tokens back to the user.

*The ERC20 associated to each outcome must be previously created on the wrapped1155Factory.*

**Parameters**

| Name   | Type            | Description          |
| ------ | --------------- | -------------------- |
| market | contract Market | The Market to split. |

**Pre-condition**

1. The ERC20 associated to each outcome must be previously created on the wrapped1155Factory.

**Logic**

1. The user send native tokens (xDAI), which will be converted to Savings xDAI (sDAI) and transferred  to <mark style="color:red;">`GnosisRouter`</mark>.
2. Call <mark style="color:red;">`conditionalTokens.splitPosition`</mark>  to transfer sDAI from <mark style="color:red;">`GnosisRouter`</mark> to <mark style="color:red;">`ConditionalTokens`</mark>, while minting corresponding ERC1155 outcome tokens to <mark style="color:red;">`GnosisRouter`</mark>.
3. Transfer ERC1155 outcome tokens from <mark style="color:red;">`GnosisRouter`</mark> to <mark style="color:red;">`Wrapped1155Factory`</mark> contract. There is a callback function in <mark style="color:red;">`Wrapped1155Factory`</mark> that will wrap the tokens into ERC20 tokens, then transfer the wrapped tokens back to the original user.

### mergeToBase

```solidity
function mergeToBase(contract Market market, uint256 amount) external
```

Merges positions and sends xDAI to the user.

*The ERC20 associated to each outcome must be previously created on the wrapped1155Factory.*

**Parameters**

| Name   | Type            | Description                            |
| ------ | --------------- | -------------------------------------- |
| market | contract Market | The Market to merge.                   |
| amount | uint256         | The amount of outcome tokens to merge. |

**Pre-condition**

1. The ERC20 associated to each outcome must be previously created on the wrapped1155Factory.
2. The caller must have \<amount> ERC20 tokens of all the outcomes of a market. For example, to call <mark style="color:red;">`mergeToBase`</mark> of a market with 3 outcomes: Yes, No, Invalid, the caller must have \<amount> tokens for each outcome.

**Logic**

Merging positions does precisely the opposite of what splitting a position does.&#x20;

1. Unwraps ERC20 outcome tokens (which will burn these tokens and transfer corresponding ERC1155 outcome tokens back to the <mark style="color:red;">`GnosisRouter`</mark> contract).&#x20;
2. Call <mark style="color:red;">`conditionalTokens.mergePositions`</mark> to burn ERC1155 outcome tokens.
3. Return sDAI to <mark style="color:red;">`GnosisRouter`</mark>, which will then be converted to xDAI and sent to the user.

### redeemToBase

```solidity
function redeemToBase(contract Market market, uint256[] outcomeIndexes, uint256[] amounts) external
```

Redeems positions and sends xDAI to the user.

*The ERC20 associated to each outcome must be previously created on the wrapped1155Factory.*

**Parameters**

| Name           | Type            | Description                          |
| -------------- | --------------- | ------------------------------------ |
| market         | contract Market | The Market to redeem.                |
| outcomeIndexes | uint256\[]      | The index of the outcomes to redeem. |
| amounts        | uint256\[]      | Amount to redeem of each outcome.    |

**Pre-condition**

1. The ERC20 associated to each outcome must be previously created on the wrapped1155Factory.
2. The caller must have ERC20 tokens of the outcomes they want to redeem. For example, to call <mark style="color:red;">`redeemToBase`</mark> for outcome Yes of a market with 3 outcomes: Yes, No, Invalid, the caller must have Yes tokens. (The call will still be processed even if the caller does not have tokens, but it will have no effect).
3. <mark style="color:red;">`conditionalTokens.reportPayouts`</mark> has been called by an oracle.

**Logic**

1. Unwraps ERC20 outcome tokens (which will burn these tokens and transfer corresponding ERC1155 outcome tokens back to the <mark style="color:red;">`GnosisRouter`</mark> contract).&#x20;
2. Call <mark style="color:red;">`conditionalTokens.redeemPositions`</mark> to burn ERC1155 outcome tokens.
3. Return sDAI to <mark style="color:red;">`GnosisRouter`</mark> . <mark style="color:red;">`GnosisRouter`</mark> will calculate the sDAI difference before and after redeeming, which will then be converted to xDAI and sent to the user.
