Skip to content

Commit

Permalink
fix: minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gagdiez committed Mar 12, 2024
1 parent bf6b56d commit 96a0c1d
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 78 deletions.
22 changes: 7 additions & 15 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@
**/target
**/Cargo.lock

# TypeScript - Sandbox
./contract-ts/sandbox-ts/package-lock.json
./contract-ts/sandbox-ts/node_modules/

# TypeScript
./contract-ts/build/
./contract-ts/node_modules/
./contract-ts/sandbox-ts/node_modules/
./contract-ts/sandbox-ts/yarn.lock
./contract-ts/yarn.lock
./contract-ts/package-lock.json
**/package-lock.json
**/node_modules/
**/build/
**/yarn.lock
**/.tsimp

# Frontend
./frontend/dist/
./frontend/node_modules/
./frontend/.parcel-cache
./frontend/yarn.lock
./frontend/package-lock.json
**/dist/
**/.parcel-cache
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Guest Book Contract Examples
# Guest Book Examples 📖

This repository contains example implementations of a Guest Book smart contract in both JavaScipt and Rust, and an examples of a frontend interacting with a Guest Book smart contract.
[![](https://img.shields.io/badge/⋈%20Examples-Basics-green)](https://docs.near.org/tutorials/welcome)
[![](https://img.shields.io/badge/Contract-JS-yellow)](contract-ts)
[![](https://img.shields.io/badge/Contract-Rust-red)](contract-rs)
[![](https://img.shields.io/badge/Frontend-JS-yellow)](frontend)
![example workflow](https://github.com/near-examples/guest-book-examples/actions/workflows/tests-ts.yml/badge.svg)
![example workflow](https://github.com/near-examples/guest-book-examples/actions/workflows/tests-rs.yml/badge.svg)

This repository contains example implementations of a Guest Book smart contract in both JavaScript and Rust, and an examples of a frontend interacting with a Guest Book smart contract.

## Repositories

Expand Down
File renamed without changes.
29 changes: 15 additions & 14 deletions contract-ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,48 +32,49 @@ get_messages({ fromIndex = 0, limit = 10 }: { fromIndex: number, limit: number }

<br />

## 1. Build and Deploy the Contract
You can automatically compile and deploy the contract in the NEAR testnet by running:
## 1. Build and Test the Contract
You can automatically compile and test the contract by running:

```bash
npm run deploy
npm run test
```

Once finished, check the `neardev/dev-account` file to find the address in which the contract was deployed:
<br />

## 2. Create an Account and Deploy
You can create a testnet account and deploy the contract by running:

```bash
cat ./neardev/dev-account
# e.g. dev-1659899566943-21539992274727
near create-account <your-account.testnet> --useFaucet
near deploy <your-account.testnet> build/release/hello_near.wasm
```

<br />

## 2. Retrieve the Stored Messages
## 3. Retrieve the Stored Messages
`get_messages` is a read-only method (`view` method) that returns a slice of the vector `messages`.

`View` methods can be called for **free** by anyone, even people **without a NEAR account**!

```bash
near view <dev-account> get_messages '{"from_index":0, "limit":10}'
near view <your-account.testnet> get_messages '{"from_index":0, "limit":10}'
```

<br />

## 3. Add a Message
## 4. Add a Message
`add_message` adds a message to the vector of `messages` and marks it as premium if the user attached more than `0.1 NEAR`.

`add_message` is a payable method for which can only be invoked using a NEAR account. The account needs to attach money and pay GAS for the transaction.

```bash
# Use near-cli to donate 1 NEAR
near call <dev-account> add_message '{"text": "a message"}' --amount 0.1 --accountId <account>
near call <your-account.testnet> add_message '{"text": "a message"}' --amount 0.1 --accountId <your-account.testnet>
```

**Tip:** If you would like to add a message using your own account, first login into NEAR using:
**Tip:** If you would like to add a message another account, first login into NEAR using:

```bash
# Use near-cli to login your NEAR account
near login
```

and then use the logged account to sign the transaction: `--accountId <your-account>`.
and then use the logged account to sign the transaction: `--accountId <your-account.testnet>`.
15 changes: 15 additions & 0 deletions contract-ts/ava.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require('util').inspect.defaultOptions.depth = 5; // Increase AVA's printing depth

module.exports = {
timeout: '30000',
files: ['sandbox-ts/*.ava.ts'],
failWithoutAssertions: false,
extensions: {
js: true,
ts: 'module'
},
require: ['ts-node/register', 'near-workspaces'],
"nodeArguments": [
"--import=tsimp"
]
};
18 changes: 11 additions & 7 deletions contract-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@
"license": "(MIT AND Apache-2.0)",
"type": "module",
"scripts": {
"build": "near-sdk-js build src/contract.ts build/guestbook.wasm",
"test": "$npm_execpath run build && cd sandbox-ts && $npm_execpath run test -- -- ../build/guestbook.wasm",
"postinstall": "cd sandbox-ts && $npm_execpath install"
"build": "near-sdk-js build src/contract.ts build/contract.wasm",
"test": "$npm_execpath build && ava -- ./build/contract.wasm"
},
"dependencies": {
"near-cli": "^3.5.0",
"near-cli": "^4.0.8",
"near-sdk-js": "1.0.0"
},
"devDependencies": {
"typescript": "^5.2.2",
"ts-morph": "^20.0.0"
"@ava/typescript": "^4.1.0",
"ava": "^6.1.2",
"near-workspaces": "^3.5.0",
"ts-morph": "^21.0.1",
"ts-node": "^10.9.2",
"tsimp": "^2.0.11",
"typescript": "^5.3.3"
}
}
}
9 changes: 0 additions & 9 deletions contract-ts/sandbox-ts/ava.config.cjs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Worker, NEAR, NearAccount } from 'near-workspaces';
import anyTest, { TestFn } from 'ava';
import { setDefaultResultOrder } from 'dns'; setDefaultResultOrder('ipv4first'); // temp fix for node >v17

const test = anyTest as TestFn<{
worker: Worker;
accounts: Record<string, NearAccount>;
}>;
// Global context
let worker: Worker;
let accounts: Record<string, NearAccount>;

test.beforeEach(async (t) => {
const test = anyTest as TestFn<{}>;

test.before(async (t) => {
// Init the worker and start a Sandbox server
const worker = await Worker.init();
worker = await Worker.init();

// deploy contract
const root = worker.rootAccount;
Expand All @@ -25,19 +27,18 @@ test.beforeEach(async (t) => {
await contract.deploy(process.argv[2]);

// Save state for test runs, it is unique for each test
t.context.worker = worker;
t.context.accounts = { root, contract, alice };
accounts = { root, contract, alice };
});

test.afterEach(async (t) => {
test.after.always(async (t) => {
// Stop Sandbox server
await t.context.worker.tearDown().catch((error) => {
await worker.tearDown().catch((error) => {
console.log("Failed to stop the Sandbox:", error);
});
});

test("send one message and retrieve it", async (t) => {
const { root, contract } = t.context.accounts;
const { root, contract } = accounts;
await root.call(contract, "add_message", { text: "aloha" });
const msgs = await contract.view("get_messages");
const expectedMessagesResult = [
Expand All @@ -47,7 +48,7 @@ test("send one message and retrieve it", async (t) => {
});

test("send two messages and expect two total", async (t) => {
const { root, contract, alice } = t.context.accounts;
const { root, contract, alice } = accounts;
await root.call(contract, "add_message", { text: "aloha" });
await alice.call(contract, "add_message", { text: "hola" }, { attachedDeposit: NEAR.parse('1') });

Expand Down
17 changes: 0 additions & 17 deletions contract-ts/sandbox-ts/package.json

This file was deleted.

9 changes: 7 additions & 2 deletions contract-ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"target": "es5",
"noEmit": true
"target": "ES5",
"noEmit": true,
"noImplicitAny": false,
},
"files": [
"sandbox-ts/main.ava.ts",
"src/contract.ts"
],
"exclude": [
"node_modules"
],
Expand Down
2 changes: 2 additions & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
"@babel/preset-react": "^7.17.12",
"@parcel/transformer-sass": "^2.8.0",
"@types/node": "^18.6.2",
"buffer": "^5.5.0||^6.0.0",
"cypress": "^11.2.0",
"env-cmd": "^10.1.0",
"events": "^3.1.0",
"nodemon": "^2.0.16",
"parcel": "^2.6.0",
"process": "^0.11.10",
Expand Down

0 comments on commit 96a0c1d

Please sign in to comment.