Skip to content

Commit fcee4e6

Browse files
committed
feat: re-organize
1 parent 428ae8d commit fcee4e6

File tree

16 files changed

+146
-194
lines changed

16 files changed

+146
-194
lines changed

.github/workflows/tests-rs.yml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: Tests Contract RS
2+
on: push
3+
jobs:
4+
workflows:
5+
strategy:
6+
matrix:
7+
platform: [ubuntu-latest, macos-latest]
8+
runs-on: ${{ matrix.platform }}
9+
steps:
10+
- uses: actions/checkout@v4
11+
- name: Install and test modules
12+
run: |
13+
cd ./contract-rs
14+
cargo test

.github/workflows/tests-ts.yml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Tests Contract TS
2+
on: push
3+
jobs:
4+
workflows:
5+
strategy:
6+
matrix:
7+
platform: [ubuntu-latest, macos-latest]
8+
node-version: [18, 20]
9+
runs-on: ${{ matrix.platform }}
10+
steps:
11+
- uses: actions/checkout@v4
12+
- uses: actions/setup-node@v4
13+
with:
14+
node-version: ${{ matrix.node-version }}
15+
- name: Install and test modules
16+
run: |
17+
cd ./contract-ts
18+
yarn
19+
yarn test

.github/workflows/tests.yml

-24
This file was deleted.

README.md

