Automate Your Strategies with dHEDGE Trading API

https://dhedge-trading.web.app/

Introduction

dHEDGE Trading API is an interface to dHEDGE's V2 standalone trading engine - powered by ether.js.

ether.js is a complete and compact library for interacting with Ethereum-based blockchains. The dHEDGE team has consolidated the process of running a node into a simple, yet powerful RESTful API. dHEDGE users can easily automate their trading strategies, request data, and monitor account balances in real time.

The dHEDGE Trading API is a programmable interface to the dHEDGE asset management protocol. For an application to connect to the API there must first be a running instance of a Vault.

Once a Vault has been established, a Manager can then utilize the API to create a new 'Trader Wallet' address that is hosted by dHEDGE in a permissionless and encrypted fashion. This drastically reduces the complexity of managing versions, ethers.js, and other requirements necessary for operating the SDK.

API Limitations

dHEDGE Trading API programming interface is designed to automate some of the operations a user would normally perform within the dHEDGE web app such as placing orders, and monitoring your account balance. There is no logic within the API other than to ensure the integrity of the exchanged messages.

Most validations and checks occur in the backend of the dHEDGE app and servers. It is important to familiarize yourself with the dHEDGE protocol in order to gain a better understanding on how the platform works before spending precious development time troubleshooting on the API side. If a certain feature or operation is not available in the dHEDGE dApp, it will also not be available on the API side.

Requests

The dHEDGE Trading API is designed to accept up to fifty messages per second coming from the client side. Anything coming from the client application to the dHEDGE Trading API counts as a message (i.e. requesting data, placing orders, requesting your portfolio data). This limitation is applied to all connected clients, where all connected client applications to the same instance of the dHEDGE Trading API combined cannot exceed this number. On the other hand, there are no limits on the amount of messages the dHEDGE Trading API can send to the client application.

Full details of available functions coming the API can be found here: https://github.com/dhedge/V2-Public/blob/master/DHedge.md


Trader Wallets

It is important to remember that dHEDGE allows Managers to designate a single Trader Wallet to perform trades on investor’s behalf. To refresh, the Trader Wallet is designated by the Manager from within the dHEDGE app.

Vaults can only have a single trading wallet at a time. Designating a Trader Wallet does not impact the Manager's ability to also initiate trades on behalf of the Vault. When trades are initiated by a Trader Wallet trading fees are expensed to the Trader Wallet and not the Manager’s. Therefore, the newly established Trader Wallet in this tutorial will need MATIC to fund trading expenses or gas fees.

dHEDGE Trading API Trading Wallets have the following rights:

  1. Approve Assets to Trade
  2. Change Assets to Trade
  3. Interact with Whitelisted DeFi Protocols

Now that we have a basic understanding of the mechanics and inner workings of the Trading Wallet and Manger Wallet, we can begin establishing a Trader Wallet with the dHEDGE Trading API.

Remember: A user will first need to create a Vault within the dHEDGE app for this process to work.

To read more about the Ops, go here.


Getting Setup

Copy your Vault Address
  1. Hint: When you visit the Vault on the dHEDGE app, the address is the last part of the url in your browser.
Visit the setup URL
  1. https://us-central1-dhedge-trading.cloudfunctions.net/createAccount?pool=[YOUR_POOL_ADDRESS]&network=[NETWORK]
Where

[YOUR_POOL_ADDRESS] = your Pool address in place of this text

[NETWORK] = 'polygon', 'optimism', 'arbitrum'

  1. Once the link above has been edited with your Pool address in place and network, visit this link in a browser.
  2. If successful, you will see an API response in your browser with this information
🦔
Note: This step creates a new hosted Trader Wallet. It is very important to store this information somewhere secure, as this information is not stored by dHEDGE. Instead, dHEDGE stores an encrypted version of this response that can only be decrypted with the API Key in this output message. If you lose your private apiKey, dHEDGE will have no way of recovering access to your Trader Wallet and you will need to set up a new Trader wallet. 
Set Trader Wallet
  1. Now that your Trader wallet has been created and you have stored the response message somewhere safe, go to https://app.dhedge.org/management and set the New Trader wallet to the newly created “traderAccount” address.
Fund Trader Wallet
  1. Send some MATIC token (5 should be enough) to cover gas fees for trading to your Trader account address.

To check if the ‘apiKey’ is valid use this url format:

  1. https://us-central1-dhedge-trading.cloudfunctions.net/checkApiKey?apiKey=[YOUR_API_KEY]
  2. A response like below means you have successfully established your hosted Trader wallet

Approving Assets to Trade

In order to utilize Toros derivative tokens along with other exchange assets, you first need to Approve the assets to trade with via the API once. Note: This is not the same as enabling the assets from within the dHEDGE web app. Approving assets is a necessary step in ensuring the Trading Wallet and managed API are able to execute trades.

