It has been a while since I've updated this repository, this has mainly been due to the fact that I covered a good part of steem-js basic functions and because the awesome https://github.com/steemit/devportal-tutorials-js has been producing some great educational content so I felt that steemsnippets was not as important as before.
If you are unfamiliar with steemsnippets I encourage you to read the genesis post : https://steemit.com/programming/@howo/introducting-steemsnippets
And to check out the repository : https://github.com/drov0/steemsnippets
Buuut hf20 came and some new stuff needs to be covered, and since I had to figure them out I was like "well let's write a few snippets while I'm at it".
As a side note, I am ditching steem-js for the more modern dsteem and am slowly converting all of my projects, so expect most of my current and future snippets to use dsteem.
This update focuses mostly on a great new feature that was brought by hf20 : discounted accounts, you can read all about them in this steemitblog post. But do bear in mind that you need to be a relatively big stakeholder to hope to claim more than like an account per few days, if you are curious about the costs, I suggest you to look at this great page by @holger80 https://beempy.com/resource_costs and your account on steemd to try to see how much of your ressource credits this will cost you.
Creating a discounted account works in two steps : first you claim discounted accounts, these are not named and will never expire, you can try to claim as much as you want. For instance @pharesim got 789 as of today.
The snippet can be found here : https://github.com/drov0/steemsnippets/blob/master/dsteem/claim_account/claim_account.js
**
* claims a discounted account.
* @param {String} creator - Name of the account that will claim the discounted account.
* @param {String} active_key - Active key of the creator account
*/
function claim_account(creator, active_key)
{
return new Promise(resolve => {
const wif = dsteem.PrivateKey.fromString(active_key);
const fee = dsteem.Asset.from(0, 'STEEM');
const op = [
'claim_account',
{
creator: creator,
extensions: [],
fee: fee
}];
client.broadcast.sendOperations([op], wif).then(function (result) {
console.log('Included in block: ' + result.block_num)
resolve("=");
}, function (error) {
console.error(error);
resolve("-");
});
});
}
This is pretty straightforward huh ?
Once you have one or more claimed account, you can use them to create an account for "free" (technically you paid resource credits)
The snippet can be found here : https://github.com/drov0/steemsnippets/blob/master/dsteem/create_discounted_account/create_discounted_account.js
/**
* Creates a discounted account, note that you need to claim discounted accounts before being able to create them this way.
* @param {String} creator - Name of the account that will create the new account. This account must have at least one claimed discounted account.
* @param {String} active_key - Active key of the creator account
* @param {String} username - username of the account to be created
* @param {String} password - password of the account to be created.
*/
function create_discounted_account(creator, active_key, username, password)
{
return new Promise(resolve => {
const wif = dsteem.PrivateKey.fromString(active_key);
const prefix = client.addressPrefix;
const ownerKey = dsteem.PrivateKey.fromLogin(username, password, 'owner').createPublic(prefix);
let owner = dsteem.Authority.from(ownerKey);
const activeKey = dsteem.PrivateKey.fromLogin(username, password, 'active').createPublic(prefix);
let active = dsteem.Authority.from(activeKey);
const postingKey = dsteem.PrivateKey.fromLogin(username, password, 'posting').createPublic(prefix);
let posting = dsteem.Authority.from(postingKey);
let memo_key = dsteem.PrivateKey.fromLogin(username, password, 'memo').createPublic(prefix);
const metadata = {date: new Date()};
const create_op = [
'create_claimed_account',
{
active,
creator,
extensions: [],
json_metadata: metadata ? JSON.stringify(metadata) : '',
memo_key,
new_account_name: username,
owner,
posting,
}
];
client.broadcast.sendOperations([create_op], wif).then(function (result) {
console.log('Included in block: ' + result.block_num);
resolve("=");
}, function (error) {
console.error(error);
resolve("-");
});
});
}
I made sure to handle most of the complexity from within the function so you only have to put the account from which you claimed the accounts, it's active key, and then the new user's username and his password and boom you're done.
As a side note, there are no requirements on the password but this password will secure the funds of that account so try to put a strong one alright ?
And that's about it ! Now you can easily run an account creation service, just claim accounts whenever you have rcs to spare and when someone comes to you to create an account, create it using those.
As a side note https://steemd.com/@teststeempress this is a discounted account that I created.
Note how it has 0 sp, so no voting power. yet it has some resource credits, that's because that account was credited the amount of resource credits that would normally be burned to create an account, and as of today the consensus sits at 3 STEEM. And with today's rc costs, this allows this account to make around 3 posts/comments per week so if you want to create an account for a friend make sure to delegate to them a bit if you want to allow them to transact a bit, or upvote them to make them self sufficient.
That's all for today folks, see you around :)