Skip to content

Commit

Permalink
feat: Add call contract with token method
Browse files Browse the repository at this point in the history
  • Loading branch information
rbajollari committed Jan 3, 2024
1 parent 9da9c10 commit e4dd261
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
20 changes: 19 additions & 1 deletion contracts/IOjo.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ interface IOjo {
/// @param timestamp Time of price data being posted.
event PriceDataPosted(uint256 indexed timestamp);


/// @notice Triggers the relaying of price data from the Ojo Network to the Ojo contract and uses said price data
/// when calling the specified contract method at the specified contract address.
/// @dev Reverts if contract method call does not succeed.
Expand All @@ -30,6 +29,25 @@ interface IOjo {
bytes calldata commandParams
) external payable;

/// @notice Triggers the relaying of price data from the Ojo Network to the Ojo contract with an ERC-20 token for
/// and uses said price data when calling the specified contract method at the specified contract address.
/// @dev Reverts if contract method call does not succeed.
/// @param assetNames List of assets to be relayed from the Ojo Network and used by the contract method.
/// @param contractAddress Address of contract containing the contract method to be called.
/// @param commandSelector First four bytes of the Keccak-256 hash of the contract method to be called.
/// @param commandParams Abi encoded parameters to be used when calling the contract method (excluding assetNames
/// parameter).
/// @param symbol The symbol of the token to be sent with the call.
/// @param amount The amount of tokens to be sent with the call.
function callContractMethodWithOjoPriceDataAndToken(
bytes32[] calldata assetNames,
address contractAddress,
bytes4 commandSelector,
bytes calldata commandParams,
string memory symbol,
uint256 amount
) external payable;

/// @notice Returns the price data of a specified asset.
/// @dev Price data is stored in a mapping, so requesting the price data of a non existent asset will return the
/// zero byte representation of OjoTypes.PriceData.
Expand Down
31 changes: 31 additions & 0 deletions contracts/Ojo.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity ^0.8.20;
import "@axelar-network/axelar-gmp-sdk-solidity/contracts/executable/AxelarExecutable.sol";
import "@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IAxelarGateway.sol";
import "@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IAxelarGasService.sol";
import "@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "./IOjo.sol";
import "./OjoTypes.sol";
Expand Down Expand Up @@ -54,6 +55,36 @@ contract Ojo is IOjo, AxelarExecutable, Ownable {
gateway.callContract(ojoChain, ojoAddress, payloadWithVersion);
}

function callContractMethodWithOjoPriceDataAndToken(
bytes32[] calldata assetNames,
address contractAddress,
bytes4 commandSelector,
bytes calldata commandParams,
string memory symbol,
uint256 amount
) external payable {
address tokenAddress = gateway.tokenAddresses(symbol);
IERC20(tokenAddress).transferFrom(msg.sender, address(this), amount);
IERC20(tokenAddress).approve(address(gateway), amount);

bytes memory payloadWithVersion = abi.encodePacked(
bytes4(uint32(0)), // version number
abi.encode(assetNames, contractAddress, commandSelector, commandParams, block.timestamp) // payload
);

gasReceiver.payNativeGasForContractCallWithToken{value: msg.value}(
address(this),
ojoChain,
ojoAddress,
payloadWithVersion,
symbol,
amount,
msg.sender
);

gateway.callContractWithToken(ojoChain, ojoAddress, payloadWithVersion, symbol, amount);
}

function _execute(
string calldata,
string calldata,
Expand Down

0 comments on commit e4dd261

Please sign in to comment.