Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
fix: bring back validators commands
Browse files Browse the repository at this point in the history
  • Loading branch information
gagdiez committed Feb 11, 2024
1 parent 457f560 commit a3d965d
Show file tree
Hide file tree
Showing 28 changed files with 264 additions and 30 deletions.
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/transactions/stake'))
.command(require('../commands/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
4 changes: 2 additions & 2 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
4 changes: 2 additions & 2 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 Down
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
44 changes: 44 additions & 0 deletions commands/transactions/stake.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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',
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);
}
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
41 changes: 41 additions & 0 deletions commands/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 <search>',
desc: 'Info on validators',
handler: validators,
builder: (yargs) => yargs
.option('search',
{
desc: '(I) "proposals": to see the current proposals or (II) "current" | "next" | block number | block hash: lookup validators at a specific epoch',
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.search) {
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.search);
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
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
14 changes: 7 additions & 7 deletions test/test_account_creation.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
set -e

timestamp=$(date +%s)
testaccount1=testaccount${timestamp}-1.testnet
testaccount2=testaccount${timestamp}-2.testnet
testaccount3=testaccount${timestamp}-3.testnet
testaccount4=testaccount${timestamp}-4.testnet
zerobalance=testaccount${timestamp}-z.testnet
withbalance=testaccount${timestamp}-y.testnet
testaccount1=test-ac-${timestamp}-1.testnet
testaccount2=test-ac-${timestamp}-2.testnet
testaccount3=test-ac-${timestamp}-3.testnet
testaccount4=test-ac-${timestamp}-4.testnet
zerobalance=test-ac-${timestamp}-z.testnet
withbalance=test-ac-${timestamp}-y.testnet

tla=${timestamp}${timestamp}${timestamp}12

Expand Down Expand Up @@ -85,7 +85,7 @@ fi

# Cannot create a useFaucet account in mainnet
ERROR=$(./bin/near create-account $testaccount4 --useFaucet --networkId mainnet 2>&1)
EXPECTED_ERROR=".+Pre-funding accounts is only possible on testnet.+"
EXPECTED_ERROR=".+Pre-funding accounts is not possible on mainnet.+"
if [[ ! "$ERROR" =~ $EXPECTED_ERROR ]]; then
echo FAILURE Unexpected output when funding pre-funded account in mainnet
echo Got: $ERROR
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 @@ -2,7 +2,7 @@
set -e

timestamp=$(date +%s)
testaccount=testaccount$timestamp$RANDOM.testnet
testaccount=test-ao-$timestamp.testnet

./bin/near create-account $testaccount --useFaucet

Expand Down
4 changes: 2 additions & 2 deletions test/test_contract.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
set -e

timestamp=$(date +%s)
contract=testaccount$timestamp-c.testnet
testaccount=testaccount$timestamp-t.testnet
contract=test-contract-$timestamp-c.testnet
testaccount=test-contract-$timestamp-t.testnet

echo Creating account
./bin/near create $contract --useFaucet
Expand Down
2 changes: 1 addition & 1 deletion test/test_deploy_init_contract.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

timestamp=$(date +%s)
contract=testaccount$timestamp.testnet
contract=test-deploy-$timestamp.testnet

echo Creating account
./bin/near create $contract --useFaucet
Expand Down
2 changes: 1 addition & 1 deletion test/test_keys.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set -e

timestamp=$(date +%s)
testaccount=testaccount${timestamp}.testnet
testaccount=test-keys-${timestamp}.testnet

# Can create a pre-funded account
./bin/near create-account $testaccount --useFaucet
Expand Down
Loading

0 comments on commit a3d965d

Please sign in to comment.