Part 3: How to manage your wallet using steem-python - Automatically manage your accounts

in #utopian-io8 years ago (edited)

steem-python.png

This tutorial is part of a series, so make sure to read the other tutorials before starting this tutorial. Today we will learn how to automatically send the rewards from your other accounts (if you have them) to your main account using steem-python!


What will I learn?

  • How to add multiple accounts to steempy
  • How to claim rewards on all your accounts
  • How to transfer these rewards to a main account

Requirements

  • Python3.6
  • steem-python

Difficulty

  • Basic

Tutorial

Adding accounts

First thing you want to do is add your account's active key and posting key (not relevant for this tutorial, but still). You can do this by typing

steempy addkey

Once you have typed this you will see the following

Private Key (wif) [Enter to quit]:

simply paste one of your keys and press enter until you have entered all of your keys. If you now type steempy listaccounts it should show all the accounts you have entered, for example for me it shows

listaccounts.png

Claiming rewards

We already learned how to do this in the previous tutorial, so we will simply adapt the code so it works with multiple accounts. First of all we need to make a list with all our accounts and change the claim_rewards() function to take a username as argument. Once we have the list we can simply loop over it and call the claim_rewards() function in every iteration, like so

def claim_rewards(username):
    reward_SBD = steem.get_account(username)["reward_sbd_balance"]
    reward_SP  = steem.get_account(username)["reward_vesting_steem"]
    print(f"{username} current rewards: {reward_SBD} and {reward_SP} POWER")

    if Amount(reward_SBD).amount + Amount(reward_SP).amount == 0:
        return
    else:
        steem.claim_reward_balance(account=username)
    print(f"Claim rewards: {reward_SBD} and {reward_SP} POWER")

steem = Steem()
accounts = ["sttest1", "sttest2"]

for account in accounts:
    claim_rewards(account)

which gives the following output

sttest1 current rewards: 0.000 SBD and 0.000 STEEM POWER
sttest2 current rewards: 0.000 SBD and 0.000 STEEM POWER

Unfortunately I don't have any rewards on these test accounts, but trust me, it actually works!

Transferring SBD

We can now claim all the rewards on all of our accounts, so the only thing left is to actually send the rewarded SBD to our main account. So instead of selling it on the market like we did in the previous tutorial, we are going to make a function called transfer_sbd() that does this for us. To do this we can use the transfer() function like so

def transfer_sbd(username):
    sbd_balance = Amount(steem.get_account(username)["sbd_balance"]).amount
    if not sbd_balance > 0:
        return
    print(f"Transferring {sbd_balance} to {main_account} from {username}")
    steem.transfer(main_account, sbd_balance, "SBD", account=username)
    
main_account = "amosbastian"

for account in accounts:
    claim_rewards(account)
    transfer_sbd(account)

where we make sure that we actually have some SBD to transfer. If you don't do this you might possibly get an error saying that you are trying to transfer a negative amount, and we don't want this! Anyway, the above code will output something similar to this

sttest1 current rewards: 0.000 SBD and 0.000 STEEM POWER
Transferring 0.011 to amosbastian from sttest1
sttest2 current rewards: 0.000 SBD and 0.000 STEEM POWER
Transferring 0.051 to amosbastian from sttest2

Going to my wallet shows it worked fine!

transfer_example.png

If you want to schedule your program to run every day for example, you can use a crontab as we discussed in the first part of this tutorial series. Don't forget that if you want to do this you would have to add each account's passphrase to the crontab as an environment variable and then load this in your Python file!


Congratulations! You can now automatically claim all the rewards on all of your accounts, then send these rewards to your main account where you can decide what to do with it!

Curriculum


The code for this tutorial can be found on my GitHub!



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @amosbastian I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

This is a great help to thr steemit peoplr , thank you so much dear friend😊😊

No problem!

Congratulations @amosbastian! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of posts published
Award for the number of upvotes
Award for the number of comments
Award for the total payout received
Award for the number of upvotes received
You published 4 posts in one day
You published a post every day of the week

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!