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

Commit

Permalink
feat: js command (#975)
Browse files Browse the repository at this point in the history
* Added initial JS structure w/ deploy subcommand

* Added encode base64

* Added remove subcommand

* Working remove subcommand

* Added js call subcommand

* Added jsvm_transact

* Rearrange top level yargs for JS cli

* Format

* Corrected deploy command

* Remove init* related arsg

* Added js view

* Added js dev-deploy

* Cleanup

* linter errors fixed

* Simple README added, command description fixed

Co-authored-by: Serhii Volovyk <sergeyvolovyk@gmail.com>
  • Loading branch information
ChaoticTempest and volovyks authored May 19, 2022
1 parent 2c21e2e commit 8fe4a1f
Show file tree
Hide file tree
Showing 7 changed files with 389 additions and 53 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ _Click on a command for more information and examples._
| [`near validators current`](#near-validators-current) | displays current [epoch](http://docs.near.org/docs/concepts/epoch) validator pool details |
| [`near validators next`](#near-validators-next) | displays validator details for the next [epoch](http://docs.near.org/docs/concepts/epoch) |
| [`near proposals`](#near-proposals) | displays validator proposals for the [epoch](http://docs.near.org/docs/concepts/epoch) _after_ next |
| **JS-SDK** | |
| [`near js`](#JS-Contracts-Enclave) | Work with JS contract enclave |
| **REPL** | |
| [`near repl`](#near-repl) | launches an interactive connection to the NEAR blockchain ([REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop)) |
| | can also run a JS/TS file which exports an async main function that takes a [context](./context/index.d.ts) object |
| | can also run a JS/TS file which exports an async main function that takes a [context](./context/index.d.ts) object |

[ [**OPTIONS**](#options) ]

Expand Down Expand Up @@ -1110,6 +1112,9 @@ Next validators (total: 49, seat price: 1,983,932):
- arguments: `none`
- options: `default`

### `JS Contracts Enclave`
You can use `near js <command> <args>` to be able to interact with JS enclaved contracts. Run `near js --help` for instractions.

**Example:**

```bash
Expand Down
1 change: 1 addition & 0 deletions bin/near-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ yargs // eslint-disable-line
.command(require('../commands/evm-dev-init'))
.command(require('../commands/evm-view'))
.command(require('../commands/set-x-api-key'))
.command(require('../commands/js'))
.config(config)
.alias({
'accountId': ['account_id'],
Expand Down
7 changes: 1 addition & 6 deletions commands/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const exitOnError = require('../utils/exit-on-error');
const connect = require('../utils/connect');
const inspectResponse = require('../utils/inspect-response');
const checkCredentials = require('../utils/check-credentials');
const chalk = require('chalk');
const handleExceededThePrepaidGasError = require('../utils/error-handlers');

module.exports = {
command: 'call <contractName> <methodName> [args]',
Expand Down Expand Up @@ -76,8 +76,3 @@ async function scheduleFunctionCall(options) {
}
}
}

function handleExceededThePrepaidGasError(error, options) {
console.log(chalk.bold(`\nTransaction ${error.transaction_outcome.id} had ${options.gas} of attached gas but used ${error.transaction_outcome.outcome.gas_burnt} of gas`));
console.log('View this transaction in explorer:', chalk.blue(`https://explorer.${options.networkId}.near.org/transactions/${error.transaction_outcome.id}`));
}
47 changes: 1 addition & 46 deletions commands/dev-deploy.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
const {
KeyPair,
transactions,
DEFAULT_FUNCTION_CALL_GAS,
utils,
} = require('near-api-js');
const exitOnError = require('../utils/exit-on-error');
const connect = require('../utils/connect');
const { readFile, writeFile, mkdir } = require('fs').promises;
const createDevAccountIfNeeded = require('../utils/create-dev-account');
const { readFileSync } = require('fs');
const { existsSync } = require('fs');

const { PROJECT_KEY_DIR } = require('../middleware/key-store');

Expand Down Expand Up @@ -112,46 +110,3 @@ async function devDeploy(options) {
eventtracking.trackDeployedContract();
console.log(`Done deploying ${options.initFunction ? 'and initializing' : 'to'} ${accountId}`);
}

async function createDevAccountIfNeeded({ near, keyStore, networkId, init, projectKeyDirectory, masterAccount }) {
// TODO: once examples and create-near-app use the dev-account.env file, we can remove the creation of dev-account
// https://github.com/near/near-cli/issues/287
const accountFilePath = `${projectKeyDirectory}/dev-account`;
const accountFilePathEnv = `${projectKeyDirectory}/dev-account.env`;
if (!init) {
try {
// throws if either file is missing
const existingAccountId = (await readFile(accountFilePath)).toString('utf8').trim();
await readFile(accountFilePathEnv);
if (existingAccountId && await keyStore.getKey(networkId, existingAccountId)) {
return existingAccountId;
}
} catch (e) {
if (e.code === 'ENOENT') {
// Create neardev directory, new account will be created below
if (!existsSync(PROJECT_KEY_DIR)) {
await mkdir(PROJECT_KEY_DIR);
}
} else {
throw e;
}
}
}
let accountId;
// create random number with at least 14 digits
const randomNumber = Math.floor(Math.random() * (99999999999999 - 10000000000000) + 10000000000000);

if (masterAccount) {
accountId = `dev-${Date.now()}.${masterAccount}`;
} else {
accountId = `dev-${Date.now()}-${randomNumber}`;
}

const keyPair = await KeyPair.fromRandom('ed25519');
await near.accountCreator.createAccount(accountId, keyPair.publicKey);
await keyStore.setKey(networkId, accountId, keyPair);
await writeFile(accountFilePath, accountId);
// write file to be used by env-cmd
await writeFile(accountFilePathEnv, `CONTRACT_NAME=${accountId}`);
return accountId;
}
Loading

0 comments on commit 8fe4a1f

Please sign in to comment.