From 7eaaba9e77b7ac4ce00b6f47f3f848655043d971 Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Fri, 9 Aug 2024 13:19:35 +0300 Subject: [PATCH 1/9] Organize example contracts into folders --- examples/package.json | 50 +++++++++---------- .../{ => basic-updates}/basic-updates-base.js | 6 +-- .../basic-updates-update.js | 4 +- .../src/{ => counter}/counter-lowlevel.js | 0 examples/src/{ => counter}/counter.js | 0 examples/src/{ => counter}/counter.ts | 0 examples/src/{ => counter}/log.ts | 0 .../cross-contract-call-loop.js | 0 .../cross-contract-call.js | 2 +- .../cross-contract-call.ts | 2 +- .../fungible-token-helper.ts | 0 .../fungible-token-lockable.js | 0 .../{ => fungible-token}/fungible-token.ts | 0 .../{standard-ft => fungible-token}/my-ft.ts | 0 .../my-nft.ts | 0 .../non-fungible-token-receiver.js | 0 .../non-fungible-token.js | 1 - .../test-approval-receiver.ts | 0 .../test-token-receiver.ts | 0 .../programmatic-update-after.ts | 0 .../programmatic-update-before.ts | 0 .../state-migration-new.ts | 0 .../state-migration-original.ts | 2 +- .../status-deserialize-class.js | 0 .../status-message-borsh.js | 0 .../status-message-collections.js | 0 .../status-message-deserialize-err.js | 0 .../status-message-migrate-add-field.js | 2 +- .../status-message-serialize-err.js | 0 .../{ => status-message}/status-message.js | 0 30 files changed, 31 insertions(+), 38 deletions(-) rename examples/src/{ => basic-updates}/basic-updates-base.js (93%) rename examples/src/{ => basic-updates}/basic-updates-update.js (92%) rename examples/src/{ => counter}/counter-lowlevel.js (100%) rename examples/src/{ => counter}/counter.js (100%) rename examples/src/{ => counter}/counter.ts (100%) rename examples/src/{ => counter}/log.ts (100%) rename examples/src/{ => cross-contract-calls}/cross-contract-call-loop.js (100%) rename examples/src/{ => cross-contract-calls}/cross-contract-call.js (94%) rename examples/src/{ => cross-contract-calls}/cross-contract-call.ts (94%) rename examples/src/{ => fungible-token}/fungible-token-helper.ts (100%) rename examples/src/{ => fungible-token}/fungible-token-lockable.js (100%) rename examples/src/{ => fungible-token}/fungible-token.ts (100%) rename examples/src/{standard-ft => fungible-token}/my-ft.ts (100%) rename examples/src/{standard-nft => non-fungible-token}/my-nft.ts (100%) rename examples/src/{ => non-fungible-token}/non-fungible-token-receiver.js (100%) rename examples/src/{ => non-fungible-token}/non-fungible-token.js (99%) rename examples/src/{standard-nft => non-fungible-token}/test-approval-receiver.ts (100%) rename examples/src/{standard-nft => non-fungible-token}/test-token-receiver.ts (100%) rename examples/src/{ => programmatic-updates}/programmatic-update-after.ts (100%) rename examples/src/{ => programmatic-updates}/programmatic-update-before.ts (100%) rename examples/src/{ => state-migration}/state-migration-new.ts (100%) rename examples/src/{ => state-migration}/state-migration-original.ts (88%) rename examples/src/{ => status-message}/status-deserialize-class.js (100%) rename examples/src/{ => status-message}/status-message-borsh.js (100%) rename examples/src/{ => status-message}/status-message-collections.js (100%) rename examples/src/{ => status-message}/status-message-deserialize-err.js (100%) rename examples/src/{ => status-message}/status-message-migrate-add-field.js (93%) rename examples/src/{ => status-message}/status-message-serialize-err.js (100%) rename examples/src/{ => status-message}/status-message.js (100%) diff --git a/examples/package.json b/examples/package.json index 17425b07a..0ccbacb58 100644 --- a/examples/package.json +++ b/examples/package.json @@ -6,36 +6,36 @@ "type": "module", "scripts": { "build": "run-s build:*", - "build:status-message": "near-sdk-js build src/status-message.js build/status-message.wasm", - "build:status-message-migrate-add-field": "near-sdk-js build src/status-message-migrate-add-field.js build/status-message-migrate-add-field.wasm", - "build:status-message-borsh": "near-sdk-js build src/status-message-borsh.js build/status-message-borsh.wasm", - "build:status-message-serialize-err": "near-sdk-js build src/status-message-serialize-err.js build/status-message-serialize-err.wasm", - "build:status-message-deserialize-err": "near-sdk-js build src/status-message-deserialize-err.js build/status-message-deserialize-err.wasm", + "build:status-message": "near-sdk-js build src/status-message/status-message.js build/status-message.wasm", + "build:status-message-migrate-add-field": "near-sdk-js build src/status-message/status-message-migrate-add-field.js build/status-message-migrate-add-field.wasm", + "build:status-message-borsh": "near-sdk-js build src/status-message/status-message-borsh.js build/status-message-borsh.wasm", + "build:status-message-serialize-err": "near-sdk-js build src/status-message/status-message-serialize-err.js build/status-message-serialize-err.wasm", + "build:status-message-deserialize-err": "near-sdk-js build src/status-message/status-message-deserialize-err.js build/status-message-deserialize-err.wasm", "build:clean-state": "near-sdk-js build src/clean-state.js build/clean-state.wasm", - "build:counter": "near-sdk-js build src/counter.js build/counter.wasm", - "build:counter-lowlevel": "near-sdk-js build src/counter-lowlevel.js build/counter-lowlevel.wasm", - "build:counter-ts": "near-sdk-js build src/counter.ts build/counter-ts.wasm", - "build:cross-contract-call": "near-sdk-js build src/status-message.js build/status-message.wasm && near-sdk-js build src/cross-contract-call.js build/cross-contract-call.wasm", - "build:cross-contract-call-ts": "near-sdk-js build src/status-message.js build/status-message.wasm && near-sdk-js build src/cross-contract-call.ts build/cross-contract-call-ts.wasm", - "build:cross-contract-call-loop": "near-sdk-js build src/counter.js build/counter.wasm && near-sdk-js build src/cross-contract-call-loop.js build/cross-contract-call-loop.wasm", - "build:fungible-token-lockable": "near-sdk-js build src/fungible-token-lockable.js build/fungible-token-lockable.wasm", - "build:fungible-token": "near-sdk-js build src/fungible-token.ts build/fungible-token.wasm && near-sdk-js build src/fungible-token-helper.ts build/fungible-token-helper.wasm", - "build:non-fungible-token": "near-sdk-js build src/non-fungible-token-receiver.js build/non-fungible-token-receiver.wasm && near-sdk-js build src/non-fungible-token.js build/non-fungible-token.wasm", - "build:status-message-collections": "near-sdk-js build src/status-message-collections.js build/status-message-collections.wasm", + "build:counter": "near-sdk-js build src/counter/counter.js build/counter.wasm", + "build:counter-lowlevel": "near-sdk-js build src/counter/counter-lowlevel.js build/counter-lowlevel.wasm", + "build:counter-ts": "near-sdk-js build src/counter/counter.ts build/counter-ts.wasm", + "build:cross-contract-call": "near-sdk-js build src/status-message/status-message.js build/status-message.wasm && near-sdk-js build src/cross-contract-calls/cross-contract-call.js build/cross-contract-call.wasm", + "build:cross-contract-call-ts": "near-sdk-js build src/status-message/status-message.js build/status-message.wasm && near-sdk-js build src/cross-contract-calls/cross-contract-call.ts build/cross-contract-call-ts.wasm", + "build:cross-contract-call-loop": "near-sdk-js build src/counter/counter.js build/counter.wasm && near-sdk-js build src/cross-contract-calls/cross-contract-call-loop.js build/cross-contract-call-loop.wasm", + "build:fungible-token-lockable": "near-sdk-js build src/fungible-token/fungible-token-lockable.js build/fungible-token-lockable.wasm", + "build:fungible-token": "near-sdk-js build src/fungible-token/fungible-token.ts build/fungible-token.wasm && near-sdk-js build src/fungible-token/fungible-token-helper.ts build/fungible-token-helper.wasm", + "build:non-fungible-token": "near-sdk-js build src/non-fungible-token/non-fungible-token-receiver.js build/non-fungible-token-receiver.wasm && near-sdk-js build src/non-fungible-token/non-fungible-token.js build/non-fungible-token.wasm", + "build:status-message-collections": "near-sdk-js build src/status-message/status-message-collections.js build/status-message-collections.wasm", "build:parking-lot": "near-sdk-js build src/parking-lot.ts build/parking-lot.wasm", - "build:programmatic-update": "near-sdk-js build src/programmatic-update-before.ts build/programmatic-update-before.wasm && near-sdk-js build src/programmatic-update-after.ts build/programmatic-update-after.wasm", + "build:programmatic-updates": "near-sdk-js build src/programmatic-updates/programmatic-update-before.ts build/programmatic-update-before.wasm && near-sdk-js build src/programmatic-updates/programmatic-update-after.ts build/programmatic-update-after.wasm", "build:nested-collections": "near-sdk-js build src/nested-collections.ts build/nested-collections.wasm", "build-nft": "run-s build:nft-*", - "build:nft-contract": "near-sdk-js build src/standard-nft/my-nft.ts build/my-nft.wasm", - "build:nft-receiver": "near-sdk-js build src/standard-nft/test-token-receiver.ts build/nft-receiver.wasm", - "build:nft-approval-receiver": "near-sdk-js build src/standard-nft/test-approval-receiver.ts build/nft-approval-receiver.wasm", - "build:ft": "near-sdk-js build src/standard-ft/my-ft.ts build/my-ft.wasm", + "build:nft-contract": "near-sdk-js build src/non-fungible-token/my-nft.ts build/my-nft.wasm", + "build:nft-receiver": "near-sdk-js build src/non-fungible-token/test-token-receiver.ts build/nft-receiver.wasm", + "build:nft-approval-receiver": "near-sdk-js build src/non-fungible-token/test-approval-receiver.ts build/nft-approval-receiver.wasm", + "build:ft": "near-sdk-js build src/fungible-token/my-ft.ts build/my-ft.wasm", "build:state-migration": "run-s build:state-migration:*", - "build:state-migration:original": "near-sdk-js build src/state-migration-original.ts build/state-migration-original.wasm", - "build:state-migration:new": "near-sdk-js build src/state-migration-new.ts build/state-migration-new.wasm", - "build:status-deserialize-class": "near-sdk-js build src/status-deserialize-class.js build/status-deserialize-class.wasm", - "build:basic-updates-base": "near-sdk-js build src/basic-updates-base.js build/basic-updates-base.wasm", - "build:basic-updates-update": "near-sdk-js build src/basic-updates-update.js build/basic-updates-update.wasm", + "build:state-migration:original": "near-sdk-js build src/state-migration/state-migration-original.ts build/state-migration-original.wasm", + "build:state-migration:new": "near-sdk-js build src/state-migration/state-migration-new.ts build/state-migration-new.wasm", + "build:status-deserialize-class": "near-sdk-js build src/status-message/status-deserialize-class.js build/status-deserialize-class.wasm", + "build:basic-updates-base": "near-sdk-js build src/basic-updates/basic-updates-base.js build/basic-updates-base.wasm", + "build:basic-updates-update": "near-sdk-js build src/basic-updates/basic-updates-update.js build/basic-updates-update.wasm", "test": "ava && pnpm test:counter-lowlevel && pnpm test:counter-ts", "test:nft": "ava __tests__/standard-nft/*", "test:ft": "ava __tests__/standard-ft/*", diff --git a/examples/src/basic-updates-base.js b/examples/src/basic-updates/basic-updates-base.js similarity index 93% rename from examples/src/basic-updates-base.js rename to examples/src/basic-updates/basic-updates-base.js index 52bb7317f..1e4173453 100644 --- a/examples/src/basic-updates-base.js +++ b/examples/src/basic-updates/basic-updates-base.js @@ -3,12 +3,8 @@ import { call, view, near, - migrate, Vector, - assert, - UnorderedMap, - LookupSet, - validateAccountId, ONE_NEAR + ONE_NEAR } from "near-sdk-js"; const POINT_ONE = ONE_NEAR / 10000n; diff --git a/examples/src/basic-updates-update.js b/examples/src/basic-updates/basic-updates-update.js similarity index 92% rename from examples/src/basic-updates-update.js rename to examples/src/basic-updates/basic-updates-update.js index d30738928..39dc7361b 100644 --- a/examples/src/basic-updates-update.js +++ b/examples/src/basic-updates/basic-updates-update.js @@ -1,6 +1,4 @@ -import {NearBindgen, call, view, near, migrate, Vector, assert, UnorderedMap, LookupSet, ONE_NEAR} from "near-sdk-js"; -import {MigrationDemo} from "../build/state-migration-new.js"; -import {Contract} from "../build/nested-collections.js"; +import {NearBindgen, call, view, near, migrate, Vector, assert, ONE_NEAR} from "near-sdk-js"; const POINT_ONE = ONE_NEAR / 10000n; diff --git a/examples/src/counter-lowlevel.js b/examples/src/counter/counter-lowlevel.js similarity index 100% rename from examples/src/counter-lowlevel.js rename to examples/src/counter/counter-lowlevel.js diff --git a/examples/src/counter.js b/examples/src/counter/counter.js similarity index 100% rename from examples/src/counter.js rename to examples/src/counter/counter.js diff --git a/examples/src/counter.ts b/examples/src/counter/counter.ts similarity index 100% rename from examples/src/counter.ts rename to examples/src/counter/counter.ts diff --git a/examples/src/log.ts b/examples/src/counter/log.ts similarity index 100% rename from examples/src/log.ts rename to examples/src/counter/log.ts diff --git a/examples/src/cross-contract-call-loop.js b/examples/src/cross-contract-calls/cross-contract-call-loop.js similarity index 100% rename from examples/src/cross-contract-call-loop.js rename to examples/src/cross-contract-calls/cross-contract-call-loop.js diff --git a/examples/src/cross-contract-call.js b/examples/src/cross-contract-calls/cross-contract-call.js similarity index 94% rename from examples/src/cross-contract-call.js rename to examples/src/cross-contract-calls/cross-contract-call.js index ca49f5078..2c761bf69 100644 --- a/examples/src/cross-contract-call.js +++ b/examples/src/cross-contract-calls/cross-contract-call.js @@ -1,4 +1,4 @@ -import { NearBindgen, call, view, initialize, near, bytes } from "near-sdk-js"; +import { NearBindgen, call, view, initialize, near } from "near-sdk-js"; @NearBindgen({ requireInit: true }) export class OnCall { diff --git a/examples/src/cross-contract-call.ts b/examples/src/cross-contract-calls/cross-contract-call.ts similarity index 94% rename from examples/src/cross-contract-call.ts rename to examples/src/cross-contract-calls/cross-contract-call.ts index 9d65938be..30fc30d53 100644 --- a/examples/src/cross-contract-call.ts +++ b/examples/src/cross-contract-calls/cross-contract-call.ts @@ -1,4 +1,4 @@ -import { NearBindgen, call, view, initialize, near, bytes } from "near-sdk-js"; +import { NearBindgen, call, view, initialize, near } from "near-sdk-js"; @NearBindgen({ requireInit: true }) export class OnCall { diff --git a/examples/src/fungible-token-helper.ts b/examples/src/fungible-token/fungible-token-helper.ts similarity index 100% rename from examples/src/fungible-token-helper.ts rename to examples/src/fungible-token/fungible-token-helper.ts diff --git a/examples/src/fungible-token-lockable.js b/examples/src/fungible-token/fungible-token-lockable.js similarity index 100% rename from examples/src/fungible-token-lockable.js rename to examples/src/fungible-token/fungible-token-lockable.js diff --git a/examples/src/fungible-token.ts b/examples/src/fungible-token/fungible-token.ts similarity index 100% rename from examples/src/fungible-token.ts rename to examples/src/fungible-token/fungible-token.ts diff --git a/examples/src/standard-ft/my-ft.ts b/examples/src/fungible-token/my-ft.ts similarity index 100% rename from examples/src/standard-ft/my-ft.ts rename to examples/src/fungible-token/my-ft.ts diff --git a/examples/src/standard-nft/my-nft.ts b/examples/src/non-fungible-token/my-nft.ts similarity index 100% rename from examples/src/standard-nft/my-nft.ts rename to examples/src/non-fungible-token/my-nft.ts diff --git a/examples/src/non-fungible-token-receiver.js b/examples/src/non-fungible-token/non-fungible-token-receiver.js similarity index 100% rename from examples/src/non-fungible-token-receiver.js rename to examples/src/non-fungible-token/non-fungible-token-receiver.js diff --git a/examples/src/non-fungible-token.js b/examples/src/non-fungible-token/non-fungible-token.js similarity index 99% rename from examples/src/non-fungible-token.js rename to examples/src/non-fungible-token/non-fungible-token.js index e18b2bd12..4028460fb 100644 --- a/examples/src/non-fungible-token.js +++ b/examples/src/non-fungible-token/non-fungible-token.js @@ -5,7 +5,6 @@ import { initialize, near, LookupMap, - bytes, assert, } from "near-sdk-js"; diff --git a/examples/src/standard-nft/test-approval-receiver.ts b/examples/src/non-fungible-token/test-approval-receiver.ts similarity index 100% rename from examples/src/standard-nft/test-approval-receiver.ts rename to examples/src/non-fungible-token/test-approval-receiver.ts diff --git a/examples/src/standard-nft/test-token-receiver.ts b/examples/src/non-fungible-token/test-token-receiver.ts similarity index 100% rename from examples/src/standard-nft/test-token-receiver.ts rename to examples/src/non-fungible-token/test-token-receiver.ts diff --git a/examples/src/programmatic-update-after.ts b/examples/src/programmatic-updates/programmatic-update-after.ts similarity index 100% rename from examples/src/programmatic-update-after.ts rename to examples/src/programmatic-updates/programmatic-update-after.ts diff --git a/examples/src/programmatic-update-before.ts b/examples/src/programmatic-updates/programmatic-update-before.ts similarity index 100% rename from examples/src/programmatic-update-before.ts rename to examples/src/programmatic-updates/programmatic-update-before.ts diff --git a/examples/src/state-migration-new.ts b/examples/src/state-migration/state-migration-new.ts similarity index 100% rename from examples/src/state-migration-new.ts rename to examples/src/state-migration/state-migration-new.ts diff --git a/examples/src/state-migration-original.ts b/examples/src/state-migration/state-migration-original.ts similarity index 88% rename from examples/src/state-migration-original.ts rename to examples/src/state-migration/state-migration-original.ts index 6ea99335a..2cc8a877e 100644 --- a/examples/src/state-migration-original.ts +++ b/examples/src/state-migration/state-migration-original.ts @@ -1,4 +1,4 @@ -import { NearBindgen, view, near, migrate, call, Vector, assert } from 'near-sdk-js' +import { NearBindgen, view, near, call, Vector } from 'near-sdk-js' import { AccountId } from 'near-sdk-js/lib/types' type Message = { diff --git a/examples/src/status-deserialize-class.js b/examples/src/status-message/status-deserialize-class.js similarity index 100% rename from examples/src/status-deserialize-class.js rename to examples/src/status-message/status-deserialize-class.js diff --git a/examples/src/status-message-borsh.js b/examples/src/status-message/status-message-borsh.js similarity index 100% rename from examples/src/status-message-borsh.js rename to examples/src/status-message/status-message-borsh.js diff --git a/examples/src/status-message-collections.js b/examples/src/status-message/status-message-collections.js similarity index 100% rename from examples/src/status-message-collections.js rename to examples/src/status-message/status-message-collections.js diff --git a/examples/src/status-message-deserialize-err.js b/examples/src/status-message/status-message-deserialize-err.js similarity index 100% rename from examples/src/status-message-deserialize-err.js rename to examples/src/status-message/status-message-deserialize-err.js diff --git a/examples/src/status-message-migrate-add-field.js b/examples/src/status-message/status-message-migrate-add-field.js similarity index 93% rename from examples/src/status-message-migrate-add-field.js rename to examples/src/status-message/status-message-migrate-add-field.js index 2d3912205..7d9e225c9 100644 --- a/examples/src/status-message-migrate-add-field.js +++ b/examples/src/status-message/status-message-migrate-add-field.js @@ -1,4 +1,4 @@ -import {NearBindgen, call, view, near, migrate, Vector, assert} from "near-sdk-js"; +import {NearBindgen, call, view, near, migrate, assert} from "near-sdk-js"; class OldStatusMessage { constructor() { diff --git a/examples/src/status-message-serialize-err.js b/examples/src/status-message/status-message-serialize-err.js similarity index 100% rename from examples/src/status-message-serialize-err.js rename to examples/src/status-message/status-message-serialize-err.js diff --git a/examples/src/status-message.js b/examples/src/status-message/status-message.js similarity index 100% rename from examples/src/status-message.js rename to examples/src/status-message/status-message.js From 1651621cbf4ff39ab3fc4a153f8a716008d8655b Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Fri, 9 Aug 2024 15:29:17 +0300 Subject: [PATCH 2/9] Add examples README.md --- examples/src/README.md | 47 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 examples/src/README.md diff --git a/examples/src/README.md b/examples/src/README.md new file mode 100644 index 000000000..b1386c11d --- /dev/null +++ b/examples/src/README.md @@ -0,0 +1,47 @@ +# Example Smart Contracts + +This directory contains example smart contracts that demonstrate various functionalities using the `near-sdk-js`. These examples are meant to serve as a starting point for developers looking to build smart contracts on the NEAR blockchain using JavaScript/TypeScript. + +## Overview + +The example smart contracts provided here showcase different use cases and features of the NEAR blockchain. Each contract is written in JavaScript/TypeScript and demonstrates best practices for developing secure, efficient, and scalable smart contracts. + +## Build and Test + +To build and test the smart contracts in this directory, follow the steps below: + +### Build + +To build all the smart contracts, run the following command in the root of the project: + +``` +pnpm build +``` + +### Test + +To run the tests for the example smart contracts, use the following command: + +``` +pnpm test +``` + +This command will execute the test suites associated with each smart contract, ensuring that they function as expected. + +## List of Example Smart Contracts + +Below is a list of the smart contracts included in this directory + +- `basic-updates` - A simple smart contract that makes state updates. +- `counter` - A simple smart contract that demonstrates state management by implementing a basic counter with increment and decrement functionalities. +- `cross-contract` - A smart contract that demonstrates how to perform cross-contract calls on the NEAR blockchain. This contract manages the assignment of a person on call by querying an external status message contract to check the availability of a given account. +- `fungible-token` - Example implementation of a Fungible Token contract which uses [near-contract-standards](https://github.com/near/near-sdk-js/tree/develop/packages/near-contract-standards) including `storage_management`. +- `non-fungible-token` - Example implementation of a NFT(Non-Fungible Token) contract which uses [near-contract-standards](https://github.com/near/near-sdk-js/tree/develop/packages/near-contract-standards). +- `programmatic-updates` - The `programmatic-update-after.js` contract is the updated version that follows `programmatic-update-before.js`. Contracts showcasing how contracts can evolve while maintaining certain logic from previous versions. +- `state-migration` - A smart contract that demonstrates how to handle state migration in a NEAR smart contract. This contract shows how to upgrade the structure of stored data while preserving the integrity of existing data. +- `status-message` - Smart Contracts that record the status messages of the accounts that call the contracts. + +## Contributing + +If you find any issues or have suggestions for improvement, feel free to open an issue or submit a pull request. Contributions are welcome! + From 9f91f5d81ffb68d985036d01fbfd1ce4cd4b1133 Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Mon, 12 Aug 2024 11:07:20 +0300 Subject: [PATCH 3/9] Update README files. --- README.md | 5 +++++ examples/src/README.md | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1a5879d86..6917c3f53 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ See more in the [Anatomy of a Contract](https://docs.near.org/build/smart-contra ## Documentation +- [Learn how to use](https://github.com/near/create-near-app/tree/master/templates) - [Learn by example with AgorApp](https://agorapp.dev/catalog/all?difficulty=&chains=near) - [Learn by example with NEAR Docs](https://docs.near.org/build/smart-contracts/quickstart) - Check our [detailed examples and tutorials](https://docs.near.org/tutorials/welcome) @@ -54,6 +55,10 @@ See more in the [Anatomy of a Contract](https://docs.near.org/build/smart-contra - node >=14 <16.6.0 || >16.6.0 - pnpm >=7 +- NEAR CLI +``` +npm install -g near-cli +``` ## Quick Start diff --git a/examples/src/README.md b/examples/src/README.md index b1386c11d..268237eac 100644 --- a/examples/src/README.md +++ b/examples/src/README.md @@ -32,7 +32,7 @@ This command will execute the test suites associated with each smart contract, e Below is a list of the smart contracts included in this directory -- `basic-updates` - A simple smart contract that makes state updates. +- `basic-updates` - A simple smart contract that can make basic state updates. - `counter` - A simple smart contract that demonstrates state management by implementing a basic counter with increment and decrement functionalities. - `cross-contract` - A smart contract that demonstrates how to perform cross-contract calls on the NEAR blockchain. This contract manages the assignment of a person on call by querying an external status message contract to check the availability of a given account. - `fungible-token` - Example implementation of a Fungible Token contract which uses [near-contract-standards](https://github.com/near/near-sdk-js/tree/develop/packages/near-contract-standards) including `storage_management`. From 3bc56c44fd301b80879e5c7591ab365e7ceb788f Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Mon, 12 Aug 2024 11:22:58 +0300 Subject: [PATCH 4/9] fix README link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6917c3f53..136efe091 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ See more in the [Anatomy of a Contract](https://docs.near.org/build/smart-contra ## Documentation -- [Learn how to use](https://github.com/near/create-near-app/tree/master/templates) +- [Learn how to use](https://github.com/near/create-near-app/tree/master/templates/contracts/ts) - [Learn by example with AgorApp](https://agorapp.dev/catalog/all?difficulty=&chains=near) - [Learn by example with NEAR Docs](https://docs.near.org/build/smart-contracts/quickstart) - Check our [detailed examples and tutorials](https://docs.near.org/tutorials/welcome) From 7195f9a088403e33922e1a3e5f299beb2db412aa Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Mon, 12 Aug 2024 14:29:11 +0300 Subject: [PATCH 5/9] fix file structure --- examples/{src => }/README.md | 0 examples/package.json | 2 +- examples/src/{status-message => }/status-deserialize-class.js | 0 3 files changed, 1 insertion(+), 1 deletion(-) rename examples/{src => }/README.md (100%) rename examples/src/{status-message => }/status-deserialize-class.js (100%) diff --git a/examples/src/README.md b/examples/README.md similarity index 100% rename from examples/src/README.md rename to examples/README.md diff --git a/examples/package.json b/examples/package.json index 0ccbacb58..f3e822520 100644 --- a/examples/package.json +++ b/examples/package.json @@ -33,7 +33,7 @@ "build:state-migration": "run-s build:state-migration:*", "build:state-migration:original": "near-sdk-js build src/state-migration/state-migration-original.ts build/state-migration-original.wasm", "build:state-migration:new": "near-sdk-js build src/state-migration/state-migration-new.ts build/state-migration-new.wasm", - "build:status-deserialize-class": "near-sdk-js build src/status-message/status-deserialize-class.js build/status-deserialize-class.wasm", + "build:status-deserialize-class": "near-sdk-js build src/status-deserialize-class.js build/status-deserialize-class.wasm", "build:basic-updates-base": "near-sdk-js build src/basic-updates/basic-updates-base.js build/basic-updates-base.wasm", "build:basic-updates-update": "near-sdk-js build src/basic-updates/basic-updates-update.js build/basic-updates-update.wasm", "test": "ava && pnpm test:counter-lowlevel && pnpm test:counter-ts", diff --git a/examples/src/status-message/status-deserialize-class.js b/examples/src/status-deserialize-class.js similarity index 100% rename from examples/src/status-message/status-deserialize-class.js rename to examples/src/status-deserialize-class.js From 382ca451410c81186bbd6c104b361500aca71bc7 Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Mon, 12 Aug 2024 15:15:13 +0300 Subject: [PATCH 6/9] fix examples links --- near-sdk-js@2.0.0-diff-1.0.0.md | 6 ++--- .../lib/fungible_token/core_impl.d.ts | 2 +- .../lib/fungible_token/core_impl.js | 2 +- .../src/fungible_token/core_impl.ts | 2 +- packages/near-sdk-js/README.md | 26 +++++++++---------- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/near-sdk-js@2.0.0-diff-1.0.0.md b/near-sdk-js@2.0.0-diff-1.0.0.md index 04767ff4a..0efe05852 100644 --- a/near-sdk-js@2.0.0-diff-1.0.0.md +++ b/near-sdk-js@2.0.0-diff-1.0.0.md @@ -1,7 +1,7 @@ # Breaking features diff from SDK 2.0.0 to 1.0.0 ## borsh data de/serializer for contract state * using for: new contracts or migrate from a borsh serialized contract -[example](https://github.com/near/near-sdk-js/blob/develop/examples/src/status-message-borsh.js) +[example](https://github.com/near/near-sdk-js/blob/develop/examples/src/status-message/status-message-borsh.js) ```js @NearBindgen({ serializer(statusMessage) { @@ -34,7 +34,7 @@ export class StatusMessage { ## js contract migration with data fields ### example1 * using for: contract state migrations -* [example](https://github.com/near/near-sdk-js/blob/develop/examples/src/status-message-migrate-add-field.js) +* [example](https://github.com/near/near-sdk-js/blob/develop/examples/src/status-message/status-message-migrate-add-field.js) ```js import {NearBindgen, call, view, near, migrate, Vector, assert} from "near-sdk-js"; @@ -65,7 +65,7 @@ export class StatusMessage { } ``` ### example2 -* another migration example can be found in [test-basic-updates.ava.js](./examples/__tests__/test-basic-updates.ava.js) with [old state contract](./examples/src/basic-updates-base.js) and new [new state contract](./examples/src/basic-updates-update.js). +* another migration example can be found in [test-basic-updates.ava.js](./examples/__tests__/test-basic-updates.ava.js) with [old state contract](./examples/src/basic-updates/basic-updates-base.js) and new [new state contract](./examples/src/basic-updates/basic-updates-update.js). with the test command in [examples directory](./examples): ```shell pnpm run test:basic-updates diff --git a/packages/near-contract-standards/lib/fungible_token/core_impl.d.ts b/packages/near-contract-standards/lib/fungible_token/core_impl.d.ts index 8c2d108d5..289770d9e 100644 --- a/packages/near-contract-standards/lib/fungible_token/core_impl.d.ts +++ b/packages/near-contract-standards/lib/fungible_token/core_impl.d.ts @@ -11,7 +11,7 @@ import { Option } from '../non_fungible_token/utils'; * - StorageManager -- interface for NEP-145 for allocating storage per account. FungibleToken provides methods for it. * - AccountRegistrar -- interface for an account to register and unregister * - * For example usage, see examples/src/standard-ft/src/my-ft.ts + * For example usage, see examples/src/fungible-token/my-ft.ts */ export declare class FungibleToken implements FungibleTokenCore, StorageManagement, FungibleTokenResolver { accounts: LookupMap; diff --git a/packages/near-contract-standards/lib/fungible_token/core_impl.js b/packages/near-contract-standards/lib/fungible_token/core_impl.js index ff180195a..a401f1320 100644 --- a/packages/near-contract-standards/lib/fungible_token/core_impl.js +++ b/packages/near-contract-standards/lib/fungible_token/core_impl.js @@ -14,7 +14,7 @@ const ERR_TOTAL_SUPPLY_OVERFLOW = "Total supply overflow"; * - StorageManager -- interface for NEP-145 for allocating storage per account. FungibleToken provides methods for it. * - AccountRegistrar -- interface for an account to register and unregister * - * For example usage, see examples/src/standard-ft/src/my-ft.ts + * For example usage, see examples/src/fungible-token/my-ft.ts */ export class FungibleToken { constructor() { diff --git a/packages/near-contract-standards/src/fungible_token/core_impl.ts b/packages/near-contract-standards/src/fungible_token/core_impl.ts index 5d281316a..e08206eae 100644 --- a/packages/near-contract-standards/src/fungible_token/core_impl.ts +++ b/packages/near-contract-standards/src/fungible_token/core_impl.ts @@ -32,7 +32,7 @@ const ERR_TOTAL_SUPPLY_OVERFLOW: string = "Total supply overflow"; * - StorageManager -- interface for NEP-145 for allocating storage per account. FungibleToken provides methods for it. * - AccountRegistrar -- interface for an account to register and unregister * - * For example usage, see examples/src/standard-ft/src/my-ft.ts + * For example usage, see examples/src/fungible-token/my-ft.ts */ export class FungibleToken implements FungibleTokenCore, StorageManagement, FungibleTokenResolver { // AccountID -> Account balance. diff --git a/packages/near-sdk-js/README.md b/packages/near-sdk-js/README.md index ed12dcbf5..7dbf9819e 100644 --- a/packages/near-sdk-js/README.md +++ b/packages/near-sdk-js/README.md @@ -15,17 +15,17 @@ Learn more in our [Quick Start guide](https://docs.near.org/develop/quickstart-g There are a couple of contract examples in the project: - [Clean contract state](https://github.com/near/near-sdk-js/tree/develop/examples/src/clean-state.js) -- [Counter using low level API](https://github.com/near/near-sdk-js/tree/develop/examples/src/counter-lowlevel.js) -- [Counter in JavaScript](https://github.com/near/near-sdk-js/tree/develop/examples/src/counter.js) -- [Counter in TypeScript](https://github.com/near/near-sdk-js/tree/develop/examples/src/counter.ts) -- [Doing cross contract call](https://github.com/near/near-sdk-js/tree/develop/examples/src/cross-contract-call.js) -- [Fungible token](https://github.com/near/near-sdk-js/tree/develop/examples/src/fungible-token.js) -- [Lockable fungible token](https://github.com/near/near-sdk-js/tree/develop/examples/src/fungible-token-lockable.js) -- [Non fungible token](https://github.com/near/near-sdk-js/tree/develop/examples/src/non-fungible-token.js) -- [Non fungible token receiver contract](https://github.com/near/near-sdk-js/tree/develop/examples/src/non-fungible-token-receiver.js) -- [Status message board](https://github.com/near/near-sdk-js/tree/develop/examples/src/status-message.js) -- [Status message board with unique messages](https://github.com/near/near-sdk-js/tree/develop/examples/src/status-message-collections.js) -- [Programmatic Update After Locking The Contract](https://github.com/near/near-sdk-js/tree/develop/examples/src/programmatic-update.js) +- [Counter using low level API](https://github.com/near/near-sdk-js/tree/develop/examples/src/counter/counter-lowlevel.js) +- [Counter in JavaScript](https://github.com/near/near-sdk-js/tree/develop/examples/src/counter/counter.js) +- [Counter in TypeScript](https://github.com/near/near-sdk-js/tree/develop/examples/src/counter/counter.ts) +- [Doing cross contract call](https://github.com/near/near-sdk-js/tree/develop/examples/src//cross-contract-calls/cross-contract-call.js) +- [Fungible token](https://github.com/near/near-sdk-js/tree/develop/examples/src/fungible-token/fungible-token.js) +- [Lockable fungible token](https://github.com/near/near-sdk-js/tree/develop/examples/src/fungible-token/fungible-token-lockable.js) +- [Non fungible token](https://github.com/near/near-sdk-js/tree/develop/examples/src/non-fungible-token/non-fungible-token.js) +- [Non fungible token receiver contract](https://github.com/near/near-sdk-js/tree/develop/examples/src/non-fungible-token/non-fungible-token-receiver.js) +- [Status message board](https://github.com/near/near-sdk-js/tree/develop/examples/src/status-message/status-message.js) +- [Status message board with unique messages](https://github.com/near/near-sdk-js/tree/develop/examples/src/status-message/status-message-collections.js) +- [Programmatic Update After Locking The Contract](https://github.com/near/near-sdk-js/tree/develop/examples/src/programmatic-updates/programmatic-update.js) To build all examples, run `pnpm build` in `examples/`. To test all examples, run `pnpm test`. You can also build and test one specific example with `pnpm build:` and `pnpm test:`, see `examples/package.json`. @@ -214,7 +214,7 @@ Asynchronous cross-contract calls allow parallel execution of multiple contracts ### NearBindgen and other decorators -You can write a simple smart contract by only using low-level APIs, such as `near.input()`, `near.storageRead()`, etc. In this case, the API of your contract will consist of all the exported JS functions. You can find an example of such a contract [here](https://github.com/near/near-sdk-js/blob/develop/examples/src/counter-lowlevel.js). +You can write a simple smart contract by only using low-level APIs, such as `near.input()`, `near.storageRead()`, etc. In this case, the API of your contract will consist of all the exported JS functions. You can find an example of such a contract [here](https://github.com/near/near-sdk-js/blob/develop/examples/src/counter/counter-lowlevel.js). But if you want to build a more complex contracts with ease, you can use decorators from this SDK that will handle serialization, deserialization, and other boilerplate operations for you. @@ -222,7 +222,7 @@ In order to do that, your contract must be a class decorated with `@NearBindgen( Your class must have a `constructor()`. You will not be able to call it, which is why it should not accept any parameters. You must declare all the parameters that you are planning to use in the constructor and set default values. -The simplest example of the contract that follows all these rules can be found [here](https://github.com/near/near-sdk-js/blob/develop/examples/src/status-message.js) +The simplest example of the contract that follows all these rules can be found [here](https://github.com/near/near-sdk-js/blob/develop/examples/src/statys-message/status-message.js) `NearBindgen` decorator can accept `requireInit parameter`. From 3b2a6ba06d6afcf971d2769c85fd75ee20ef7871 Mon Sep 17 00:00:00 2001 From: Pavel Ivanov Date: Tue, 13 Aug 2024 14:40:42 +0300 Subject: [PATCH 7/9] improve examples/README.md --- examples/README.md | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/examples/README.md b/examples/README.md index 268237eac..02a6ce5f6 100644 --- a/examples/README.md +++ b/examples/README.md @@ -18,7 +18,7 @@ To build all the smart contracts, run the following command in the root of the p pnpm build ``` -### Test +### Test To run the tests for the example smart contracts, use the following command: @@ -30,18 +30,33 @@ This command will execute the test suites associated with each smart contract, e ## List of Example Smart Contracts -Below is a list of the smart contracts included in this directory +The following smart contracts demonstrate various capabilities of the NEAR blockchain, using JavaScript/TypeScript. +Each example highlights specific features and serves as a practical guide for developers. -- `basic-updates` - A simple smart contract that can make basic state updates. -- `counter` - A simple smart contract that demonstrates state management by implementing a basic counter with increment and decrement functionalities. -- `cross-contract` - A smart contract that demonstrates how to perform cross-contract calls on the NEAR blockchain. This contract manages the assignment of a person on call by querying an external status message contract to check the availability of a given account. -- `fungible-token` - Example implementation of a Fungible Token contract which uses [near-contract-standards](https://github.com/near/near-sdk-js/tree/develop/packages/near-contract-standards) including `storage_management`. -- `non-fungible-token` - Example implementation of a NFT(Non-Fungible Token) contract which uses [near-contract-standards](https://github.com/near/near-sdk-js/tree/develop/packages/near-contract-standards). -- `programmatic-updates` - The `programmatic-update-after.js` contract is the updated version that follows `programmatic-update-before.js`. Contracts showcasing how contracts can evolve while maintaining certain logic from previous versions. -- `state-migration` - A smart contract that demonstrates how to handle state migration in a NEAR smart contract. This contract shows how to upgrade the structure of stored data while preserving the integrity of existing data. -- `status-message` - Smart Contracts that record the status messages of the accounts that call the contracts. +**[basic-updates](https://github.com/near/near-sdk-js/tree/documentation-improvements/examples/src/basic-updates)**
+A simple smart contract that can make basic state updates. + +**[counter](https://github.com/near/near-sdk-js/tree/documentation-improvements/examples/src/counter)**
+A simple smart contract that demonstrates state management by implementing a basic counter with increment and decrement functionalities. + +**[cross-contract](https://github.com/near/near-sdk-js/tree/documentation-improvements/examples/src/cross-contract)**
+A smart contract that demonstrates how to perform cross-contract calls on the NEAR blockchain, including querying external contracts to manage on-chain interactions. + +**[fungible-token](https://github.com/near/near-sdk-js/tree/documentation-improvements/examples/src/fungible-token)**
+Example implementation of a Fungible Token (FT) contract using the [near-contract-standards](https://github.com/near/near-sdk-js/tree/develop/packages/near-contract-standards), including `storage_management`. + +**[non-fungible-token](https://github.com/near/near-sdk-js/tree/documentation-improvements/examples/src/non-fungible-token)**
+Example implementation of a Non-Fungible Token (NFT) contract using the [near-contract-standards](https://github.com/near/near-sdk-js/tree/develop/packages/near-contract-standards). + +**[programmatic-updates](https://github.com/near/near-sdk-js/tree/documentation-improvements/examples/src/programmatic-updates)**
+Contracts showcasing how contracts can evolve while maintaining certain logic from previous versions. + +**[state-migration](https://github.com/near/near-sdk-js/tree/documentation-improvements/examples/src/state-migration)**
+A smart contract that demonstrates how to handle state migration in a NEAR smart contract. + +**[status-message](https://github.com/near/near-sdk-js/tree/documentation-improvements/examples/src/status-message)**
+Smart contracts that record the status messages of the accounts that call the contracts. ## Contributing If you find any issues or have suggestions for improvement, feel free to open an issue or submit a pull request. Contributions are welcome! - From 00f9161a80ae2e860d3e781ad4e3f8a0827867a1 Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Mon, 2 Sep 2024 14:58:51 +0300 Subject: [PATCH 8/9] fix typo --- packages/near-sdk-js/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/near-sdk-js/README.md b/packages/near-sdk-js/README.md index 7dbf9819e..ed0c360d1 100644 --- a/packages/near-sdk-js/README.md +++ b/packages/near-sdk-js/README.md @@ -222,7 +222,7 @@ In order to do that, your contract must be a class decorated with `@NearBindgen( Your class must have a `constructor()`. You will not be able to call it, which is why it should not accept any parameters. You must declare all the parameters that you are planning to use in the constructor and set default values. -The simplest example of the contract that follows all these rules can be found [here](https://github.com/near/near-sdk-js/blob/develop/examples/src/statys-message/status-message.js) +The simplest example of the contract that follows all these rules can be found [here](https://github.com/near/near-sdk-js/blob/develop/examples/src/status-message/status-message.js) `NearBindgen` decorator can accept `requireInit parameter`. From d272b1dfc561fcfc2a4c8f5627c4baa5c2f2b989 Mon Sep 17 00:00:00 2001 From: Georgi Tsonev Date: Mon, 30 Sep 2024 15:41:49 +0300 Subject: [PATCH 9/9] remove NEAR CLI from Prerequisites --- README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README.md b/README.md index 136efe091..0d92f9ed0 100644 --- a/README.md +++ b/README.md @@ -55,10 +55,6 @@ See more in the [Anatomy of a Contract](https://docs.near.org/build/smart-contra - node >=14 <16.6.0 || >16.6.0 - pnpm >=7 -- NEAR CLI -``` -npm install -g near-cli -``` ## Quick Start