Compound’s Gateway: a deep dive into setting up a validator node.

Timothy McCallum
May 12 · 11 min read

Compound’s Gateway is an interest-bearing stable-coin bridge between compatible DeFi blockchains [1]. Amongst many other things, Compound’s Gateway allows you to upload an asset of your choosing (as collateral) and then borrow a different asset (against your collateral). More on on borrowing below, please read on 😊

Compound’s Gateway is built on Substrate; a modular framework that enables you to create purpose-built blockchains by composing custom or pre-built components [2].

https://www.parity.io/substrate/

As highlighted by Compound Finance … “We chose Substrate so that we could focus on building application code, instead of inventing consensus algorithms; it’s a modern framework built on a modern language, Rust.

Mozilla Foundation, CC BY 4.0 <https://creativecommons.org/licenses/by/4.0>, via Wikimedia Commons

Why Gateway?

A unique benefit of Gateway is the fact that users are not just wrapping assets and transacting on the Ethereum blockchain. Instead Gateway is actually a stand-alone distributed ledger (blockchain) which provides an opportunity to borrow assets native to one chain i.e. Ethereum, with collateral from another chain i.e. Polkadot.

It is important to know that both native network assets and tokenized assets can all be processed on Gateway. For example assets can include DOT, ETH, BAT, DAI, UNI, USDC, USDT and even wrapped asset such as Wrapped Bitcoin (WBTC) as shown below.

Source: https://app.compound.cash/

Compound Chain / Gateway

You may stumble across the term “Compound Chain”, Don’t be confused, just remember that the term “Compound Chain” is now just simply known as “Gateway”.

Blockchain interoperability

Gateway is designed from the ground up to enable the bridging of value between, what are known as, “peer” chains.

A peer chain is capable of locking and unlocking assets. In the context of an Ethereum compatible blockchain a peer chain will have the ability to execute functions on Compound’s “Starport.sol” (connector contract).

“Starport” contract deployed on each peer chain.

Starport is a contract to link a peer chain to Gateway. For example, link Ethereum mainnet to Gateway. Once linked, Gateway is capable of integrating with that peer chain. For example, supporting a list of collateral assets from that peer chain i.e ETH, UNI, DAI and so forth [3].

With Gateway, you can quickly and inexpensively transfer any asset in Gateway to other users — even wallets from other (peer chain) blockchains.

Users simply interact with Gateway using their existing blockchain private/public key pairs. We have an example of using the Gateway DApp (with screenshots below). For now, let’s take a look at the actual smart contract code which the DApp calls (on behalf of the users).

Gateway allows users to upload collateral assets into Gateway via the Starport (smart contract). Here is the actual code for the Starport contract’s lock(asset, amount)function [4].

Compound Finance’s Starport.sol contract — lock function

Running a Starport on your own Ethereum compatible blockchain network

Running a Starport contract on your peer chain begins with deploying the Starport.sol smart contract. Written in Solidity, this contract can be compiled and deployed to many different Ethereum compatible blockchains, two that I closely know of are:

Why become a validator

Gateway is secured by a distributed group of governance-approved validators. Validators are tasked with all running the same state transition function [3] and in doing so make up a Byzantine Fault Tolerant Proof-of-Authority (PoA) network.

Gateway is a re-imagination of the original Compound Protocol; it solves some of the limitations that the original protocol faced. For example high transaction costs, as well as, the inability to support assets that are not tokenized on Ethereum.

Why wouldn’t you want to be part of this? Oh … also, did I mention that you can earn CASH 🤓

Gateway Validators

Compound governance (on Ethereum) governs Gateway. More specifically, one of the steps to becoming a Validator is to be approved by Compound governance via a proposal on Compound’s pre-existing governance mechanism (which of course runs on the Ethereum mainnet) [5].

Gateway validators, stream logs from the peer chain and witness events. For example validators would be on the lookout for any “locking activity” by closely monitoring for the associated Lock event log (emitted by Starport.sol whenever a collateral asset is successfully locked).

Compound Finance’s Starport.sol contract — Lock event

Once approved, the set of validators are essentially operating a Byzantine Fault Tolerant Proof of Authority (PoA) network; Gateway.

The responsibilities of validators include:

  • authoring blocks
  • validating blocks (which are generated by other nodes)
  • monitoring peer chains for events
  • signing valid notices for peer chains
  • updating oracle prices

We mentioned above that Gateway is built using Substrate. Logically, Gateway utilises session keys [6]. Session keys (in Substrate nomenclature) are “hot keys” that are used by validators to sign consensus-related messages.

Each validator should have a hardware wallet (key) which can be used to sign extrinsics to set session keys (for their own validating node).The validator provides the public address (which is associated with this specific hardware wallet keypair) to governance. This is known as a “controller key” [5].

