Skip to content

Commit

Permalink
Bring back validators commands
Browse files Browse the repository at this point in the history
  • Loading branch information
gagdiez authored Feb 12, 2024
2 parents 457f560 + 40c99b8 commit aeffcf3
Show file tree
Hide file tree
Showing 29 changed files with 299 additions and 38 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test-matrix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ on:
jobs:
tests:
strategy:
fail-fast: false
matrix:
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
Expand Down
2 changes: 2 additions & 0 deletions bin/near-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ yargs // eslint-disable-line
.command(require('../commands/account/state'))
.command(require('../commands/contract/storage'))
.command(require('../commands/transactions/status'))
.command(require('../commands/validators/stake'))
.command(require('../commands/validators/validators'))
.command(require('../commands/contract/view'))
.command(require('../commands/deprecated'))
.option('verbose', { alias: ['v'], desc: 'Prints out verbose output', type: 'boolean', default: false })
Expand Down
16 changes: 12 additions & 4 deletions commands/account/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = {
required: false
})
.option('networkId', {
desc: 'Which network to use. Supports: mainnet, testnet',
desc: 'Which network to use. Supports: mainnet, testnet, custom',
type: 'string',
default: DEFAULT_NETWORK
}),
Expand Down Expand Up @@ -65,7 +65,7 @@ async function create(options) {
}

if (options.useFaucet) {
if (options.networkId !== 'testnet') throw new Error('Pre-funding accounts is only possible on testnet');
if (options.networkId === 'mainnet') throw new Error('Pre-funding accounts is not possible on mainnet');
} else {
if (!options.useAccount) throw new Error('Please specify an account to sign the transaction (--useAccount)');
await assertCredentials(options.useAccount, options.networkId, options.keyStore);
Expand Down Expand Up @@ -117,11 +117,14 @@ async function create(options) {
try {
// Handle response
const response = await promise;

if (keyPair) {
storeCredentials(newAccountId, options.networkId, options.keyStore, keyPair, true);
} else {
console.log(chalk`{bold.white ${newAccountId}} created successfully, please add its credentials manually.`);
}

// The faucet does not throw on error, so we force it here
if (options.useFaucet) { await response.state(); }

inspectResponse.prettyPrintResponse(response, options);
} catch (error) {
// Handle errors
Expand All @@ -144,6 +147,11 @@ async function create(options) {
console.error(chalk`\nYou cannot create Top Level Accounts.`);
process.exit(1);
break;
case 'AccountDoesNotExist':
if (!options.useFaucet) throw error;
console.error(chalk`\nThe faucet reported {bold.white no errors}, but we {bold.red cannot} find ${options.newAccountId}. Check if it exists with "near state ${options.newAccountId} --networkId ${options.networkId}".\n`);
process.exit(1);
break;
default:
throw error;
}
Expand Down
31 changes: 24 additions & 7 deletions commands/account/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = {
desc: 'Delete account, sending remaining NEAR to a beneficiary',
builder: (yargs) => yargs
.option('networkId', {
desc: 'Which network to use. Supports: mainnet, testnet',
desc: 'Which network to use. Supports: mainnet, testnet, custom',
type: 'string',
default: DEFAULT_NETWORK
})
Expand All @@ -25,7 +25,7 @@ module.exports = {

const confirmDelete = function (accountId, beneficiaryId) {
return askYesNoQuestion(
chalk`This will {bold.red delete ${accountId}}, transferring {bold.white the remaining NEAR tokens} to the {bold.green beneficiary ${beneficiaryId}}. This action will {bold.red NOT transfer} {bold.white FTs, NFTs} or other assets the account holds, make sure you to {bold.white manually transfer} them before deleting or they will be {bold.red lost}. Do you want to proceed? {bold.green (y/n)}`,
chalk`This will {bold.red delete ${accountId}}, transferring {bold.white the remaining NEAR tokens} to the {bold.green beneficiary ${beneficiaryId}}. This action will {bold.red NOT transfer} {bold.white FTs, NFTs} or other assets the account holds, make sure you to {bold.white manually transfer} them before deleting or they will be {bold.red lost}. Do you want to proceed? {bold.green (y/n)} `,
false);
};

Expand All @@ -46,13 +46,30 @@ async function deleteAccount(options) {
}
}

if (options.force || await confirmDelete(options.accountId, options.beneficiaryId)) {
const account = await near.account(options.accountId);
console.log(`Deleting account ${options.accountId}, beneficiary: ${options.beneficiaryId}`);
if (!options.force && !(await confirmDelete(options.accountId, options.beneficiaryId))) {
return console.log(chalk`{bold.white Deletion of account {bold.blue ${options.accountId}} was {bold.red cancelled}}`);
}

const account = await near.account(options.accountId);
console.log(`Deleting account ${options.accountId}, beneficiary: ${options.beneficiaryId}`);

try {
const result = await account.deleteAccount(options.beneficiaryId);
console.log(`Account ${options.accountId} for network "${options.networkId}" was deleted.`);
inspectResponse.prettyPrintResponse(result, options);
} else {
console.log(chalk`{bold.white Deletion of account {bold.blue ${options.accountId}} was {bold.red cancelled}}`);
} catch (error) {
switch (error.type) {
case 'KeyNotFound':
console.log(chalk`\n{bold.white ${options.accountId}} was not found in the network ${options.networkId}\n`);
process.exit(1);
break;
case 'SignerDoesNotExist':
// On re-sending a transaction, the signer might have been deleted already
console.log('RPC returned an error, please check if the account is deleted and try again');
process.exit(0);
break;
default:
throw error;
}
}
}
2 changes: 1 addition & 1 deletion commands/account/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module.exports = {
desc: 'Login through a web wallet (default: MyNearWallet)',
builder: (yargs) => yargs
.option('networkId', {
desc: 'Which network to use. Supports: mainnet, testnet',
desc: 'Which network to use. Supports: mainnet, testnet, custom',
type: 'string',
default: DEFAULT_NETWORK
}),
Expand Down
2 changes: 1 addition & 1 deletion commands/account/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
desc: 'View account\'s state (balance, storage_usage, code_hash, etc...)',
builder: (yargs) => yargs
.option('networkId', {
desc: 'Which network to use. Supports: mainnet, testnet',
desc: 'Which network to use. Supports: mainnet, testnet, custom',
type: 'string',
default: DEFAULT_NETWORK
}),
Expand Down
2 changes: 1 addition & 1 deletion commands/contract/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
type: 'string'
})
.option('networkId', {
desc: 'Which network to use. Supports: mainnet, testnet',
desc: 'Which network to use. Supports: mainnet, testnet, custom',
type: 'string',
default: DEFAULT_NETWORK
})
Expand Down
2 changes: 1 addition & 1 deletion commands/contract/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = {
default: '0'
})
.option('networkId', {
desc: 'Which network to use. Supports: mainnet, testnet',
desc: 'Which network to use. Supports: mainnet, testnet, custom',
type: 'string',
default: DEFAULT_NETWORK
})
Expand Down
2 changes: 1 addition & 1 deletion commands/contract/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
desc: 'View contract\'s storage (stored key-value pairs)',
builder: (yargs) => yargs
.option('networkId', {
desc: 'Which network to use. Supports: mainnet, testnet',
desc: 'Which network to use. Supports: mainnet, testnet, custom',
type: 'string',
default: DEFAULT_NETWORK
})
Expand Down
2 changes: 1 addition & 1 deletion commands/contract/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
default: null
})
.option('networkId', {
desc: 'Which network to use. Supports: mainnet, testnet',
desc: 'Which network to use. Supports: mainnet, testnet, custom',
type: 'string',
default: DEFAULT_NETWORK
}),
Expand Down
2 changes: 1 addition & 1 deletion commands/credentials/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = {
required: false,
})
.option('networkId', {
desc: 'Which network to use. Supports: mainnet, testnet',
desc: 'Which network to use. Supports: mainnet, testnet, custom',
type: 'string',
default: DEFAULT_NETWORK
})
Expand Down
2 changes: 1 addition & 1 deletion commands/credentials/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = {
default: false
})
.option('networkId', {
desc: 'Which network to use. Supports: mainnet, testnet',
desc: 'Which network to use. Supports: mainnet, testnet, custom',
type: 'string',
default: DEFAULT_NETWORK
})
Expand Down
13 changes: 12 additions & 1 deletion commands/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ const chalk = require('chalk');