To approve assets use this url format:

https://us-central1-dhedge-trading.cloudfunctions.net/approve?apiKey=[YOUR_API_KEY]&asset=[ASSET_CONTRACT]&platform=[EXCHANGE_VENUE]

Where:

[YOUR_API_KEY] = Your apiKey to the Trader wallet

[ASSET_CONTRACT] = Asset contract address to approved for trading

[EXCHANGE_VENUE] = possible selections [ “1inch”, “sushiswap”, ”quickswap”, ”balancer”, ”toros” ]

To approve Toros assets, remember to use the "toros" exchange venue option as well. Otherwise Toros assets will not be approved properly.

If successful, you should see a success message json output as such, and the asset is good to trade with the API.


Utilizing the Trading Engine

Update this link with your API Key in place of “YOUR_API_KEY” in the URL and adjust the parameters of ‘from’ and ‘to’ utilizing the asset’s contract address to designate which assets you want to trade from and to.

‘From’ means the asset you want to trade from within the Pool, and ‘to’ designates the asset you want to trade into.


To place trades use this url format:

https://us-central1-dhedge-trading.cloudfunctions.net/trade?apiKey=[YOUR_API_KEY]&from=[ASSET_CONTRACT_1]&to=[ASSET_CONTRACT_2]&share=[PERCENT_1]&slippage=[PERCENT_2]&platform=[EXCHANGE_VENUE]

Where:

[YOUR_API_KEY] = Your apiKey to the Trader wallet

[ASSET_CONTRACT_1] = Asset contract address (from = Sell)

[ASSET_CONTRACT_2] = Asset contract address (to = Buy)

[PERCENT_1] = Percent as a positive integer of [ASSET_CONTRACT_1] you want to exchange for [ASSET_CONTRACT_2] (where ‘100’ = 100%).

[PERCENT_2] = Percent as a positive integer of acceptable slippage (where ‘100’ = 100%)

[EXCHANGE_VENUE] = possible selections [ “1inch”, “sushiswap”, ”quickswap”, ”balancer”, ”toros” ]

  • As an option: Instead of using &share=[PERCENT_1], you may use &amount=[AMOUNT_1]
  • This URL variable is used to place a trade in quantity rather than percentages of underlying holdings. The amounts passed need to be in raw, whole numbers. For example, if you wanted to trade 45.5373 USDC you would need to pass 45537300 (45.5373 * 10**6, because USDC token has 6 decimals)

Slippage is the difference between the expected price of an order and the price when the order actually executes. The slippage percentage shows how much the price for a specific asset has moved. Due to the volatility of cryptocurrency, the price of an asset can fluctuate often depending on trade volume and activity.

If no exchange venue is passed in the URL, 1inch will be used as the default venue.

Toros Derivatives

After enabling the Toros assets in the Pool, add a special parameter to the order http URL call like this: &platform=toros in order for the trade URL to execute properly.


Check Order Details

https://dashboard.tenderly.co/explorer

Utilize the below resource to review any trade processes and troubleshooting before reaching out to the dHEDGE team. Many times, this tool can be used to examine trades and solve issues in order execution prior to reaching out for help.


API Infrastructure

https://dhedge-trading.web.app/

The default venue that the API utilized is 1inch. The dHEDGE team has found this exchange offers traders the best order execution prices and has the most stability. The team plans on adding the various other protocol exchanges to the hosted API service in the future.

The trading engine is actually a hosted wallet service created using Google Firebase Cloud Functions. The team pays for the infrastructure, costs, upkeep, scaling, and network utilizing Firebase. Thus, Traders can focus on optimizing strategies and not on maintaining a node.

The entire response messages at the time of Wallet and API creation is encrypted (hashed) and stored by dHEDGE. Thus, even if some breach were to occur on the backend, access to Trader Pool’s assets are still secured as hashes are stored only. The apiKey in the initial response is the only way to decrypt the information and send responses to and from the API service.

Hedge on.


More About dHEDGE

dHEDGE is a decentralized asset management protocol connecting the world’s best investment managers with investors on the blockchain in a permission less, non-custodial, trustless fashion. dHEDGE aims to democratize the investing experience leveraging Blockchain technology.

Supported by some of the biggest names in crypto, including Framework Ventures, Three Arrows Capital, BlockTower Capital, DACM, Maple Leaf Capital, Cluster Capital, Lemniscap, LD Capital, IOSG Ventures, NGC Ventures, Bitscale Capital, Divergence Ventures, Genblock Capital, Trusted Volumes, Altonomy, Continue Capital, The LAO, bitfwd (比特未来) and Loi Luu, Co-Founder and CEO of Kyber Network,

Join the dHEDGE community on Discord, Twitter, Telegram English language, Telegram Chinese language and on WeChat.