dHEDGE Bot SDK

Radek Ostrowski
dHEDGE_ORG
Published in
4 min readNov 2, 2020

dHEDGE is a decentralized asset management protocol which lets the portfolio managers trade investors’ assets in a non-custodial way. The managers can be humans or bots or the combination of both. dHEDGE is designed to serve all cases equally well.

In this tutorial, we will show you how to implement a custom strategy to be automatically executed by a bot. To keep things simple, we will just demo a rebalancing strategy, which keeps the pool assets in predefined proportions: 50% ETH and 50% USD and rebalance only when the difference is more than 5%.

dHEDGE resident blockchain hero Vidor has built and deployed the Node.js SDK (https://github.com/dhedge/dhedge-sdk) as an npmpackage for your convenience, so it’s straight forward to import it:

“dependencies”: { “dhedge-sdk”: “0.0.1” }

To get started, lets first clone this repository:

https://github.com/dhedge/dhedge-sdk-examples

Create an account with a seed phrase (you can use MyCrypto app for instance). Later, copy .env.example into .env and prefill the variables:

MNEMONIC=”magna volatile mice juggle ubiquitous nudist …”
ACCOUNT_ID=”0"
PROVIDER=”https://ropsten.infura.io/v3/your-code-here"
FACTORY_ADDRESS=”0xdd1Ee9e21bbd0012d1C710Ed94057A3CBE3E02D7"

Above FACTORY_ADDRESS is for Ropsten, the factory on Mainnet is at “0x03d20ef9bdc19736f5e8baf92d02c8661a5941f7”.

You can request the test ether from the following faucets:

Once you have some ether, you can get sUSD from the Ropsten Synthetix Depot, just go to: https://ropsten.etherscan.io/address/0x6E6624355D4c1DE475EB48677fce6025d69aAf22#writeContract. Connect your wallet and exchange some test ether for sUSD with exchangeEtherForSynths function.

Easy way to get some test sUSD on Ropsten is to use Synthetix Depot

Now you are finally ready to try out the bot. Lets look at the example code in rebalance.js. Here’s the import statement:

const { Factory } = require(‘dhedge-sdk’)

You can then create the pool with the SDK or by hand from the UI.

pool = await factory.createPool(false, ‘Manager Name’, ‘Pool Name’, [‘sETH’])

Edit the manager and pool names as well as a list of initial supported assets in this pool, if you don’t you will end up with the default names like we did in this tutorial. If you would like to create a private pool, change the first parameter to true.

If you want to create the pool from within the example code just leave the following set to true

const CREATE_POOL = true

And run the bot like this:

npm i
node rebalance.js

It will create the pool and print simple pool summary:

Default debug output for pool summary

You will be able to see the pool in the UI and by clicking it, you will it’s address.

The new pool popped up in the UI

Now that we have the pool, let’s update the file like this:


const CREATE_POOL = false
const POOL_ADDRESS = ‘0x77b75f942fee478f58c67b94483b5a8a66c166bf’

The pool instance will get loaded when calling the factory method:

pool = await factory.loadPool(POOL_ADDRESS)

After running the bot for the first time with the pool, you will see something like this:

Rebalance required and performed

The example code initially approved and deposited 100 sUSD into the pool:

let sUSD = await pool.getAsset(‘sUSD’)
await sUSD.approve(pool.getAddress(), '100000000000000000000')
await pool.deposit(‘100000000000000000000’)

It then checked the current composition of the pool represented in USD value:

let composition = await pool.getComposition()let sUsdEffectiveValue = composition[‘sUSD’].balance
let sEthEffectiveValue = await exchangeRates.getEffectiveValue(
‘sETH’,
composition[‘sETH’].balance.toString(),
‘sUSD’
)

As the USD value between the assets is bigger than 5%, it decided that the rebalance is needed:

let totalValue = await pool.getPoolValue()
let balanceThreshold = totalValue
.mul(BigNumber.from(5))
.div(BigNumber.from(100)) // 5%
if (sUsdEffectiveValue.sub(sEthEffectiveValue).abs()
.gte(balanceThreshold))
{
console.log(‘Rebalance needed’)
...
}

and it exchanged half sUSD of into sETH:

let target = sUsdEffectiveValue.sub(sEthEffectiveValue).abs().div(2)
...
await pool.exchange('sUSD', target, 'sETH')

When run for the second time (with another 100 sUSD deposited) it rebalanced again:

Rebalance required and performed

The UI is showing both of those rebalance trades:

Trade History shows two rebalancing transactions

Now, we will comment out the deposit lines:

// await sUSD.approve(pool.getAddress(), ‘100000000000000000000’) 
// await pool.deposit(‘100000000000000000000’)

And this time, running the rebalance script again leaves the pool unchanged as expected:

No rebalance required
Pool composition is balanced

What’s left to do is just to schedule the bot to run on the regular invervals and it will rebalance the pool composition if required.

We hope that this tutorial will help you get started with writing new or adapting your old trading bots for dHEDGE.

As this is a first version of the bot SDK, we really appreciate your feedback, so please ping us on Discord.

Power over to you.

--

--