-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
64d0126
commit 7e31c2f
Showing
3 changed files
with
218 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
/** | ||
* SwapMock.sol - SKALE Interchain Messaging Agent | ||
* Copyright (C) 2021-Present SKALE Labs | ||
* @author Dmytro Stebaiev | ||
* | ||
* SKALE IMA is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* SKALE IMA is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with SKALE IMA. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
|
||
pragma solidity 0.8.27; | ||
|
||
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
|
||
contract SwapMock { | ||
IERC20 tokenA; | ||
IERC20 tokenB; | ||
|
||
error UnknownToken(IERC20 token); | ||
|
||
function swap(IERC20 token, uint256 amount) external returns (uint256 resultAmount) { | ||
IERC20 anotherToken = getAnotherToken(token); | ||
address user = msg.sender; | ||
token.transferFrom(user, address(this), amount); | ||
anotherToken.transfer(user, amount); | ||
return amount; | ||
} | ||
|
||
function setTokenA(IERC20 token) external { | ||
tokenA = token; | ||
} | ||
|
||
function setTokenB(IERC20 token) external { | ||
tokenB = token; | ||
} | ||
|
||
// public | ||
|
||
function getAnotherToken(IERC20 token) public view returns (IERC20 anotherToken) { | ||
require(tokenA == token || tokenB == token, UnknownToken(token)); | ||
if (token == tokenA) { | ||
return tokenB; | ||
} else if (token == tokenB) { | ||
return tokenA; | ||
} else { | ||
revert UnknownToken(token); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
/** | ||
* Executor.sol - SKALE Interchain Messaging Agent | ||
* Copyright (C) 2024-Present SKALE Labs | ||
* @author Dmytro Stebaiev | ||
* | ||
* SKALE IMA is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* SKALE IMA is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with SKALE IMA. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
pragma solidity 0.8.27; | ||
|
||
import "hardhat/console.sol"; | ||
|
||
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
import {ExecutorId} from "@skalenetwork/ima-interfaces/schain/ExecutionLayer/IExecutor.sol"; | ||
import {TokenInfo} from "@skalenetwork/ima-interfaces/schain/ExecutionLayer/IActionExecutor.sol"; | ||
|
||
import {Executor} from "../../../schain/ExecutionLayer/Executor.sol"; | ||
import {SwapMock} from "../SwapMock.sol"; | ||
|
||
contract SwapMockSwap is Executor { | ||
ExecutorId public constant ID = ExecutorId.wrap(keccak256("SwapMockSwap")); | ||
SwapMock exchange; | ||
|
||
function setExchange(SwapMock exchangeAddress) external { | ||
exchange = exchangeAddress; | ||
} | ||
|
||
function execute( | ||
TokenInfo[] memory inputTokens, | ||
bytes memory | ||
) | ||
external | ||
override | ||
returns (TokenInfo[] memory outputTokens) | ||
{ | ||
outputTokens = new TokenInfo[](inputTokens.length); | ||
for (uint256 i = 0; i < inputTokens.length; ++i) { | ||
IERC20 token = IERC20(getTokenAddress(inputTokens[i])); | ||
IERC20 anotherToken = exchange.getAnotherToken(token); | ||
uint256 anotherValue = exchange.swap(token, inputTokens[i].value); | ||
outputTokens[i].token = address(anotherToken); | ||
outputTokens[i].value = anotherValue; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters