Skip to content

Commit

Permalink
change blank project content
Browse files Browse the repository at this point in the history
  • Loading branch information
icerove committed Oct 28, 2019
1 parent 9e0807f commit d8ed21b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 89 deletions.
42 changes: 14 additions & 28 deletions blank_project/assembly/main.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
//@nearfile
import { near, context, storage, logging } from "near-runtime-ts";
import { Greeter } from "./model";
import { logging } from "near-runtime-ts";
// available class: near, context, storage, logging, base58, base64,
// PersistentMap, PersistentVector, PersistentDeque, PersistentTopN, ContractPromise, math
import { TextMessage } from "./model";

// --- contract code goes below
const NAME = ". Welcome to NEAR Protocol chain"

// 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);
export function welcome(name: string): TextMessage {
logging.log("simple welcome test");
let message = new TextMessage()
const s = printString(NAME);
message.text = "Welcome, " + name + s;
return message;
}

// 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);
}
function printString(s: string): string {
return s;
}
16 changes: 3 additions & 13 deletions blank_project/assembly/model.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
//@nearfile
// Basic data model
export class Greeter {
text: string;

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

greet(userId: string): string {
return "Hello, " + userId;
}
}
export class TextMessage {
text: string;
}
1 change: 1 addition & 0 deletions blank_project/src/gray_near_logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 8 additions & 15 deletions blank_project/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,17 @@
</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 className="image-wrapper">
<img className="logo" src="gray_near_logo.svg" alt="NEAR logo" />
<p><span role="img" aria-label="fish">🐟</span> NEAR protocol is a new blockchain focused on developer productivity and useability!<span role="img" aria-label="fish">🐟</span></p>
<p><span role="img" aria-label="chain"></span> This little react app is connected to blockchain right now. <span role="img" aria-label="chain"></span></p>
<p id="speech"></p>
</div>
</div>
<div id="signed-in-flow" class="d-none">
<div class="row">
<h3>Hi, <i id="account-id"></i>!</h3>
<div id="signed-out-flow" class="d-none">
<button id="sign-in-button" class="btn btn-primary btn-lg">Sign-in with NEAR</button>
</div>
<div id="signed-in-flow" class="d-none">
<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>
Expand Down
34 changes: 5 additions & 29 deletions blank_project/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,16 @@ async function InitContract() {
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'],
viewMethods: ['welcome'],
// Change methods can modify the state. But you don't receive the returned value when called.
changeMethods: ['sayHi'],
changeMethods: [],
// 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();
Expand All @@ -47,9 +44,7 @@ function signedOutFlow() {
// 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.
'Welcome to NEAR'
);
});
}
Expand All @@ -59,35 +54,16 @@ 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);
});
window.contract.welcome({name:window.accountId}).then(response => document.getElementById('speech').innerText = response);

// Adding an event to a sing-out button.
// Adding an event to a sign-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)
Expand Down
6 changes: 2 additions & 4 deletions blank_react_project/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ async function InitContract() {
// Initializing our contract APIs by contract name and configuration.
window.contract = await window.near.loadContract(window.nearConfig.contractName, {
// View methods are read only. They don't modify the state, but usually return some value.
viewMethods: [
"welcome",],
viewMethods: ['welcome',],
// Change methods can modify the state. But you don't receive the returned value when called.
changeMethods: [
],
changeMethods: [],
// Sender is the account ID to initialize transactions.
sender: window.accountId
});
Expand Down

0 comments on commit d8ed21b

Please sign in to comment.