diff --git a/bin/near b/bin/near index 50e69fd9..985dbf7b 100755 --- a/bin/near +++ b/bin/near @@ -16,19 +16,10 @@ const deploy = { command: 'deploy', desc: 'deploy your smart contract', builder: (yargs) => yargs - .option('nodeUrl', { - desc: 'Near node url', - type: 'string', - default: 'http://localhost:3030' - }) .option('wasmFile', { desc: 'Path to wasm file to deploy', type: 'string', default: './out/main.wasm' - }) - .option('accountId', { - desc: 'Your developer account id. If you have only one account, that one will be selected by default', - type: 'string', }), handler: (argv) => exitOnError(main.deploy(argv)) }; @@ -54,17 +45,7 @@ const build = { const createAccount = { command: 'create_account', desc: 'create a developer account', - builder: (yargs) => yargs - .option('nodeUrl', { - desc: 'Near node url', - type: 'string', - default: 'http://localhost:3030' - }) - .option('accountId', { - desc: 'Unique identifier for the new account', - type: 'string', - required: true - }), + builder: (yargs) => yargs, handler: (argv) => exitOnError(main.createDevAccount(argv)) }; @@ -92,12 +73,29 @@ const newProject = { handler: (argv) => exitOnError(main.newProject(argv)) }; +const config = require(process.cwd() + '/src/config')(process.env.NODE_ENV || 'development'); yargs // eslint-disable-line + .option('nodeUrl', { + desc: 'NEAR node URL', + type: 'string', + default: 'http://localhost:3030' + }) + .option('helperUrl', { + desc: 'NEAR contract helper URL', + type: 'string', + default: 'http://localhost:3030' + }) + .option('accountId', { + desc: 'Unique identifier for the new account', + type: 'string', + required: true + }) .command(createAccount) .command(build) .command(deploy) .command(clean) .command(newProject) + .config(config) .alias({ 'accountId': ['account_id', 'contractName', 'contract_name'], 'nodeUrl': 'node_url', diff --git a/blank_project/src/config.js b/blank_project/src/config.js index 363ee11b..8983c6ff 100644 --- a/blank_project/src/config.js +++ b/blank_project/src/config.js @@ -8,20 +8,13 @@ case 'development': return { nodeUrl: 'https://studio.nearprotocol.com/devnet', - baseUrl: 'https://studio.nearprotocol.com/contract-api', + helperUrl: 'https://studio.nearprotocol.com/contract-api', contractName: CONTRACT_NAME, - deps: { - createAccount: (accountId, publicKey) => - nearlib.dev.createAccountWithContractHelper( - { baseUrl: 'https://studio.nearprotocol.com/contract-api' }, accountId, publicKey) - } }; case 'local': case 'test': return { - deps: { - createAccount: nearlib.dev.createAccountWithLocalNodeConnection - }, + nodeUrl: 'http://localhost:8080', contractName: CONTRACT_NAME }; default: @@ -29,7 +22,7 @@ } } - const cookieConfig = Cookies.getJSON('fiddleConfig'); + const cookieConfig = typeof Cookies != 'undefined' && Cookies.getJSON('fiddleConfig'); if (typeof module !== 'undefined' && module.exports) { module.exports = getConfig; } else { diff --git a/index.js b/index.js index ecbc03c2..47a3bec7 100755 --- a/index.js +++ b/index.js @@ -33,25 +33,19 @@ exports.clean = async function() { }; // Only works for dev environments -exports.createDevAccount = async function(argv) { +exports.createDevAccount = async function(options) { const keyPair = await KeyPair.fromRandomSeed(); - const accountId = argv.accountId; - const nodeUrl = argv.nodeUrl; - const options = { - nodeUrl, - accountId, - useDevAccount: true, - deps: { - keyStore: new InMemoryKeyStore(), - storage: {}, - } + options.useDevAccount = true; + options.deps = { + keyStore: new InMemoryKeyStore(), + storage: {}, }; await neardev.connect(options); - await neardev.createAccountWithLocalNodeConnection(accountId, keyPair.getPublicKey()); + await options.deps.createAccount(options.accountId, keyPair.getPublicKey()); const keyStore = new UnencryptedFileSystemKeyStore(); - keyStore.setKey(accountId, keyPair); + keyStore.setKey(options.accountId, keyPair); }; async function deployContractAndWaitForTransaction(accountId, data, near) { @@ -60,36 +54,30 @@ async function deployContractAndWaitForTransaction(accountId, data, near) { return waitResult; } -exports.deploy = async function(argv) { +exports.deploy = async function(options) { + console.log('deploy', options); const keyStore = new UnencryptedFileSystemKeyStore(); - let accountId = argv.accountId; - if (!accountId) { + if (!options.accountId) { // see if we only have one account in keystore and just use that. const accountIds = await keyStore.getAccountIds(); if (accountIds.length == 1) { - accountId = accountIds[0]; + options.accountId = accountIds[0]; } } - if (!accountId) { - throw new Error('Please provide account id and make sure you created an account using near create_account'); + if (!options.accountId) { + throw new Error('Please provide account id and make sure you created an account using `near create_account`'); } - const nodeUrl = argv.nodeUrl; - const options = { - nodeUrl, - accountId, - deps: { - keyStore, - storage: {}, - } + options.deps = { + keyStore, + storage: {}, }; const near = await neardev.connect(options); - const contractData = [...fs.readFileSync(argv.wasmFile)]; + const contractData = [...fs.readFileSync(options.wasmFile)]; console.log( - "Starting deployment. Account id " + accountId + ", contract " + accountId + ", url " + nodeUrl, ", file " + argv.wasmFile); - const res = await deployContractAndWaitForTransaction( - accountId, contractData, near); + `Starting deployment. Account id: ${options.accountId}, node: ${options.nodeUrl}, helper: ${options.helperUrl}, file: ${options.wasmFile}`); + const res = await deployContractAndWaitForTransaction(options.accountId, contractData, near); if (res.status == "Completed") { console.log("Deployment succeeded."); } else {