Skip to content

Instantly share code, notes, and snippets.

@KINGdotNET
Forked from fbslo/oracle.md
Created November 6, 2020 07:43
Show Gist options
  • Save KINGdotNET/09df5f263d8e74e12b83968b6990327b to your computer and use it in GitHub Desktop.
Save KINGdotNET/09df5f263d8e74e12b83968b6990327b to your computer and use it in GitHub Desktop.
Welcome file

Wrapped Hive Engine Tokens

EARLY ACCESS, not tested yet

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 hhot wallet (while you keep most of the tokens in cold wallet). It requires small modification to smart contract. See https://github.com/fbslo/wLEO-contract.

What do you need?

  • Linux server (tested on Ubuntu 18.04)
  • API key from EthGasStation
  • Infura.io API key / Ethereum node enndpoint
  • 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

Now you will need to edit token details:

nano contracts/wToken.sol

Edit this line:

contract wToken is WrappedToken, ERC20Detailed("Your token name", "YOURTOKENSYMBOL", decimal_places) {}

Example for token named Wrapped Hive, with symbol WHIVE and 3 decimals places (not "quote" signs here):

contract wToken is WrappedToken, ERC20Detailed(Wrapped Hive", "WHIVE", 3) {}

Now let's build and deploy the contract:

truffle build

Now prepare for deployment by adding the private key of your address and Ethereum endpoint (from Infura.io):

mv truffle-config.demo.js truffle-config.js

Let's add Ethereum endpoint and private key:

echo 'PRIVATE_KEY=yourEthereumPrivateKeyHere...' >> .env echo 'ETHEREUM_ENDPOINT=https://mainnet.infura...' >> .env

Now finally deploy token:

truffle deploy --network mainnet

Congrats, you now have your own Ethereum token. You will see your contract address, store it.

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

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

You will need to keep the app running even when a SSH connection is closed. I'll use pm2, but if you prefer any other tool (like screen), feel free to use it.

Let's install pm2:

npm install pm2@latest -g

Now start the app:

pm2 start src/index.js --name frontend

Learn more about how to use pm2: PM2 quickstart

To see logs, use pm2 logs frontend command.

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

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:

npm install

Again, now you need to edit config file:

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

TOKEN_SYMBOL=
HIVE_TOKEN_PRECISION=
ETHEREUM_TOKEN_PRECISION=

HIVE_ACCOUNT=
HIVE_ACCOUNT_PRIVATE_KEY=

ETHEREUM_ADDRESS=
ETHEREUM_PRIVATE_KEY=
ETHEREUM_CONTRACT_ADDRESS=

ETHEREUM_CHAIN_ID=
ETHEREUM_CHAIN=
ETHEREUM_CONTRACT_FUNCTION=mint
ETHEREUM_GAS_LIMIT=100000
ETH_FEE_SPEED=fast

MIN_AMOUNT=
MAX_AMOUNT=
PERCENTAGE_DEPOSIT_FEE=


ETHEREUM_ENDPOINT=
HIVE_RPC_NODES = https://api.hive.blog,https://anyx.io,https://api.openhive.network,https://hived.privex.io,https://api.hivekings.com
MONGODB=

ETH_GAS_STATON_API_KEY=

You will need to keep the app running even when a SSH connection is closed. I'll use pm2, but if you prefer any other tool (like screen), feel free to use it.

Let's install pm2:

npm install pm2@latest -g

Now start the app:

pm2 start src/index.js --name wToken

Learn more about how to use pm2: PM2 quickstart

To see logs, use pm2 logs wToken command.

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