Skip to content

Commit

Permalink
Update hello.wasm project to use staging build tools and libraries (#…
Browse files Browse the repository at this point in the history
…1197)

* Update hello.wasm project to use staging build tools and libraries

* Yarn lock update (#1198)

* Change to match API changes in near-runtime-ts

* Update hello contract with latest runtime API changes
  • Loading branch information
vgrichina authored Aug 27, 2019
1 parent ca24f6c commit f574c10
Show file tree
Hide file tree
Showing 6 changed files with 3,761 additions and 57 deletions.
Binary file modified tests/hello.wasm
Binary file not shown.
58 changes: 30 additions & 28 deletions tests/hello/assembly/main.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
import { context, storage, ContractPromise, ContractPromiseResult, near } from "./near";
// @nearfile
import { context, storage, ContractPromise, near, logging } from "near-runtime-ts";

import { PromiseArgs, InputPromiseArgs, MyCallbackResult, MyContractPromiseResult } from "./model.near";
import { PromiseArgs, InputPromiseArgs, MyCallbackResult, MyContractPromiseResult } from "./model";

import { u128 } from "./bignum/integer/u128";
import { u128 } from "bignum";

export function hello(name: string): string {

return "hello " + name;
}

export function setKeyValue(key: string, value: string): void {
storage.setItem(key, value);
storage.setString(key, value);
}

export function getValueByKey(key: string): string {
return storage.getItem(key);
return storage.getString(key)!;
}

export function setValue(value: string): string {
storage.setItem("name", value);
storage.setString("name", value);
return value;
}

export function getValue(): string {
return storage.getItem("name");
return storage.getString("name")!;
}

export function getAllKeys(): string[] {
Expand All @@ -36,7 +37,7 @@ export function getAllKeys(): string[] {
export function benchmark(): string[] {
let i = 0;
while (i < 10) {
storage.setItem(i.toString(), "123123");
storage.setString(i.toString(), "123123");
i += 1;
}
return storage.keys("");
Expand All @@ -45,13 +46,13 @@ export function benchmark(): string[] {
export function benchmark_storage(n: i32): string {
let i = 0;
while (i < n) {
storage.setItem(i.toString(), i.toString());
storage.setString(i.toString(), i.toString());
i += 1;
}
i = 0;
let sum: u64 = 0;
while (i < n) {
let item = I32.parseInt(storage.getItem(i.toString()));
let item = I32.parseInt(storage.getString(i.toString()));
sum += item;
i += 1;
}
Expand All @@ -62,10 +63,10 @@ export function limited_storage(max_storage: u64): string {
let i = 0;
while (context.storageUsage <= max_storage) {
i += 1;
storage.setItem(i.toString(), i.toString());
storage.setString(i.toString(), i.toString());
}
if (context.storageUsage > max_storage) {
storage.removeItem(i.toString());
storage.delete(i.toString());
}
return i.toString()
}
Expand All @@ -81,26 +82,26 @@ export function benchmark_sum_n(n: i32): string {
}

export function generateLogs(): void {
storage.setItem("item", "value");
near.log("log1");
near.log("log2");
storage.setString("item", "value");
logging.log("log1");
logging.log("log2");
}

export function returnHiWithLogs(): string {
near.log("loooog1");
near.log("loooog2");
logging.log("loooog1");
logging.log("loooog2");
return "Hi"
}

export function triggerAssert(): void {
near.log("log before assert");
logging.log("log before assert");
assert(false, "expected to fail");
}

export function testSetRemove(value: string): void {
storage.setItem("test", value);
storage.removeItem("test");
assert(storage.getItem("test") == null, "Item must be empty");
storage.setString("test", value);
storage.delete("test");
assert(storage.getString("test") == null, "Item must be empty");
}

function buildString(n: i32): string {
Expand All @@ -118,14 +119,14 @@ function buildString(n: i32): string {
export function insertStrings(from: i32, to: i32): void {
let str = buildString(to);
for (let i = from; i < to; i++) {
storage.setItem(str.substr(to - i) + "b", "x");
storage.setString(str.substr(to - i) + "b", "x");
}
}

export function deleteStrings(from: i32, to: i32): void {
let str = buildString(to);
for (let i = to - 1; i >= from; i--) {
storage.removeItem(str.substr(to - i) + "b");
storage.delete(str.substr(to - i) + "b");
}
}

Expand All @@ -144,14 +145,16 @@ export function callPromise(args: PromiseArgs): void {
let promise = ContractPromise.create(
args.receiver,
args.methodName,
inputArgs.encode(),
inputArgs.encode().serialize(),
new u128(args.balance));
if (args.callback) {
inputArgs.args = args.callbackArgs;
let callbackBalance = args.callbackBalance as u64;

promise = promise.then(
args.receiver,
args.callback,
inputArgs.encode(),
inputArgs.encode().serialize(),
new u128(callbackBalance));
}
promise.returnAsResult();
Expand All @@ -162,7 +165,7 @@ export function callbackWithName(args: PromiseArgs): MyCallbackResult {
let allRes = Array.create<MyContractPromiseResult>(contractResults.length);
for (let i = 0; i < contractResults.length; ++i) {
allRes[i] = new MyContractPromiseResult();
allRes[i].ok = contractResults[i].success;
allRes[i].ok = (contractResults[i].status == 1);
if (allRes[i].ok && contractResults[i].buffer != null && contractResults[i].buffer.length > 0) {
allRes[i].r = MyCallbackResult.decode(contractResults[i].buffer);
}
Expand All @@ -171,12 +174,11 @@ export function callbackWithName(args: PromiseArgs): MyCallbackResult {
rs: allRes,
n: context.contractName,
};
let bytes = result.encode();
let bytes = result.encode().serialize();
storage.setBytes("lastResult", bytes);
return result;
}

export function getLastResult(): MyCallbackResult {
return MyCallbackResult.decode(storage.getBytes("lastResult"));
}

1 change: 1 addition & 0 deletions tests/hello/assembly/model.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @nearfile
export class PromiseArgs {
receiver: string;
methodName: string;
Expand Down
29 changes: 5 additions & 24 deletions tests/hello/gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
const gulp = require("gulp");
const nearUtils = require("near-shell/gulp-utils");

gulp.task("build:model", callback => {
nearUtils.generateBindings("model.ts", "../out/model.near.ts", callback);
});
function build_wasm(done){
nearUtils.compile("./assembly/main.ts", "./out/main.wasm", done);
};

gulp.task("build:bindings", ["build:model"], callback => {
nearUtils.generateBindings("main.ts", "../out/main.near.ts", callback);
});
const build = gulp.series(build_wasm);

gulp.task("build", ["build:bindings"], callback => {
nearUtils.compile("../out/main.near.ts", "../out/main.wasm", callback);
});

gulp.task("default", ["build"]);

// TODO: Extract all following boilerplate into library

// 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,
});
});
exports.default = build;
10 changes: 5 additions & 5 deletions tests/hello/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"name": "hello-wasm",
"version": "0.1.0",
"dependencies": {
"assemblyscript-json": "github:nearprotocol/assemblyscript-json",
"near-runtime-ts": "github:nearprotocol/near-runtime-ts"
"assemblyscript-json": "github:nearprotocol/assemblyscript-json#staging",
"near-runtime-ts": "github:nearprotocol/near-runtime-ts#staging"
},
"scripts": {
"build": "mkdir -p ./out && near build && cp ./out/main.wasm ../hello.wasm"
},
"devDependencies": {
"gulp": "^3.0.0",
"near-shell": "github:nearprotocol/near-shell",
"bignum": "github:MaxGraey/bignum.wasm"
"bignum": "github:MaxGraey/bignum.wasm",
"gulp": "^4.0.2",
"near-shell": "github:nearprotocol/near-shell#staging"
}
}
Loading

0 comments on commit f574c10

Please sign in to comment.