Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added new arg base64file to support loading base64 blob from file int… #938

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,7 @@ With NEAR REPL you have complete access to [`near-api-js`](https://github.com/ne
| `--contractName` | Account name of contract [string] |
| `--masterAccount` | Master account used when creating new accounts [string] |
| `--helperAccount` | Expected top-level account for a network [string] |
| `--base64file` | Load a base64-encoded BLOB file into the args sent to a function call [string] |
| `-v, --verbose` | Prints out verbose output [boolean] [default: false] |
|`-f, --force` | Forcefully execute the desired action even if it is unsafe to do so [boolean] [default: false]|

Expand Down
20 changes: 19 additions & 1 deletion commands/call.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { readFileSync } = require('fs');
const { DEFAULT_FUNCTION_CALL_GAS, providers, utils } = require('near-api-js');
const exitOnError = require('../utils/exit-on-error');
const connect = require('../utils/connect');
Expand Down Expand Up @@ -39,6 +40,11 @@ module.exports = {
required: true,
desc: 'Unique identifier for the account that will be used to sign this call',
type: 'string'
})
.option('base64file', {
desc: 'Load a base64-encoded BLOB file into the args sent to a function call.',
type: 'string',
default: false
}),
handler: exitOnError(scheduleFunctionCall)
};
Expand All @@ -51,7 +57,19 @@ async function scheduleFunctionCall(options) {

const near = await connect(options);
const account = await near.account(options.accountId);
const parsedArgs = options.base64 ? Buffer.from(options.args, 'base64') : JSON.parse(options.args || '{}');
let parsedArgs;
// load args rare order first
if (options.base64file) {
try {
let fileargs = await readFileSync(options.base64file);
parsedArgs = Buffer.from(fileargs, 'base64');
console.log(`Loaded base64 args file, size ${parsedArgs.length}`);
} catch (e) {
console.log('Could not load base64 file!');
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this throw so as exit? Currently all errors throw and you end up with a large noisy help message, but there are plans to refactor how errors are handled.

Copy link
Author

Choose a reason for hiding this comment

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

Good call, changed!

}
}
if (!parsedArgs && options.base64) parsedArgs = Buffer.from(readFileSync(options.args), 'base64');
if (!parsedArgs) parsedArgs = JSON.parse(options.args || '{}');
console.log('Doing account.functionCall()');
try {
const functionCallResponse = await account.functionCall({
Expand Down