Once a controller key is accepted by governance, the validator must go ahead and run a validating node on which the validator will then execute the SetKeys function at least one time (using a set of public session keys) or the validator might be deemed unavailable [5].

Gateway’s health

In an effort to keep Gateway’s network healthy, transaction fees are implemented to discourage frivolous transactions. Fees are charged in CASH and are rewarded to network validators.

CASH is Gateway’s native unit of account [7]. CASH is created by the protocol through borrowing, therefore the amount of CASH held by users and validators is always equal to the amount of CASH debt. All CASH held by users and validators earns a continually compounding interest rate (yield) which increments globally through an interest rate index.

Becoming a Validator Node

There is a Gateway Validator Guide [8] which has a section called “Booting a Validator Node”. The following is derived from that document. There is also a node-operation web page with similar details. Let’s start at the beginning.

First, we need to install Rust

cd ~
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup toolchain install nightly
rustup target add wasm32-unknown-unknown --toolchain nightly

We then go ahead and get Substrate dependencies

cd ~
curl https://getsubstrate.io -sSf | bash -s -- --fast

The following commands will build the binary from source

cd ~
git clone https://github.com/compound-finance/gateway.git
cd gateway
git checkout m8
cargo +nightly build --release

Gateway uses ports 30333, 9944 and 9933.

Nodes connect to each other (by default) using TCP via port 30333. You will need to configure your firewall/router (port forwarding etc.) to accomodate this.

In addition to port 30333, Nodes also use Web Socket port 9944.

Lastly, there is a public RPC port (optional), which is 9933.

We need to set a bunch of environmental variables before we proceed.

Add the following line to your ~/.profile file.

  • public address of an account that you created (to be your miner account)
  • the RPC endpoint of Eth blockchain (at this stage in testing we are of course going to use Ropsten RPC endpoint). This ultimately allows your node to connect to (and read events from) the Ropsten Starport contract.
  • open price feed (OPF) URL
  • an Ethereum private key (of an account which you own); must not be the same account as the miner account above.
export MINER="ETH:0x1234567890etc"
export ETH_RPC_URL="https://ropsten.infura.io/v3/asdf"
export OPF_URL="https://prices.compound.finance/coinbase"
export ETH_KEY="anEthPrivateKey"

Set these variables for your current session

source ~/.profile

Start the node in unsafe mode to set your author key

./target/release/gateway \
--chain ./chains/testnet/chain-spec-raw.json \
--pruning=archive \
--rpc-methods Unsafe \
--validator \
--bootnodes /dns/brr-alice.compound.cash/tcp/30333/p2p/12D3KooWCdDZiHogARXgUTce1BCdg26dog6kvPaBSW6293cYdz4b

From the same machine (localhost) please go ahead and run the following command. The port will be 9933 assuming you did not change it in the config.

curl -H "Content-Type: application/json" -d '{"id":1, "jsonrpc":"2.0", "method": "author_rotateKeys", "params":[]}' http://localhost:9933

The above command will return the following result.

{"jsonrpc":"2.0","result":"0xdafbdc07ee5db3341ef57f2ad8990ebb84937dc25e9cbcef1781fd1e47517e6572be43581de70e900ad5462637022fa5a8666ee841b9db79af842c2763a70d2b","id":1}

We have now essentially rotated our session keys. The output of this function is your public keys which are privately stored in your node. They are not considered privileged.

We now need to associate the keys (which we generated in the last step) with our Substrate account. Essentially, we are telling Gateway about our Session keys by signing and submitting an extrinsic. This is what associates our Validator node with our Controller account.

