create account

delegationOnboardBot - a new bot for managing delegation to referred accounts created with hiveonboarding by holger80

View this thread on: hive.blogpeakd.comecency.com
· @holger80 ·
$87.88
delegationOnboardBot - a new bot for managing delegation to referred accounts created with hiveonboarding
The post [Bounty to Develop Delegation Manager for Hiveonboard](https://peakd.com/hive-101690/@sportstalksocial/bounty-to-develop-delegation-manager-for-hiveonboard) has inspired me to develop a python based delegation manager bot for hiveonboard. The source code can be found in  https://github.com/holgern/delegationOnboardBot

![Running bot](https://images.hive.blog/DQmVXtw5zhwnfq9wrCLie1wSi1EoxyV6KMrzcpXQaKWXWJs/Running%20bot)

## How does the bot work?
The bot receives a list of all referred accounts by using the API from hiveonboard. Then the current RC level of these accounts is monitored, whenever it drops below a threshold a delegation is given.

The given delegation is removed by the bot when one of the following conditions hold:
* The account has gained sufficient Hive Power
* The delegation period exceeds a specific time duration (can be disabled)
* The account posts without setting beneficiaries to the referrer (can be disabled)
* Bad behavior was detected (managed by a mute from muteAccount)

The parameter that control the bot are set in the config.json.
A description of the parameter in the config.json can be found in the [readme](https://github.com/holgern/delegationOnboardBot/blob/master/README.md).

### Installing and running the bot
Detailed instruction can be found in the [readme](https://github.com/holgern/delegationOnboardBot/blob/master/README.md).

### Receiving all created accounts from the hiveonboard api
The following code part fetches all created account that have a referrer equal to `peakd`
```
import requests
limit = 20
offset = 0
referrerAccount = "peakd"
onboard_api = "https://hiveonboard.com/api/referrer/" + referrerAccount
last_result = []
cnt = 0
result = []
while last_result is not None and len(last_result) == limit or cnt == 0:
    cnt += 1        
    r = requests.get(onboard_api + '?offset=%d' % (offset))
    if r.ok:
        last_result = r.json()["items"]
        if last_result is not None and len(last_result) > 0:
            result += last_result
            offset += limit
```

## Streaming blocks
### Checking for new created accounts
The bot streams now all hive blocks and whenever a new account was created, it checks if this account was created through hiveonboard. When this is the case and the referrer account is the correct one, it is added to the account list.
```
elif op["type"] == "create_claimed_account":
    if op["json_metadata"] == "":
        continue
    meta_data = json.loads(op["json_metadata"])
    if "beneficiaries" not in meta_data:
        continue
    for entry in meta_data["beneficiaries"]:
        if entry["label"] == "referrer" and entry["name"] == self.config["referrerAccount"]:
            self.accounts[op["new_account_name"]] = {"timestamp": None, "weight": None, "muted": False, "rc": 0, "hp": 0,
                                                     "delegated_hp": 0, "delegation_timestamp": None, "rc_comments": 0,
                                                     "delegation_revoked": False}
            self.accounts[op["new_account_name"]]["weight"] = entry["weight"]
            self.accounts[op["new_account_name"]]["timestamp"] = op["timestamp"].replace(tzinfo=None)
            store_data(self.data_file, "accounts", self.accounts)

```
### Checking Comment, Vote, Transfer and Custom_json
Whenever a referred account is broadcasting either a comment, a vote, a transfer or a custom_json operation, it is checked if the account has sufficient RC. When the account is not able to broadcast at least `minPostRC` posts, he will receive a small delegation, when the following conditions apply:
* no delegation yet
* no revoked delegation
* less owned HP than `maxUserHP`
  

```
def check_account_on_activity(self, account, timestamp):
    if account not in self.accounts:
        return
    acc = Account(account, blockchain_instance=self.hive)
    self.accounts[account]["rc"] = acc.get_rc_manabar()["current_mana"]
    self.accounts[account]["hp"] = acc.get_token_power(only_own_vests=True)
    self.accounts[account]["rc_comments"] = self.accounts[account]["rc"] / self.comment_rc_costs
    store_data(self.data_file, "accounts", self.accounts)
    if self.accounts[account]["delegated_hp"] > 0:
        return
    if self.accounts[account]["delegation_revoked"]:
        return
    if self.accounts[account]["hp"] > self.config["maxUserHP"]:
        return
    if self.accounts[account]["rc_comments"] < self.config["minPostRC"]:
        ok = self.add_delegation(account, timestamp)
        if ok:
            self.notify_account(account, self.config["delegationMsg"])
```
where `comment_rc_costs` is defined as follows:
```
rc = RC(blockchain_instance=self.hive)
self.comment_rc_costs = rc.comment(tx_size=4000, permlink_length=40, parent_permlink_length=0)
```
### Checking if beneficiaries are correctly set
Whenever a referred account is broadcasting a new post, it is checked if the beneficiaries are correctly set. When they are not set and the account had received an delegation, the delegation is revoked.

```
def check_beneficiaries(self, author, permlink):
    if author not in self.accounts:
        return
    if self.accounts[author]["delegated_hp"] == 0:
        return
    if self.accounts[author]["delegation_revoked"]:
        return
    if not self.config["beneficiaryRemoval"]:
        return
    comment = None
    cnt = 0
    while comment is None and cnt < 10:
        cnt += 1
        try:
            comment = Comment(construct_authorperm(author, permlink), blockchain_instance=self.hive)
        except:
            comment = None
            time.sleep(3)
    referrer_ok = False
    for bene in comment["beneficiaries"]:
        if bene["account"] == self.config["referrerAccount"] and bene["weight"] == self.accounts[author]["weight"]:
            referrer_ok = True
    if not referrer_ok:
        self.remove_delegation(author)
        self.notify_account(author, self.config["delegationBeneficiaryMsg"])
```

### Checking if an account is muted by "muteAccount"
Whenever an account is muted by `muteAccount` and had receveid an delegation, the delegation is revoked.

## More checks
It is also checked if an account reaches `maxUserHP` HP, when this is the case, the delegation is removed.

It is also possible to limit the delegation to a certain duration by setting `delegationLength`. When this is set, delegations are monitored and when the delegation age is higher than the specified threshold, the delegation is removed.

The remaining Hive Power of the `delegationAccount` is also monitored and when it drops below a threshold a transfer memo is broadcasted.

### Storing and loading the current state
The bot writes all important state variables on every change into a data container and loads them on startup. 

This makes the bot in combination with systemd very robust. Whenever something goes wrong, the bot is restarted by systemd and the state variables are restored.


## Summary
The bot helps services which are using hiveonboard for refering new users and which are managing HP delegation manually by now.

The bot is managing delegation/undelegation of HP to new created accounts through [hiveonboard](https://hiveonboard.com/). 

Every service who is using hiveonboard for referring new user, can use this bot for automatic delegation management. 


___

*If you like what I do, consider casting a vote for me as witness on [Hivesigner](https://hivesigner.com/sign/account-witness-vote?witness=holger80&approve=1) or on [PeakD](https://peakd.com/witnesses)*
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 493 others
πŸ‘Ž  
properties (23)
authorholger80
permlinkdelegationonboardbot---a-new-bot-for-managing-delegation-to-referred-accounts-created-with-hiveonboarding
categoryhive-139531
json_metadata"{"canonical_url": "https://hive.blog/hive-139531/@holger80/delegationonboardbot---a-new-bot-for-managing-delegation-to-referred-accounts-created-with-hiveonboarding", "community": "hive-139531", "app": "beempy/0.24.6", "image": ["https://images.hive.blog/DQmVXtw5zhwnfq9wrCLie1wSi1EoxyV6KMrzcpXQaKWXWJs/Running%20bot"], "links": ["https://peakd.com/hive-101690/@sportstalksocial/bounty-to-develop-delegation-manager-for-hiveonboard", "https://github.com/holgern/delegationOnboardBot", "https://github.com/holgern/delegationOnboardBot/blob/master/README.md", "https://hiveonboard.com/api/referrer/", "https://hiveonboard.com/", "https://hivesigner.com/sign/account-witness-vote?witness=holger80&approve=1", "https://peakd.com/witnesses"], "tags": ["development", "beem", "python", "hiveonboard"]}"
created2020-08-19 22:07:00
last_update2020-08-19 22:07:00
depth0
children12
last_payout2020-08-26 22:07:00
cashout_time1969-12-31 23:59:59
total_payout_value49.396 HBD
curator_payout_value38.482 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length7,541
author_reputation358,857,509,568,825
root_title"delegationOnboardBot - a new bot for managing delegation to referred accounts created with hiveonboarding"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,163,062
net_rshares216,641,189,337,756
author_curate_reward""
vote details (558)
@chitty ·
I have picked your post for my daily hive voting initiative, Keep it up and Hive On!!
properties (22)
authorchitty
permlinkre-delegationonboardbot---a-new-bot-for-managing-delegation-to-referred-accounts-created-with-hiveonboarding-20200824t000652
categoryhive-139531
json_metadata""
created2020-08-24 00:06:57
last_update2020-08-24 00:06:57
depth1
children0
last_payout2020-08-31 00:06:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length86
author_reputation86,901,300,608,582
root_title"delegationOnboardBot - a new bot for managing delegation to referred accounts created with hiveonboarding"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,237,505
net_rshares0
@co2admin ·
Upvoted to thank you @holger80 for supporting the [CO2Fund](https://peakd.com/@co2fund) by, e.g., supporting posts, banner presentation, SP/HP delegation, dustsweeper gifts, helpful tools, etc.
properties (22)
authorco2admin
permlinkre-delegationonboardbot---a-new-bot-for-managing-delegation-to-referred-accounts-created-with-hiveonboarding-20200819t221150z
categoryhive-139531
json_metadata"{"app": "rewarding/0.1.0"}"
created2020-08-19 22:11:51
last_update2020-08-19 22:11:51
depth1
children0
last_payout2020-08-26 22:11:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length193
author_reputation3,792,666,189
root_title"delegationOnboardBot - a new bot for managing delegation to referred accounts created with hiveonboarding"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,163,119
net_rshares0
@dalz ·
Great one!
I was hoping someone will do something like this.
properties (22)
authordalz
permlinkqfcnfz
categoryhive-139531
json_metadata{"app":"hiveblog/0.1"}
created2020-08-20 06:31:12
last_update2020-08-20 06:31:12
depth1
children0
last_payout2020-08-27 06:31:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length60
author_reputation1,940,273,536,359,641
root_title"delegationOnboardBot - a new bot for managing delegation to referred accounts created with hiveonboarding"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,168,052
net_rshares0
@demo123 ·
Good work!
properties (22)
authordemo123
permlinkre-holger80-2020820t194015832z
categoryhive-139531
json_metadata{"tags":["development","beem","python","hiveonboard"],"app":"ecency/3.0.3-mobile","format":"markdown+html"}
created2020-08-20 16:40:15
last_update2020-08-20 16:40:15
depth1
children0
last_payout2020-08-27 16:40:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length10
author_reputation510,583,572,156
root_title"delegationOnboardBot - a new bot for managing delegation to referred accounts created with hiveonboarding"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,176,393
net_rshares0
@denisdenis ·
it looks interesting ... it will only take me a while to study it for the sake of my English and language but a little ochu ps.  interesting
properties (22)
authordenisdenis
permlinkqh76fi
categoryhive-139531
json_metadata{"app":"hiveblog/0.1"}
created2020-09-25 04:42:54
last_update2020-09-25 04:42:54
depth1
children0
last_payout2020-10-02 04:42:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length140
author_reputation75,539,149,214,246
root_title"delegationOnboardBot - a new bot for managing delegation to referred accounts created with hiveonboarding"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,805,422
net_rshares0
@gitplait ·
Upvoted by GITPLAIT!

We have a curation trial on Hive.vote. you can earn a passive income by delegating to [@gitplait](https://hive.vote/dash.php?i=15&id=1&user=gitplait)
We share 80 % of the curation rewards with the delegators.
___

_To delegate, use the links or adjust_ [10HIVE](https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=gitplait&vesting_shares=10%20HP), [20HIVE](https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=gitplait&vesting_shares=20%20HP), [50HIVE](https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=gitplait&vesting_shares=50%20HP), [100HIVE](https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=gitplait&vesting_shares=100%20HP),   [200HIVE](https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=gitplait&vesting_shares=200%20HP), [500HIVE](https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=gitplait&vesting_shares=500%20HP), [1,000HIVE](https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=gitplait&vesting_shares=1000%20HP), [10,000HIVE](https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=gitplait&vesting_shares=10000%20HP), [100,000HIVE](https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=gitplait&vesting_shares=100000%20HP)

___

Join the [Community](https://hive.blog/trending/hive-103590) and chat with us on [Discord](https://discord.gg/CWCj3rw)  let’s solve problems & build together.
properties (22)
authorgitplait
permlinkqfdkwf
categoryhive-139531
json_metadata{"links":["https://hive.vote/dash.php?i=15&id=1&user=gitplait","https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=gitplait&vesting_shares=10%20HP","https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=gitplait&vesting_shares=20%20HP","https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=gitplait&vesting_shares=50%20HP","https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=gitplait&vesting_shares=100%20HP","https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=gitplait&vesting_shares=200%20HP","https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=gitplait&vesting_shares=500%20HP","https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=gitplait&vesting_shares=1000%20HP","https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=gitplait&vesting_shares=10000%20HP","https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=gitplait&vesting_shares=100000%20HP","https://hive.blog/trending/hive-103590","https://discord.gg/CWCj3rw"],"app":"hiveblog/0.1"}
created2020-08-20 18:33:51
last_update2020-08-20 18:33:51
depth1
children0
last_payout2020-08-27 18:33:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,493
author_reputation911,220,543,569
root_title"delegationOnboardBot - a new bot for managing delegation to referred accounts created with hiveonboarding"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,178,271
net_rshares0
@jarvie ·
This is pretty awesome!!
properties (22)
authorjarvie
permlinkre-holger80-qfc1hn
categoryhive-139531
json_metadata{"tags":["hive-139531"],"app":"peakd/2020.08.3"}
created2020-08-19 22:37:00
last_update2020-08-19 22:37:00
depth1
children0
last_payout2020-08-26 22:37:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length24
author_reputation388,491,264,112,133
root_title"delegationOnboardBot - a new bot for managing delegation to referred accounts created with hiveonboarding"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,163,378
net_rshares0
@roomservice ·
$0.05
Incredible work @holger80! Most impressing how fast you managed to create this.

Just a sidenote:
This app isn't limited to referred accounts created by hiveonboard, since every dApp could use the Open Standard Referral System right now. One example would be https://quello.io which adapted this system from early on and are creating accounts following this system as well.
πŸ‘  , ,
properties (23)
authorroomservice
permlinkre-holger80-qfcl2p
categoryhive-139531
json_metadata{"tags":["hive-139531"],"app":"peakd/2020.08.3"}
created2020-08-20 05:40:09
last_update2020-08-20 05:40:09
depth1
children1
last_payout2020-08-27 05:40:09
cashout_time1969-12-31 23:59:59
total_payout_value0.024 HBD
curator_payout_value0.025 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length373
author_reputation11,573,428,661,334
root_title"delegationOnboardBot - a new bot for managing delegation to referred accounts created with hiveonboarding"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,167,516
net_rshares217,907,139,729
author_curate_reward""
vote details (3)
@shawnsporter ·
Just now looking into if Hive onboarding through linkedin would be viable, have to look up with the open standard referral system is...
properties (22)
authorshawnsporter
permlinkre-roomservice-qwo5ub
categoryhive-139531
json_metadata{"tags":["hive-139531"],"app":"peakd/2021.07.2"}
created2021-07-22 23:13:24
last_update2021-07-22 23:13:24
depth2
children0
last_payout2021-07-29 23:13:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length135
author_reputation9,073,837,738,988
root_title"delegationOnboardBot - a new bot for managing delegation to referred accounts created with hiveonboarding"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id105,072,877
net_rshares0
@tazi ·
Great work @holger80 !! 
Thank you very much for your work!
Hive on ;)
properties (22)
authortazi
permlinkre-holger80-qfcnm3
categoryhive-139531
json_metadata{"tags":["hive-139531"],"app":"peakd/2020.08.3"}
created2020-08-20 06:34:51
last_update2020-08-20 06:34:51
depth1
children0
last_payout2020-08-27 06:34:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length70
author_reputation80,165,070,749,190
root_title"delegationOnboardBot - a new bot for managing delegation to referred accounts created with hiveonboarding"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,168,098
net_rshares0
@utopiaeducators · (edited)
Thank you! I'm a huge user of HiveOnboard, this is awesome!

```bash
[john@john-desktop delegationOnboardBot]$ delegationonboardbot config.json --datadir=. --logconfig=logger.json
2020-08-20 11:00 INFO - main: Loading config: config.json
2020-08-20 11:00 INFO - main: <Hive node=https://hive.roelandp.nl, nobroadcast=False>
2020-08-20 11:00 INFO - check_config: config was found.
2020-08-20 11:00 INFO - print_account_info: 20 accounts have been created with referrer utopiaeducators
2020-08-20 11:00 INFO - print_account_info: 1 accounts have received a delegation (5.000 HP)
2020-08-20 11:00 INFO - print_account_info: 0 accounts have been revoked
2020-08-20 11:00 INFO - main: starting delegation manager for onboarding..
```
πŸ‘  
πŸ‘Ž  
properties (23)
authorutopiaeducators
permlinkre-holger80-qfdaqj
categoryhive-139531
json_metadata{"tags":["hive-139531"],"app":"peakd/2020.08.3"}
created2020-08-20 14:54:21
last_update2020-08-20 15:02:42
depth1
children0
last_payout2020-08-27 14:54:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length728
author_reputation-4,088,797,786,952
root_title"delegationOnboardBot - a new bot for managing delegation to referred accounts created with hiveonboarding"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,174,564
net_rshares0
author_curate_reward""
vote details (2)
@ziomar ·
cool thx for your hard work
πŸ‘Ž  
properties (23)
authorziomar
permlinkqfdlnk
categoryhive-139531
json_metadata{"app":"hiveblog/0.1"}
created2020-08-20 18:50:09
last_update2020-08-20 18:50:09
depth1
children0
last_payout2020-08-27 18:50:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length27
author_reputation-135,928,847,292
root_title"delegationOnboardBot - a new bot for managing delegation to referred accounts created with hiveonboarding"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,178,483
net_rshares0
author_curate_reward""
vote details (1)