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

Commit

Permalink
Revert "delete blank project and fix issues 161, 164, 165"
Browse files Browse the repository at this point in the history
This reverts commit 2c69773.
  • Loading branch information
vgrichina committed Nov 5, 2019
1 parent 9345fa8 commit ddcc320
Show file tree
Hide file tree
Showing 18 changed files with 403 additions and 89 deletions.
31 changes: 10 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,23 @@ near <command>

### Commands

For account:
```bash
near login # create a developer account
near create_account <accountId> # create a developer account with masterAccount, publicKey and initialBalance
near view <accountId> # view account state
near keys <accountId> # view account public keys
near send <sender> <receiver> <amount> # send tokens to given receiver
near stake <accountId> <stakingKey> <amount> # create staking transaction (stakingKey is base58 encoded)
near delete <accountId> <beneficiaryId> # delete an account and transfer funds to beneficiary account
```

For smart contract:
```bash
near create_account <accountId> # create a developer account
near state <accountId> # view account
near tx-status <hash> # lookup transaction status by hash
near build # build your smart contract
near deploy # deploy your smart contract
near call <contractName> <methodName> # schedule smart contract call which
[args] # can modify state
near view <contractName> <methodName> # make smart contract call which can
[args] # view state
near clean # clean the smart contract build locally(remove ./out )
```
near state <accountId> # view account
near send <receiver> <amount> # send tokens to given receiver
near clean # clean the build environment
near new_project [projectDir] # create a new blank project
near stake [accountId] [publicKey] [amount] # create staking transaction
near login # create a developer account

For transactions:
```bash
near tx-status <hash> # lookup transaction status by hash
```

### Options
Expand All @@ -59,7 +51,4 @@ For transactions:
| --helperUrl | NEAR contract helper URL | [string] | |
| --keyPath | Path to master account key | [string] | |
| --homeDir | Where to look for master account | [string] |"~/.near" |
| --accountId, --account_id | Unique identifier for the account | [string] [required]| |
| --masterAccount | Account used to create requested account. | [string] [required]| |
| --publicKey | Public key to initialize the account with | [string] [required]| |
| --initialBalance | Number of tokens to transfer to newly account | [string] [required]| |
| --accountId, --account_id | Unique identifier for the account | [string] | |
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
6 changes: 6 additions & 0 deletions blank_project/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Greeter example in AssemblyScript

## Description

The contract implements a single function to return a greeting.
For instructions on how to run this project, please follow our online tutorial https://docs.nearprotocol.com/quick-start/local-development
32 changes: 32 additions & 0 deletions blank_project/assembly/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//@nearfile
import { near, context, storage, logging } from "near-runtime-ts";
import { Greeter } from "./model";

// --- contract code goes below

// It's good to use common constant, but not required.
const LAST_SENDER_KEY = "last_sender";

// This is our change method. It modifies the state of the contract by
// storing the account_id of the sender under the key "last_sender" on the blockchain
export function sayHi(): void {
// context.sender is the account_id of the user who sent this call to the contract
// It's provided by the Blockchain runtime. For now we just store it in a local variable.
let sender = context.sender;
// `near` class contains some helper functions, e.g. logging.
// 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).
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.
storage.setString(LAST_SENDER_KEY, sender);
}

