Karaka Tutorial #6: foreach saving you time and space
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.
- Karaka Tutorial #1: A Basic Tutorial config.json example
- Karaka Tutorial #2: Hive-Engine Tokens
- Karaka Tutorial #3: Constants
- Karaka Tutorial #4: Import facts between chains
- 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.
Nice! 💪😎🤙 very useful little tool.
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:
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):
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!
Sounds great! Might watch the other video and tutorials thanks for sharing this. Keep it up and more power.
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 🙏⚡🚀