Skip to content

Commit

Permalink
Merge pull request #227 from nearprotocol/fix-initial-balance
Browse files Browse the repository at this point in the history
Convert --initialBalance from NEAR tokens to integer units
  • Loading branch information
janedegtiareva committed Dec 23, 2019
2 parents fd986b5 + a54493e commit fc430fc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 44 deletions.
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 = {
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'
}),
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);
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
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 comments on commit fc430fc

Please sign in to comment.