# Energiswap smart-contracts

Energiswap is a decentralized protocol for automated token exchange.

## Prerequisites for deploying Energiswap smart-contracts locally

Make sure you are running  [Ganache CLI](https://github.com/trufflesuite/ganache-cli) with the following command:

```
ganache-cli -a 12 -e 1000000 -l 10000000 -p 7545 -m "tackle crazy sibling hobby hurry cat donate abstract sword city 
enough crime"
```

## Deploying Energiswap smart-contracts locally

1. Git clone repo with `git clone git@git.energi.software:energi/defi/uniswap/energiswap-contracts.git`
2. Run  `yarn install` to install the necessary dependencies for the project.
3. Run `truffle compile` to compile the contracts inside the *contracts* folder.
4. Run `truffle migrate` to deploy the contracts to the local *ganache* blockchain.

## Testing
Test contracts are defined under the *test* directory. To run the tests run:
`truffle test`

## Architecture

See the architecture diagram in `./energiswap-contracts-architecture-diagram.png`

## Upgrades using Energi governance

The following Energiswap contracts are fully upgradeable using Energi governance:

`EnergiswapRouter.sol`
`EnergiswapFactory.sol`
`EnergiswapPairsManager.sol`

In order to upgrade `EnergiswapPairsERC20.sol`, one should deploy a new implementation for this contract, then deploy a 
new implementation for `EnergiswapPairsManager.sol` pointing to the new `EnergiswapPairsERC20.sol` implementation, and 
finally upgrade `EnergiswapPairsManager.sol` to the new implementation using Energi governance.

`EnergiswapPairsManager.sol` and `EnergiswapPairsERC20.sol` hold the logic for Energiswap pairs. Upgrading those 
contracts upgrades the logic for all Energiswap pairs.

## Deploying Energiswap to Energi blockchain

`WNRG.sol` contract is a dependency to Energiswap smart contracts, it must be deployed for Energiswap to work.

`Multicall.sol` contract is a dependency to EnergiswapFactory and energiswap interface, it must be deployed for 
Energiswap to work.

Energi governance contracts must be deployed prior to deploying Energiswap Contracts.

#### Deploy EnergiswapFactory

```
factory = await EnergiswapFactory.new(feeToSetter, sporkProxy, '0x0000000000000000000000000000000000000000');
```

`feeToSetter` is the address of account which has permission to set the `feeTo` account which will receive trading fees.

`sporkProxy` is the address of the `SporkProxy.sol` smart contract which is part of Energi governance infrastructure.

Setting the last parameter to `0x0000000000000000000000000000000000000000` will result in `EnergiswapFactoryGovernedProxy`
contract being deployed automatically. Alternatively, the address of an already deployed proxy contract can be passed.

The address of the deployed `EnergiswapFactoryGovernedProxy` will be used later in the deployment process. It is available 
via the `proxy()` function of `EnergiswapFactory`.

#### Deploy EnergiswapPairsERC20

`pairsERC20 = await EnergiswapPairsERC20.new();`

#### Deploy EnergiswapPairsManager

```
pairsManager = await EnergiswapPairsManager.new(factoryProxyAddress, pairsERC20.address, sporkProxy, '0x0000000000000000000000000000000000000000');
```

`factoryProxyAddress` is the address of the deployed `EnergiswapFactoryGovernedProxy` contract.

`pairsERC20.address` is the address of the deployed `EnergiswapPairsERC20` contract.

`sporkProxy` is the address of the `SporkProxy.sol` smart contract which is part of Energi governance infrastructure.

Setting the last parameter to `0x0000000000000000000000000000000000000000` will result in `EnergiswapPairsManagerGovernedProxy`
contract being deployed automatically. Alternatively, the address of an already deployed proxy contract can be passed.

The address of the deployed `EnergiswapPairsManagerGovernedProxy` will be used later in the deployment process. It is available 
via the `proxy()` function of `EnergiswapPairsManager`.

#### Initialize EnergiswapPairsERC20

```
await pairsERC20.initialize(pairsManager.address);
```

`pairsManager.address` is the address of the deployed `EnergiswapPairsManager` contract.

#### Deploy EnergiswapRouter

```
router = await EnergiswapRouter.new(factoryProxyAddress, wnrg.address, sporkProxy, '0x0000000000000000000000000000000000000000');
```

`factoryProxyAddress` is the address of the deployed `EnergiswapFactoryGovernedProxy` contract.

`wnrg.address` is the address of the deployed `WNRG` contract.

`sporkProxy` is the address of the `SporkProxy.sol` smart contract which is part of Energi governance infrastructure.

Setting the last parameter to `0x0000000000000000000000000000000000000000` will result in `EnergiswapRouterGovernedProxy`
contract being deployed automatically. Alternatively, the address of an already deployed proxy contract can be passed.

The address of the deployed `EnergiswapRouterGovernedProxy` will be used later in the deployment process. It is available 
via the `proxy()` function of `EnergiswapRouter`.

#### Initialize EnergiswapFactory

```
await factory.initialize(multicall.address, routerProxyAddress, pairsManagerProxyAddress, pairsERC20.address);
```
`multicall.address` is the address of the deployed `Multicall` contract.

`routerProxyAddress` is the address of the deployed `EnergiswapRouterGovernedProxy` contract.

`pairsManagerProxyAddress` is the address of the deployed `EnergiswapPairsManagerGovernedProxy` contract.

`pairsERC20.address` is the address of the deployed `EnergiswapPairsERC20` contract.


## Energiswap Upgrades via Energi Governance

Start with deploying the desired upgrade implementation:

#### Deploying EnergiswapFactoryUpgrade

```
factoryUpgrade = await EnergiswapFactoryUpgrade.new(factoryStorageAddress, factoryProxyAddress);
```

`factoryStorageAddress` is the address of the existing `EnergiswapFactoryStorage` contract

`factoryProxyAddress` is the address of the existing `EnergiswapFactoryGovernedProxy` contract


#### Deploying EnergiswapPairsManagerUpgrade and EnergiswapPairsERC20Upgrade

```
pairsManagerUpgrade = await EnergiswapPairsManagerUpgrade.new(
      pairsManagerProxyAddress,
      pairsManagerStorageAddress,
      pairsERC20Upgrade.address
    );
```

`pairsManagerProxyAddress` is the address of the existing `EnergiswapPairsManagerGovernedProxy` contract

`pairsManagerStorageAddress` is the address of the existing `energiswapPairsManagerStorage` contract

`pairsERC20Upgrade.address` is the address of the deployed `EnergiswapPairsERC20` contract. This could be either the 
former `EnergiswapPairsERC20` contract, or a new `EnergiswapPairsERC20Upgrade` contract.

```
pairsERC20Upgrade.initialize(pairsManagerUpgrade.address);
```

#### Deploying EnergiswapRouterUpgrade

```
routerUpgrade = await EnergiswapRouterUpgrade.new(routerStorageAddress, routerProxyAddress);
```

`routerStorageAddress` is the address of the existing `EnergiswapRouterStorage` contract

`routerProxyAddress` is the address of the existing `EnergiswapRouterGovernedProxy` contract


#### Proposing a deployed upgrade

Once this is done, call the `proposeUpgrade()` function on the `GovernedProxy` contract of the contract being upgraded, 
with the address of the deployed upgrade implementation.

#### Masternodes voting and actual upgrade

Masternode owners can now vote to accept or reject the proposed upgrade. If quorum majority is reached, the upgrade 
proposal is accepted and anyone can now call the `upgrade()` function on the `GovernedProxy` contract of the contract 
being upgraded, with the address of the deployed upgrade proposal.
