Just for fun: Writing python code with ChatGPT to get a Steem account's current voting power

in #programming2 years ago (edited)

This morning I wanted to see if ChatGPT could tell us how to get an account's voting power on the Steem blockchain. It's an interesting conversation, so I thought I'd publish it.

Basically, what you need to do is get the account's current voting power - as reflected by the blockchain, and then adjust it at a rate of 20% per day by the amount of time that has passed since the account's last vote. Here is the conversation:

Q1. Write python code to get an account's voting power in the steem blockchain

A1

image.png

Commentary

This is a good start, but it doesn't make adjustments for regeneration after the most recent vote. On to question 2.

Q2. Write python code to get the last time and date that a steem account voted

A2

image.png

Commentary

Obviously, this doesn't solve the problem, but I just wanted to see if ChatGPT could answer the easy question before complicating it. (note that the answer is backwards here, it looks at an older vote, not the most recent... We'll come back to this point later.)

Q3. Write python code to calculate voting power regeneration at a rate of 20% per day from the last time a Steem account voted until now.

A3

image.png

Commentary

Making it slightly more complicated here, but we haven't put the pieces together yet. (Another "to-do" here is that it's calculating regeneration in terms of full days, not fractions of days.)

Q4. Write python code to get a Steem account's voting power and adjust it by 20% per day from the time of the last vote until now.

A4 (screen is getting tight, so I'm removing the additional commentary from here on - code only)

image.png

Commentary

Now we're getting there. This looked plausible - at first, but it needed refinement because it's got the dates sorted backwards and it's not looking at fractions of days.

Q5. get_account_history returns in chronological order, not reverse chronological order. Rewrite that code to use the last vote instead of the first.

A4. (code only, again)

image.png

Commentary

That was easy. I didn't really expect ChatGPT to understand the prompt, but I was wrong.

Q6. Rewrite that code to use seconds since last vote instead of days since last vote.

A6 (code only)

image.png

Commentary

And now we've got it regenerating in seconds, instead of days. This code is not fully tested, but it looks plausible. It seems like we got to the right answer in six questions (assuming that there's a vote in the last 101 transactions). Here's the same code in textual format.

import steem
import datetime

# Connect to the Steem blockchain
steem_client = steem.Steem()

# Get the account's voting power
account_name = '<YOUR ACCOUNT NAME HERE>'
account = steem_client.get_accounts([account_name])[0]
voting_power = account['voting_power']

# Get the account's history
history = steem_client.get_account_history(account_name, -1, 100)

# Find the last vote in the history
last_vote_time = None
for entry in history:
    if entry[1]['op'][0] == 'vote':
        last_vote_time = entry[1]['timestamp']

if last_vote_time is None:
    print(f'{account_name} has never voted')
else:
    # Convert the timestamp to a datetime object
    last_vote_datetime = datetime.datetime.strptime(last_vote_time, '%Y-%m-%dT%H:%M:%S')

    # Calculate the number of seconds that have passed since the last vote
    current_time = datetime.datetime.utcnow()
    seconds_since_last_vote = (current_time - last_vote_datetime).total_seconds()

    # Adjust the voting power by the number of seconds that have passed since the last vote
    # (assuming a regeneration rate of 20% per day)
    adjusted_voting_power = voting_power + (seconds_since_last_vote / 86400) * 20

    print(f'{account_name} has an adjusted voting power of {adjusted_voting_power}')

image.png

Pixabay license, source

Reminder


Visit the /promoted page and #burnsteem25 to support the inflation-fighters who are helping to enable decentralized regulation of Steem token supply growth.

Sort:  

Very astonishing. On the one hand, that the bot even knows what "voting power" is and how it is determined.... But even more remarkable is the interaction with the adjustments in the code. In the end, you get the desired result, even if the bot unfortunately overlooked that the account already contains the entry last_vote_time .
This saves the part from

Get the account's history

and the following loop.

Unfortunately, the result is only almost correct :-)
When calculation the adjusted_voting_power the voting_power must first be divided by 100.

Conclusion: The bot already offers good support, but the results still need to be checked.

A funny thing is that after I posted this, I tried a new conversation to see if I could get a correct answer in a single question with more precise wording. I didn't, but that time the bot knew about "last_vote_time".

I gave it a break on dividing by 100 because the blockchain works with integers, so there may be cases where it's convenient to just leave it the way it is.

I definitely agree with your conclusion:

Conclusion: The bot already offers good support, but the results still need to be checked.

that time the bot knew about "last_vote_time".

This is curious, because the bot says in other chats it cannot retrieve any current information.

I gave it a break on dividing by 100

Yes, you can do that. I also find integers more "beautiful". But in this case, the other summand must be multiplied with 100. Otherwise the result is not correct...

Ah, yes, right. I had overlooked that mismatch.

I think that help is never superfluous, the bot is a support, but you are the one who reacts, let me explain, I have always thought that humans come first, ,we must lean on technology

I think it's great work. thanks. I used chatgpt recently. I think it's very good. Deals with search engines.

This post has been featured in the latest edition of Steem News...


If you like challenges, check out The Big 80 - launching soon with 80,000 SP in prizes...


An interesting bot, I've never used it. But it's complicated for me) I'm wondering if there is a bot that will be able to vote after publishing a post at a certain second. You had an interesting conversation.

I'm wondering if there is a bot that will be able to vote after publishing a post at a certain second.

Yes, that is certainly possible. I'm certain that many bots here have timing logic to control when they vote on a post.

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.028
BTC 62868.73
ETH 3108.34
USDT 1.00
SBD 2.50