Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add multiversx example #169

Merged
merged 20 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ aptos-artifacts
chain-config/*.json
.aptos
!./**/artifacts/send_receive.wasm

.multiversx
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ If not specified, this will print balances of the wallet for testnet.
- [Evm Examples](/examples/evm/)
- [Aptos Examples](/examples/aptos/)
- [Cosmos Examples](/examples/cosmos/)
- [MultiversX Examples](/examples/multiversx/)
- [Web Examples](/examples-web/)
3 changes: 3 additions & 0 deletions build-multiversx.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# !/bin/bash

cd examples/multiversx/call-contract/hello-world && mxpy contract build
3 changes: 3 additions & 0 deletions config/ci.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
},
"cosmos": {
"enabled": false
},
"multiversx": {
"enabled": false
}
}
3 changes: 3 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
},
"cosmos": {
"enabled": false
},
"multiversx": {
"enabled": true
}
}
58 changes: 58 additions & 0 deletions examples/multiversx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# MultiversX Examples

## Prerequisite

0. You should have Docker & Docker Compose installed.

1. Install Mxpy CLI Tool

Download from here: https://docs.multiversx.com/sdk-and-tools/sdk-py/installing-mxpy/

> **Note**: Our examples are tested on Mxpy version `9.4.1`, but newer versions might also work.

2. Optional: Build MultiversX contracts (make sure you use Rust Nightly version at least 1.76.0 - nightly-2023-12-11).
However this is optional, a default wasm contract is provided.

```bash
npm run build-multiversx
```

3. Run Elasticsearch

`dcker-compose up -d` (in this folder)

4. Create & run a MultiversX Localnet

More info: https://docs.multiversx.com/developers/setup-local-testnet

```bash
mkdir -p .multiversx && cd .multiversx
mxpy localnet setup
```

You will now have `localnet` folder populate with the subfolders `validator00`, `validator01`, `validator02`.

Copy the [external.toml](external.toml) from this folder into all the validators `config` folder (eg full path: `.multiversx/localnet/validator00/config`)
and overwrite the existing file.

This will connect MultiversX nodes to Elasticsearch to index events used by the MultiversXRelayer.

Then start the localnet: (wait a bit afterwards for everything to initialize)

```bash
mxpy localnet start
````

5. Check that `multiversx` - `enabled` flag is set to `true` in `config/default.json` file

6. Run the local server (from the project root)

```bash
npm run start
```

Wait for all the contracts to be successfully deployed.

## Basic

- [call-contract](call-contract/)
38 changes: 38 additions & 0 deletions examples/multiversx/call-contract/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Call Contract

Relay a message bi-directional between MultiversX and an EVM chain.

Deploy:

```bash
npm run deploy multiversx/call-contract local
```

Run the test:

```bash
npm run execute multiversx/call-contract local ${evmChain} ${message}
```

**Default Values**:

- `evmChain` is `Avalanche`. Valid values are Moonbeam, Avalanche, Fantom, Ethereum, and Polygon
- `message` is `Hello World`

## Example

```bash
npm run deploy multiversx/call-contract local
npm run execute multiversx/call-contract local "Avalanche" 'Hello World'
```

Output:

```
--- Initially ---
value at Avalanche is ""
value at MultiversX is ""
--- After ---
value at Avalanche is "Hello Avalanche from MultiversX, it is 7:05:48 PM."
value at MultiversX is "Hello MultiversX from Avalanche, it is 7:05:48 PM."
```
53 changes: 53 additions & 0 deletions examples/multiversx/call-contract/contracts/HelloWorld.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import { AxelarExecutable } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/executable/AxelarExecutable.sol';
import { IAxelarGateway } from '@axelar-network/axelar-cgp-solidity/contracts/interfaces/IAxelarGateway.sol';
import { IAxelarGasService } from '@axelar-network/axelar-cgp-solidity/contracts/interfaces/IAxelarGasService.sol';

contract HelloWorld is AxelarExecutable {
string public value;
string public sourceChain;
string public sourceAddress;
IAxelarGasService gasService;

constructor(address _gateway, address _gasReceiver)
AxelarExecutable(_gateway)
{
gasService = IAxelarGasService(_gasReceiver);
}

event Executed();

// Call this function to update the value of this contract along with all its siblings'.
function setRemoteValue(
string memory destinationChain,
string memory destinationAddress,
string calldata message
) external payable {
require(msg.value > 0, 'Gas payment is required');

bytes memory payload = abi.encodePacked(message);
gasService.payNativeGasForContractCall{value: msg.value}(
address(this),
destinationChain,
destinationAddress,
payload,
msg.sender
);
gateway.callContract(destinationChain, destinationAddress, payload);
}

// Handles calls created by setAndSend. Updates this contract's value
function _execute(
string calldata sourceChain_,
string calldata sourceAddress_,
bytes calldata payload_
) internal override {
(value) = abi.decode(payload_, (string));
sourceChain = sourceChain_;
sourceAddress = sourceAddress_;

emit Executed();
}
}
9 changes: 9 additions & 0 deletions examples/multiversx/call-contract/hello-world/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Generated by Cargo
# will have compiled files and executables
/target/
*/target/

# The mxpy output
/output/*

!/output/hello-world.wasm
Loading