-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
315 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Greeter example in AssemblyScript | ||
|
||
## Description | ||
|
||
The contract implements a single function to return a greeting. | ||
|
||
## To Run | ||
|
||
|
||
## To Test | ||
|
||
*In NEAR Studio (https://studio.nearprotocol.com)* | ||
|
||
``` | ||
npm install | ||
npm run-script build | ||
npm test | ||
``` | ||
|
||
## To Explore | ||
|
||
- `assembly/main.ts` for the contract code | ||
- `src/main.html` for the front-end HTML | ||
- `src/main.js` for the JavaScript front-end code and how to integrate contracts | ||
- `src/test.js` for the JS tests for the contract |
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,18 @@ | ||
import "allocator/arena"; | ||
export { memory }; | ||
|
||
import { context, storage, near } from "./near"; | ||
|
||
import { Greeter } from "./model.near"; | ||
|
||
// --- contract code goes below | ||
|
||
// >> hello-snippet | ||
// To be able to call this function in the contract we need to export it | ||
// using `export` keyword. | ||
|
||
export function hello(): string { | ||
let greeter = new Greeter("Hello"); | ||
return greeter.greet("world"); | ||
} | ||
// << hello-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,12 @@ | ||
// 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,82 @@ | ||
const NodeEnvironment = require('jest-environment-node'); | ||
const dev = require('nearlib/dev'); | ||
const fs = require('fs'); | ||
const nearlib = require('nearlib'); | ||
|
||
|
||
class LocalTestEnvironment extends NodeEnvironment { | ||
constructor(config) { | ||
super(config); | ||
} | ||
|
||
async setup() { | ||
this.global.ArrayBuffer = ArrayBuffer; | ||
this.global.DataView = DataView; | ||
|
||
this.global.Uint8Array = Uint8Array; | ||
this.global.Uint8ClampedArray = Uint8ClampedArray; | ||
this.global.Uint16Array = Uint16Array; | ||
this.global.Uint32Array = Uint32Array; | ||
this.global.Int8Array = Int8Array; | ||
this.global.Int16Array = Int16Array; | ||
this.global.Int32Array = Int32Array; | ||
this.global.Float32Array = Float32Array; | ||
this.global.Float64Array = Float64Array; | ||
this.global.Map = Map; | ||
this.global.Set = Set; | ||
this.global.Promise = Promise; | ||
this.global.nearlib = require('nearlib'); | ||
this.global.nearlib.dev = require('nearlib/dev'); | ||
this.global.window = {}; | ||
this.global.testSettings = { | ||
contractName: "test" + Date.now(), | ||
accountId: "test" + Date.now(), | ||
nodeUrl: "http://localhost:3030", | ||
deps: { | ||
storage: this.createFakeStorage(), | ||
keyStore: new nearlib.InMemoryKeyStore(), | ||
createAccount: dev.createAccountWithLocalNodeConnection | ||
} | ||
}; | ||
const near = await dev.connect(this.global.testSettings); | ||
|
||
const keyWithRandomSeed = await nearlib.KeyPair.fromRandomSeed(); | ||
await dev.createAccountWithLocalNodeConnection(this.global.testSettings.contractName, keyWithRandomSeed.getPublicKey()); | ||
this.global.testSettings.deps.keyStore.setKey(this.global.testSettings.contractName, keyWithRandomSeed); | ||
|
||
// deploy contract | ||
const data = [...fs.readFileSync('./out/main.wasm')]; | ||
await near.waitForTransactionResult( | ||
await near.deployContract(this.global.testSettings.contractName, data)); | ||
|
||
await super.setup(); | ||
} | ||
|
||
async teardown() { | ||
await super.teardown(); | ||
} | ||
|
||
runScript(script) { | ||
return super.runScript(script); | ||
} | ||
|
||
createFakeStorage() { | ||
let store = {}; | ||
return { | ||
getItem: function(key) { | ||
return store[key]; | ||
}, | ||
setItem: function(key, value) { | ||
store[key] = value.toString(); | ||
}, | ||
clear: function() { | ||
store = {}; | ||
}, | ||
removeItem: function(key) { | ||
delete store[key]; | ||
} | ||
}; | ||
}; | ||
} | ||
|
||
module.exports = LocalTestEnvironment |
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,23 @@ | ||
{ | ||
"name": "@wasm/hello_world_ts", | ||
"description": "", | ||
"version": "1.0.0", | ||
"scripts": { | ||
"build": "node_modules/near-shell/near build", | ||
"test": "jest test --env=./local_test_environment.js" | ||
}, | ||
"devDependencies": { | ||
"assemblyscript": "github:nearprotocol/assemblyscript.git", | ||
"gulp": "^3", | ||
"assemblyscript-json": "github:nearprotocol/assemblyscript-json", | ||
"jest-environment-node": "^24.5.0", | ||
"near-runtime-ts": "github:nearprotocol/near-runtime-ts", | ||
"near-shell": "0.0.10" | ||
}, | ||
"wasmStudio": { | ||
"name": "Hello World Example", | ||
"description": "The contract implements a single function to return \"Hello, World!\" using AssemblyScript", | ||
"icon": "typescript-lang-file-icon" | ||
}, | ||
"dependencies": {} | ||
} |
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,17 @@ | ||
<!-- >> markup-snippet --> | ||
<!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"> | ||
<div class="container"> | ||
Contract says: | ||
<h1 id="contract-message"></h1> | ||
</div> | ||
<script src="https://cdn.jsdelivr.net/npm/nearlib@0.3.3/dist/nearlib.js"></script> | ||
<script src="./main.js"></script> | ||
</body> | ||
</html> | ||
<!-- << markup-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,43 @@ | ||
// Initializing contract | ||
|
||
// >> frontend-snippet | ||
|
||
async function doInitContract() { | ||
// Getting config from cookies that are provided by the NEAR Studio. | ||
const config = await nearlib.dev.getConfig(); | ||
console.log("nearConfig", config); | ||
|
||
// Initializing connection to the NEAR DevNet. | ||
window.near = await nearlib.dev.connect(); | ||
|
||
// Initializing our contract APIs by contract name and configuration. | ||
window.contract = await near.loadContract(config.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: ["hello"], | ||
// Change methods can modify the state. But you don't receive the returned value when called. | ||
changeMethods: [], | ||
// Sender is the account ID to initialize transactions. | ||
// For devnet we create accounts on demand. See other examples on how to authorize accounts. | ||
sender: nearlib.dev.myAccountId | ||
}); | ||
|
||
// Once everything is ready, we can start using contract | ||
return doWork(); | ||
} | ||
|
||
// Using initialized contract | ||
async function doWork() { | ||
// Calling method hello on the blockchain for our contract. | ||
// .hello() returns a promise which we awaiting. | ||
const message = await contract.hello(); | ||
// Displaying the message once we have it. | ||
document.getElementById('contract-message').innerText = message; | ||
} | ||
|
||
// COMMON CODE BELOW: | ||
// Loads nearlib and this contract into window scope. | ||
|
||
window.nearInitPromise = doInitContract().catch(console.error); | ||
|
||
// << frontend-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,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.dev.connect(testSettings); | ||
accountId = testSettings.accountId ? testSettings.accountId : nearlib.dev.myAccountId; | ||
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: ["hello"], | ||
// 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.hello(); | ||
expect(result).toBe("Hello, world"); | ||
}); | ||
}); | ||
}); | ||
// << 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
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