Crea Developer logo

Crea Developer Portal

JS: Set Withdraw Route

Set routes to an account’s de-energizes or withdraws.

Full, runnable src of Set Withdraw Route can be downloaded as part of the JS tutorials repository.

We will learn how to allocate a percentage for withdrawal to other accounts using Creaconnect as well as with the client-side signing method. This tutorial runs on the main Crea blockchain. Therefore, any accounts used here will affect real funds on the live network. Use with caution.

Intro

This tutorial will demonstrate a few functions such as querying account by name and determining the vesting balance of the related account. This will allow us to set “withdraw routes” to other accounts with a percent selection and auto energize function. This feature is quite useful if you want to withdraw a portion of your CREA to a separate account or ENERGIZE other accounts as you withdraw from one account.

Steps

  1. App setup Setup dcrea to use the proper connection and network.
  2. Get account routes Get account’s current routes
  3. Fill form Fill form with appropriate data
  4. Set withdraw route Set route with Creaconnect or client-side signing

1. App setup

Below, we have dcrea 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:

const dcrea = require('dcrea');
let opts = {};
//connect to production server
opts.addressPrefix = 'STM';
opts.chainId =
    '0000000000000000000000000000000000000000000000000000000000000000';
//connect to server which is connected to the network/production
const client = new dcrea.Client('https://nodes.creary.net');

2. Get account routes

After the account name field is provided, using the Get withdraw routes button, we can fetch the current withdraw routes, if they exist. The related HTML input forms can be found in the index.html file. The values are pulled from that screen with the following:

    const accSearch = document.getElementById('username').value;

    const _account = await client.database.call('get_withdraw_routes', [accSearch]);
    console.log(`_account:`, _account);

3. Fill form

After we have fetched the account data, we will show a list of current routes, if they exist, and display information to the user about how many much they can apply to other accounts.

let info = '';
let sum = 0;
if (_account.length > 0) {
    for (var i = 0; i < _account.length; i++) {
        info += `${_account[i].to_account} - ${_account[i].percent / 100}%<br>`;
        sum += _account[i].percent / 100;
    }
} else {
    info += `No route is available!<br>`;
}
info += `You can set ${100 - sum}% remaining part to other accounts!`;
document.getElementById('accInfo').innerHTML = info;

Previous routes can be overwritten by changing and submitting a new transaction to the same account.

We also generate a Creaconnect signing link.

window.openSC = async () => {
    const link = `https://creaconnect.com/sign/set-withdraw-vesting-route?from_account=${
        document.getElementById('username').value
    }&percent=${document.getElementById('crea').value * 100}&to_account=${
        document.getElementById('account').value
    }&auto_vest=${document.getElementById('percent').checked}`;
    window.open(link);
};

4. Set withdraw route

We have two options on how to De-energize: Creaconnect and client-side signing. Since this action requires Active authority, both client-side and Stemconnect signing will require the Active Private key to sign the transaction. The transaction submission function appears as follows:

window.submitTx = async () => {
    const privateKey = dcrea.PrivateKey.fromString(
        document.getElementById('wif').value
    );
    const op = [
        'set_withdraw_vesting_route',
        {
            from_account: document.getElementById('username').value,
            to_account: document.getElementById('account').value,
            percent: document.getElementById('crea').value * 100,
            auto_vest: document.getElementById('percent').checked,
        },
    ];
    client.broadcast.sendOperations([op], privateKey).then(
        function(result) {
            document.getElementById('result').style.display = 'block';
            document.getElementById(
                'result'
            ).innerHTML = `<br/><p>Included in block: ${
                result.block_num
            }</p><br/><br/>`;
        },
        function(error) {
            console.error(error);
        }
    );
};

That’s it!

To run this tutorial

  1. clone this repo
  2. cd tutorials/26_set_withdraw_route
  3. npm i
  4. npm run dev-server or npm run start
  5. After a few moments, the server should be running at http://localhost:3000/