Karaka Tutorial #2: Hive-Engine Tokens

in #engine3 months ago (edited)

image.png

Karaka automation also supports Hive Engine tokens with a powerful set of commands to suit many situations. Let's walk through some example rules that demonstrate staking, transferring, buying and selling tokens.

Karaka is an Automation (clerk) for Hive and Hive Engine blockchains.
Karaka is a Maori word meaning variously: an English loanword for 'clerk' or 'clock' and a significant coastal plant 'Corynocarpus laevigatus'.

Get Karaka from https://github.com/Stormrose/karaka

Before we begin

It's helpful if you've read the previous tutorial. This tutorial talks about the basic config.json file structure and introduces the concept of rules and facts.

Hive engine config is contained within a section at the top level of the JSON file. Don't put this inside the hive blockchain section. The basic config.json file

{
    "intervalmins": 43,
    "hiveengine": {
        "apinode": "https://api.hive.blog",
        "sidechainuri": "https://api.hive-engine.com/rpc",
        "accounts": {
            "eturnerx": "***YourPrivateActiveKeyHere***"
        },
        "rules": [
            ...
        ]
    }
}

Within the hiveengine blockchain section are apinode, sidechainuri, a map of accounts and their keys, and a list of rules. Hive engine configuration is the same as for Hive, but with a Hive Engine API endpoint. If you're not sure which sidechainuri to use, use the one in the example above.

If your config file includes a hive section, you can omit the apinode entry. Also, if your config has a hive section and you already have the account plus key in the accounts map of the hive section, then you can refer to that like this for inside your hiveengine section:

"accounts": {
    "eturnerx": { "from": "hive" }
}

Where does Karaka get facts Facts from?

Just like for Hive, Karaka assets facts about the tokens held. These also take the format of accountname.factname. Fact names include the token symbol and the suffixes _balance and _stake. Here are some examples:

  • eturnerx.SWAP.HIVE_balance
  • eturnerx.VIBES_balance
  • eturnerx.PAL_stake

What about the commands?

Karaka's then clause commands work pretty much the same for Hive and Hive Engine tokens. The transfer and stake commands use the same prefixes and parameters.

A key difference is that the sell command can only sell a token for SWAP.HIVE and the buy command must be used when exchanging SWAP.HIVE for another token. In the buy command, the keyword of is used instead of for.

sell 10 PAL for SWAP.HIVE from eturnerx at market
buy 10 SWAP.HIVE of LEO from eturnerx at market

The full range of mathematical expressions can be used in rules, too, just like in rules written for Hive.

A complete example

Here's a completed config.json file with a rule filled in.

{
    "intervalmins": 43,
    "hiveengine": {
        "apinode": "https://api.hive.blog",
        "sidechainuri": "https://api.hive-engine.com/rpc",
        "accounts": {
            "eturnerx": "***YourPrivateActiveKeyHere***"
        },
        "rules": [
            {
                "if": "eturnerx.SWAP.HIVE_balance > 1.0",
                "then": "buy 1.0 SWAP.HIVE of LEO from eturnerx at market"
            }
        ]
    }
}

Some recipes

Let's learn by stepping through some basic recipes that you can adjust to your own purposes.

Sell half, stake half

As you collect tribal tokens, you might want to sell some and stake some automatically. This example does 50% either way once there are at least two tokens.

"if": "eturnerx.PAL_balance > 2.0",
"then": [
    "sell (eturnerx.PAL_balance * 0.5) PAL for SWAP.HIVE from eturnerx at market",
    "stake (eturnerx.PAL_balance * 0.5) PAL from eturnerx"
]

Sell some, stake some, keep some

You might also want to maintain a minimum liquid reserve of a tribal token. In this case, let's hold 100 tokens, sell 20% over that and stake 80%. The rule doesn't trigger unless there are at least two tokens over the reserve of 100.

"if": "eturnerx.PAL_balance > 102",
"then": [
    "sell ((eturnerx.PAL_balance - 100) * 0.2) PAL for SWAP.HIVE from eturnerx at market",
    "stake ((eturnerx.PAL_balance - 100) * 0.8) PAL from eturnerx"
]

Stack tribal tokens

In these two rules, we use any SWAP.HIVE, we have to purchase our favourite tribal token and power it up. First, Karaka will buy the VIBES token, but the VIBES balance is not updated until the next round, and so this is a two-round process.

{
    "if": "eturnerx.SWAP.HIVE_balance > 1.0",
    "then": "buy (eturnerx.SWAP.HIVE_balance) SWAP.HIVE of VIBES from eturnerx at market"
}, {
    "if": "eturnerx.VIBES_balance > 1.0",
    "then": "stake (eturnerx.VIBES_balance) VIBES from eturnerx"
}

Withdraw SWAP.HIVE to HIVE using beeswap

Once you've earned some SWAP.HIVE, you might want to withdraw some as HIVE. Karaka can do this with a transfer to @hiveswap. A memo with some text is compulsory; otherwise, your coins might go missing. Also, I would only do this with small amounts.

"if": "eturnerx.SWAP.HIVE_balance > 102.0",
"transfer (eturnerx.SWAP.HIVE_balance - 100) SWAP.HIVE from eturnerx to hiveswap memo \"Withdrawal for HE\""

Some further notes: Memos must be enclosed in backslash-escaped-straight-double-quotes \"Like this\". Also, check that the @hiveswap service is still running - it is as of May 2021, but things may change in future.

Give it a try

I hope these recipes give you plenty of ideas about how Karaka can automate how you manage your Hive Engine tokens. I've personally found my admin workload decrease, and Karaka rules remove the spur-of-the-moment emotional decision making. Decide once, execute consistently can be a powerful way to meet your stacking goals, and Karaka excels at this.

If you have any questions about Karaka, then please get in touch.

Sort:  

!LUV
!PIZZA
!BEER
for you

Connect

Trade


@eturnerx! I sent you a slice of $PIZZA on behalf of @eii.

Learn more about $PIZZA Token at hive.pizza

Thank you!

Congratulations @eturnerx! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

You published more than 150 posts.
Your next target is to reach 200 posts.
You received more than 13000 HP as payout for your posts and comments.
Your next payout target is 14000 HP.
The unit is Hive Power equivalent because your rewards can be split into HP and HBD

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Support the HiveBuzz project. Vote for our proposal!

Hi @eturnerx, you were just shared some LUV thanks to @eii. Holding 10 LUV in your wallet enables you to give up to 3 LUV per day, for free. See the LUV in your wallet at https://hive-engine.com or learn about LUV at https://peakd.com/@luvshares