Skip to content

Commit

Permalink
Make command-line tools work with config
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrichina committed Apr 24, 2019
1 parent 1e76806 commit 27b925c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 61 deletions.
38 changes: 18 additions & 20 deletions bin/near
Expand Up @@ -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))
};
Expand All @@ -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))
};

Expand Down Expand Up @@ -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',
Expand Down
13 changes: 3 additions & 10 deletions blank_project/src/config.js
Expand Up @@ -8,28 +8,21 @@
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:
throw Error(`Unconfigured environment '${env}'. Can be configured in src/config.js.`);
}
}

const cookieConfig = Cookies.getJSON('fiddleConfig');
const cookieConfig = typeof Cookies != 'undefined' && Cookies.getJSON('fiddleConfig');
if (typeof module !== 'undefined' && module.exports) {
module.exports = getConfig;
} else {
Expand Down
50 changes: 19 additions & 31 deletions index.js
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down

0 comments on commit 27b925c

Please sign in to comment.