Karaka Tutorial #6: foreach saving you time and space

in #engine2 years ago

image.png

Karaka is an Automation (clerk) for Hive and Hive Engine blockchains. This tutorial teaches how to use constants to automate Hive and Hive Engine. Get Karaka from https://github.com/Stormrose/karaka

Karaka is a Maori word meaning variously: an English loanword for 'clerk' or 'clock' and a significant coastal plant 'Corynocarpus laevigatus'.

This tutorial builds on the first three tutorials, so try those out first.

  1. Karaka Tutorial #1: A Basic Tutorial config.json example
  2. Karaka Tutorial #2: Hive-Engine Tokens
  3. Karaka Tutorial #3: Constants
  4. Karaka Tutorial #4: Import facts between chains
  5. Karaka Tutorial #5: Oracles

Why foreach?

By now, config.json probably has several rules that are very similar. Whether staking tribal tokens on hive-engine or transferring HIVE balances from multiple accounts to a single gathering place, having similar rules is a readability and maintenance nightmare. Foreach allows wildcard matching within Karaka rules, and that keeps rules maintainable and consistent.

How to use foreach

Foreach is a property within any Karaka rule that is a [list] of "strings". Each string in the list is substituted wherever {0} appears in the rest of the Karaka rule. Perhaps an example is in order:

{
    "foreach": [ "PAL", "VIBES", "WAIV" ],
    "name": "@eturnerx sell and stake {0}",
    "comment": "@eturnerx sell and stake {0}",
    "if": "eturnerx.{0}_balance > 0",
    "then": "stake eturnerx.{0}_balance {0} from eturnerx"
}

This rule will stake PAL, VIBES or WAIV whenever there is a non-zero balance in the @eturnerx account. Under the hood, Karaka generates a rule per foreach list item with the wildcard {0} resolved. The wildcard is a direct text replacement that occurs when the config is loaded when Karaka start.
While this example uses hive-engine tokens, you could also use foreach to list accounts, such as:

{
    "foreach": [ "accountone", "accounttwo", "accountthree" ],
    "if": "{0}.hive_balance > 0",
    "then": "transfer ({0}.hive_balance) HIVE from {0} to eturnerx"
}

In this example, the rule instructs Karaka to transfer any non-zero hive balance in either accountone, accounttwo or accountthree to @eturnerx.

Summary

Using foreach is a great way to keep rules compact, readable and consistent. Once a rule is set up with foreach, adding more items to the list, such as a new tribal token, is simple.

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

Sort:  

Nice! 💪😎🤙 very useful little tool.

Sounds great! Might watch the other video and tutorials thanks for sharing this. Keep it up and more power.

Hive-Engine Pools next? =)

I haven't the need, so didn't bother. What types of things would you want to do? So I know what facts to extract for reasoning and what actions to support.

The basic ones are:

  • Swap tokens on pools (a more advanced thing could be to check what's worth if swapping or sell/buy on the market and decide based on price)
  • Add and remove liquidity to pairs to the pools (can be based on APR or static rules like you are doing for the rest already)

Most if not all will be in contracts/marketpools.js

Quite more advanced (this will go in the direction of "trading" automation most likely, so not sure if that's your idea):

  • Allow conditions on prices to be made and then either buy/sell or swap, whichever is the "rule" for selling/buying that generates a better deal.
  • Allow (experimental) "predictions" on swapping via multiple jumps between pools (ie. instead of SWAP.HIVE -> WOO, do SWAP.HIVE -> PIZZA -> WOO). There are a lot of pools already, so this becomes interesting sometimes.
  • ALERT "remove liquidity" mode... if certain price changes on the pool (in % of the last check or last few checks), remove % liquidity to avoid losing too much. This could be useful for people that don't want to lose the token when the price crashes on either side of the pair, leading to an undesirable outcome.

You are already doing something within the lines with the Oracles feature... =)

I would be a frequent tester with this one for sure. Pools are quite "better" positioned against the markets in many cases. Meaning that most of the time you get better prices via pools.

Thanks. I'll add all that to my list. I was trying to avoid making a trading bot. I'll see what info I can pull from the pools then, most of what you want can be done with a few pool / liquidity "facts" and some commands to handle liquidity. Might take me awhile though.

Yeah, I figured. No rush on this... I think providing the basic functions (swap, add, remove liquidity) is probably already a huge help for many.

Until we are on the other side of the HF26 without any sorts of things to sort out, any new stuff is just secondary for me. First stability and HA, etc..

Right now I am busy with the HE stuff and Hive, but later would like to see your code better. I feel from the diagonal read that I have something to learn about on those facts collections. Looks nice... Also feels this is going on somewhere nice as a tool. =)

Dear, @eturnerx

May we ask you to review and support our @cryptobrewmaster GameFi proposal on DHF? It can be found here

If you havent tried playing CryptoBrewMaster you can give it a shot. Our @hivefest presentation available here on the YouTube with a pitchdeck of what we building in general

Vote with Peakd.com, Ecency.com, Hivesigner

Thank you!

All thèse is very complicated for me

i'm glad to see you are still here and making improvement for all the communty folks. you are a genius! i still remember you everyday and rember all the thing you did for me and the people in my country, i'm grateful with you all my life @eturnerx

Hi! Glad to see you're still around. I hope things had improved for you.

in some way it all improved because of you and @sircork , im grateful!

i was making a living from spliterlands for a while but now i want to engage again in hive, it is a good place indeed.

many many years ahead now i have to say you thanks you really much for al the sbi you invested in my account @eturnerx those sbi are healping me really much in my posting

Malos how are youu? I hope that You are good My friend. Wow that old times with sircork and youarehope was very good. Blessings

those were good times indeed my friend.

i'm fine thanks god, we have to keep in touch again, will reach you in discord

Vale perfecto ;) si vale fue una época genial
Un gran abrazo. Y me alegra que estes bien

Hii I have a question but about another thing 🙏😌

Find me on discord. eturnerx

Oki doki 🤘⚡🙏🎸

It's done 🙏⚡🚀