create account

Here is how you can easily delegate SP and convert between VEST and STEEM programmatically by gaottantacinque

View this thread on: hive.blogpeakd.comecency.com
· @gaottantacinque · (edited)
$0.70
Here is how you can easily delegate SP and convert between VEST and STEEM programmatically
<center>https://cdn.steemitimages.com/DQmUFn39XJ5bVh4hg9y1GiFBQH8ikfBqzMLmViUMQafmQMG/image.png</center>

Earlier today I was trying to programmatically delegate to one of my accounts with the following (correct) code:

```const dsteem = require('dsteem');
const client = new dsteem.Client('https://api.steemit.com');

const delegate = async (config) => {
  const {
    delegation, privateKey: privateActiveKey, delegator, delegatee,
  } = config;
  const privateKey = dsteem.PrivateKey.fromString(privateActiveKey);
  const op = [
    'delegate_vesting_shares',
    {
      delegator,
      delegatee,
      vesting_shares: delegation,
    },
  ];
  client.broadcast.sendOperations([op], privateKey).then(
    result => console.log('Delegation done.', JSON.stringify(result)),
    console.error,
  );
};

const config = {
  delegatee: 'bot-test',
  delegator: 'marcocasario',
  privateKey: '51111111111111111111111111111111111111111111111RlFx', // private active key
  delegation: '29456.000000 VESTS', // Around 15 SP
};
delegate(config);
```
<br>
Initially though I was passing in as input STEEM instead of VEST so the script was erroring out. After reading a bit around I found out how to easily convert one into the other. Here is the JS code: 
<br>
```
  const cache = {
    totVestShares: null,
    totSteem: null,
  };
    
  const getProps = () => new Promise((res) => {
      fetch('https://api.steemit.com', {
        method: 'POST',
        cache: 'no-cache',
        credentials: 'omit',
        headers: {
          'Content-Type': 'application/json',
        },
        redirect: 'follow',
        referrerPolicy: 'no-referrer',
        body: JSON.stringify({
          jsonrpc: '2.0',
          method: 'condenser_api.get_dynamic_global_properties',
          params: [],
          id: 1,
        }),
      }).then(res => res.json()).then((data) => {console.log(data);res(data.result)});
  });
    
  // Usage: steemToVest('15 STEEM'); >> '29456.000000 VESTS'
  const steemToVest = async (steem) => {
    if (!cache.totSteem) {
      const { total_vesting_shares, total_vesting_fund_steem } = await getProps();
      cache.totVestShares = +total_vesting_shares.split(' ')[0];
      cache.totSteem = +total_vesting_fund_steem.split(' ')[0];
    }
    const steemAmount = +steem.split(' ')[0];
    const vests = steemAmount * cache.totVestShares / cache.totSteem;
    // Always 6 decimals and rounded
    const roundedVests = (Math.round(vests * 1000000 + Number.EPSILON) / 1000000).toFixed(6);
    console.log(`${steemAmount} STEEM is ${roundedVests} VESTS.`);
    return roundedVests;
  };
  
  // Usage: vestsToSteem('29456.61988576255 VESTS'); >> '15.000 STEEM'
  const vestsToSteem = async (vests) => {
    if (!cache.totSteem) {
      const { total_vesting_shares, total_vesting_fund_steem } = await getProps();
      cache.totVestShares = +total_vesting_shares.split(' ')[0];
      cache.totSteem = +total_vesting_fund_steem.split(' ')[0];
    }
    const vestsAmount = +vests.split(' ')[0];
    const steem = vestsAmount * cache.totSteem / cache.totVestShares;
    const roundedSteem = (Math.round(steem * 1000 + Number.EPSILON) / 1000).toFixed(3);
    console.log(`${vestsAmount} VESTS is ${roundedSteem} STEEM.`);
    // Always 3 decimals and rounded
    return roundedSteem;
  };
```

<div class="phishy pull-left">
<h4>USAGE:</h4>
This is even easier than <a href="https://steemit.com/steemit/@gaottantacinque/here-is-how-you-can-easily-get-steemit-accounts-information-programmatically">retrieving balances of a list of Steemit accounts</a> or <a href="https://steemit.com/steemit/@gaottantacinque/here-is-how-you-can-easily-get-a-steemit-account-resource-credit-amount-programmatically">calculate an account's RC programmatically</a> as there is no need to import SteemJs or SteemdJs for the VEST <=> STEEM conversion.

The script simply uses a cross domain http requests. This means that you can try this simple script in any webpage.

<b>Step by step instructions:</b>