The session key is set via the polkadot.js.org/apps online software. Firstly open the following Polkadot/Substrate Portal URL which is pointing to the testnet implimentation (wss://alice.compound.cash ) of Gateway.

First, paste the contents of this JSON file into the textbox at this polkadot.js UI.

Then click “Save”.

Essentially we want to paste the contents of this officialtypes.json file into that area, however at the time of writing there was an issue with the latest version of this file (which is why we are using an older commit).

Next, choose the “Extrinsics” option from the “Developer” dropdown menu.

Click on the word “system” (which is shown under the “submit the following extrinsic” text). This will produce a dropdown list.

Select “session” from the dropdown list and you will then see a second dropdown list on the right-hand side. Select “setKeys”, as shown in the screen capture above.

The “keys” argument is the output result of rotateKeys (step which we performed just a few minutes ago).

The “proof” argument should be 0x00.

Submit the transaction signed with the Account that you will use to control your validator keys ( an externally owned account ). You will be presented with an additional “Sign and Submit” button in a new “authorize transaction” window (as shown below).

It is sometimes necessary to purge the chain data on our local node (which was generated from the preliminary starting of our node in the previous steps).

To perform this association, stop the previous ./target/release/gateway ...

Once the previous gateway is stopped, run the following command to purge the old blockchain data.

./target/release/gateway purge-chain \
--chain ./chains/testnet/chain-spec-raw.json \
--database paritydb -y

The purge-chain command, above, will produce a result similar to the following.

/home/tpmccallum/.local/share/gateway/chains/local_testnet/paritydb" removed.

Starting our node entails removing the unsafe RPC mode (remember we previously started our node in unsafe mode) and in addition, enabling telemetry. We also have to change the name before we restart for keeps.

Once you are satisfied with the parameters, you can run your equivalent of the following command.

./target/release/gateway \
--chain ./chains/testnet/chain-spec-raw.json \
--bootnodes /dns/brr-alice.compound.cash/tcp/30333/p2p/12D3KooWCdDZiHogARXgUTce1BCdg26dog6kvPaBSW6293cYdz4b \
--validator \
--no-mdns \
--telemetry-url "wss://telemetry.polkadot.io/submit/ 0" \
--name "ParaState's TestNet Node"

The result will initially look like the screen capture below

Using Gateway’s UI

The front-end user-interface of Gateway is intuitive and easy to use. The following section shows you how to upload and download assets and more. Let’s get started …

There is a Gateway Dashboard guide [10] which demonstrates how the Gateway is designed to be used. At present, the Gateway is only deployed on Ropsten testnet.

When you upload an asset, it counts as collateral. To upload an asset, you simply have to press the up-arrow button (shown in the screen shot below).

At this point you enter the amount you would like to upload and press the green “Upload” button (shown in the screen shot below).

The DApp handles this transaction via a web3 provider.

The in-browser wallet software then prompts you to “Confirm” this transaction.

Once confirmed, you will see your new balance i.e. the amount of collateral which you now have inside the Compound DApp.

Perhaps one of the most important things to be aware of, is your “risk limit”. Risk limit value is generated, by the DApp, when you upload an asset.

In order to Download an asset you must have previously uploaded a different asset and or have a “Collateral Asset Balance” on Gateway [10].

You can Download any asset that is both a supported asset on Gateway and is compatible with your connected wallet. For instance, you could not download Ether to a Polkadot wallet [10]. In this demonstration, we are downloading ETH via CryptoWallets/MetaMask.

👇👇👇

Warning: You will be Liquidated if Risk Limit Used meets or exceeds 100%

👆👆👆

Once submitted to the Gateway blockchain the transaction will undergo approval from the aforementioned validators. The transaction will still be “in-progress” until you go ahead and press the “Submit L1” button (shown in the screen capture below).

These in-progress transactions will show as “Pending” for a short time. Once complete the assets will have been moved from Gateway to your actual wallet software.

https://www.parastate.io/

At the start of this article, we mentioned Substrate. ParaState provides a smart contract virtual machine (VM) for all Substrate-based blockchains. All existing Ethereum smart contracts work on ParaState.

Evidenced by this article, ParaState is building the necessary infrastructure as well as actively researching and testing the inner workings of Compound’s Gateway. The ultimate outcome is for ParaState to participate in the compound network; both as a peer chain and as a validating node. Compound’s Gateway provides opportunities for peer blockchains to participate in fundamental decentralised finance (DeFi) operations. This is a huge step forward for the future of cryptocurrency and (DeFi) and one that ParaState strives to play an active role in.

https://www.parastate.io/

References

[1] https://github.com/compound-finance/gateway

[2] https://substrate.dev/

[3]https://compound.cash/documents/Compound.Cash.Whitepaper.pdf

[4] https://github.com/compound-finance/gateway/blob/develop/ethereum/contracts/Starport.sol#L53

[5] https://compound.cash/network/becoming-validator

[6] https://substrate.dev/docs/en/knowledgebase/learn-substrate/session-keys

[7] https://compound.cash/

[8] https://docs.google.com/document/d/1Dz-pAFn49A1vss0hxOeRIC4XZHXBTbhYrjle-TrvMgA/edit#heading=h.yr1dpc9noa5k

[9] https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html

[10] https://docs.google.com/document/d/12EY6Gqt4tIp9TP0icBpsOuZZFh_bVln6Fn9YNa5XpkU/edit

ParaState

ParaState is your best choice of next-gen Web3 applications one stop development platform.

ParaState

ParaState has officially launched the public testnet, Plato, running as a standalone Substrate blockchain right now. ParaState aims to bridge application and developer ecosystem between Polkadot and Ethereum, and other chains who want to provide Ethereum compatibilities as well.

Timothy McCallum

Written by

Researcher and Open Source Core Developer at secondstate.io — Book contributions Mastering Ethereum, Building Blockchain Apps — Mentor Google Summer of Code…

ParaState

ParaState has officially launched the public testnet, Plato, running as a standalone Substrate blockchain right now. ParaState aims to bridge application and developer ecosystem between Polkadot and Ethereum, and other chains who want to provide Ethereum compatibilities as well.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store