module.exports = [
{
command: ['clean', 'repl', 'stake', 'evm-call', 'evm-view', 'set-api-key', 'js', 'validators', 'proposals'],
command: ['clean', 'repl', 'evm-call', 'evm-view', 'set-api-key', 'js'],
desc: false,
handler: deprecated
},
{
command: 'dev-deploy [...]',
desc: false,
handler: deprecatedDevDeploy
},
{
command: 'proposals',
desc: false,
handler: deprecatedProposals
}

];

async function deprecated() {
Expand All @@ -20,4 +26,9 @@ async function deprecated() {
async function deprecatedDevDeploy() {
console.log(chalk`\nThis command has been {bold.red deprecated}`);
console.log(chalk`Please use: {bold.white near create-account <accId> --useFaucet} to create a pre-funded account, and then {bold.white near deploy} to deploy the contract\n`);
}

async function deprecatedProposals() {
console.log(chalk`\nThis command has been {bold.red deprecated}`);
console.log(chalk`Please use: {bold.white near validators proposals`);
}
2 changes: 1 addition & 1 deletion commands/keys/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = {
default: '0'
})
.option('networkId', {
desc: 'Which network to use. Supports: mainnet, testnet',
desc: 'Which network to use. Supports: mainnet, testnet, custom',
type: 'string',
default: DEFAULT_NETWORK
}),
Expand Down
2 changes: 1 addition & 1 deletion commands/keys/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
desc: 'Delete access key',
builder: (yargs) => yargs
.option('networkId', {
desc: 'Which network to use. Supports: mainnet, testnet',
desc: 'Which network to use. Supports: mainnet, testnet, custom',
type: 'string',
default: DEFAULT_NETWORK
})
Expand Down
2 changes: 1 addition & 1 deletion commands/keys/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
desc: 'Query public keys of an account',
builder: (yargs) => yargs
.option('networkId', {
desc: 'Which network to use. Supports: mainnet, testnet',
desc: 'Which network to use. Supports: mainnet, testnet, custom',
type: 'string',
default: DEFAULT_NETWORK
}),
Expand Down
2 changes: 1 addition & 1 deletion commands/transactions/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = {
type: 'string',
})
.option('networkId', {
desc: 'Which network to use. Supports: mainnet, testnet',
desc: 'Which network to use. Supports: mainnet, testnet, custom',
type: 'string',
default: DEFAULT_NETWORK
}),
Expand Down
2 changes: 1 addition & 1 deletion commands/transactions/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = {
type: 'string',
})
.option('networkId', {
desc: 'Which network to use. Supports: mainnet, testnet',
desc: 'Which network to use. Supports: mainnet, testnet, custom',
type: 'string',
default: DEFAULT_NETWORK
}),
Expand Down
45 changes: 45 additions & 0 deletions commands/validators/stake.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const qs = require('querystring');
const { utils } = require('near-api-js');