+15-40
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,26 @@
1-
# Coin Flip 🪙
2-
[![](https://img.shields.io/badge/⋈%20Examples-Basics-green)](https://docs.near.org/tutorials/welcome)
3-
[![](https://img.shields.io/badge/Gitpod-Ready-orange)](https://gitpod.io/#/https://github.com/near-examples/coin-flip-js)
4-
[![](https://img.shields.io/badge/Contract-js-yellow)](https://docs.near.org/develop/contracts/anatomy)
5-
[![](https://img.shields.io/badge/Frontend-JS-yellow)](https://docs.near.org/develop/integrate/frontend)
6-
[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fnear-examples%2Fcoin-flip-js%2Fbadge%3Fref%3Dmain&style=flat&label=Tests&ref=main)](https://actions-badge.atrox.dev/near-examples/coin-flip-js/goto?ref=main)
7-
8-
Coin Flip is a game were the player tries to guess the outcome of a coin flip. It is one of the simplest contracts implementing random numbers.
1+
# Coin Flip Examples 🪙
92

3+
[![](https://img.shields.io/badge/⋈%20Examples-Basics-green)](https://docs.near.org/tutorials/welcome)
4+
[![](https://img.shields.io/badge/Contract-JS-yellow)](contract-ts)
5+
[![](https://img.shields.io/badge/Contract-Rust-red)](contract-rs)
6+
[![](https://img.shields.io/badge/Frontend-JS-yellow)](frontend)
7+
![example workflow](https://github.com/near-examples/coin-flip-examples/actions/workflows/tests-ts.yml/badge.svg)
8+
![example workflow](https://github.com/near-examples/coin-flip-examples/actions/workflows/tests-rs.yml/badge.svg)
109

11-
# What This Example Shows
10+
This repository contains examples of a simple coin flip contract in both JavaScript and Rust, as well as a frontend interacting with a deployed coin flip smart contract.
1211

13-
1. How to store and retrieve information in the NEAR network.
14-
2. How to integrate a smart contract in a web frontend.
15-
3. How to generate random numbers in a contract.
12+
- [Coin Flip Contract - JavaScript](contract-ts)
13+
- [Coin Flip Contract - Rust](contract-ts)
14+
- [Coin Flip Frontend](frontend)
1615

1716
<br />
1817

19-
# Quickstart
20-
21-
Clone this repository locally or [**open it in gitpod**](https://gitpod.io/#/https://github.com/near-examples/coin-flip-js). Then follow these steps:
22-
23-
### 1. Install Dependencies
24-
```bash
25-
npm install
26-
```
18+
# What These Examples Show
2719

28-
### 2. Test the Contract
29-
Deploy your contract in a sandbox and simulate interactions from users.
20+
1. How to generate a random number.
3021

31-
```bash
32-
npm test
33-
```
34-
35-
### 3. Deploy the Contract
36-
Build the contract and deploy it in a testnet account
37-
```bash
38-
npm run deploy
39-
```
40-
41-
### 4. Start the Frontend
42-
Start the web application to interact with your smart contract
43-
```bash
44-
npm start
45-
```
46-
47-
---
22+
<br />
4823

4924
# Learn More
50-
1. Learn more about the contract through its [README](./contract/README.md).
25+
1. Learn more about the contract through its [README](./contract-ts/README.md).
5126
2. Check [**our documentation**](https://docs.near.org/develop/welcome).

contract-rs/README.md

+23-44
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,40 @@
1-
# Coin Flip 🪙
2-
[![](https://img.shields.io/badge/⋈%20Examples-Basics-green)](https://docs.near.org/tutorials/welcome)
3-
[![](https://img.shields.io/badge/Gitpod-Ready-orange)](https://gitpod.io/#/https://github.com/near-examples/coin-flip-js)
4-
[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fnear-examples%2Fcoin-flip-js%2Fbadge%3Fref%3Dmain&style=flat&label=Tests&ref=main)](https://actions-badge.atrox.dev/near-examples/coin-flip-js/goto?ref=main)
1+
# contract-rs
52

6-
Coin Flip is a game were the player tries to guess the outcome of a coin flip. It is one of the simplest contracts implementing random numbers.
3+
cargo-near-new-project-description
74

5+
## How to Build Locally?
86

9-
# What This Example Shows
10-
11-
1. How to store and retrieve information in the NEAR network.
12-
2. How to integrate a smart contract in a web frontend.
13-
3. How to generate random numbers in a contract.
14-
15-
<br />
16-
17-
# Quickstart
18-
19-
1. Make sure you have installed [rust](https://rust.org/).
20-
2. Install the [`NEAR CLI`](https://github.com/near/near-cli#setup)
21-
22-
<br />
23-
24-
## 1. Build, Test and Deploy
25-
To build the contract you can execute the `./build.sh` script, which will in turn run:
7+
Install [`cargo-near`](https://github.com/near/cargo-near) and run:
268

279
```bash
28-
rustup target add wasm32-unknown-unknown
29-
cargo build --target wasm32-unknown-unknown --release
10+
cargo near build
3011
```
3112

32-
Then, deploy your contract using following commands:
13+
## How to Test Locally?
3314

3415
```bash
35-
export CONTRACT_ID=test.near
36-
near deploy "'$CONTRACT_ID'" ./target/wasm32-unknown-unknown/release/contract.wasm
16+
cargo test
3717
```
3818

39-
<br />
40-
41-
## 2. Retrieve the user points
19+
## How to Deploy?
4220

43-
`points_of` is a `view` method.
44-
45-
`View` methods can be called for **free** by anyone, even people **without a NEAR account**!
21+
To deploy manually, install [`cargo-near`](https://github.com/near/cargo-near) and run:
4622

4723
```bash
48-
# Use near-cli to get the greeting
49-
near view $CONTRACT_ID points_of '{"player": "'$CONTRACT_ID'"}'
50-
```
51-
52-
<br />
24+
# Create a new account
25+
cargo near create-dev-account
5326

54-
## 3. Flip a coin
55-
`flip_coin` changes the contract's state, for which it is a `call` method.
27+
# Deploy the contract on it
28+
cargo near deploy <account-id>
29+
```
5630

57-
`Call` methods can only be invoked using a NEAR account, since the account needs to pay GAS for the transaction. In this case, we are asking the account we created in step 1 to sign the transaction.
31+
## Useful Links
5832

59-
```bash
60-
near call $CONTRACT_ID flip_coin '{"player_guess": "tails"}' --accountId $CONTRACT_ID
61-
```
33+
- [cargo-near](https://github.com/near/cargo-near) - NEAR smart contract development toolkit for Rust
34+
- [near CLI](https://near.cli.rs) - Iteract with NEAR blockchain from command line
35+
- [NEAR Rust SDK Documentation](https://docs.near.org/sdk/rust/introduction)
36+
- [NEAR Documentation](https://docs.near.org)
37+
- [NEAR StackOverflow](https://stackoverflow.com/questions/tagged/nearprotocol)
38+
- [NEAR Discord](https://near.chat)
39+
- [NEAR Telegram Developers Community Group](https://t.me/neardev)
40+
- NEAR DevHub: [Telegram](https://t.me/neardevhub), [Twitter](https://twitter.com/neardevhub)

contract-ts/README.md

+15-12
Original file line numberDiff line numberDiff line change
@@ -45,35 +45,38 @@ flip_coin({ player_guess }: { player_guess: Side }): Side {
4545

4646
<br />
4747

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

5151
```bash
52-
npm run deploy
52+
npm run test
5353
```
5454

55-
Once finished, check the `neardev/dev-account` file to find the address in which the contract was deployed:
55+
<br />
56+
57+
## 2. Create Account and Deploy the Contract
58+
You can create a new account and deploy the contract on it by running:
5659

5760
```bash
58-
cat ./neardev/dev-account
59-
# e.g. dev-1659899566943-21539992274727
61+
near create-account <your-account>.testnet --useFaucet
62+
near deploy <your-account>.testnet build/contract.wasm
6063
```
6164

6265
<br />
6366

64-
## 2. Get the Score
67+
## 3. Get the Score
6568
`points_of` performs read-only operations, therefore it is a `view` method.
6669

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

6972
```bash
7073
# Use near-cli to get the points
71-
near view <dev-account> points_of '{"player": "<dev-account>"}'
74+
near view <your-account> points_of '{"player": "<dev-account>"}'
7275
```
7376

7477
<br />
7578

76-
## 3. Flip a Coin and Try to Guess the Outcome
79+
## 4. Flip a Coin and Try to Guess the Outcome
7780
`flip_coin` takes a guess ("heads" or "tails"), simulates a coin flip and gives/removes points to the player.
7881

7982
It changes the contract's state, for which it is a `call` method.
@@ -82,17 +85,17 @@ It changes the contract's state, for which it is a `call` method.
8285

8386
```bash
8487
# Use near-cli to play
85-
near call <dev-account> flip_coin '{"player_guess":"tails"}' --accountId <dev-account>
88+
near call <your-account> flip_coin '{"player_guess":"tails"}' --accountId <your-account>
8689
```
8790

88-
**Tip:** If you would like to call `flip_coin` using your own account, first login into NEAR using:
91+
**Tip:** If you would like to call `flip_coin` using another account, first login into NEAR using:
8992

9093
```bash
9194
# Use near-cli to login your NEAR account
9295
near login
9396
```
9497

95-
and then use the logged account to sign the transaction: `--accountId <your-account>`.
98+
and then use the logged account to sign the transaction: `--accountId <another-account>`.
9699

97100
## A Note on Random Numbers
98101
Generating random numbers in an adversarial environment such as a blockchain is very difficult. This spawns from

contract-ts/ava.config.cjs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require('util').inspect.defaultOptions.depth = 5; // Increase AVA's printing depth
2+
3+
module.exports = {
4+
timeout: '10000',
5+
files: ['sandbox-ts/*.ava.ts'],
6+
failWithoutAssertions: false,
7+
extensions: {
8+
js: true,
9+
ts: 'module'
10+
},
11+
require: ['ts-node/register', 'near-workspaces'],
12+
"nodeArguments": [
13+
"--import=tsimp"
14+
]
15+
};

contract-ts/build.sh

-5
This file was deleted.

contract-ts/deploy.sh

-7
This file was deleted.

contract-ts/integration-tests/ava.config.cjs

-9
This file was deleted.

contract-ts/integration-tests/package.json

-16
This file was deleted.

contract-ts/package.json

+9-6
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
"license": "(MIT AND Apache-2.0)",
55
"type": "module",
66
"scripts": {
7-
"build": "./build.sh",
8-
"deploy": "./deploy.sh",
9-
"test": "npm run build && npm run test:integration",
10-
"test:integration": "cd integration-tests && npm test"
7+
"build": "near-sdk-js build src/contract.ts build/contract.wasm",
8+
"test": "ava -- ./build/contract.wasm"
119
},
1210
"dependencies": {
13-
"near-cli": "^4.0.10",
11+
"near-cli": "^4.0.8",
1412
"near-sdk-js": "1.0.0"
1513
},
1614
"devDependencies": {
15+
"@ava/typescript": "^4.1.0",
16+
"ava": "^6.1.2",
17+
"near-workspaces": "^3.5.0",
1718
"ts-morph": "^21.0.1",
19+
"ts-node": "^10.9.2",
20+
"tsimp": "^2.0.11",
1821
"typescript": "^5.3.3"
1922
}
20-
}
23+
}

0 commit comments

Comments
 (0)