Skip to content

Commit

Permalink
Merge pull request #2 from matiasbenary/main
Browse files Browse the repository at this point in the history
Update the examples
  • Loading branch information
gagdiez committed Feb 21, 2024
2 parents 0c9b882 + fe2474c commit 1b1754a
Show file tree
Hide file tree
Showing 110 changed files with 723 additions and 36,088 deletions.
10 changes: 10 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"image": "mcr.microsoft.com/devcontainers/universal:2",
"features": {
"ghcr.io/devcontainers/features/rust:1": {},
"ghcr.io/devcontainers/features/node:1": {
"version":"20"
}
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Tests
name: Tests Contract Advanced RS
on: push
jobs:
workflows:
Expand All @@ -10,8 +10,8 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "16"
- name: Install modules
run: yarn
- name: Run tests
run: yarn test
node-version: "18"
- name: Install and test modules
run: |
cd ./contract-advanced-rs
./test.sh
18 changes: 18 additions & 0 deletions .github/workflows/tests-advanced-ts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Tests Contract Advanced TS
on: push
jobs:
workflows:
strategy:
matrix:
platform: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "18"
- name: Install and test modules
run: |
cd ./contract-advanced-ts
yarn
yarn test
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Tests
name: Tests Contract Simple RS
on: push
jobs:
workflows:
Expand All @@ -10,8 +10,8 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "16"
- name: Install modules
run: yarn
- name: Run tests
run: yarn test
node-version: "18"
- name: Install and test modules
run: |
cd ./contract-simple-rs
./test.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Tests
name: Tests Contract Simple TS
on: push
jobs:
workflows:
Expand All @@ -10,8 +10,9 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: "16"
- name: Install modules
run: yarn
- name: Run tests
run: yarn test
node-version: "18"
- name: Install and test modules
run: |
cd ./contract-simple-ts
yarn
yarn test
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
**/sandbox-ts/node_modules
**/sandbox-ts/package-lock.json
**/target
**/Cargo.lock

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



10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Cross-Contract Call Examples

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

## Repositories

- [cross-contract-hello-js](cross-contract-hello-js) - JavaScript example
- [cross-contract-hello-rust](cross-contract-hello-rust) - Rust example
- [xcc-advanced-example](xcc-advanced-example) - Advanced example
- [contract-hello-rs](contract-hello-rs) - Rust example
- [contract-hello-ts](contract-hello-ts) - JavaScript example
- [contract-advanced-rs](contract-advanced-rs) - Rust advanced example
- [contract-advanced-ts](ccontract-advanced-ts) - JavaScript advanced example

File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "contract"
name = "cross-contract"
version = "1.0.0"
authors = ["Near Inc <hello@near.org>"]
edition = "2021"
Expand All @@ -8,9 +8,12 @@ edition = "2021"
crate-type = ["cdylib"]

[dependencies]
near-sdk = "4.0.0"
near-sdk = "4.1.1"
uint = { version = "0.9.3", default-features = false }

[patch.crates-io]
parity-secp256k1 = { git = 'https://github.com/paritytech/rust-secp256k1.git' }

[profile.release]
codegen-units = 1
opt-level = "z"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,36 @@ 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.

<br />

## 1. Batch Actions

## Structure of the Contract

```bash
┌── sandbox-ts # sandbox testing
│ ├── src
│ │ ├── external-contracts
│ │ │ ├── counter.wasm
│ │ │ ├── guest-book.wasm
│ │ │ └── hello-near.wasm
│ │ └── main.ava.ts
│ ├── ava.config.cjs
│ └── package.json
├── 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
```

## 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
Expand All @@ -29,7 +56,7 @@ action** from the chain.

<br />

## 2. Calling Multiple Contracts
### 2. Calling Multiple Contracts

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**.
Expand All @@ -53,7 +80,7 @@ value returned by each call, or an error message.

<br />

## 3. Calling Contracts With the Same Return Type
### 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.
Expand All @@ -75,3 +102,24 @@ for index in 0..3 {
}
}
```
---
## Quickstart



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
```
File renamed without changes.
4 changes: 4 additions & 0 deletions contract-advanced-rs/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[toolchain]
channel = "1.73.0"
components = ["rustfmt"]
targets = ["wasm32-unknown-unknown"]
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions contract-advanced-rs/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

# unit testing
cargo test

# sandbox testing
./build.sh
cd sandbox-ts
npm i
npm run test -- -- "../target/wasm32-unknown-unknown/release/cross_contract.wasm"
130 changes: 130 additions & 0 deletions contract-advanced-ts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Complex Cross-Contract Calls Examples

This contract presents 3 examples on how to do complex cross-contract calls. Particularly, it shows:

1. How to batch method calls to a same contract.
2. How to call multiple contracts in parallel, each returning a different type.
3. Different ways of handling the responses in the callback.



## Structure of the Example

```bash
┌── sandbox-ts # sandbox testing
│ ├── src
│ │ ├── external-contracts
│ │ │ ├── counter.wasm
│ │ │ ├── guest-book.wasm
│ │ │ └── hello-near.wasm
│ │ └── main.ava.ts
│ ├── ava.config.cjs
│ └── package.json
├── package.json
├── src # contract's code
│ ├── internal
│ │ ├── batch_actions.ts
│ │ ├── constants.ts
│ │ ├── multiple_contracts.ts
│ │ ├── similar_contracts.ts
│ │ └── utils.ts
│ └── contract.ts
├── package.json
├── README.md
└── tsconfig.json
```

## 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**.

```ts
// Promise with batch actions
const promise = NearPromise.new(accountId)
.functionCall(...)
.functionCall(...)
.functionCall(...)
.functionCall(...)
.then(
NearPromise.new(near.currentAccountId())
.functionCall(...)
)
```

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

<br />

### 2. Calling Multiple Contracts

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**.

```ts
const promise1 = NearPromise.new(contract.hello_account)
.functionCall(...)
const promise2 = NearPromise.new(contract.counter_account)
.functionCall(...)
const promise3 = NearPromise.new(contract.guestbook_account)
.functionCall(...)

return promise1
.and(promise2)
.and(promise3)
.then(
NearPromise.new(near.currentAccountId())
.functionCall(...)
)
```

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.

<br />

### 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.

```ts
for (let i = 0; i < number_promises; i++) {
near.log(`Get index result: ${i}`);
let { success, result } = promiseResult(i);

if (success) {
allResults.push(result);
near.log(`Success! Index: ${i}, Result: ${result}`);
} else {
near.log("Promise failed...");
return [];
}
}
return allResults;
```
---
## Quickstart



1. Make sure you have installed [Node.s](https://nodejs.org/en/download)
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
yarn build

# To build and execute the contract's tests
yarn test
```
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"license": "(MIT AND Apache-2.0)",
"type": "module",
"scripts": {
"build": "./build.sh",
"deploy": "./deploy.sh",
"test": "echo no unit testing"
"build": "near-sdk-js build src/contract.ts build/cross_contract.wasm",
"test": "$npm_execpath run build && cd sandbox-ts && $npm_execpath run test -- -- ../build/cross_contract.wasm",
"postinstall": "cd sandbox-ts && $npm_execpath install"
},
"dependencies": {
"near-cli": "^3.5.0",
Expand Down
Binary file not shown.
Binary file not shown.

0 comments on commit 1b1754a

Please sign in to comment.