Skip to content

Commit

Permalink
Merge pull request #3 from matiasbenary/update-and-reorganize
Browse files Browse the repository at this point in the history
Update and reorganize
  • Loading branch information
gagdiez authored Mar 1, 2024
2 parents 99b32dc + e6f272d commit 76edd26
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 205 deletions.
2 changes: 0 additions & 2 deletions contract-rs/.cargo/config

This file was deleted.

28 changes: 17 additions & 11 deletions contract-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
[package]
name = "hello_near"
version = "1.0.0"
authors = ["Near Inc <[email protected]>"]
name = "contract-rs"
description = "cargo-near-new-project-description"
version = "0.1.0"
edition = "2021"
# TODO: Fill out the repository field to help NEAR ecosystem tools to discover your project.
# NEP-0330 is automatically implemented for all contracts built with https://github.com/near/cargo-near.
# Link to the repository will be available via `contract_source_metadata` view-function.
#repository = "https://github.com/xxx/xxx"

[lib]
crate-type = ["cdylib"]
crate-type = ["cdylib", "rlib"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
near-sdk = "4.1.1"
uint = { version = "0.9.3", default-features = false }
near-sdk = "5.0.0"

[patch.crates-io]
parity-secp256k1 = { git = 'https://github.com/paritytech/rust-secp256k1.git' }
[dev-dependencies]
near-sdk = { version = "5.0.0", features = ["unit-testing"] }
near-workspaces = { version = "0.10.0", features = ["unstable"] }
tokio = { version = "1.12.0", features = ["full"] }
serde_json = "1"

[profile.release]
codegen-units = 1
# Tell `rustc` to optimize for small code size.
opt-level = "z"
lto = true
debug = false
panic = "abort"
# Opt into extra safety checks on arithmetic operations https://stackoverflow.com/a/64136471/249801
overflow-checks = true

[workspace]
members = ["sandbox-rs"]
103 changes: 20 additions & 83 deletions contract-rs/README.md
Original file line number Diff line number Diff line change
@@ -1,100 +1,37 @@
# Hello NEAR Contract
# contract-rs

The smart contract exposes two methods to enable storing and retrieving a greeting in the NEAR network.
cargo-near-new-project-description

```rust
const DEFAULT_GREETING: &str = "Hello";
## How to Build Locally?

#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
pub struct Contract {
greeting: String,
}

impl Default for Contract {
fn default() -> Self {
Self { greeting: DEFAULT_GREETING.to_string() }
}
}

#[near_bindgen]
impl Contract {
// Public: Returns the stored greeting, defaulting to 'Hello'
pub fn get_greeting(&self) -> String {
return self.greeting.clone();
}

// Public: Takes a greeting, such as 'howdy', and records it
pub fn set_greeting(&mut self, greeting: String) {
// Record a log permanently to the blockchain!
log!("Saving greeting {}", greeting);
self.greeting = greeting;
}
}
```

<br />

# Quickstart

1. Make sure you have installed [rust](https://rust.org/).
2. Install the [`NEAR CLI`](https://github.com/near/near-cli#setup)

<br />

## 1. Build, Test and Deploy
To build the contract you can execute the `./build.sh` script, which will in turn run:
Install [`cargo-near`](https://github.com/near/cargo-near) and run:

```bash
rustup target add wasm32-unknown-unknown
cargo build --target wasm32-unknown-unknown --release
cargo near build
```

Then, run the `./deploy.sh` script, which will in turn run:
## How to Test Locally?

```bash
near dev-deploy --wasmFile ./target/wasm32-unknown-unknown/release/hello_near.wasm
cargo test
```

the command [`near dev-deploy`](https://docs.near.org/tools/near-cli#near-dev-deploy) automatically creates an account in the NEAR testnet, and deploys the compiled contract on it.
## How to Deploy?

Once finished, check the `./neardev/dev-account` file to find the address in which the contract was deployed:
Deployment is automated with GitHub Actions CI/CD pipeline.
To deploy manually, install [`cargo-near`](https://github.com/near/cargo-near) and run:

```bash
cat ./neardev/dev-account
# e.g. dev-1659899566943-21539992274727
cargo near deploy <account-id>
```

<br />

## 2. Retrieve the Greeting

`get_greeting` 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 greeting
near view <dev-account> get_greeting
```

<br />

## 3. Store a New Greeting
`set_greeting` changes the contract's state, for which it is a `change` method.

`Change` 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.

```bash
# Use near-cli to set a new greeting
near call <dev-account> set_greeting '{"greeting":"howdy"}' --accountId <dev-account>
```

**Tip:** If you would like to call `set_greeting` using your own account, first login into NEAR using:

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

and then use the logged account to sign the transaction: `--accountId <your-account>`.
- [cargo-near](https://github.com/near/cargo-near) - NEAR smart contract development toolkit for Rust
- [near CLI](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)
3 changes: 0 additions & 3 deletions contract-rs/build.sh

This file was deleted.

2 changes: 0 additions & 2 deletions contract-rs/deploy.sh

This file was deleted.

2 changes: 1 addition & 1 deletion contract-rs/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[toolchain]
channel = "1.74"
channel = "stable"
components = ["rustfmt"]
targets = ["wasm32-unknown-unknown"]
14 changes: 0 additions & 14 deletions contract-rs/sandbox-rs/Cargo.toml

This file was deleted.

65 changes: 0 additions & 65 deletions contract-rs/sandbox-rs/src/tests.rs

This file was deleted.

24 changes: 10 additions & 14 deletions contract-rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
// Find all our documentation at https://docs.near.org
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::env::log_str;
use near_sdk::near_bindgen;
use near_sdk::borsh::{BorshDeserialize, BorshSerialize};
use near_sdk::{log, near_bindgen};

// Define the contract structure
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
#[borsh(crate = "near_sdk::borsh")]
pub struct Contract {
greeting: String,
}

// Define the default, which automatically initializes the contract
impl Default for Contract {
fn default() -> Self {
Self { greeting: "Hello".to_string() }
Self {
greeting: "Hello".to_string(),
}
}
}

Expand All @@ -22,12 +24,12 @@ impl Default for Contract {
impl Contract {
// Public method - returns the greeting saved, defaulting to DEFAULT_GREETING
pub fn get_greeting(&self) -> String {
return self.greeting.clone();
self.greeting.clone()
}

// Public method - accepts a greeting, such as "howdy", and records it
pub fn set_greeting(&mut self, greeting: String) {
log_str(&format!("Saving greeting: {greeting}"));
log!("Saving greeting: {greeting}");
self.greeting = greeting;
}
}
Expand All @@ -44,19 +46,13 @@ mod tests {
fn get_default_greeting() {
let contract = Contract::default();
// this test did not call set_greeting so should return the default "Hello" greeting
assert_eq!(
contract.get_greeting(),
"Hello".to_string()
);
assert_eq!(contract.get_greeting(), "Hello");
}

#[test]
fn set_then_get_greeting() {
let mut contract = Contract::default();
contract.set_greeting("howdy".to_string());
assert_eq!(
contract.get_greeting(),
"howdy".to_string()
);
assert_eq!(contract.get_greeting(), "howdy");
}
}
9 changes: 0 additions & 9 deletions contract-rs/test.sh

This file was deleted.

26 changes: 26 additions & 0 deletions contract-rs/tests/test_basics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use serde_json::json;

#[tokio::test]
async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>> {
let sandbox = near_workspaces::sandbox().await?;
let contract_wasm = near_workspaces::compile_project("./").await?;

let contract = sandbox.dev_deploy(&contract_wasm).await?;

let user_account = sandbox.dev_create_account().await?;

let outcome = user_account
.call(contract.id(), "set_greeting")
.args_json(json!({"greeting": "Hello World!"}))
.transact()
.await?;
assert!(outcome.is_success());

let user_message_outcome = contract
.view("get_greeting")
.args_json(json!({}))
.await?;
assert_eq!(user_message_outcome.json::<String>()?, "Hello World!");

Ok(())
}
2 changes: 1 addition & 1 deletion contract-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"postinstall": "cd sandbox-ts && $npm_execpath install"
},
"dependencies": {
"near-cli": "^3.5.0",
"near-cli": "^4.0.7",
"near-sdk-js": "1.0.0"
},
"devDependencies": {
Expand Down

0 comments on commit 76edd26

Please sign in to comment.