-
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.
[new project] source for a template project
- Loading branch information
1 parent
11a80f1
commit 2ab431a
Showing
14 changed files
with
6,669 additions
and
0 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(context.sender); | ||
} | ||
// << 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,40 @@ | ||
const gulp = require("gulp"); | ||
|
||
gulp.task("build:bindings", callback => { | ||
console.log("build:bindings impl"); | ||
const asc = require("assemblyscript/bin/asc"); | ||
asc.main([ | ||
"main.ts", | ||
"--baseDir", "assembly", | ||
"--binaryFile", "../out/main.wasm", | ||
"--nearFile", "../out/main.near.ts", | ||
"--measure" | ||
], callback); | ||
}); | ||
|
||
gulp.task("build", ["build:bindings"], callback => { | ||
console.log("build impl"); | ||
|
||
const asc = require("assemblyscript/bin/asc"); | ||
asc.main([ | ||
"../out/main.near.ts", | ||
"--baseDir", "assembly", | ||
"--binaryFile", "../out/main.wasm", | ||
"--sourceMap", | ||
"--measure" | ||
], callback); | ||
}); | ||
|
||
gulp.task("default", ["build"]); | ||
|
||
// This task is not required when running the project locally. Its purpose is to set up the | ||
// AssemblyScript compiler when a new project has been loaded in WebAssembly Studio. | ||
gulp.task("project:load", () => { | ||
const utils = require("@wasm/studio-utils"); | ||
utils.eval(utils.project.getFile("setup.js").getData(), { | ||
logLn, | ||
project, | ||
monaco, | ||
fileTypeForExtension, | ||
}); | ||
}); |
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 |
Oops, something went wrong.