.devcontainer
    devcontainer.json
.github
    workflows
        tests-rs.yml
        tests-ts.yml
README.md
contract-rs
    Cargo.toml
    README.md
    rust-toolchain.toml
    src
        lib.rs
    tests
        sandbox.rs
contract-ts
    README.md
    package.json
    sandbox-ts
        main.ava.ts
    src
        contract.ts
    tsconfig.json
frontend
    assets
        global.css
    index.html
    index.js
    near-wallet.js
    package.json

# Counter Examples

This repository contains examples for a simple counter in both Rust and JavaScript, and an examples of a frontend interacting with a Counter smart contract. 

## Repositories

- [Counter TS Examples](contract-ts)
- [Counter RS Example](contract-rs)
- [Counter Frontend Example](frontend)
# Count on NEAR Contract Example

The smart contract exposes methods to interact with a counter stored in the NEAR
network.

## How to Build Locally?

Install [`cargo-near`](https://github.com/near/cargo-near) and run:

```bash
cargo near build
```

## How to Test Locally?

```bash
cargo test
```

## How to Deploy?

Deployment is automated with GitHub Actions CI/CD pipeline. To deploy manually,
install [`cargo-near`](https://github.com/near/cargo-near) and run:

```bash
cargo near deploy <account-id>
```

## How to Interact?

_In this example we will be using [NEAR CLI](https://github.com/near/near-cli)
to intract with the NEAR blockchain and the smart contract_

_If you want full control over of your interactions we recommend using the
[near-cli-rs](https://near.cli.rs)._

### Get the Counter

`get_num` is a read-only method (aka `view` method).

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

```bash
# Use near-cli to get the counter value
near view <contract-account-id> get_num
```

### Modify the Counter

`increment`, `decrement` and `reset` change the contract's state, for which they
are `call` methods.

`Call` methods can only be invoked using a NEAR account, since the account needs
to pay GAS for the transaction.

```bash
# Use near-cli to set increment the counter
near call <contract-account-id> increment --accountId --accountId <your-account>
```

## Useful Links

- [cargo-near](https://github.com/near/cargo-near) - NEAR smart contract
  development toolkit for Rust
- [near CLI-rs](https://near.cli.rs) - Iteract with NEAR blockchain from command
  line
- [NEAR Rust SDK Documentation](https://docs.near.org/sdk/rust/introduction)
- [NEAR Documentation](https://docs.near.org)
- [NEAR StackOverflow](https://stackoverflow.com/questions/tagged/nearprotocol)
- [NEAR Discord](https://near.chat)
- [NEAR Telegram Developers Community Group](https://t.me/neardev)
- NEAR DevHub: [Telegram](https://t.me/neardevhub),
  [Twitter](https://twitter.com/neardevhub)
# Count on NEAR Contract

The smart contract exposes methods to interact with a counter stored in the NEAR network.

```ts
val: number = 0;

@view
// Public read-only method: Returns the counter value.
get_num(): number {
  return this.val
}

@call
// Public method: Increment the counter.
increment() {
  this.val += 1;
  near.log(`Increased number to ${this.val}`)
}

@call
// Public method: Decrement the counter.
decrement() {
  this.val -= 1;
  near.log(`Decreased number to ${this.val}`)
}

@call
// Public method - Reset to zero.
reset() {
  this.val = 0;
  near.log(`Reset counter to zero`)
}
```

<br />

# Quickstart

1. Make sure you have installed [node.js](https://nodejs.org/en/download/package-manager/) >= 16.
2. Install the [`NEAR CLI`](https://github.com/near/near-cli#setup)

<br />

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

```bash
npm run deploy
```

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

```bash
cat ./neardev/dev-account
# e.g. dev-1659899566943-21539992274727
```

<br />

## 2. Get the Counter

`get_num` is a read-only method (aka `view` method).

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

```bash
# Use near-cli to get the counter value
near view <dev-account> get_num
```

<br />

## 3. Modify the Counter
`increment`, `decrement` and `reset` change the contract's state, for which they are `call` methods.

`Call` methods can only be invoked using a NEAR account, since the account needs to pay GAS for the transaction.

```bash
# Use near-cli to set increment the counter
near call <dev-account> increment --accountId <dev-account>
```

**Tip:** If you would like to call `increment` using your own 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>`.
