generated from near/near-sdk-js-template-project
-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit 993de70
Showing
10 changed files
with
2,537 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,19 @@ | ||
name: Tests | ||
on: push | ||
jobs: | ||
workflows: | ||
strategy: | ||
matrix: | ||
platform: [ubuntu-latest, macos-latest] | ||
runs-on: ${{ matrix.platform }} | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-node@v2 | ||
with: | ||
node-version: "16" | ||
- name: Install modules | ||
run: yarn | ||
- name: Build contract | ||
run: yarn build | ||
- name: Run tests | ||
run: yarn test |
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 @@ | ||
build | ||
node_modules |
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,15 @@ | ||
# NEAR-SDK-JS template project | ||
|
||
This is a template project. It implements a counter. You can copy this folder to start writing your first contract. | ||
|
||
# Build the contract | ||
|
||
``` | ||
npm i | ||
npm run build | ||
``` | ||
|
||
# Run tests | ||
``` | ||
npm run test | ||
``` |
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,64 @@ | ||
import { Worker } from 'near-workspaces'; | ||
import test from 'ava'; | ||
|
||
test.beforeEach(async t => { | ||
// Init the worker and start a Sandbox server | ||
const worker = await Worker.init(); | ||
|
||
// Prepare sandbox for tests, create accounts, deploy contracts, etc. | ||
const root = worker.rootAccount; | ||
|
||
// Deploy the counter contract. | ||
const counter = await root.createAndDeploy( | ||
root.getSubAccount('counter').accountId, | ||
'./build/contract.wasm' | ||
); | ||
|
||
// Init the contract | ||
await counter.call(counter, 'init', {}); | ||
|
||
// Test users | ||
const ali = await root.createSubAccount('ali'); | ||
const bob = await root.createSubAccount('bob'); | ||
|
||
// Save state for test runs | ||
t.context.worker = worker; | ||
t.context.accounts = { root, counter, ali, bob }; | ||
}); | ||
|
||
// If the environment is reused, use test.after to replace test.afterEach | ||
test.afterEach(async t => { | ||
await t.context.worker.tearDown().catch(error => { | ||
console.log('Failed to tear down the worker:', error); | ||
}); | ||
}); | ||
|
||
test('Initial count is 0', async t => { | ||
const { counter } = t.context.accounts; | ||
const result = await counter.view('getCount', {}); | ||
t.is(result, 0); | ||
}); | ||
|
||
test('Increase works', async t => { | ||
const { counter, ali, bob } = t.context.accounts; | ||
await ali.call(counter, 'increase', {}); | ||
|
||
let result = await counter.view('getCount', {}); | ||
t.is(result, 1); | ||
|
||
await bob.call(counter, 'increase', { n: 4 }); | ||
result = await counter.view('getCount', {}); | ||
t.is(result, 5); | ||
}); | ||
|
||
test('Decrease works', async t => { | ||
const { counter, ali, bob } = t.context.accounts; | ||
await ali.call(counter, 'decrease', {}); | ||
|
||
let result = await counter.view('getCount', {}); | ||
t.is(result, -1); | ||
|
||
await bob.call(counter, 'decrease', { n: 4 }); | ||
result = await counter.view('getCount', {}); | ||
t.is(result, -5); | ||
}); |
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,8 @@ | ||
require('util').inspect.defaultOptions.depth = 5; // Increase AVA's printing depth | ||
|
||
module.exports = { | ||
timeout: '300000', | ||
files: ['**/*.ava.js'], | ||
failWithoutAssertions: false, | ||
extensions: ['js'], | ||
}; |
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 @@ | ||
{ | ||
"plugins": [ | ||
"near-sdk-js/lib/build-tools/near-bindgen-exporter", | ||
["@babel/plugin-proposal-decorators", {"version": "legacy"}] | ||
] | ||
} |
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,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"experimentalDecorators": true, | ||
}, | ||
"exclude": [ | ||
"node_modules" | ||
], | ||
} |
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,21 @@ | ||
{ | ||
"name": "template", | ||
"version": "1.0.0", | ||
"description": "Contract template with near-sdk-js", | ||
"main": "index.js", | ||
"type": "module", | ||
"scripts": { | ||
"build": "near-sdk-js build", | ||
"test": "ava" | ||
}, | ||
"author": "Near Inc <[email protected]>", | ||
"license": "Apache-2.0", | ||
"dependencies": { | ||
"near-sdk-js": "^0.4.0-2", | ||
"lodash-es": "^4.17.21" | ||
}, | ||
"devDependencies": { | ||
"ava": "^4.2.0", | ||
"near-workspaces": "^2.0.0" | ||
} | ||
} |
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,34 @@ | ||
import { NearContract, NearBindgen, near, call, view } from 'near-sdk-js' | ||
import { isUndefined } from 'lodash-es' | ||
|
||
@NearBindgen | ||
class Counter extends NearContract { | ||
constructor({ initial = 0 }) { | ||
super() | ||
this.count = initial | ||
} | ||
|
||
@call | ||
increase({ n = 1 }) { | ||
this.count += n | ||
near.log(`Counter increased to ${this.count}`) | ||
} | ||
|
||
@call | ||
decrease({ n }) { | ||
// you can use default argument `n=1` too | ||
// this is to illustrate a npm dependency: lodash can be used | ||
if (isUndefined(n)) { | ||
this.count -= 1 | ||
} else { | ||
this.count -= n | ||
} | ||
near.log(`Counter decreased to ${this.count}`) | ||
} | ||
|
||
@view | ||
getCount() { | ||
return this.count | ||
} | ||
} | ||
|
Oops, something went wrong.