Skip to content

Commit

Permalink
[new project] source for a template project
Browse files Browse the repository at this point in the history
  • Loading branch information
janedegtiareva committed Mar 21, 2019
1 parent 11a80f1 commit 2ab431a
Show file tree
Hide file tree
Showing 14 changed files with 6,669 additions and 0 deletions.
25 changes: 25 additions & 0 deletions blank_project/README.md
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
18 changes: 18 additions & 0 deletions blank_project/assembly/main.ts
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
12 changes: 12 additions & 0 deletions blank_project/assembly/model.ts
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;
}
}
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"
]
}
40 changes: 40 additions & 0 deletions blank_project/gulpfile.js
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,
});
});
82 changes: 82 additions & 0 deletions blank_project/local_test_environment.js
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

0 comments on commit 2ab431a

Please sign in to comment.