// 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 | null {
// getString returns a string value for a given string key.
return storage.getString(LAST_SENDER_KEY);
}
13 changes: 13 additions & 0 deletions blank_project/assembly/model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//@nearfile
// Basic data model
export class Greeter {
text: string;

constructor(text: string) {
this.text = text;
}

greet(userId: string): string {
return "Hello, " + userId;
}
}
6 changes: 6 additions & 0 deletions blank_project/assembly/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../node_modules/assemblyscript/std/assembly.json",
"include": [
"./**/*.ts"
]
}
11 changes: 11 additions & 0 deletions blank_project/gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const gulp = require('gulp');
const nearUtils = require('near-shell/gulp-utils');

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

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"}
1 change: 1 addition & 0 deletions blank_project/neardev/shared-test/test.near.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"account_id":"test.near","private_key":"ed25519:2wyRcSwSuHtRVmkMCGjPwnzZmQLeXLzLLyED1NDMt4BjnKgQL6tF85yBx6Jr26D2dUNeC716RBoTxntVHsegogYw"}
4 changes: 4 additions & 0 deletions blank_project/out/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
30 changes: 30 additions & 0 deletions blank_project/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "hello_world_ts",
"description": "",
"version": "0.0.1",
"scripts": {
"build": "mkdir -p out/ && gulp",
"deploy:contract": "near deploy",
"deploy:pages": "gh-pages -d src",
"deploy": "npm run build && npm run deploy:contract && npm run deploy:pages",
"prestart": "npm run build && npm run deploy:contract",
"start": "serve src",
"test": "npm run build && jest test --env=near-shell/test_environment"
},
"devDependencies": {
"gh-pages": "^2.1.1",
"gulp": "^4.0.2",
"jest": "^24.8.0",
"jest-environment-node": "^24.8.0",
"near-runtime-ts": "^0.5.1",
"near-shell": "github:nearprotocol/near-shell"
},
"wasmStudio": {
"name": "Hello World Example",
"description": "The contract implements a single function to return \"Hello, World!\" using AssemblyScript",
"icon": "typescript-lang-file-icon"
},
"dependencies": {
"serve": "^11.0.1"
}
}
File renamed without changes.
38 changes: 38 additions & 0 deletions blank_project/src/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
</head>
<body style="background: #fff; margin-top: 3em">
<div class="container">
<div class="jumbotron">
<h3><i id="who">???</i> was the last person to say "Hi!"</h3>
&nbsp;<button id="refresh-button" class="btn btn-info btn-sm">Click here to find out!</button>
</div>
<hr>
<div id="signed-out-flow" class="d-none">
<div class="row">
<button id="sign-in-button" class="btn btn-primary btn-lg">Sign-in with NEAR</button>
</div>
</div>
<div id="signed-in-flow" class="d-none">
<div class="row">
<h3>Hi, <i id="account-id"></i>!</h3>
</div>
<div class="row">
<div class="col-sm-3">
<button id="say-hi" class="btn btn-success btn-lg btn-block ">Say "Hi!"</button>
</div>
<div class="col-sm-3">
<button id="sign-out-button" class="btn btn-danger btn-lg btn-block">Sign-out</button>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/gh/nearprotocol/[email protected]/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>
</body>
</html>
94 changes: 94 additions & 0 deletions blank_project/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Initializing contract
async function initContract() {
console.log('nearConfig', nearConfig);

// Initializing connection to the NEAR DevNet.
window.near = await nearlib.connect(Object.assign({ deps: { keyStore: new nearlib.keyStores.BrowserLocalStorageKeyStore() } }, nearConfig));

// Initializing Wallet based Account. It can work with NEAR DevNet wallet that
// is hosted at https://wallet.nearprotocol.com
window.walletAccount = new nearlib.WalletAccount(window.near);

// Getting the Account ID. If unauthorized yet, it's just empty string.
window.accountId = window.walletAccount.getAccountId();

// Initializing our contract APIs by contract name and configuration.
window.contract = await near.loadContract(nearConfig.contractName, { // eslint-disable-line require-atomic-updates
// NOTE: This configuration only needed while NEAR is still in development
// View methods are read only. They don't modify the state, but usually return some value.
viewMethods: ['whoSaidHi'],
// Change methods can modify the state. But you don't receive the returned value when called.
changeMethods: ['sayHi'],
// Sender is the account ID to initialize transactions.
sender: window.accountId,
});
}

// Using initialized contract
async function doWork() {
// Setting up refresh button
document.getElementById('refresh-button').addEventListener('click', updateWhoSaidHi);

// Based on whether you've authorized, checking which flow we should go.
if (!window.walletAccount.isSignedIn()) {
signedOutFlow();
} else {
signedInFlow();
}
}

// Function that initializes the signIn button using WalletAccount
function signedOutFlow() {
// Displaying the signed out flow container.
document.getElementById('signed-out-flow').classList.remove('d-none');
// Adding an event to a sing-in button.
document.getElementById('sign-in-button').addEventListener('click', () => {
window.walletAccount.requestSignIn(
// The contract name that would be authorized to be called by the user's account.
window.nearConfig.contractName,
// This is the app name. It can be anything.
'Who was the last person to say "Hi!"?',
// We can also provide URLs to redirect on success and failure.
// The current URL is used by default.
);
});
}

