Skip to content

Instantly share code, notes, and snippets.

@fbslo
Last active November 13, 2020 06:26
Show Gist options
  • Save fbslo/52b5ce87024e994719ed8032d52b76d5 to your computer and use it in GitHub Desktop.
Save fbslo/52b5ce87024e994719ed8032d52b76d5 to your computer and use it in GitHub Desktop.
Welcome file

Wrapped Hive Engine Tokens

As some of you might be aware, I've been working on Wrapped tokens for the last 3 months. First Wrapped Hive, then modified oracle for wLEO and now I'm presenting an out-of-a-box solution for anyone to create their own Hive Engine Wrapped Token. It's meant mostly for "owners" of HE tribes, but anyone can start one if they want.

I tried to make setup as easy as possible, but you will still need to follow some instructions. But it's as close to no-code as possible. I tried to create the same experience as the hive-in-a-box does for the witness setup.

First, why would you even want to wrap Hive Engine tokens to Ethereum when HE has free, almost instant transactions (compared to quite expensive fees on Ethereum). The answer is simple, users, and liquidity.

Even most "liquid" tokens on Hive Engine such as LEO or DEC have under $50k in liquidity. There is less than $100k (1M) swap.hive wrapped on HE. Compared to $2.9B ($2,900,000,000) on Uniswap alone and $11B locked in entire DeFi, you can see there is a big difference.

Before starting with the actual setup, you should know how this works (don't worry, no technical details here) The setup (and app itself) is made of 3 parts:

  • Frontend: The frontend is a nice website that will show information about your wrapped token and provide an interface for interacting with smart contracts to non-technical users. It's using generic whive.network design, but you can modify header color in config ;) All other info such as token name, symbol, platforms... is updated directly from the config file, so no coding required. It does not have access to any private info such as private keys.

  • Backend: This is an actual app that will track events on both Hive & Ethereum blockchain and take care of wrapping and unwrapping tokens. It's completely separated from the frontend and should be run on a separate server for maximum security.

  • Smart Contract: Smart contract for ERC20 Ethereum token.

Security

As you might know, wLEO got hacked a few weeks ago and over $100k in liquity was stolen. Here are some ways I implemented to prevent possible future disasters.

This oracle app is centralized, and as such, there is no way to make is 100% secure. But we can do a lot to reduce effect any hack/exit scam/bug can cause.

While Hive Engine is (currently) centralized, this app have an option to prevent attacks from this. You can run your own Hive Engine node as secondary node, and (if enabled), app will check any incoming HE transaction in both "official" server and your backup. Only if transaction is valid on both nodes, ERC20 tokens will be issued.

While the default method for creating tokens is mint (new tokens are later burned), the app has native support for transfer method, where tokens are not minted, bbut instead transfered from hot wallet (while you keep most of the tokens in cold wallet). You can select your method during contract setup.

What do you need?

  • Linux server (tested on Ubuntu 18.04)
  • API key from EthGasStation
  • Infura.io API key / Ethereum node endpoint
  • Some ETH to pay for fees (0.1 should be enough, but it varies a lot) when deploying a new token
  • ~20-30 minutes

ERC20 Token Smart Contract deployment

Let's start with the smart contract deployment, so we will have a contract address later. ERC20 tokens are simply smart contracts on Ethereum that include a set of at least 6 basic functions.

Our token is using industry standard OpenZeppelin contract with 2 extra functions, convertTokenWithBurn()/ convertTokenWithTransfer()and convertTokenFromWithBurn() that are used when converting tokens back to native asset.


Install NodeJS:

curl -sL https://deb.nodesource.com/setup_12.x -o nodesource_setup.sh`
sudo bash nodesource_setup.sh
sudo apt install nodejs

Let's just clone source code:

git clone https://github.com/fbslo/wToken-contract && cd wToken-contract && npm install -g truffle && npm install && npm run install_cli

If you encounter any EACCES: permission denied errors, try running this commands (especially npm install -g truffle as root, so sudo npm install -g truffle)

Now run CLI: contract

You will be asked questions about installation, and at the end, you will receive contract address. It's also stored in state.json. ABI will be generated in tokenABI.js file.

Now let's delete the config file:

sudo apt install wipe && wipe .env

Frontend

As I mentioned earlier, it should be running on a separate server as backend, but if you want to use only one server, use cloudflare to hide real IP and prevent DDoS attacks.

Install NodeJS (if you didn't already do it for token deployment):

curl -sL https://deb.nodesource.com/setup_12.x -o nodesource_setup.sh`
sudo bash nodesource_setup.sh
sudo apt install nodejs

Now clone the project source code from GitHub:

git clone https://github.com/fbslo/whe-frontend

Move to the project directory and install dependencies:

cd whe-frontend && npm install && npm run install_cli && npm install pm2 -g && npm install pm2

Now you only need to edit config file. But first, let's rename it and then open it with your favorite text editor (I prefer nano):

mv .env.demo .env && nano .env

You will see the config file, I think it's self-explanatory, but here you can see config file with comments.

Once you are done, save & close it with ctrl+x

Now customize "platforms":

Edit platforms.json file: nano platforms.json Add objects to array, like seen in a example below. Font Awesome icon names can be found at https://fontawesome.com/.

{
  "platforms": [
    {
      "url": "https://url.com",
      "name": "platform",
      "fa_icon": "fa-home"
    },
    {
      "url": "https://url2.com",
      "name": "platform2",
      "fa_icon": "fa-home"
    }
  ]
}

Now start app with CLI: frontend start

You might see error ReferenceError: document is not defined, just ignore it.

List of all available commands:

start, restart, logs, help, stop, status

Visit http://your_ip_or_localhost:8080 and you should see your website!

It's recommended to use reverse proxy (such as nginx) to redirect requests from ports 80 (http) and 443 (https) to 8080.

Backend

As before, you need NodeJS installed, so if you don't have it yet, install it:

curl -sL https://deb.nodesource.com/setup_12.x -o nodesource_setup.sh`
sudo bash nodesource_setup.sh
sudo apt install nodejs

Now clone source code:

git clone https://github.com/fbslo/whe-backend && cd whe-backend

Install MongoDB:

sudo apt install -y mongodb

Install dependencies and CLI:

npm install && npm run install_cli && npm install pm2 -g && npm install pm2

Again, now you need to edit config file:

mv .env.demo .env && nano .env

You will see the config file, I think it's self-explanatory, but here you can see config file with comments.

Now you can start the oracle using command: oracle start

List of all available commands:

start, restart, logs, help, stop, status

CLI is just wrapper for PM2 with some extra stuff (e.g. database status...).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment