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

Commit

Permalink
Use new nearlib, added view account command
Browse files Browse the repository at this point in the history
  • Loading branch information
ilblackdragon committed Jun 8, 2019
2 parents 1af7984 + baf598a commit 121fb92
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 12,539 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ near <command>
```
### Commands:
```Bash
near create_account # create a developer account
near deploy # deploy your smart contract
near create_account <account_id> # create a developer account
near new_project [dir] # generate a new project from template
near deploy # deploy your smart contract
near call <contractName> <methodName> [args] # submits transaction, can change state, account required
near view <contractName> <methodName> [args] # cannot change state, account is contract name
```
Expand Down
41 changes: 38 additions & 3 deletions bin/near
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ const deploy = {
const scheduleFunctionCall = {
command: 'call <contractName> <methodName> [args]',
desc: 'schedule smart contract call which can modify state',
builder: (yargs) => yargs,
builder: (yargs) => yargs
.option('amount', {
desc: 'Number of tokens to attach',
type: 'string',
default: '1000000000'
}),
handler: (argv) => exitOnError(main.scheduleFunctionCall(argv))
};

Expand All @@ -41,6 +46,13 @@ const callViewFunction = {
handler: (argv) => exitOnError(main.callViewFunction(argv))
};

const sendTokens = {
command: 'send <receiver> <amount>',
desc: 'send tokens to given receiver',
builder: (yargs) => yargs,
handler: (argv) => exitOnError(main.sendTokens(argv))
};

const { spawn } = require('child_process');
const build = {
command: 'build',
Expand All @@ -60,7 +72,7 @@ const build = {
};

const createAccount = {
command: 'create_account',
command: 'create_account <accountId>',
desc: 'create a developer account',
builder: (yargs) => yargs
.option('accountId', {
Expand All @@ -71,6 +83,18 @@ const createAccount = {
handler: (argv) => exitOnError(main.createAccount(argv))
};

const viewAccount = {
command: 'state <accountId>',
desc: 'view account',
builder: (yargs) => yargs
.option('accountId', {
desc: 'Account to view',
type: 'string',
required: true
}),
handler: (argv) => exitOnError(main.viewAccount(argv))
};

const clean = {
command: 'clean',
desc: 'clean the build environment',
Expand All @@ -84,7 +108,7 @@ const clean = {
};

const newProject = {
command: 'new_project',
command: 'new_project [projectDir]',
desc: 'create a new blank project',
builder: (yargs) => yargs
.option('projectDir', {
Expand All @@ -111,6 +135,15 @@ yargs // eslint-disable-line
desc: 'NEAR contract helper URL',
type: 'string',
})
.option('keyPath', {
desc: 'Path to master account key',
type: 'string',
})
.option('homeDir', {
desc: 'Where to look for master account, default is ~/.near',
type: 'string',
default: `${process.env.HOME}/.near`,
})
.option('useDevAccount', {
desc: 'Forces use of special "developer" account (only works on local node)',
type: 'boolean',
Expand All @@ -120,10 +153,12 @@ yargs // eslint-disable-line
type: 'string',
})
.command(createAccount)
.command(viewAccount)
.command(build)
.command(deploy)
.command(scheduleFunctionCall)
.command(callViewFunction)
.command(sendTokens)
.command(clean)
.command(newProject)
.config(config)
Expand Down
2 changes: 1 addition & 1 deletion blank_project/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Contract says:
<h1 id="contract-message"></h1>
</div>
<script src="https://cdn.jsdelivr.net/npm/nearlib@0.5.4/dist/nearlib.js"></script>
<script src="https://cdn.jsdelivr.net/npm/nearlib@0.10/dist/nearlib.js"></script>
<script src="https://cdn.jsdelivr.net/npm/js-cookie@2/src/js.cookie.min.js"></script>
<script src="./config.js"></script>
<script src="./main.js"></script>
Expand Down
1 change: 1 addition & 0 deletions gulp-utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// FUTURE PEOPLE: This file is called "gulp-utils" but it's not related to the deprecated library called "gulp-utils". Don't panic.
function generateBindings(inputFile, outputFile, callback) {
const asc = getAsc();
asc.main([
Expand Down
42 changes: 25 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ ncp.limit = 16;

// TODO: Fix promisified wrappers to handle error properly

exports.newProject = async function() {
exports.newProject = async function(options) {
// Need to wait for the copy to finish, otherwise next tasks do not find files.
const projectDir = yargs.argv.projectDir;
const projectDir = options.projectDir;
const sourceDir = __dirname + "/blank_project";
console.log(`Copying files to new project directory (${projectDir}) from template source (${sourceDir}).`);
const copyDirFn = () => {
return new Promise(resolve => {
ncp (sourceDir, yargs.argv.projectDir, response => resolve(response));
ncp (sourceDir, options.projectDir, response => resolve(response));
})};
await copyDirFn();
console.log('Copying project files complete.')
Expand All @@ -33,31 +33,32 @@ exports.clean = async function() {
};

async function connect(options) {
const keyStore = new UnencryptedFileSystemKeyStore('./');
if (!options.accountId) {
// see if we only have one account in keystore and just use that.
const accountIds = await keyStore.getAccounts(options.networkId);
if (accountIds.length == 1) {
options.accountId = accountIds[0];
}
}
if (!options.accountId) {
throw new Error('Please provide account id and make sure you created an account using `near create_account`');
if (options.keyPath === undefined && options.helperUrl === undefined) {
const homeDir = options.homeDir || `${process.env.HOME}/.near`;
options.keyPath = `${homeDir}/validator_key.json`;
}
// TODO: search for key store.
const keyStore = new UnencryptedFileSystemKeyStore('./neardev');
options.deps = {
keyStore,
};

return await nearjs.connect(options);
}

exports.createAccount = async function(options) {
let near = await connect(options);
const keyPair = await KeyPair.fromRandom('ed25519');
await near.createAccount(options.accountId, keyPair.getPublicKey());
const keyStore = new UnencryptedFileSystemKeyStore('./');
keyStore.setKey(options.networkId, options.accountId, keyPair);
console.log(`Account ${options.accountId} for ${options.networkId} was created.`);
near.connection.signer.keyStore.setKey(options.networkId, options.accountId, keyPair);
console.log(`Account ${options.accountId} for network "${options.networkId}" was created.`);
}

exports.viewAccount = async function(options) {
let near = await connect(options);
let account = await near.account(options.accountId);
let state = await account.state();
console.log(`Account ${options.accountId}`);
console.log(state);
}

exports.deploy = async function(options) {
Expand All @@ -84,6 +85,13 @@ exports.scheduleFunctionCall = async function(options) {
options.contractName, options.methodName, JSON.parse(options.args || '{}'))));
};

exports.sendTokens = async function(options) {
console.log(`Sending ${options.amount} NEAR to ${options.receiver}`);
const near = await connect(options);
await near.waitForTransactionResult(
await near.sendTokens(options.amount, options.accountId, options.receiver));
};

exports.callViewFunction = async function(options) {
console.log(`View call: ${options.contractName}.${options.methodName}(${options.args || ''})`);
const near = await connect(options);
Expand Down
Loading

0 comments on commit 121fb92

Please sign in to comment.