# Self-hosted Uniswap Frontend

Instructions to setup and deploy Uniswap app on Urbit

Build and deploy:

- Urbit
- Uniswap app

## Setup

Clone required repositories:

```bash
laconic-so --stack uniswap-urbit-app setup-repositories --pull

# If this throws an error as a result of being already checked out to a branch/tag in a repo, remove the repositories and re-run the command
```

Build the container images:

```bash
laconic-so --stack uniswap-urbit-app build-containers
```

## Create a deployment

First, create a spec file for the deployment, which will map the stack's ports and volumes to the host:

```bash
laconic-so --stack uniswap-urbit-app deploy init --output uniswap-urbit-app-spec.yml
```

### Ports

Edit `uniswap-urbit-app-spec.yml` such that it looks like:

```yml
stack: uniswap-urbit-app
deploy-to: compose
network:
  ports:
    proxy-server:
      - '4000:4000'
    urbit-fake-ship:
      - '8080:80'
    ipfs:
     - '4001'
     - '8081:8080'
     - 0.0.0.0:5001:5001
volumes:
  urbit_app_builds: ./data/urbit_app_builds
  urbit_data: ./data/urbit_data
  ipfs-import: ./data/ipfs-import
  ipfs-data: ./data/ipfs-data
```

Note: Skip the `ipfs` ports if using an externally running IPFS node, set via `config.env`, below.

### Data volumes

Container data volumes are bind-mounted to specified paths in the host filesystem.
The default setup (generated by `laconic-so deploy init`) places the volumes in the `./data` subdirectory of the deployment directory. The default mappings can be customized by editing the "spec" file generated by `laconic-so deploy init`.

---

Once you've made any needed changes to the spec file, create a deployment from it:

```bash
laconic-so --stack uniswap-urbit-app deploy create --spec-file uniswap-urbit-app-spec.yml --deployment-dir uniswap-urbit-app-deployment
```

## Set env variables

Inside the deployment directory, open the file `config.env` and set the following env variables:

  ```bash
  # App to be installed (Do not change)
  CERC_URBIT_APP=uniswap

  # External RPC endpoints
  # https://docs.infura.io/getting-started#2-create-an-api-key
  CERC_INFURA_KEY=

  # Uniswap API GQL Endpoint
  # Set this to GQL proxy server endpoint for uniswap app
  # (Eg. http://localhost:4000/v1/graphql - in case stack is being run locally with proxy enabled)
  # (Eg. https://abc.xyz.com/v1/graphql - in case https://abc.xyz.com is pointed to the proxy endpoint)
  CERC_UNISWAP_GQL=http://localhost:4000/v1/graphql

  # Optional

  # Whether to enable app installation on Urbit
  # (just builds and uploads the glob file if disabled) (Default: true)
  CERC_ENABLE_APP_INSTALL=

  # Whether to run the proxy GQL server
  # (disable only if proxy not required to be run) (Default: true)
  CERC_ENABLE_PROXY=

  # Proxy server configuration
  # Used only if proxy is enabled

  # Upstream API URL
  # (Eg. https://api.example.org)
  CERC_PROXY_UPSTREAM=https://api.uniswap.org

  # Origin header to be used in the proxy
  # (Eg. https://app.example.org)
  CERC_PROXY_ORIGIN_HEADER=https://app.uniswap.org

  # IPFS configuration

  # IFPS endpoint to host the glob file on
  # (Default: http://ipfs:5001 pointing to in-stack IPFS node)
  CERC_IPFS_GLOB_HOST_ENDPOINT=

  # IFPS endpoint to fetch the glob file from
  # (Default: http://ipfs:8080 pointing to in-stack IPFS node)
  CERC_IPFS_SERVER_ENDPOINT=
  ```

## Start the stack

Start the deployment:

```bash
laconic-so deployment --dir uniswap-urbit-app-deployment start
```

* List and check the health status of all the containers using `docker ps` and wait for them to be `healthy`

* Run the following to get login password for Urbit web interface:

  ```bash
  laconic-so deployment --dir uniswap-urbit-app-deployment exec urbit-fake-ship "curl -s --data '{\"source\":{\"dojo\":\"+code\"},\"sink\":{\"stdout\":null}}' http://localhost:12321"

  # Expected output: "<PASSWORD>\n"%
  ```

* Open the Urbit web UI at http://localhost:8080 and use the `PASSWORD` from previous step to login

* The uniswap app is not available when starting stack for the first time. Check `urbit-fake-ship` logs to see that app has installed:

  ```bash
  laconic-so deployment --dir uniswap-urbit-app-deployment logs -f

  # Expected output:
  # laconic-3ccf7ee79bdae874-urbit-fake-ship-1    | docket: fetching %http glob for %uniswap desk
  # laconic-3ccf7ee79bdae874-urbit-fake-ship-1    | ">="">="uniswap app installed
  ```

* The uniswap app will be now visible at http://localhost:8080

## Clean up

To stop all uniswap-urbit-app services running in the background, while preserving data:

```bash
# Only stop the docker containers
laconic-so deployment --dir uniswap-urbit-app-deployment stop

# Run 'start' to restart the deployment
```

To stop all uniswap-urbit-app services and also delete data:

```bash
# Stop the docker containers
laconic-so deployment --dir uniswap-urbit-app-deployment stop --delete-volumes

# Remove deployment directory (deployment will have to be recreated for a re-run)
rm -r uniswap-urbit-app-deployment
```
