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

Commit

Permalink
Merge pull request #131 from nearprotocol/staging-merge
Browse files Browse the repository at this point in the history
Merge staging changes into master
  • Loading branch information
janedegtiareva authored Sep 4, 2019
2 parents 9b34d22 + 2c93ce0 commit 7c7c600
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 2,597 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package-lock.json linguist-generated=true -diff
yarn.lock linguist-generated=true -diff
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
node_modules
tmp-project
neardev/*
package-lock.json
yarn.lock
2 changes: 2 additions & 0 deletions blank_project/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package-lock.json linguist-generated=true -diff
yarn.lock linguist-generated=true -diff
8 changes: 5 additions & 3 deletions blank_project/assembly/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { context, storage, near } from "./near";
//@nearfile
import { near, context, storage, logging } from "near-runtime-ts";
import { Greeter } from "./model";

// --- contract code goes below

Expand All @@ -15,7 +17,7 @@ export function sayHi(): void {
// Logs are not persistently stored on the blockchain, but produced by the blockchain runtime.
// It's helpful to use logs for debugging your functions or when you need to get some info
// from the change methods (since change methods don't return values to the front-end).
near.log(sender + " says \"Hi!\"");
logging.log(sender + " says \"Hello mate!\"");
// storage is a helper class that allows contracts to modify the persistent state
// and read from it. setString allows you to persitently store a string value for a given string key.
// We'll store the last sender of this contract who called this method.
Expand All @@ -24,7 +26,7 @@ export function sayHi(): void {

// This is our view method. It returns the last account_id of a sender who called `sayHi`.
// It reads value from the persistent store under the key "last_sender" and returns it.
export function whoSaidHi(): string {
export function whoSaidHi(): string | null {
// getString returns a string value for a given string key.
return storage.getString(LAST_SENDER_KEY);
}
1 change: 1 addition & 0 deletions blank_project/assembly/model.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@nearfile
// Basic data model
export class Greeter {
text: string;
Expand Down
13 changes: 2 additions & 11 deletions blank_project/gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
const gulp = require("gulp");
const nearUtils = require("near-shell/gulp-utils");


function build_bindings(done){
nearUtils.generateBindings("main.ts", "../out/main.near.ts", done);
}

function build_model(done){
nearUtils.generateBindings("model.ts", "../out/model.near.ts", done);
}

function build_wasm(done){
nearUtils.compile("../out/main.near.ts", "../out/main.wasm", done);
nearUtils.compile("./assembly/main.ts", "./out/main.wasm", done);
};

const build = gulp.series(build_model, build_bindings, build_wasm);
const build = gulp.series(build_wasm);


exports.default = build;
1 change: 1 addition & 0 deletions blank_project/neardev/shared-test-staging/test.near.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"account_id":"test.near","private_key":"ed25519:2wyRcSwSuHtRVmkMCGjPwnzZmQLeXLzLLyED1NDMt4BjnKgQL6tF85yBx6Jr26D2dUNeC716RBoTxntVHsegogYw"}
8 changes: 3 additions & 5 deletions blank_project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "",
"version": "0.0.1",
"scripts": {
"build": "mkdir -p out/ && gulp",
"build": "gulp",
"deploy:contract": "near deploy",
"deploy:pages": "gh-pages -d src",
"deploy": "npm run build && npm run deploy:contract && npm run deploy:pages",
Expand All @@ -12,12 +12,10 @@
"test": "npm run build && jest test --env=near-shell/test_environment"
},
"devDependencies": {
"assemblyscript-json": "github:nearprotocol/assemblyscript-json",
"bignum": "github:MaxGraey/bignum.wasm",
"gh-pages": "^2.0.1",
"gh-pages": "^2.1.1",
"gulp": "^4.0.2",
"jest": "^24.8.0",
"jest-environment-node": "^24.5.0",
"jest-environment-node": "^24.8.0",
"near-runtime-ts": "github:nearprotocol/near-runtime-ts",
"near-shell": "github:nearprotocol/near-shell"
},
Expand Down
17 changes: 17 additions & 0 deletions blank_project/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

function getConfig(env) {
switch (env) {

case 'production':
case 'development':
return {
Expand All @@ -13,6 +14,14 @@
walletUrl: 'https://wallet.nearprotocol.com',
initialBalance: 100000000,
};
case 'staging':
return {
networkId: 'staging',
nodeUrl: 'https://staging-rpc.nearprotocol.com/',
contractName: CONTRACT_NAME,
walletUrl: 'https://near-wallet-staging.onrender.com',
initialBalance: 100000000,
};
case 'local':
return {
networkId: 'local',
Expand All @@ -39,6 +48,14 @@
masterAccount: 'test.near',
initialBalance: 100000000,
};
case 'ci-staging':
return {
networkId: 'shared-test-staging',
nodeUrl: 'http://staging-shared-test.nearprotocol.com:3030',
contractName: CONTRACT_NAME,
masterAccount: 'test.near',
initialBalance: 100000000,
};
default:
throw Error(`Unconfigured environment '${env}'. Can be configured in src/config.js.`);
}
Expand Down
71 changes: 30 additions & 41 deletions gulp-utils.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
// 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([
inputFile,
"--baseDir", "assembly",
"--nearFile", outputFile,
"--measure"
], callback);
}
var path = require("path");

function compile(inputFile, outputFile, callback) {
const asc = getAsc();
asc.main([
inputFile,
// TODO: Optimiziation is very slow, enable it only conditionally for "prod" builds?
"-O1",
"--baseDir", "assembly",
"--baseDir", process.cwd(),
"--binaryFile", outputFile,
"--sourceMap",
"--textFile",outputFile.substring(0,outputFile.lastIndexOf("."))+ ".wat",
"--measure",
"--runtime", "stub"
], callback);
Expand All @@ -30,9 +21,25 @@ function getAsc() {
}

asc = require("assemblyscript/bin/asc");

const fs = require("fs");
const pathModule = require("path");

// Create parent directories if they don't exist
function mkdirp(path){
let dirname = pathModule.dirname(path);
let paths = []
while (!fs.existsSync(dirname)){
paths.unshift(pathModule.basename(dirname));
dirname = pathModule.dirname(dirname);
}
if (paths.length > 0){
for (const i in paths){
fs.mkdirSync(pathModule.join(dirname, ...paths.slice(0,i + 1)))
}
}
}

asc.main = (main => (args, options, fn) => {
if (typeof options === "function") {
fn = options;
Expand All @@ -44,46 +51,28 @@ function getAsc() {
stdout: process.stdout || asc.createMemoryStream(logLn),
stderr: process.stderr || asc.createMemoryStream(logLn),
readFile: (filename, baseDir) => {
baseDir = pathModule.relative(process.cwd(), baseDir);
let path = pathModule.join(baseDir, filename);
if (path.startsWith("out/") && path.indexOf(".near.ts") == -1) {
path = path.replace(/^out/, baseDir );
} else if (path.startsWith(baseDir) && path.indexOf(".near.ts") != -1) {
path = path.replace(new RegExp("^" + baseDir), "out");
}

if (!fs.existsSync(path)) {
// TODO: Try node_modules instead of fixed hardcode
const mapping = {
"assembly/near.ts" : "./node_modules/near-runtime-ts/near.ts",
"assembly/json/encoder.ts" : "./node_modules/assemblyscript-json/assembly/encoder.ts",
"assembly/json/decoder.ts" : "./node_modules/assemblyscript-json/assembly/decoder.ts",
"bignum/integer/u128.ts" : "./node_modules/bignum/assembly/integer/u128.ts",
};
if (path in mapping) {
path = mapping[path]
} else if (path.startsWith("assembly/node_modules/bignum/assembly")) {
// TODO: resolve two ways of importing bignum due to need to test near-runtime-ts separately
path = path.replace("assembly", ".");
} else if (path.startsWith("assembly/bignum")) {
path = path.replace("assembly/bignum", "./node_modules/bignum/assembly");
}
}

if (!fs.existsSync(path)) {
return null;
}

return fs.readFileSync(path).toString("utf8");
},
writeFile: (filename, contents) => {
const name = filename.startsWith("../") ? filename.substring(3) : filename;
mkdirp(name);
fs.writeFileSync(name, contents);
},
listFiles: () => []
listFiles: (dirname, baseDir) => {
try {
return fs.readdirSync(path.join(baseDir, dirname)).filter(file => /^(?!.*\.d\.ts$).*\.ts$/.test(file));
} catch (e) {
return null;
}
}
}, fn);
})(asc.main);
return asc;
}

module.exports = { generateBindings, compile };

module.exports = { compile };
17 changes: 12 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ exports.deploy = async function(options) {
`Starting deployment. Account id: ${options.accountId}, node: ${options.nodeUrl}, helper: ${options.helperUrl}, file: ${options.wasmFile}`);
const near = await connect(options);
const contractData = [...fs.readFileSync(options.wasmFile)];
await near.deployContract(options.accountId, contractData);
const account = await near.account(options.accountId);
await account.deployContract(contractData);
};

exports.scheduleFunctionCall = async function(options) {
Expand Down Expand Up @@ -141,16 +142,22 @@ exports.login = async function(options) {
// check that the key got added
const near = await connect(options);
let account = await near.account(accountId);
let state = await account.state();
if (state.public_keys.includes(keyPair.getPublicKey())) {
let keys = await account.getAccessKeys();
let keyFound = false;
for (let i = 0; i < keys.length; i++) {
if (keys[i].public_key == keyPair.getPublicKey()) {
keyFound = true;
}
}
if (keyFound) {
const keyStore = new UnencryptedFileSystemKeyStore('./neardev');
keyStore.setKey(options.networkId, accountId, keyPair);
console.log(`Logged in with ${accountId}`);
} else {
console.log('Log in did not succeed. Please try again.')
}
} catch (_) {
console.log('Log in did not succeed. Please try again.')
} catch (e) {
console.log(e)
}
rl.close();
});
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "near-shell",
"version": "0.9.5",
"version": "0.10.0",
"description": "Command line utilities to interact with near blockchain",
"main": "index.js",
"scripts": {
Expand All @@ -27,7 +27,8 @@
"bs58": "^4.0.1",
"jest-environment-node": "^24.5.0",
"ncp": "^2.0.0",
"nearlib": "^0.11.0",
"near-runtime-ts": "github:nearprotocol/near-runtime-ts",
"nearlib": "^0.13.0",
"rimraf": "^2.6.3",
"yargs": "^13.2.1"
},
Expand Down
Loading

0 comments on commit 7c7c600

Please sign in to comment.