Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulsalam8 authored Oct 19, 2022
0 parents commit 993de70
Show file tree
Hide file tree
Showing 10 changed files with 2,537 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/tests.yml
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build
node_modules
15 changes: 15 additions & 0 deletions README.md
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
```
64 changes: 64 additions & 0 deletions __tests__/test-template.ava.js
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);
});
8 changes: 8 additions & 0 deletions ava.config.cjs
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'],
};
6 changes: 6 additions & 0 deletions babel.config.json
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"}]
]
}
8 changes: 8 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"experimentalDecorators": true,
},
"exclude": [
"node_modules"
],
}
21 changes: 21 additions & 0 deletions package.json
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"
}
}
34 changes: 34 additions & 0 deletions src/index.js
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
}
}

Loading

0 comments on commit 993de70

Please sign in to comment.