Skip to content

Commit

Permalink
feat: added mock token for valued example balance increasing now when…
Browse files Browse the repository at this point in the history
… running locally
  • Loading branch information
benjamin852 committed Apr 3, 2024
1 parent c383971 commit 052d47c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ pragma solidity 0.8.9;
import { AxelarExecutable } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/executable/AxelarExecutable.sol';
import { AxelarValuedExpressExecutable } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/express/AxelarValuedExpressExecutable.sol';
import { IAxelarGateway } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IAxelarGateway.sol';
import { IERC20 } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IERC20.sol';
import { IAxelarGasService } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IAxelarGasService.sol';
import { Upgradable } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/upgradable/Upgradable.sol';
import { MockERC20 } from './mocks/MockERC20.sol';

contract CallContractWithValuedExpress is AxelarValuedExpressExecutable {
IAxelarGasService public immutable gasService;
Expand All @@ -18,13 +18,13 @@ contract CallContractWithValuedExpress is AxelarValuedExpressExecutable {
function sendValuedMessage(
string memory _destinationChain,
string memory _destinationAddress,
string memory _symbol,
address _tokenAddr,
uint256 _amount,
address _receiver
) external payable {
require(msg.value > 0, 'insufficient funds');

bytes memory valuedMsg = _deriveMsgValueForNonGatewayTokenValueTransfer(_symbol, _amount, _receiver);
bytes memory valuedMsg = _deriveMsgValueForNonGatewayTokenValueTransfer(_tokenAddr, _amount, _receiver);

gasService.payNativeGasForContractCall{ value: msg.value }(
address(this),
Expand All @@ -38,7 +38,7 @@ contract CallContractWithValuedExpress is AxelarValuedExpressExecutable {

function _execute(string calldata, string calldata, bytes calldata _payload) internal override {
(address tokenAddress, uint256 value, address reciever) = abi.decode(_payload, (address, uint256, address));
IERC20(tokenAddress).transfer(reciever, value);
MockERC20(tokenAddress).mint(reciever, value);
}

function contractCallWithTokenValue(
Expand All @@ -58,11 +58,10 @@ contract CallContractWithValuedExpress is AxelarValuedExpressExecutable {
}

function _deriveMsgValueForNonGatewayTokenValueTransfer(
string memory _symbol,
address _tokenAddr,
uint256 _amount,
address _receiver
) internal view returns (bytes memory valueToBeTransferred) {
address tokenAddress = gateway.tokenAddresses(_symbol);
valueToBeTransferred = abi.encode(tokenAddress, _amount, _receiver);
) internal pure returns (bytes memory valueToBeTransferred) {
valueToBeTransferred = abi.encode(_tokenAddr, _amount, _receiver);
}
}
26 changes: 23 additions & 3 deletions examples/evm/call-contract-with-valued-express/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Call Contract with Valued Express
This is an example of sending an express transaction where the value is not a cross chain asset transfer using `callContractWithToken()`. Rather the value is in the `payload` of the transaction. Once the payload has been decoded and derived to a specific contract address that value can be transfered to the end receiver.

This is an example of sending an express transaction where the value is not a cross chain asset transfer using `callContractWithToken()`. Rather the value is in the `payload` of the transaction. Once the payload has been decoded and derived to a specific contract address that value can be transfered to the end receiver.

### Deployment

Expand All @@ -9,9 +10,9 @@ To deploy the contract, run the following command:
npm run deploy evm/call-contract-with-valued-express [local|testnet]
```

The aforementioned command pertains to specifying the intended environment for a project to execute on. It provides the option to choose between local and testnet environments by appending either `local` or `testnet` after the command.
The aforementioned command pertains to specifying the intended environment for a project to execute on. It provides the option to choose between local and testnet environments by appending either `local` or `testnet` after the command.

An example of its usage is demonstrated as follows: `npm run deploy evm/call-contract-with-valued-express local` or `npm run deploy evm/call-contract-with-valued-express testnet`.
An example of its usage is demonstrated as follows: `npm run deploy evm/call-contract-with-valued-express local` or `npm run deploy evm/call-contract-with-valued-express testnet`.

### Execution

Expand All @@ -30,3 +31,22 @@ Currently, our whitelisted contract addresses for this example are:

- Avalanche: `0x4E3b6C3d284361Eb4fA9aDE5831eEfF85578b72c`
- Polygon: `0xAb6dAb12aCCAe665A44E69d44bcfC6219A30Dd32`

## Example

```bash
npm run deploy evm/call-contract-with-valued-express local
npm run execute evm/call-contract-with-token local "Avalanche" "Fantom" 100 0xBa86A5719722B02a5D5e388999C25f3333c7A9fb

### Output:

```

--- Initially ---
0xBa86A5719722B02a5D5e388999C25f3333c7A9fb has 100 tokens
--- After ---
0xBa86A5719722B02a5D5e388999C25f3333c7A9fb has 200 tokens

```
```
12 changes: 7 additions & 5 deletions examples/evm/call-contract-with-valued-express/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ const {
const CallContractWithValuedExpress = rootRequire(
'./artifacts/examples/evm/call-contract-with-valued-express/CallContractWithValuedExpress.sol/CallContractWithValuedExpress.json',
);
const MockERC20 = rootRequire('./artifacts/examples/evm/call-contract-with-valued-express/mocks/MockERC20.sol/MockERC20.json');
async function deploy(chain, wallet) {
console.log(`Deploying Call Contract Valued Express for ${chain.name}.`);
chain.callContractWithValuedExpress = await deployContract(wallet, CallContractWithValuedExpress, [chain.gateway, chain.gasService]);
chain.mockToken = await deployContract(wallet, MockERC20);
chain.wallet = wallet;
console.log(`Deployed CallContractWithTokenExpress for ${chain.name} at ${chain.callContractWithValuedExpress.address}.`);
console.log(`Deployed CallContractWithValuedExpress for ${chain.name} at ${chain.callContractWithValuedExpress.address}.`);
}

async function execute(chains, wallet, options) {
Expand All @@ -22,14 +24,14 @@ async function execute(chains, wallet, options) {
const expressFee = await calculateBridgeExpressFee(source, destination);

// Get the balance of the first account.
const initialBalance = await destination.usdc.balanceOf(accounts[0]);
const initialBalance = await destination.mockToken.balanceOf(accounts[0]);

// Get the amount to send.
const amount = Math.floor(parseFloat(args[2])) * 1e6 || 10e6;

async function logAccountBalances() {
for (const account of accounts) {
console.log(`${account} has ${(await destination.usdc.balanceOf(account)) / 1e6} aUSDC on ${destination.name}`);
console.log(`${account} has ${(await destination.mockToken.balanceOf(account)) / 1e6} tokens on ${destination.name}`);
}
}

Expand All @@ -44,7 +46,7 @@ async function execute(chains, wallet, options) {
const sendTx = await source.callContractWithValuedExpress.sendValuedMessage(
destination.name,
destination.callContractWithValuedExpress.address,
'aUSDC',
destination.mockToken.address,
amount,
accounts[0],
{
Expand All @@ -55,7 +57,7 @@ async function execute(chains, wallet, options) {
console.log('Sent valued msg to destination contract:', sendTx.hash);

// Wait for the distribution to complete by checking the balance of the first account.
while ((await destination.usdc.balanceOf(accounts[0])).eq(initialBalance)) {
while ((await destination.mockToken.balanceOf(accounts[0])).eq(initialBalance)) {
await sleep(1000);
}

Expand Down
12 changes: 12 additions & 0 deletions examples/evm/call-contract-with-valued-express/mocks/MockERC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import '@openzeppelin/contracts/token/ERC20/ERC20.sol';

contract MockERC20 is ERC20 {
constructor() ERC20("Mock Token", 'MTK') {}

function mint(address to, uint256 amount) public {
_mint(to, amount);
}
}
2 changes: 1 addition & 1 deletion examples/tests/evm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('Verify EVM Examples', function () {
if (example.deploy) {
await deploy('local', chains, wallet, example);
}

await executeEVMExample('local', chains, [], wallet, example);
});
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/libs/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ async function executeEVMExample(env, chains, args, wallet, example) {
// Get source and destination chains.
const source = getSourceChain(chains, args, example.sourceChain);
const destination = getDestChain(chains, args, example.destinationChain);

// Listen for GMP events on testnet for printing an Axelarscan link for tracking.
const startBlockNumber = await source.provider.getBlockNumber();
listenForGMPEvent(env, source, startBlockNumber);
Expand Down

0 comments on commit 052d47c

Please sign in to comment.