diff --git a/docs/2.develop/contracts/crosscontract.md b/docs/2.develop/contracts/crosscontract.md index ad897bf4e59..1ab2f56f6d2 100644 --- a/docs/2.develop/contracts/crosscontract.md +++ b/docs/2.develop/contracts/crosscontract.md @@ -23,17 +23,17 @@ There is a delay between the call and the callback in which everyone can still i While making your contract, it is likely that you will want to query information from another contract. Below, you can see a basic example in which we query the greeting message from our [Hello NEAR](quickstart.md) example. - - + - + url="https://github.com/near-examples/cross-contract-calls/blob/main/contract-simple-rs/src/lib.rs" + start="25" end="48" /> + @@ -45,15 +45,15 @@ Calling another contract passing information is also a common scenario. Bellow y + url="https://github.com/near-examples/cross-contract-calls/blob/main/contract-simple-rs/src/external.rs" /> @@ -118,12 +118,12 @@ The callback methods in your contract must be public, so it can be called when t @@ -134,12 +134,12 @@ In case the call finishes successfully, the resulting object will have a `status diff --git a/docs/3.tutorials/examples/advanced-xcc.md b/docs/3.tutorials/examples/advanced-xcc.md index 18af1276e96..54169353f56 100644 --- a/docs/3.tutorials/examples/advanced-xcc.md +++ b/docs/3.tutorials/examples/advanced-xcc.md @@ -7,129 +7,326 @@ import TabItem from '@theme/TabItem'; import {CodeTabs, Language, Github} from "@site/src/components/codetabs" This example presents 3 instances of complex cross-contract calls. Particularly, it shows: + 1. How to batch multiple function 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. +:::info Simple Cross-Contract Calls + +Check the tutorial on how to use [simple cross-contract calls](xcc.md) + +::: + +--- + +## Obtaining the Cross Contract Call Example + +You have two options to start the Donation Example: + +1. You can use the app through `Github Codespaces`, which will open a web-based interactive environment. +2. Clone the repository locally and use it from your computer. + +| Codespaces | Clone locally | +| ------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------- | +| [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/near-examples/cross-contract-calls?quickstart=1) | 🌐 `https://github.com/near-examples/cross-contract-calls` | + --- -## Batch Actions +## Structure of the Example + +The smart contract is available in two flavors: Rust and JavaScript + + + + + +```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 +β”‚ β”œβ”€β”€ internal +β”‚ β”‚ β”œβ”€β”€ batch_actions.ts +β”‚ β”‚ β”œβ”€β”€ constants.ts +β”‚ β”‚ β”œβ”€β”€ multiple_contracts.ts +β”‚ β”‚ β”œβ”€β”€ similar_contracts.ts +β”‚ β”‚ └── utils.ts +β”‚ └── contract.ts +β”œβ”€β”€ package.json +β”œβ”€β”€ README.md +└── tsconfig.json +``` + + + + + +```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 +``` + + + + + +--- + +## Smart Contract + +### 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**. + + + + - - - - #### Getting the Last Response + In this case, the callback has access to the value returned by the **last action** from the chain. + + + + + - - - - - --- -## Calling Multiple Contracts +### 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**. + + + + - - - - #### Getting All Responses + 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. + + + + + - - - - - --- -## Multiple Calls - Same Result Type +### Multiple Calls - Same Result Type -This example is a particular case of the previous one ([2. Calling Multiple Contracts](#2-calling-multiple-contracts)). +This example is a particular case of the previous one ([Calling Multiple Contracts](#2-calling-multiple-contracts)). It simply showcases a different way to check the results by directly accessing the `promise_result` array. In this case, we call multiple contracts that will return the same type: + + + + - - - - #### Getting All Responses + In this case, the callback again has access to an **array of responses**, which we can iterate checking the results. + + + + + - - - - - - \ No newline at end of file + + +--- + +### Testing 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 +cd contract-advanced-ts +yarn +yarn test +``` + + + + + ```bash + cd contract-advanced-rs + ./test.sh + ``` + + + + + +:::tip +The `integration tests` use a sandbox to create NEAR users and simulate interactions with the contract. +::: + +
+ +### Deploying the Contract to the NEAR network + +In order to deploy the contract you will need to [create a NEAR account](/develop/contracts/quickstart#create-a-testnet-account). + + + + +```bash +# Optional - create an account +near create-account --useFaucet + +# Deploy the contract +cd contract-advanced-ts +yarn build +near deploy ./build/cross_contract.wasm --initFunction init --initArgs '{"hello_account":"hello.near-example.testnet","guestbook_account":"guestbook_account.near-example.testnet","counter_account":"counter_account.near-example.testnet"}' +``` + + + + +```bash +# Optional - create an account +near create-account --useFaucet + +# Deploy the contract +cd contract-advanced-rs +./build.sh +near deploy ./target/wasm32-unknown-unknown/release/cross_contract.wasm --initFunction init --initArgs '{"hello_account":"hello.near-example.testnet","guestbook_account":"guestbook_account.near-example.testnet","counter_account":"counter_account.near-example.testnet"}' + +``` + + + + +
+ +### CLI: Interacting with the Contract + +To interact with the contract through the console, you can use the following commands: + +```bash +# Execute contracts sequentially +# Replace with your account ID +near call batch_actions --accountId --gas 300000000000000 + +# Execute contracts in parallel +# Replace with your account ID +near call multiple_contracts --accountId --gas 300000000000000 + +# Execute multiple instances of the same contract in parallel +# Replace with your account ID +near call similar_contracts --accountId --gas 300000000000000 +``` + +:::info Note +If the contract exceeds the execution time, additional gas must be provided. For further details [click here](/develop/contracts/environment/#gas). +::: diff --git a/docs/3.tutorials/examples/xcc.md b/docs/3.tutorials/examples/xcc.md index e544f267e63..1df4016df84 100644 --- a/docs/3.tutorials/examples/xcc.md +++ b/docs/3.tutorials/examples/xcc.md @@ -2,6 +2,7 @@ id: xcc title: Cross Contract Call --- + import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import {CodeTabs, Language, Github} from "@site/src/components/codetabs" @@ -15,110 +16,170 @@ Check the tutorial on how to perform cross-contract calls [in batches and in par --- -## Starting with the Project -You have two options to start using the project. The first and recommended is to use the app through Gitpod, which will open a web-based interactive environment. The second option is to clone the repository locally, for which you will need to install all the [Prerequisites](../../2.develop/prerequisites.md). - - - - - +## Obtaining the Cross Contract Call Example - | Gitpod | Clone locally | - | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------- | - | Open in Gitpod | 🌐 `https://github.com/near-examples/cross-contract-hello-js.git` | +You have two options to start the Donation Example: - - - - - | Gitpod | Clone locally | - | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------- | - | Open in Gitpod | πŸ¦€ `https://github.com/near-examples/cross-contract-hello-rust.git` | - - - +1. You can use the app through `Github Codespaces`, which will open a web-based interactive environment. +2. Clone the repository locally and use it from your computer. +| Codespaces | Clone locally | +| ----------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------- | +| [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/near-examples/cross-contract-calls?quickstart=1) | 🌐 `https://github.com/near-examples/cross-contract-calls` | --- -### Interacting With the Contract -Since this example does not have a frontend, we will interact with it through the [NEAR CLI](../../4.tools/cli.md). +## Structure of the Example - -Check the README.md. Briefly, you will need to: +The smart contract is available in two flavors: Rust and JavaScript -#### 1. Build and Deploy the Contract -You can automatically compile and deploy the contract in the NEAR testnet by running: + -```bash -./contract/deploy.sh -``` - -Once finished, check the `neardev/dev-account` file to find the address in which the contract was deployed: + ```bash -cat ./contract/neardev/dev-account # e.g. dev-1659899566943-21539992274727 +β”Œβ”€β”€ sandbox-ts # sandbox testing +β”‚ β”œβ”€β”€ src +β”‚ β”‚ β”œβ”€β”€ hello-near +β”‚ β”‚ β”‚ └── hello-near.wasm +β”‚ β”‚ └── main.ava.ts +β”‚ β”œβ”€β”€ ava.config.cjs +β”‚ └── package.json +β”œβ”€β”€ src # contract's code +β”‚ └── contract.ts +β”œβ”€β”€ package.json +β”œβ”€β”€ README.md +└── tsconfig.json ``` -#### 2. Get the Greeting - -`query_greeting` performs a cross-contract call, calling the `get_greeting()` method from `hello-nearverse.testnet`. + -`Call` methods can only be invoked using a NEAR account, since the account needs to pay GAS for the transaction. + ```bash -# Use near-cli to ask the contract to query the greeting -near call query_greeting --accountId +β”Œβ”€β”€ sandbox-ts # sandbox testing +β”‚ β”œβ”€β”€ src +β”‚ β”‚ β”œβ”€β”€ hello-near +β”‚ β”‚ β”‚ └── hello-near.wasm +β”‚ β”‚ └── main.ava.ts +β”‚ β”œβ”€β”€ ava.config.cjs +β”‚ └── package.json +β”œβ”€β”€ src # contract's code +β”‚ β”œβ”€β”€ external.rs +β”‚ └── lib.rs +β”œβ”€β”€ build.sh # build script +β”œβ”€β”€ Cargo.toml # package manager +β”œβ”€β”€ README.md +β”œβ”€β”€ rust-toolchain.toml +└── test.sh # test script ``` + + + + --- -### Contract +## Smart Contract + The contract exposes methods to query the greeting and change it. These methods do nothing but calling `get_greeting` and `set_greeting` in the `hello-near` example. - + ---- +### Testing the Contract -## Testing +The contract readily includes a set of unit and sandbox testing to validate its functionality. To execute the tests, run the following commands: -When writing smart contracts it is very important to test all methods exhaustively. In this -project you have two types of tests: unit and integration. Before digging in them, -go ahead and perform the tests present in the dApp through the command `yarn test`. + + -### Unit test +```bash +cd contract-simple-ts +yarn +yarn test +``` -Unit tests check individual functions in the smart contract. They are written in the -same language as the smart contract is. + + + + ```bash + cd contract-simple-rs + ./test.sh + ``` -Since this example handles Cross-contract calls, in the unit tests we only test the `initialize` -method works. This is because unit tests are **cannot test** cross-contract calls. + -### Integration test + -In this project in particular, the integration tests first deploy the `hello-near` contract. Then, -they test that the cross-contract call correctly sets and retrieves the message. You will find the integration tests -in `integration-tests/`. +:::tip +The `integration tests` use a sandbox to create NEAR users and simulate interactions with the contract. +::: - - - - - +
+ +### Deploying the Contract to the NEAR network + +In order to deploy the contract you will need to [create a NEAR account](/develop/contracts/quickstart#create-a-testnet-account). + + + + +```bash +# Optional - create an account +near create-account --useFaucet + +# Deploy the contract +cd contract-simple-ts +yarn build +near deploy ./build/cross_contract.wasm init --initFunction init --initArgs '{"hello_account":"hello.near-example.testnet"}' +``` + + + + +```bash +# Optional - create an account +near create-account --useFaucet + +# Deploy the contract +cd contract-simple-rs +./build.sh +near deploy ./target/wasm32-unknown-unknown/release/cross_contract.wasm --initFunction init --initArgs '{"hello_account":"hello.near-example.testnet"}' + +``` + + + + +
+ +### CLI: Interacting with the Contract + +To interact with the contract through the console, you can use the following commands: + +```bash +# Get message from the hello-near contract +# Replace with your account ID +near call query_greeting --accountId + +# Set a new message for the hello-near contract +# Replace with your account ID +near call change_greeting '{"new_greeting":"XCC Hi"}' --accountId +``` --- @@ -129,5 +190,6 @@ contract!. In this way, you can try to make a cross-contract call that attaches and to return the money to the user in case of error. ### Advanced Cross Contract Calls + Your contract can perform multiple cross-contract calls in simultaneous, creating promises that execute in parallel, or as a batch transaction. Check the [advanced cross contract calls tutorial](./advanced-xcc) to learn more. diff --git a/docs/sdk/rust/promises/token-tx.md b/docs/sdk/rust/promises/token-tx.md index f452ddd6d88..149ce1457c8 100644 --- a/docs/sdk/rust/promises/token-tx.md +++ b/docs/sdk/rust/promises/token-tx.md @@ -48,7 +48,7 @@ Most of this is boilerplate you're probably familiar with by now – imports, s * `AccountId`: this will automatically check that the provided string is a well-formed NEAR account ID, and panic with a useful error if not. -* Returning `Promise`: This allows NEAR Explorer, near-cli, near-api-js, and other tooling to correctly determine if a whole chain of transactions is successful. If your function does not return `Promise`, tools like near-cli will return immediately after your function call. And then even if the `transfer` fails, your function call will be considered successful. You can see an example of this behavior [here](https://github.com/near-examples/xcc-advanced). +* Returning `Promise`: This allows NEAR Explorer, near-cli, near-api-js, and other tooling to correctly determine if a whole chain of transactions is successful. If your function does not return `Promise`, tools like near-cli will return immediately after your function call. And then even if the `transfer` fails, your function call will be considered successful. You can see an example of this behavior [here](/tutorials/examples/advanced-xcc). Using near-cli or near-cli-rs, someone could invoke this function with a call like: