This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "delete blank project and fix issues 161, 164, 165"
This reverts commit 2c69773.
- Loading branch information
Showing
18 changed files
with
403 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "../node_modules/assemblyscript/std/assembly.json", | ||
"include": [ | ||
"./**/*.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"account_id":"test.near","private_key":"ed25519:2wyRcSwSuHtRVmkMCGjPwnzZmQLeXLzLLyED1NDMt4BjnKgQL6tF85yBx6Jr26D2dUNeC716RBoTxntVHsegogYw"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"account_id":"test.near","private_key":"ed25519:2wyRcSwSuHtRVmkMCGjPwnzZmQLeXLzLLyED1NDMt4BjnKgQL6tF85yBx6Jr26D2dUNeC716RBoTxntVHsegogYw"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Ignore everything in this directory | ||
* | ||
# Except this file | ||
!.gitignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.