// Main function for the signed-in flow (already authorized by the wallet).
function signedInFlow() {
// Displaying the signed in flow container.
document.getElementById('signed-in-flow').classList.remove('d-none');

// Displaying current account name.
document.getElementById('account-id').innerText = window.accountId;

// Adding an event to a say-hi button.
document.getElementById('say-hi').addEventListener('click', () => {
// We call say Hi and then update who said Hi last.
window.contract.sayHi().then(updateWhoSaidHi);
});

// Adding an event to a sing-out button.
document.getElementById('sign-out-button').addEventListener('click', () => {
walletAccount.signOut();
// Forcing redirect.
window.location.replace(window.location.origin + window.location.pathname);
});
}

// Function to update who said hi
function updateWhoSaidHi() {
// JavaScript tip:
// This is another example of how to use promises. Since this function is not async,
// we can't await for `contract.whoSaidHi()`, instead we attaching a callback function
// usin `.then()`.
contract.whoSaidHi().then((who) => {
// If the result doesn't have a value we fallback to the text
document.getElementById('who').innerText = who || 'Nobody (but you can be the first)';
});
}

// Loads nearlib and this contract into window scope.
window.nearInitPromise = initContract()
.then(doWork)
.catch(console.error);
41 changes: 41 additions & 0 deletions blank_project/src/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// >> tests-snippet
describe('Greeter', function() {
let near;
let contract;
let accountId;

jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;

// Common setup below
beforeAll(async function() {
if (window.testSettings === undefined) {
window.testSettings = {};
}
near = await nearlib.connect(testSettings);
accountId = testSettings.accountId;
const contractName = testSettings.contractName ?
testSettings.contractName :
(new URL(window.location.href)).searchParams.get('contractName');
contract = await near.loadContract(contractName, {
// NOTE: This configuration only needed while NEAR is still in development
// View methods are read only. They don't modify the state, but usually return some value.
viewMethods: ['whoSaidHi'],
// Change methods can modify the state. But you don't receive the returned value when called.
changeMethods: [],
sender: accountId
});
});

// Multiple tests can be described below. Search Jasmine JS for documentation.
describe('simple', function() {
beforeAll(async function() {
// There can be some common setup for each test.
});

it('get hello message', async function() {
const result = await contract.whoSaidHi();
expect(result).toBeNull();
});
});
});
// << tests-snippet
28 changes: 28 additions & 0 deletions blank_project/src/wallet/login/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
</head>
<body style="background: #fff; margin-top: 3em">
<div>Please run the following command in shell, then enter account id here. masterAccountId default: test.near
</div>
<div>
<code id="shell-command"></code>
</div>
<input type="text" id="accountId" name="accountId" placeholder="Account id"></input>
<button type="button" onClick="done()">done</button>
<script>
const currentUrl = new URL(window.location.href);
const message = `NODE_ENV=local near create_account {newAccountId} --masterAccount {masterAccountId} --publicKey ${currentUrl.searchParams.get('public_key')} --initialAmount 10000000000000000000`;
document.getElementById('shell-command').innerText = message;

function done() {
const successUrl = new URL(currentUrl.searchParams.get('success_url'));
successUrl.searchParams.set('account_id', document.getElementById('accountId').value);
successUrl.searchParams.set('public_key', currentUrl.searchParams.get('public_key'));
window.location.assign(successUrl.toString());
}
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion get-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = function getConfig() {
} catch (e) {
if (e.code == 'MODULE_NOT_FOUND') {
console.warn(`[WARNING] Didn't find config at ${configPath}, using default shell config`);
const defaultConfig = require('./config')(process.env.NODE_ENV || 'development');
const defaultConfig = require('./blank_project/src/config')(process.env.NODE_ENV || 'development');
console.log(defaultConfig);
return defaultConfig;
}
Expand Down
Loading

0 comments on commit ddcc320

Please sign in to comment.