|
| 1 | +# Delegate STEEM POWER |
| 2 | + |
| 3 | +_In this tutorial, we will learn how to delegate power to other users using Steemconnect as well as with Client-side signing method._ |
| 4 | + |
| 5 | +This tutorial runs on the main Steem blockchain. And accounts queried/searched are real accounts with their available VESTS balances and estimated STEEM POWER holdings. |
| 6 | + |
| 7 | +## Intro |
| 8 | + |
| 9 | +This tutorial will show few functions such as querying account by name and getting account vesting balance. We then convert VESTS to STEEM POWER for convenience of user. And allow user to choose portion or all holdings of VESTS to delegate other users. A simple HTML interface is provided to capture the account with search and its VESTS balance as well as allowing interactively delegate. |
| 10 | + |
| 11 | +## Steps |
| 12 | + |
| 13 | +1. [**App setup**](#app-setup) Setup `dsteem` to use the proper connection and network. |
| 14 | +2. [**Search account**](#search-account) Get account details after input has account name |
| 15 | +3. [**Calculate and Fill form**](#fill-form) Calculate available vesting shares and Fill form with details |
| 16 | +4. [**Delegate power**](#delegate-power) Delegate VESTS with Steemconnect or Client-side signing. |
| 17 | + |
| 18 | +#### 1. App setup <a name="app-setup"></a> |
| 19 | + |
| 20 | +Below we have `dsteem` pointing to the production network with the proper chainId, addressPrefix, and endpoint. There is a `public/app.js` file which holds the Javascript segment of this tutorial. In the first few lines we define the configured library and packages: |
| 21 | + |
| 22 | +```javascript |
| 23 | +const dsteem = require('dsteem'); |
| 24 | +let opts = {}; |
| 25 | +//connect to production server |
| 26 | +opts.addressPrefix = 'STM'; |
| 27 | +opts.chainId = |
| 28 | + '0000000000000000000000000000000000000000000000000000000000000000'; |
| 29 | +//connect to server which is connected to the network/production |
| 30 | +const client = new dsteem.Client('https://api.steemit.com'); |
| 31 | +``` |
| 32 | + |
| 33 | +#### 2. Search account <a name="search-account"></a> |
| 34 | + |
| 35 | +After account name field is filled with some name, we do automatic search for account by name when input is focused out. HTML input forms can be found in the `index.html` file. The values are pulled from that screen with the below: |
| 36 | + |
| 37 | +```javascript |
| 38 | + const accSearch = document.getElementById('username').value; |
| 39 | + const _account = await client.database.call('get_accounts', [[accSearch]]); |
| 40 | + console.log(`_account:`, _account); |
| 41 | +``` |
| 42 | + |
| 43 | +#### 3. Calculate and Fill form <a name="fill-form"></a> |
| 44 | + |
| 45 | +After we fetched account data, we will fill form with VESTS balance and show current balance details. Note, that in order to get available VESTS balance we will have to check if account is already powering down and how much is powering down, how much of VESTS were delegated out which locks them from being powered down. Available balance will be in `avail` variable, next for convenience of user, we convert available VESTS to STEEM with `getDynamicGlobalProperties` function and fill form fields accordingly. |
| 46 | + |
| 47 | +```javascript |
| 48 | + const name = _account[0].name; |
| 49 | + const avail = parseFloat(_account[0].vesting_shares) - (parseFloat(_account[0].to_withdraw) - parseFloat(_account[0].withdrawn)) / 1e6 - parseFloat(_account[0].delegated_vesting_shares); |
| 50 | + |
| 51 | + const props = await client.database.getDynamicGlobalProperties(); |
| 52 | + const vestSteem = parseFloat(parseFloat(props.total_vesting_fund_steem) * |
| 53 | + (parseFloat(avail) / parseFloat(props.total_vesting_shares)),6); |
| 54 | + |
| 55 | + const balance = `Available Vests for ${name}: ${avail} VESTS ~ ${vestSteem} STEEM POWER<br/><br/>`; |
| 56 | + document.getElementById('accBalance').innerHTML = balance; |
| 57 | + document.getElementById('steem').value = avail+' VESTS'; |
| 58 | +``` |
| 59 | + |
| 60 | +Once form is filled with maximum available VESTS balance, you can choose portion or lesser amount of VESTS to delegate other user. |
| 61 | + |
| 62 | +#### 4. Delegate power <a name="delegate-power"></a> |
| 63 | + |
| 64 | +We have 2 options on how to delegate others. Steemconnect and Client-side signing options. By default we generate Steemconnect link to delegate power (delegate vesting shares), but you can choose client signing option to delegate right inside tutorial, note client-side signing will require Active Private key to perform the operation. |
| 65 | + |
| 66 | +In order to enable client signing, we will generate operation and also show Active Private key (wif) field to sign transaction client side. |
| 67 | +Below you can see example of operation and signing transaction, after successful operation broadcast result will be shown in user interface. It will be block number that transaction was included. |
| 68 | + |
| 69 | +```javascript |
| 70 | +window.submitTx = async () => { |
| 71 | + const privateKey = dsteem.PrivateKey.fromString( |
| 72 | + document.getElementById('wif').value |
| 73 | + ); |
| 74 | + const op = [ |
| 75 | + 'delegate_vesting_shares', |
| 76 | + { |
| 77 | + delegator: document.getElementById('username').value, |
| 78 | + delegatee: document.getElementById('account').value, |
| 79 | + vesting_shares: document.getElementById('steem').value, |
| 80 | + }, |
| 81 | + ]; |
| 82 | + client.broadcast.sendOperations([op], privateKey).then( |
| 83 | + function(result) { |
| 84 | + document.getElementById('result').style.display = 'block'; |
| 85 | + document.getElementById( |
| 86 | + 'result' |
| 87 | + ).innerHTML = `<br/><p>Included in block: ${ |
| 88 | + result.block_num |
| 89 | + }</p><br/><br/>`; |
| 90 | + }, |
| 91 | + function(error) { |
| 92 | + console.error(error); |
| 93 | + } |
| 94 | + ); |
| 95 | +}; |
| 96 | +``` |
| 97 | + |
| 98 | +That's it! |
| 99 | + |
| 100 | +### To run this tutorial |
| 101 | + |
| 102 | +1. clone this repo |
| 103 | +1. `cd tutorials/25_delegate_power` |
| 104 | +1. `npm i` |
| 105 | +1. `npm run dev-server` or `npm run start` |
| 106 | +1. After a few moments, the server should be running at [http://localhost:3000/](http://localhost:3000/) |
0 commit comments