<b>STEP 1</b>
Open your favorite browser on any site (I strongly recommend using [Brave browser](https://brave.com/myc159)) and open the DevTools (Ctrl + Shift + J on Linux/Windows and Cmd + Opt + J on Mac).

<b>STEP 2</b>
Copy and paste my script above in the Console.

<b>STEP 3</b>
Press enter and there you go! You can now execute the 2 commands to convert between STEEM and VESTS.

Enjoy!! =]

NOTE:
As usual, this script is completely safe as it does not require any sort of keys to function and can be executed in any webpage!
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 38 others
👎  ,
properties (23)
authorgaottantacinque
permlinkhere-is-how-you-can-easily-delegate-sp-and-convert-between-vest-and-steem-programmatically
categorysteemit
json_metadata{"tags":["stem","blockchain","neoxian","palnet","programming","javascript","art"],"image":["https://cdn.steemitimages.com/DQmUFn39XJ5bVh4hg9y1GiFBQH8ikfBqzMLmViUMQafmQMG/image.png"],"links":["https://steemit.com/steemit/@gaottantacinque/here-is-how-you-can-easily-get-steemit-accounts-information-programmatically","https://steemit.com/steemit/@gaottantacinque/here-is-how-you-can-easily-get-a-steemit-account-resource-credit-amount-programmatically","https://brave.com/myc159"],"app":"steemit/0.1","format":"markdown"}
created2020-01-31 07:44:51
last_update2020-01-31 07:59:21
depth0
children4
last_payout2020-02-07 07:44:51
cashout_time1969-12-31 23:59:59
total_payout_value0.341 HBD
curator_payout_value0.361 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,533
author_reputation13,463,378,539,470
root_title"Here is how you can easily delegate SP and convert between VEST and STEEM programmatically"
beneficiaries
0.
accountsteempeak
weight1,000
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id94,951,992
net_rshares3,666,493,540,343
author_curate_reward""
vote details (104)
@beerlover ·
<div class='pull-right'>https://cdn.steemitimages.com/DQmaHThyECGhEx8tSfHZbiMFRNYjJ35K92cDgiJjkzBUaJo/One%20sip%20of%20BEER%20for%20you.gif<p><sup><a href='https://steem-engine.com/?p=market&t=BEER'>View or trade </a> <code>BEER</code>.</sup></p></div><center><br> <p> Hey @gaottantacinque, here is a little bit of <code>BEER</code> from @eii for you. Enjoy it!</p> <p>Learn how to <a href='https://steemit.com/beer/@beerlover/what-is-proof-of-stake-with-beer'>earn FREE BEER each day </a> by staking.</p> </center><div></div>
properties (22)
authorbeerlover
permlinkre-here-is-how-you-can-easily-delegate-sp-and-convert-between-vest-and-steem-programmatically-20200131t092829z
categorysteemit
json_metadata"{"app": "beem/0.21.1"}"
created2020-01-31 09:28:33
last_update2020-01-31 09:28:33
depth1
children0
last_payout2020-02-07 09:28:33
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_length526
author_reputation25,764,855,350,472
root_title"Here is how you can easily delegate SP and convert between VEST and STEEM programmatically"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id94,953,854
net_rshares0
@eii ·
!COFFEEA
!shop
$trdo
!BEER
properties (22)
authoreii
permlinkre-gaottantacinque-q4ysz1
categorysteemit
json_metadata{"tags":["steemit"],"app":"steempeak/2.2.8"}
created2020-01-31 09:28:15
last_update2020-01-31 09:28:15
depth1
children1
last_payout2020-02-07 09:28: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_length26
author_reputation181,092,811,369,851
root_title"Here is how you can easily delegate SP and convert between VEST and STEEM programmatically"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id94,953,845
net_rshares0
@trendotoken ·
Congratulations @eii, you successfuly trended the post shared by @gaottantacinque!
@gaottantacinque will receive <b>0.04182638</b> [TRDO](https://steem-engine.com/?p=history&t=TRDO) & @eii will get <b>0.02788425</b> [TRDO](https://steem-engine.com/?p=history&t=TRDO) curation in 3 Days from Post Created Date!

<b>"Call [TRDO](https://steem-engine.com/?p=history&t=TRDO), Your Comment Worth Something!"</b>
---
<sup>To view or trade TRDO go to [steem-engine.com](https://steem-engine.com/?p=market&t=TRDO)
Join [TRDO Discord Channel](https://discord.gg/wySP8T9) or Join [TRDO Web Site](http://www.trendotoken.info/)</sup>
properties (22)
authortrendotoken
permlinkre-eii-re-gaottantacinque-q4ysz1-20200131t092824364z
categorysteemit
json_metadata{"tags":["comments-scot","trendo-bot"],"app":"comments-scot/1.1","format":"markdown"}
created2020-01-31 09:28:24
last_update2020-01-31 09:28:24
depth2
children0
last_payout2020-02-07 09:28: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_length622
author_reputation5,546,209,053,433
root_title"Here is how you can easily delegate SP and convert between VEST and STEEM programmatically"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id94,953,849
net_rshares0
@trendotoken ·
Congratulations @gaottantacinque, your post successfully recieved <b>0.04182638</b> [TRDO](https://steem-engine.com/?p=history&t=TRDO) from below listed TRENDO callers:<br>

>	<sup>@eii earned : **0.02788425** [TRDO](https://steem-engine.com/?p=history&t=TRDO) curation</sup> 

---
<sup>To view or trade TRDO go to [steem-engine.com](https://steem-engine.com/?p=market&t=TRDO)
Join [TRDO Discord Channel](https://discord.gg/wySP8T9) or Join [TRDO Web Site](http://www.trendotoken.info/)</sup>
properties (22)
authortrendotoken
permlinkre-gaottantacinque-here-is-how-you-can-easily-delegate-sp-and-convert-between-vest-and-steem-programmatically-20200203t075115286z
categorysteemit
json_metadata{"tags":["trdo","trendo-bot"],"app":"comments-scot/1.1","format":"markdown"}
created2020-02-03 07:51:21
last_update2020-02-03 07:51:21
depth1
children0
last_payout2020-02-10 07:51: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_length492
author_reputation5,546,209,053,433
root_title"Here is how you can easily delegate SP and convert between VEST and STEEM programmatically"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id95,047,439
net_rshares0