8000 Convert --initialBalance from NEAR tokens to integer units by vgrichina · Pull Request #227 · near/near-cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Oct 4, 2024. It is now read-only.

Convert --initialBalance from NEAR tokens to integer units #227

Merged
merged 4 commits into from
Dec 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 1 addition & 27 deletions bin/near-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,6 @@ const main = require('../');
const exitOnError = require('../utils/exit-on-error');

// For account:
const createAccount = {
command: 'create_account <accountId>',
desc: 'create a new developer account',
builder: (yargs) => yargs
.option('accountId', {
desc: 'Unique identifier for the newly created account',
type: 'string',
required: true
})
.option('masterAccount', {
desc: 'Account used to create requested account.',
type: 'string',
required: true
})
.option('publicKey', {
desc: 'Public key to initialize the account with',
type: 'string',
required: false
})
.option('initialBalance', {
desc: 'Number of tokens to transfer to newly created account',
type: 'string',
default: '10'
}),
handler: exitOnError(main.createAccount)
};

const login = {
command: 'login',
Expand Down Expand Up @@ -207,7 +181,7 @@ yargs // eslint-disable-line
desc: 'Unique identifier for the account',
type: 'string',
})
.command(createAccount)
.command(require('../commands/create-account'))
.command(viewAccount)
.command(deleteAccount)
.command(keys)
Expand Down
50 changes: 50 additions & 0 deletions commands/create-account.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

const exitOnError = require('../utils/exit-on-error');
const connect = require('../utils/connect');
const { KeyPair, utils } = require('nearlib');

module.exports = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to pulling out commands into separate files!

command: 'create_account <accountId>',
desc: 'create a new developer account',
builder: (yargs) => yargs
.option('accountId', {
desc: 'Unique identifier for the newly created account',
type: 'string',
required: true
})
.option('masterAccount', {
desc: 'Account used to create requested account.',
type: 'string',
required: true
})
.option('publicKey', {
desc: 'Public key to initialize the account with',
type: 'string',
required: false
})
.option('initialBalance', {
desc: 'Number of tokens to transfer to newly created account',
type: 'string',
default: '0.1'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why we change it to 0.1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So that if you have created your master account in wallet, you can create multiple accounts in shell without completely draining master account balance (it's 10 NEAR by default).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is not a good way to do so. give more instructions to user what create_account command is from and how to use it make it more clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you suggest not having default amount at all? or different default amount?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe not having default amount but required amount from user is better. since 0.1 or even less is not good number to guess how many account user want.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe not having default amount but required amount from user is better.

I agree there is some trade off between discoverability and convenience here. However I highly prefer tools where you can succeed most of the time with reasonable details. We need to make sure to have --initialBalance example in docs though /cc @amgando

}),
handler: exitOnError(createAccount)
};

async function createAccount(options) {
options.initialBalance = utils.format.parseNearAmount(options.initialBalance);
// NOTE: initialBalance is passed as part of config here
let near = await connect(options);
let keyPair;
let publicKey;
if (options.publicKey) {
publicKey = options.publicKey;
} else {
keyPair = await KeyPair.fromRandom('ed25519');
publicKey = keyPair.getPublicKey();
}
await near.createAccount(options.accountId, publicKey);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanna know the function createAccount here is never received the arg initialBalance, how could it really pass the commend arg initialBalance to create new account with specific token?

Copy link
Contributor Author
@vgrichina vgrichina Dec 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above:

    // NOTE: initialBalance is passed as part of config here
    let near = await connect(options);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it

if (keyPair) {
await near.connection.signer.keyStore.setKey(options.networkId, options.accountId, keyPair);
}
console.log(`Account ${options.accountId} for network "${options.networkId}" was created.`);
}
16 changes: 0 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,6 @@ exports.callViewFunction = async function(options) {
};

// For account:
exports.createAccount = async function(options) {
let near = await connect(options);
let keyPair;
let publicKey;
if (options.publicKey) {
publicKey = options.publicKey;
} else {
keyPair = await KeyPair.fromRandom('ed25519');
publicKey = keyPair.getPublicKey();
}
await near.createAccount(options.accountId, publicKey);
if (keyPair) {
await near.connection.signer.keyStore.setKey(options.networkId, options.accountId, keyPair);
}
console.log(`Account ${options.accountId} for network "${options.networkId}" was created.`);
};

exports.login = async function(options) {
if (!options.walletUrl) {
Expand Down
2 changes: 1 addition & 1 deletion test/test_account_operations.sh
5D62
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ echo Create account
echo Get account state
RESULT=$(../bin/near state $testaccount | strip-ansi)
echo $RESULT
EXPECTED=".+Account $testaccount.+amount:.+'10'.+ "
EXPECTED=".+Account $testaccount.+amount:.+'100000000000000000000000'.+ "
if [[ ! "$RESULT" =~ $EXPECTED ]]; then
echo FAILURE Unexpected output from near view
exit 1
Expand Down
0