This example of contracts demostrates how to use generated clients to call another contracts, in this case, it is used in the Proxy contract which will use the contracts clients from ping and traffic light contracts that are in this repository.
The Proxy contract will send the messages to both contracts using the clients.
If you don't have Rust installed, follow the steps below:
-
Linux users need to install GCC and Clang (according to their distribution’s documentation).
-
For ubuntu users:
sudo apt install -y build-essential clang cmake curl
-
On macOS, you can get a compiler toolset with the command:
xcode-select --install
-
-
Install rust, in this case Rustup. With rustup you will install Rust, you will be able to change the Rust version and install additional toolchains.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
-
Then, you need to install the target to compile your contracts to Wasm:
rustup target add wasm32-unknown-unknown
-
Finally, you have to install a wasm-opt to optmize your contracts wasm files.
-
With linux users:
sudo apt install binaryen
-
With macOs users:
brew install binaryen
-
If you already have installed rust, you need to check for updates:
-
To compile your contracts, you need to have rust 1.81 or newer to be able to compile the contract.
rustup install 1.81 rustup default 1.81
-
Then, you need to install the target to compile your contracts to Wasm (In case you don't have it):
rustup target add wasm32-unknown-unknown
-
Finally, you have to install a wasm-opt to optmize your contracts wasm files (In case you don't have it):
-
With linux users:
sudo apt install binaryen
-
With macOs users:
brew install binaryen
-
The Proxy contract state use RefCell to store the state of the contract as a part of the Program itself, this avoids the errors of static variables and is safer to handle state, and for clients, it save on gas fees by keeping them in the state.
When you compile a contract, you can set that in compilation time it generates the client for the contract, it helps to send messages to the contract.
To use this clients in your contracts, you need to move that file in your contract directories, in this case, the Proxy Contract contains a clients
directory where stores all the clients for others contracts, this directory is in proxy_contract/app/src/clients
, where you will find the clients for ping and traffic light contracts.
In order to establish your own client, you must follow the following steps:
-
In case you need to set up your own client, your
build.rs
file must be configured to be able to generate it. You can see theproxy_contract/wasm/build.rs
file as an example to configure your contract. -
Next, you will need to copy the generated client file into the
clients
directory of the proxy contract, and, in themod.rs
file, you have to "import" your client (with pub mod file_name.rs). -
Then, you need to create the service to make calls to your contract using the client, you need to set the service with generics to be able to use the client in your custom service (you need to create the file and add it to the
mod.rs
file in the services directory). -
Once you create your new service to make calls to your contract, you have to add the route to your service in the program, and create the RefCell with the client to save in gas fees (you can follow the routes of the ping and traffic light contracts).
In the directory you will find the proxy
, ping
and traffic light
contracts, that you can compile and upload in the Gear IDEA
-
First you will have to clone the repository to the directory you want and enter to the template:
git clone https://github.com/Vara-Lab/Proxy-Contract-Template.git cd Proxy-Contract-Template
-
Second, you need to compile all the contracts, with (it's only one command):
cd ping_pong_contract && cargo b -r && cd ../traffic_light_contract && cargo b -r && cd ../proxy_contract && cargo b -r && cd ..
-
You need to deploy all the contracts to the Gear IDEA, you need to follow the next steps:
- To interact with the Gear IDEA and deploy your contract, you will need to download a wallet extension such as Polkadot-JS, Talisman, or Subwallet to interact with Substrate-based chains.
- Then you have to upload the contracts on Gear IDEA:
- Access Gear IDEA using your web browser.
- Connect your Substrate wallet to Gear IDE.
- Upload the
wasm.opt.wasm
(which is inside thetarget\wasm32-unknown-unknown\release
directory in each of the contracts ) andapp.idl
(which is inside thewasm
directory in each of the contracts) files by clicking the "Upload Program" button. - Repeat this steps with all contracts.
-
Next, you will need to configure the proxy contract so that it can send messages to the ping and traffic light contracts. This, establishing the ids of both contracts in the Proxy contract sending the messages with the ids.
-
By following the steps, you will have your proxy contract configured!, which can send messages to both contracts, as well as read the state of the contracts.