├── .github ├── ISSUE_TEMPLATE │ └── BOUNTY.yml └── workflows │ └── tests.yml ├── .gitignore ├── .near-credentials └── workspaces │ └── testnet │ └── templateprojectmaster.testnet.json ├── README.md ├── __tests__ ├── test-template.ava.js └── test-testnet-dev-deploy.ava.js ├── ava.config.cjs ├── babel.config.json ├── jsconfig.json ├── package.json ├── scripts └── near_cli_deploy.sh ├── src └── index.js └── yarn.lock /.github/ISSUE_TEMPLATE/BOUNTY.yml: -------------------------------------------------------------------------------- 1 | name: "Simple Bounty" 2 | description: "Use this template to create a HEROES Simple Bounty via Github bot" 3 | title: "Bounty: " 4 | labels: ["bounty"] 5 | assignees: heroes-bot-test 6 | body: 7 | - type: markdown 8 | attributes: 9 | value: | 10 | Hi! Let's set up your bounty! Please don't change the template - @heroes-bot-test won't be able to help you. 11 | 12 | - type: dropdown 13 | id: type 14 | attributes: 15 | label: What talent are you looking for? 16 | options: 17 | - Marketing 18 | - Development 19 | - Design 20 | - Other 21 | - Content 22 | - Research 23 | - Audit 24 | 25 | - type: textarea 26 | id: description 27 | attributes: 28 | label: What you need to be done? 29 | 30 | - type: dropdown 31 | id: tags 32 | attributes: 33 | label: Tags 34 | description: Add tags that match the topic of the work 35 | multiple: true 36 | options: 37 | - API 38 | - Blockchain 39 | - Community 40 | - CSS 41 | - DAO 42 | - dApp 43 | - DeFi 44 | - Design 45 | - Documentation 46 | - HTML 47 | - Javascript 48 | - NFT 49 | - React 50 | - Rust 51 | - Smart contract 52 | - Typescript 53 | - UI/UX 54 | - web3 55 | - Translation 56 | - Illustration 57 | - Branding 58 | - Copywriting 59 | - Blogging 60 | - Editing 61 | - Video Creation 62 | - Social Media 63 | - Graphic Design 64 | - Transcription 65 | - Product Design 66 | - Artificial Intelligence 67 | - Quality Assurance 68 | - Risk Assessment 69 | - Security Audit 70 | - Bug Bounty 71 | - Code Review 72 | - Blockchain Security 73 | - Smart Contract Testing 74 | - Penetration Testing 75 | - Vulnerability Assessment 76 | - BOS 77 | - News 78 | - Hackathon 79 | - NEARCON2023 80 | - NEARWEEK 81 | 82 | - type: input 83 | id: deadline 84 | attributes: 85 | label: Deadline 86 | description: "Set a deadline for your bounty. Please enter the date in format: DD.MM.YYYY" 87 | placeholder: "19.05.2027" 88 | 89 | - type: dropdown 90 | id: currencyType 91 | attributes: 92 | label: Currency 93 | description: What is the currency you want to pay? 94 | options: 95 | - USDC.e 96 | - USDT.e 97 | - DAI 98 | - wNEAR 99 | - USDt 100 | - XP 101 | - marmaj 102 | - NEKO 103 | - JUMP 104 | - USDC 105 | - NEARVIDIA 106 | default: 0 107 | validations: 108 | required: true 109 | 110 | - type: input 111 | id: currencyAmount 112 | attributes: 113 | label: Amount 114 | description: How much it will be cost? 115 | 116 | - type: markdown 117 | attributes: 118 | value: "## Advanced settings" 119 | 120 | - type: checkboxes 121 | id: kyc 122 | attributes: 123 | label: KYC 124 | description: "Use HEROES' KYC Verification, only applicants who passed HEROES' KYC can apply and work on this bounty!" 125 | options: 126 | - label: Use KYC Verification 127 | 128 | - type: markdown 129 | attributes: 130 | value: | 131 | ### This cannot be changed once the bounty is live! 132 | -------------------------------------------------------------------------------- /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | on: 3 | pull_request: 4 | push: 5 | branches: 6 | - master 7 | - develop 8 | 9 | jobs: 10 | workflows_ubuntu: 11 | runs-on: ubuntu-latest 12 | environment: 13 | name: production 14 | steps: 15 | - uses: actions/checkout@v2 16 | - uses: actions/setup-node@v2 17 | with: 18 | node-version: "16" 19 | - name: Install modules 20 | run: yarn 21 | - name: Build contract 22 | run: yarn build 23 | - name: Set master credentials 24 | env: 25 | TESTNET_MASTER_ACCOUNT_WALLET: ${{ secrets.TESTNET_MASTER_ACCOUNT_WALLET }} 26 | run: echo "$TESTNET_MASTER_ACCOUNT_WALLET" > .near-credentials/workspaces/testnet/templateprojectmaster.testnet.json 27 | - name: Run ci tests 28 | run: yarn testci 29 | - name: Run near-cli tests 30 | run: yarn run test:clidevdeploy 31 | 32 | workflows_macos: 33 | needs: workflows_ubuntu 34 | runs-on: macos-latest 35 | steps: 36 | - uses: actions/checkout@v2 37 | - uses: actions/setup-node@v2 38 | with: 39 | node-version: "16" 40 | - name: Install modules 41 | run: yarn 42 | - name: Build contract 43 | run: yarn build 44 | - name: Set master credentials 45 | env: 46 | TESTNET_MASTER_ACCOUNT_WALLET: ${{ secrets.TESTNET_MASTER_ACCOUNT_WALLET }} 47 | run: echo "$TESTNET_MASTER_ACCOUNT_WALLET" > .near-credentials/workspaces/testnet/templateprojectmaster.testnet.json 48 | - name: Run ci tests 49 | run: yarn testci 50 | - name: Run near-cli tests 51 | run: yarn run test:clidevdeploy 52 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | node_modules 3 | .idea 4 | package-lock.json -------------------------------------------------------------------------------- /.near-credentials/workspaces/testnet/templateprojectmaster.testnet.json: -------------------------------------------------------------------------------- 1 | {"account_id":"","public_key":"","private_key":""} -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NEAR-SDK-JS template project 2 | 3 | This is a template project. It implements a counter. You can copy this folder to start writing your first contract. 4 | 5 | # Build the contract 6 | 7 | ``` 8 | npm i 9 | npm run build 10 | ``` 11 | 12 | # Run tests on local node 13 | ``` 14 | npm run test:template 15 | ``` 16 | 17 | # Run tests on testnet 18 | save 19 | ```shell 20 | echo "your testnet master account wallet" >> .near-credentials/workspaces/testnet/'$TESTNET_MASTER_ACCOUNT_ID'.json 21 | ``` 22 | 23 | ```shell 24 | TESTNET_MASTER_ACCOUNT_ID='your master account id' npm run test:testnetdeploy 25 | ``` 26 | current master account run by ci on testnet is [templateprojectmaster.testnet](https://testnet.nearblocks.io/address/templateprojectmaster.testnet) 27 | 28 | # in gitlab ci, the testnet master account's secret is reserved on git secrets: https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions 29 | 30 | # testnet deploy use near-cli 31 | This test script will create accounts with faucet to deploy and call contract 32 | * run test shell 33 | ```shell 34 | npm run test:clidevdeploy 35 | ``` -------------------------------------------------------------------------------- /__tests__/test-template.ava.js: -------------------------------------------------------------------------------- 1 | import { Worker } from 'near-workspaces'; 2 | import test from 'ava'; 3 | 4 | test.beforeEach(async t => { 5 | // Init the worker and start a Sandbox server 6 | const worker = await Worker.init(); 7 | 8 | // Prepare sandbox for tests, create accounts, deploy contracts, etc. 9 | const root = worker.rootAccount; 10 | 11 | // Deploy the counter contract. 12 | const counter = await root.createSubAccount("counter"); 13 | await counter.deploy( 14 | './build/contract.wasm' 15 | ); 16 | 17 | // Test users 18 | const ali = await root.createSubAccount('ali'); 19 | const bob = await root.createSubAccount('bob'); 20 | 21 | // Save state for test runs 22 | t.context.worker = worker; 23 | t.context.accounts = { root, counter, ali, bob }; 24 | }); 25 | 26 | // If the environment is reused, use test.after to replace test.afterEach 27 | test.afterEach(async t => { 28 | await t.context.worker.tearDown().catch(error => { 29 | console.log('Failed to tear down the worker:', error); 30 | }); 31 | }); 32 | 33 | test('Initial count is 0', async t => { 34 | const { counter } = t.context.accounts; 35 | const result = await counter.view('getCount', {}); 36 | t.is(result, 0); 37 | }); 38 | 39 | test('Increase works', async t => { 40 | const { counter, ali, bob } = t.context.accounts; 41 | await ali.call(counter, 'increase', {}); 42 | 43 | let result = await counter.view('getCount', {}); 44 | t.is(result, 1); 45 | 46 | await bob.call(counter, 'increase', { n: 4 }); 47 | result = await counter.view('getCount', {}); 48 | t.is(result, 5); 49 | }); 50 | 51 | test('Decrease works', async t => { 52 | const { counter, ali, bob } = t.context.accounts; 53 | await ali.call(counter, 'decrease', {}); 54 | 55 | let result = await counter.view('getCount', {}); 56 | t.is(result, -1); 57 | 58 | await bob.call(counter, 'decrease', { n: 4 }); 59 | result = await counter.view('getCount', {}); 60 | t.is(result, -5); 61 | }); -------------------------------------------------------------------------------- /__tests__/test-testnet-dev-deploy.ava.js: -------------------------------------------------------------------------------- 1 | import { Worker } from 'near-workspaces'; 2 | import test from 'ava'; 3 | import process from "process"; 4 | 5 | test.beforeEach(async t => { 6 | // Init the worker and start a Sandbox server 7 | const worker = await Worker.init({network: 'testnet'}); 8 | 9 | // Prepare sandbox for tests, create accounts, deploy contracts, etc. 10 | const root = worker.rootAccount; 11 | // Deploy the clean-state contract. 12 | const counter = await root.devDeploy('./build/contract.wasm', {initialBalance: "6 N"}); 13 | 14 | // Test users 15 | const ali = await root.createSubAccount('ali', {initialBalance : "1 N"}); 16 | const bob = await root.createSubAccount('bob', {initialBalance : "1 N"}); 17 | 18 | // Save state for test runs 19 | t.context.worker = worker; 20 | t.context.accounts = { root, counter, ali, bob }; 21 | }); 22 | 23 | // If the environment is reused, use test.after to replace test.afterEach 24 | test.afterEach(async t => { 25 | const { root, counter, ali, bob } = t.context.accounts; 26 | const masterAcc = process.env.TESTNET_MASTER_ACCOUNT_ID; 27 | await root.delete(masterAcc); 28 | await counter.delete(masterAcc); 29 | await ali.delete(masterAcc); 30 | await bob.delete(masterAcc); 31 | await t.context.worker.tearDown().catch(error => { 32 | console.log('Failed to tear down the worker:', error); 33 | }); 34 | }); 35 | 36 | test('Initial count is 0, Increase works, Decrease works', async t => { 37 | const { counter, ali, bob } = t.context.accounts; 38 | let result = await counter.view('getCount', {}); 39 | t.is(result, 0); 40 | 41 | await ali.call(counter, 'increase', {}); 42 | 43 | result = await counter.view('getCount', {}); 44 | t.is(result, 1); 45 | 46 | await bob.call(counter, 'increase', { n: 4 }); 47 | result = await counter.view('getCount', {}); 48 | t.is(result, 5); 49 | 50 | await ali.call(counter, 'decrease', {}); 51 | 52 | result = await counter.view('getCount', {}); 53 | t.is(result, 4); 54 | 55 | await bob.call(counter, 'decrease', { n: 5 }); 56 | result = await counter.view('getCount', {}); 57 | t.is(result, -1); 58 | }); 59 | -------------------------------------------------------------------------------- /ava.config.cjs: -------------------------------------------------------------------------------- 1 | require('util').inspect.defaultOptions.depth = 5; // Increase AVA's printing depth 2 | 3 | module.exports = { 4 | timeout: '300000', 5 | files: ['**/*.ava.js'], 6 | failWithoutAssertions: false, 7 | extensions: ['js'], 8 | }; 9 | -------------------------------------------------------------------------------- /babel.config.json: -------------------------------------------------------------------------------- 1 | { 2 | "plugins": [ 3 | "near-sdk-js/lib/cli/build-tools/near-bindgen-exporter", 4 | ["@babel/plugin-proposal-decorators", {"version": "legacy"}] 5 | ] 6 | } -------------------------------------------------------------------------------- /jsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "experimentalDecorators": true, 4 | }, 5 | "exclude": [ 6 | "node_modules" 7 | ], 8 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "template", 3 | "version": "1.0.0", 4 | "description": "Contract template with near-sdk-js", 5 | "main": "index.js", 6 | "type": "module", 7 | "scripts": { 8 | "build": "near-sdk-js build", 9 | "test": "yarn test:template && yarn test:testnetdeploy", 10 | "test:template": "ava ./__tests__/test-template.ava.js", 11 | "test:testnetdeploy": "ava ./__tests__/test-testnet-dev-deploy.ava.js", 12 | "testci": "yarn testci:template && yarn testci:testnetdeploy", 13 | "testci:template": "ava ./__tests__/test-template.ava.js", 14 | "testci:testnetdeploy": "TESTNET_MASTER_ACCOUNT_ID='templateprojectmaster.testnet' ava ./__tests__/test-testnet-dev-deploy.ava.js", 15 | "test:clidevdeploy": "bash scripts/near_cli_deploy.sh" 16 | }, 17 | "author": "Near Inc ", 18 | "license": "Apache-2.0", 19 | "dependencies": { 20 | "near-sdk-js": "^1.0.0", 21 | "lodash-es": "^4.17.21" 22 | }, 23 | "devDependencies": { 24 | "ava": "^4.2.0", 25 | "near-workspaces": "^3.3.0", 26 | "near-cli": "^4.0.13" 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /scripts/near_cli_deploy.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # generate random account 3 | uuid=$(uuidgen | tr 'A-Z' 'a-z') 4 | CONTRACT="devcontract-"${uuid:0:10}".testnet" 5 | echo $CONTRACT 6 | USER="devuser-"${uuid:0:10}".testnet" 7 | echo $USER 8 | BENEFICIARY="templateprojectmaster.testnet" 9 | 10 | near create-account $CONTRACT --useFaucet 11 | near deploy $CONTRACT ./build/contract.wasm 12 | near create-account $USER --useFaucet 13 | near view $CONTRACT getCount '' 14 | near call $CONTRACT increase '{ "n": 1 }' --accountId $USER 15 | near view $CONTRACT getCount '' 16 | near call $CONTRACT decrease '{ "n": 2 }' --accountId $USER 17 | near view $CONTRACT getCount '' 18 | 19 | echo y | near delete $CONTRACT $BENEFICIARY 20 | echo y | near delete $USER $BENEFICIARY -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import { NearBindgen, near, call, view} from 'near-sdk-js' 2 | import { isUndefined } from 'lodash-es' 3 | 4 | @NearBindgen({}) 5 | export class Counter { 6 | constructor() { 7 | this.count = 0; 8 | } 9 | 10 | @call({}) 11 | increase({ n = 1 }) { 12 | this.count += n 13 | near.log(`Counter increased to ${this.count}`) 14 | } 15 | 16 | @call({}) 17 | decrease({ n }) { 18 | // you can use default argument `n=1` too 19 | // this is to illustrate a npm dependency: lodash can be used 20 | if (isUndefined(n)) { 21 | this.count -= 1 22 | } else { 23 | this.count -= n 24 | } 25 | near.log(`Counter decreased to ${this.count}`) 26 | } 27 | 28 | @view({}) 29 | getCount() { 30 | return this.count 31 | } 32 | } -------------------------------------------------------------------------------- /yarn.lock: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/near/near-sdk-js-template-project/36fd552c2c0c702ecefc1fae6eed0a374306caa2/yarn.lock --------------------------------------------------------------------------------