-
Notifications
You must be signed in to change notification settings - Fork 6
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 #2 from matiasbenary/main
Update the examples
- Loading branch information
Showing
110 changed files
with
723 additions
and
36,088 deletions.
There are no files selected for viewing
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,10 @@ | ||
{ | ||
"image": "mcr.microsoft.com/devcontainers/universal:2", | ||
"features": { | ||
"ghcr.io/devcontainers/features/rust:1": {}, | ||
"ghcr.io/devcontainers/features/node:1": { | ||
"version":"20" | ||
} | ||
} | ||
|
||
} |
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 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,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 |
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 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 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,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 | ||
|
||
|
||
|
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,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.
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 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
File renamed without changes.
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,4 @@ | ||
[toolchain] | ||
channel = "1.73.0" | ||
components = ["rustfmt"] | ||
targets = ["wasm32-unknown-unknown"] |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,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" |
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,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 | ||
``` |
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
File renamed without changes.
File renamed without changes.
Binary file not shown.
Binary file added
BIN
+103 KB
contract-advanced-ts/sandbox-ts/src/external-contracts/guest-book.wasm
Binary file not shown.
File renamed without changes.
Oops, something went wrong.