| title | JS: Set Withdraw Route |
|---|---|
| position | 28 |
| description | _Set routes to an account's power downs or withdraws._ |
| layout | full |
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 dPayID as well as with the client-side signing method. This tutorial runs on the main dPay blockchain. Therefore, any accounts used here will affect real funds on the live network. Use with caution.
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 power up function. This feature is quite useful if you want to withdraw a portion of your BEX to a separate account or POWER UP other accounts as you withdraw from one account.
- App setup Setup
ddpaysto use the proper connection and network. - Get account routes Get account's current routes
- Fill form Fill form with appropriate data
- Set withdraw route Set route with dPayID or client-side signing
Below, we have ddpays 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 ddpays = require('ddpays');
let opts = {};
//connect to production server
opts.addressPrefix = 'STM';
opts.chainId =
'38f14b346eb697ba04ae0f5adcfaa0a437ed3711197704aa256a14cb9b4a8f26';
//connect to server which is connected to the network/production
const client = new ddpays.Client('https://api.dpays.io');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);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 dPayID signing link.
window.openSC = async () => {
const link = `https://dpayid.io/sign/set-withdraw-vesting-route?from_account=${
document.getElementById('username').value
}&percent=${document.getElementById('bex').value * 100}&to_account=${
document.getElementById('account').value
}&auto_vest=${document.getElementById('percent').checked}`;
window.open(link);
};We have two options on how to Power down: dPayID and client-side signing. Since this action requires Active authority, both client-side and dPayID signing will require the Active Private key to sign the transaction. The transaction submission function appears as follows:
window.submitTx = async () => {
const privateKey = ddpays.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('bex').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!
git clone https://github.com/dpays/developer-docs-tutorials-js.gitcd devportal-tutorials-js/tutorials/26_set_withdraw_routenpm inpm run dev-serverornpm run start- After a few moments, the server should be running at http://localhost:3000/