const connect = require('../../utils/connect');
const inspectResponse = require('../../utils/inspect-response');
const { assertCredentials } = require('../../utils/credentials');
const { DEFAULT_NETWORK } = require('../../config');

module.exports = {
command: 'validator-stake accountId stakingKey amount',
aliases: ['stake'],
desc: 'Create a staking transaction (for **validators** only)',
builder: (yargs) => yargs
.option('accountId', {
desc: 'Account that wants to become a network validator',
type: 'string',
required: true,
})
.option('stakingKey', {
desc: 'Public key to stake with (base58 encoded)',
type: 'string',
required: true,
})
.option('amount', {
desc: 'Amount to stake',
type: 'string',
required: true,
})
.option('networkId', {
desc: 'Which network to use. Supports: mainnet, testnet, custom',
type: 'string',
default: DEFAULT_NETWORK
}),
handler: stake
};


async function stake(options) {
await assertCredentials(options.accountId, options.networkId, options.keyStore);
console.log(`Staking ${options.amount} (${utils.format.parseNearAmount(options.amount)}N) on ${options.accountId} with public key = ${qs.unescape(options.stakingKey)}.`);
const near = await connect(options);
const account = await near.account(options.accountId);
const result = await account.stake(qs.unescape(options.stakingKey), utils.format.parseNearAmount(options.amount));
inspectResponse.prettyPrintResponse(result, options);
}
41 changes: 41 additions & 0 deletions commands/validators/validators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { DEFAULT_NETWORK } = require('../../config');
const connect = require('../../utils/connect');
const validatorsInfo = require('../../utils/validators-info');

module.exports = {
command: 'validators <query>',
desc: 'Info on validators',
handler: validators,
builder: (yargs) => yargs
.option('query',
{
desc: 'current | next | <block number> | <block hash> to lookup validators at a specific epoch, or "proposals" to see the current proposals',
type: 'string',
default: 'current'
}
)
.option('networkId', {
desc: 'Which network to use. Supports: mainnet, testnet, custom',
type: 'string',
default: DEFAULT_NETWORK
}),
};

async function validators(options) {
const near = await connect(options);

switch (options.query) {
case 'proposals':
await validatorsInfo.showProposalsTable(near);
break;
case 'current':
await validatorsInfo.showValidatorsTable(near, null);
break;
case 'next':
await validatorsInfo.showNextValidatorsTable(near);
break;
default:
await validatorsInfo.showValidatorsTable(near, options.query);
break;
}
}
9 changes: 9 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ function getConfig(env) {
helperAccount: 'testnet',
};
break;
case 'custom':
config = {
networkId: 'custom',
nodeUrl: process.env.NEAR_CUSTOM_RPC,
walletUrl: process.env.NEAR_CUSTOM_WALLET,
helperUrl: process.env.NEAR_CUSTOM_HELPER,
helperAccount: process.env.NEAR_CUSTOM_TLA,
};
break;
default:
throw Error(`Unconfigured environment '${env}'. Can be configured in src/config.js.`);
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "near-cli",
"version": "4.0.2",
"version": "4.0.4",
"description": "Simple CLI for interacting with NEAR Protocol",
"engines": {
"node": ">= 16"
Expand Down Expand Up @@ -36,6 +36,7 @@
"typescript": "^5.3.3"
},
"dependencies": {
"ascii-table": "^0.0.9",
"bs58": "^5.0.0",
"chalk": "^4.1.2",
"flagged-respawn": "^2.0.0",
Expand Down
Loading

0 comments on commit aeffcf3

Please sign in to comment.