-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from matiasbenary/update-and-reorganize
Update and reorganize
- Loading branch information
Showing
12 changed files
with
75 additions
and
205 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters