Skip to content

Commit

Permalink
gateway swap
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Sep 3, 2024
1 parent 9f170e4 commit 414b6ff
Show file tree
Hide file tree
Showing 24 changed files with 9,759 additions and 2 deletions.
4 changes: 2 additions & 2 deletions omnichain/swap/tasks/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { HardhatRuntimeEnvironment } from "hardhat/types";
const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
const network = hre.network.name as ParamChainName;

if (!/zeta_(testnet|mainnet)/.test(network)) {
if (!/zeta_(testnet|mainnet|localhost)/.test(network)) {
throw new Error(
'🚨 Please use either "zeta_testnet" or "zeta_mainnet" network to deploy to ZetaChain.'
'🚨 Please use either "zeta_testnet", "zeta_mainnet" or "localhost" network to deploy to ZetaChain.'
);
}

Expand Down
6 changes: 6 additions & 0 deletions universal/swap/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.yarn
artifacts
cache
coverage
node_modules
typechain-types
47 changes: 47 additions & 0 deletions universal/swap/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const path = require("path");

/**
* @type {import("eslint").Linter.Config}
*/
module.exports = {
env: {
browser: false,
es2021: true,
mocha: true,
node: true,
},
extends: ["plugin:prettier/recommended"],
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: 12,
},
plugins: [
"@typescript-eslint",
"prettier",
"simple-import-sort",
"sort-keys-fix",
"typescript-sort-keys",
],
rules: {
"@typescript-eslint/sort-type-union-intersection-members": "error",
camelcase: "off",
"simple-import-sort/exports": "error",
"simple-import-sort/imports": "error",
"sort-keys-fix/sort-keys-fix": "error",
"typescript-sort-keys/interface": "error",
"typescript-sort-keys/string-enum": "error",
},
settings: {
"import/parsers": {
"@typescript-eslint/parser": [".js", ".jsx", ".ts", ".tsx", ".d.ts"],
},
"import/resolver": {
node: {
extensions: [".js", ".jsx", ".ts", ".tsx", ".d.ts"],
},
typescript: {
project: path.join(__dirname, "tsconfig.json"),
},
},
},
};
17 changes: 17 additions & 0 deletions universal/swap/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
node_modules
.env
coverage
coverage.json
typechain
typechain-types
dependencies

# Hardhat files
cache
artifacts

# Foundry files
out
cache_forge

access_token
21 changes: 21 additions & 0 deletions universal/swap/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 ZetaChain

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
15 changes: 15 additions & 0 deletions universal/swap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# ZetaChain Contracts Template

## Getting Started

Install dependencies:

```
yarn
```

## Next Steps

Ready to dive in? Follow our [**🚀 smart contract
tutorials**](https://www.zetachain.com/docs/developers/tutorials/intro/) to
start building universal app contracts.
94 changes: 94 additions & 0 deletions universal/swap/contracts/Swap.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

import "./shared/SystemContract.sol";
import "./shared/SwapHelperLib.sol";
import "./shared/BytesHelperLib.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "./shared/libraries/UniswapV2Library.sol";

import {RevertContext, RevertOptions} from "@zetachain/protocol-contracts/contracts/Revert.sol";
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/UniversalContract.sol";
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/IGatewayZEVM.sol";

contract Swap is UniversalContract {
SystemContract public systemContract;
uint256 constant BITCOIN = 18332;
address constant gatewayAddress =
0xA51c1fc2f0D1a1b8494Ed1FE312d7C3a78Ed91C0;

constructor(address systemContractAddress) {
systemContract = SystemContract(systemContractAddress);
}

struct Params {
address target;
bytes to;
}

function onCrossChainCall(
zContext calldata context,
address zrc20,
uint256 amount,
bytes calldata message
) external override {
Params memory params = Params({target: address(0), to: bytes("")});
if (context.chainID == BITCOIN) {
params.target = BytesHelperLib.bytesToAddress(message, 0);
params.to = abi.encodePacked(
BytesHelperLib.bytesToAddress(message, 20)
);
} else {
(address targetToken, bytes memory recipient) = abi.decode(
message,
(address, bytes)
);
params.target = targetToken;
params.to = recipient;
}

uint256 inputForGas;
address gasZRC20;
uint256 gasFee;
uint256 swapAmount;

(gasZRC20, gasFee) = IZRC20(params.target).withdrawGasFee();

if (gasZRC20 == zrc20) {
swapAmount = amount - gasFee;
} else {
inputForGas = SwapHelperLib.swapTokensForExactTokens(
systemContract,
zrc20,
gasFee,
gasZRC20,
amount
);
swapAmount = amount - inputForGas;
}

uint256 outputAmount = SwapHelperLib.swapExactTokensForTokens(
systemContract,
zrc20,
swapAmount,
params.target,
0
);
IZRC20(gasZRC20).approve(gatewayAddress, gasFee);
IZRC20(params.target).approve(gatewayAddress, outputAmount);
IGatewayZEVM(gatewayAddress).withdraw(
params.to,
outputAmount,
params.target,
RevertOptions({
revertAddress: address(0),
callOnRevert: false,
abortAddress: address(0),
revertMessage: "",
onRevertGasLimit: 0
})
);
}

function onRevert(RevertContext calldata revertContext) external override {}
}
63 changes: 63 additions & 0 deletions universal/swap/contracts/shared/BytesHelperLib.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.26;

library BytesHelperLib {
error OffsetOutOfBounds();

function bytesToAddress(
bytes calldata data,
uint256 offset
) internal pure returns (address output) {
bytes memory b = data[offset:offset + 20];
assembly {
output := mload(add(b, 20))
}
}

function bytesMemoryToAddress(
bytes memory data,
uint256 offset
) internal pure returns (address output) {
assembly {
output := mload(add(add(data, offset), 32))
}
}

function bytesToUint32(
bytes calldata data,
uint256 offset
) internal pure returns (uint32 output) {
bytes memory b = data[offset:offset + 4];
assembly {
output := mload(add(b, 4))
}
}

function addressToBytes(
address someAddress
) internal pure returns (bytes32) {
return bytes32(uint256(uint160(someAddress)));
}

function bytesToBech32Bytes(
bytes calldata data,
uint256 offset
) internal pure returns (bytes memory) {
bytes memory bech32Bytes = new bytes(42);
for (uint i = 0; i < 42; i++) {
bech32Bytes[i] = data[i + offset];
}

return bech32Bytes;
}

function bytesToBool(
bytes calldata data,
uint256 offset
) internal pure returns (bool) {
if (offset >= data.length) {
revert OffsetOutOfBounds();
}
return uint8(data[offset]) != 0;
}
}
Loading

0 comments on commit 414b6ff

Please sign in to comment.