Skip to content

Commit

Permalink
test: Start ojo unit test implementation with mock axelar setup
Browse files Browse the repository at this point in the history
  • Loading branch information
rbajollari committed Nov 15, 2023
1 parent 9475402 commit 11b101c
Show file tree
Hide file tree
Showing 18 changed files with 1,389 additions and 50 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
test-unit-contract:
test-unit:
yarn hardhat test

compile-contract:
compile-contracts:
yarn hardhat compile && yarn hardhat export-abi

lint-contract:
Expand Down
44 changes: 24 additions & 20 deletions contracts/Ojo.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import "./OjoTypes.sol";
contract Ojo is IOjo, AxelarExecutable {
IAxelarGasService public immutable gasReceiver;

string public ojoChain;
string private ojoChain;

string public ojoAddress;
string private ojoAddress;

mapping(bytes32 => OjoTypes.PriceData) public priceData;
mapping(bytes32 => OjoTypes.PriceData) private priceData;

constructor(
address gateway_,
Expand Down Expand Up @@ -50,36 +50,40 @@ contract Ojo is IOjo, AxelarExecutable {
}

function _execute(
string calldata sourceChain,
string calldata sourceAddress,
string calldata,
string calldata,
bytes calldata payload
) internal override {
(
OjoTypes.PriceData[] memory _priceData,
bytes memory _encodedPriceData,
bytes32[] memory assetNames,
address contractAddress,
bytes4 commandSelector,
bytes memory commandParams
) = abi.decode(
payload,
(OjoTypes.PriceData[], bytes32[], address, bytes4, bytes)
(bytes, bytes32[], address, bytes4, bytes)
);

OjoTypes.PriceData[] memory _priceData = abi.decode(_encodedPriceData, (OjoTypes.PriceData[]));
postPriceData(_priceData);

(bool success, bytes memory result) = contractAddress.call(
abi.encodeWithSelector(commandSelector, assetNames, commandParams)
);

if (!success) {
if (result.length == 0) {
require(success, 'Failed with no reason');
} else {
// rethrow same error
assembly {
let start := add(result, 0x20)
let end := add(result, mload(result))
revert(start, end)
// Call contract only if command selector is non empty
if (commandSelector != OjoTypes.EMPTY_COMMAND_SELECTOR) {
(bool success, bytes memory result) = contractAddress.call(
abi.encodeWithSelector(commandSelector, assetNames, commandParams)
);

if (!success) {
if (result.length == 0) {
require(success, 'Failed with no reason');
} else {
// rethrow same error
assembly {
let start := add(result, 0x20)
let end := add(result, mload(result))
revert(start, end)
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions contracts/OjoTypes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ library OjoTypes {
bytes32 constant USD = bytes32("USD");
uint256 constant USD_PRICE= 10**9;

bytes4 constant EMPTY_COMMAND_SELECTOR = bytes4(keccak256(bytes("")));

struct PriceData {
// Name of asset ex: ATOM
bytes32 assetName;
Expand Down
Loading

0 comments on commit 11b101c

Please sign in to comment.