Skip to content

Commit

Permalink
Feat/gmp api examples canh (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
canhtrinh authored Oct 3, 2024
1 parent 31b27ff commit 16e15df
Show file tree
Hide file tree
Showing 29 changed files with 547 additions and 710 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
EVM_PRIVATE_KEY=YOUR_PRIVATE_KEY_HERE

# For amplifier examples
GMP_API_URL=
ENVIRONMENT= # e.g. devnet-amplifier, testnet, or mainnet
CRT_PATH= # e.g. './client.crt'
KEY_PATH= # e.g. './client.key'
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ chain-config/*.json
!./**/artifacts/send_receive.wasm

.multiversx

*.crt
*.key
321 changes: 112 additions & 209 deletions examples/amplifier/README.md

Large diffs are not rendered by default.

118 changes: 0 additions & 118 deletions examples/amplifier/amplifier.js

This file was deleted.

117 changes: 0 additions & 117 deletions examples/amplifier/amplifier.proto

This file was deleted.

12 changes: 0 additions & 12 deletions examples/amplifier/chains.json

This file was deleted.

14 changes: 14 additions & 0 deletions examples/amplifier/config/chains.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"devnet-amplifier": [
{
"name": "avalanche-fuji",
"rpc": "https://rpc.ankr.com/avalanche_fuji",
"gateway": "0xF128c84c3326727c3e155168daAa4C0156B87AD1"
},
{
"name": "xrpl-evm-sidechain",
"rpc": "https://rpc-evm-sidechain.xrpl.org",
"gateway": "0x48CF6E93C4C1b014F719Db2aeF049AA86A255fE2"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
const fs = require('fs');

const https = require('https');
const dotenv = require('dotenv');

// Load environment variables from .env file
dotenv.config();

// Load the certificate and key
const cert = fs.readFileSync(process.env.CRT_PATH);
const key = fs.readFileSync(process.env.KEY_PATH);

// Default configuration values
const defaults = {
// gRPC
HOST: "localhost",
PORT: "50051",
HOST: 'localhost',
PORT: '50051',

// GMP API
GMP_API_URL: "http://localhost:8080",
GMP_API_URL: 'http://localhost:8080',
};

const chainsConfigFile = JSON.parse(fs.readFileSync('./examples/amplifier/config/chains.json', 'utf8'));
const environment = process.env.ENVIRONMENT;
const chainsConfig = chainsConfigFile[environment];

function getConfig() {
const serverHOST = process.env.HOST || defaults.HOST;
const serverPort = process.env.PORT || defaults.PORT;
Expand All @@ -25,15 +33,17 @@ function getConfig() {
serverHOST,
serverPort,
gmpAPIURL,
chains: chainsConfig,
httpsAgent: new https.Agent({
cert,
key,
rejectUnauthorized: false,
}),
};
}

const chainsConfigFile = './examples/amplifier/chains.json';

function getChainConfig(chainName) {
const chainsConfig = JSON.parse(fs.readFileSync(chainsConfigFile, 'utf8'));

const chainConfig = chainsConfig.find(c => c.name === chainName);
const chainConfig = chainsConfig.find((c) => c.name === chainName);

if (!chainConfig) {
throw new Error(`RPC URL not found for chain: ${chainName}`);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"01924dc4-698e-7ad2-9f9b-0bad962771ef"
42 changes: 42 additions & 0 deletions examples/amplifier/contracts/AmplifierGMPTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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-gmp-sdk-solidity/contracts/interfaces/IAxelarGateway.sol';

/**
* @title AmplifierGMPTest
* @notice Send a message from chain A to chain B and stores gmp message
*/
contract AmplifierGMPTest is AxelarExecutable {
string public message;
string public sourceChain;
string public sourceAddress;

constructor(address _gateway) AxelarExecutable(_gateway) {}

/**
* @notice Send message from chain A to chain B
* @dev message param is passed in as gmp message
* @param destinationChain name of the dest chain (ex. "Fantom")
* @param destinationAddress address on dest chain this tx is going to
* @param _message message to be sent
*/
function setRemoteValue(string calldata destinationChain, string calldata destinationAddress, string calldata _message) external {
bytes memory payload = abi.encode(_message);
gateway.callContract(destinationChain, destinationAddress, payload);
}

/**
* @notice logic to be executed on dest chain
* @dev this is triggered automatically by relayer
* @param _sourceChain blockchain where tx is originating from
* @param _sourceAddress address on src chain where tx is originating from
* @param _payload encoded gmp message sent from src chain
*/
function _execute(string calldata _sourceChain, string calldata _sourceAddress, bytes calldata _payload) internal override {
(message) = abi.decode(_payload, (string));
sourceChain = _sourceChain;
sourceAddress = _sourceAddress;
}
}
Loading

0 comments on commit 16e15df

Please sign in to comment.