Skip to content

Commit

Permalink
Merge pull request #4 from garikbesson/migrate-sdk
Browse files Browse the repository at this point in the history
Migration to Rust SDK 5.0.0
  • Loading branch information
gagdiez committed Mar 19, 2024
2 parents 648ff74 + 7e59625 commit 98a2379
Show file tree
Hide file tree
Showing 51 changed files with 572 additions and 860 deletions.
19 changes: 10 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
**/sandbox-ts/node_modules
**/sandbox-ts/package-lock.json
# Rust
**/target
**/Cargo.lock

**/build
**/node_modules
**/sandbox-ts/node_modules
**/sandbox-ts/package-lock.json
**/sandbox-ts/yarn.lock


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

# Frontend
**/dist/
**/.parcel-cache
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
# Cross-Contract Call Examples
# Cross-Contract Call Examples 👋
[![](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/cross-contract-calls/actions/workflows/tests-ts.yml/badge.svg)
![example workflow](https://github.com/near-examples/cross-contract-calls/actions/workflows/tests-rs.yml/badge.svg)

This repository contains examples on how to perform cross-contract calls in Rust and JavaScript Smart Contracts.
This repository contains examples on how to perform cross-contract calls in both JavaScript and Rust.

## Repositories

- [contract-hello-rs](contract-hello-rs) - Rust example
- [contract-hello-ts](contract-hello-ts) - JavaScript example
- [contract-simple-rs](contract-simple-rs) - Rust example
- [contract-simple-ts](contract-simple-ts) - JavaScript example
- [contract-advanced-rs](contract-advanced-rs) - Rust advanced example
- [contract-advanced-ts](ccontract-advanced-ts) - JavaScript advanced example

<br />

# What These Examples Show

1. How to make cross contract call and handle their responses information in the NEAR network.
2. How to handle their responses.

<br />

# Learn More
1. Learn more about the contract through its [README](./contract-ts/README.md).
2. Check [**our documentation**](https://docs.near.org/tutorials/examples/xcc).
18 changes: 12 additions & 6 deletions contract-advanced-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
[package]
name = "cross-contract"
name = "cross_contract"
version = "1.0.0"
authors = ["Near Inc <hello@near.org>"]
edition = "2021"

[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"
schemars = "0.8.16"

[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]
Expand Down
128 changes: 32 additions & 96 deletions contract-advanced-rs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,119 +6,55 @@ This contract presents 3 examples on how to do complex cross-contract calls. Par
2. How to call multiple contracts in parallel, each returning a different type.
3. Different ways of handling the responses in the callback.

## How to Build Locally?


## Structure of the Contract
Install [`cargo-near`](https://github.com/near/cargo-near) and run:

```bash
┌── sandbox-ts # sandbox testing
│ ├── src
│ │ ├── external-contracts
│ │ │ ├── counter.wasm
│ │ │ ├── guest-book.wasm
│ │ │ └── hello-near.wasm
│ │ └── main.ava.ts
│ ├── ava.config.cjs
│ └── package.json
├── src # contract's code
│ ├── batch_actions.rs
│ ├── lib.rs
│ ├── multiple_contracts.rs
│ └── similar_contracts.rs
├── build.sh # build script
├── Cargo.toml # package manager
├── README.md
├── rust-toolchain.toml
└── test.sh # test script
cargo near build
```

## Smart Contract

### 1. Batch Actions

You can aggregate multiple actions directed towards one same contract into a batched transaction.
Methods called this way are executed sequentially, with the added benefit that, if one fails then
they **all get reverted**.
## How to Test Locally?

```rust
// Promise with batch actions
Promise::new(self.hello_account.clone())
.function_call( ... )
.function_call( ... )
.function_call( ... )
.function_call( ... )
.then( Self::ext(env::current_account_id()).batch_actions_callback() )
```bash
cargo test
```

In this case, the callback has access to the value returned by the **last
action** from the chain.

<br />
## How to Deploy?

### 2. Calling Multiple Contracts
To deploy manually, install [`cargo-near`](https://github.com/near/cargo-near) and run:

A contract can call multiple other contracts. This creates multiple transactions that execute
all in parallel. If one of them fails the rest **ARE NOT REVERTED**.

```rust
let hello_promise = Promise::new(self.hello_account).function_call( ... );
let counter_promise = Promise::new(self.counter_account).function_call( ... );
let guestbook_promise = Promise::new(self.guestbook_account).function_call( ... );
```bash
# Create a new account
cargo near create-dev-account

// Calling multiple contracts in parallel
hello_promise
.and(counter_promise)
.and(guestbook_promise)
.then(
Self::ext(env::current_account_id()).multiple_contracts_callback(),
)
# Deploy the contract on it
cargo near deploy <account-id>
```

In this case, the callback has access to an **array of responses**, which have either the
value returned by each call, or an error message.
## CLI: Interacting with the Contract

<br />
To interact with the contract through the console, you can use the following commands

### 3. Calling Contracts With the Same Return Type

This example is a particular case of the previous one ([2. Calling Multiple Contracts](#2-calling-multiple-contracts)).
It simply showcases a different way to check the results by directly accessing the `promise_result` array.
```bash
# Replace <your-account.testnet> with your account ID
# Call batch_actions method
near call <your-account.testnet> batch_actions --gas 300000000000000 --accountId <your-account.testnet>

```rust
for index in 0..3 {
let result = env::promise_result(index); // response of the i-th call
# Call multiple_contracts method
near call <your-account.testnet> multiple_contracts --gas 300000000000000 --accountId <your-account.testnet>

match result {
PromiseResult::Successful(value) => {
if let Ok(message) = near_sdk::serde_json::from_slice::<String>(&value) {
results.push(message.clone());
log!(format!("Call {index} returned: {message}"));
} else {
log!(format!("Error deserializing call {index} result."));
}
}
...
}
}
# Call similar_contracts method
near call <your-account.testnet> similar_contracts --gas 300000000000000 --accountId <your-account.testnet>
```
---
## Quickstart

# Useful Links


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


## Build and Test the Contract
The contract readily includes a set of unit and sandbox testing to validate its functionality. To execute the tests, run the following commands:



```bash
# To solely build the contract
./build.sh

# To build and execute the contract's tests
./test.sh
```
- [cargo-near](https://github.com/near/cargo-near) - NEAR smart contract development toolkit for Rust
- [near CLI](https://near.cli.rs) - Interact 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)
6 changes: 0 additions & 6 deletions contract-advanced-rs/build.sh

This file was deleted.

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

This file was deleted.

17 changes: 0 additions & 17 deletions contract-advanced-rs/sandbox-ts/package.json

This file was deleted.

0 comments on commit 98a2379

Please sign in to comment.