Skip to content

Commit

Permalink
Keep only universal app examples
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Sep 12, 2024
1 parent 4833312 commit 6763ecf
Show file tree
Hide file tree
Showing 16 changed files with 9,178 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 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 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 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 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 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.
98 changes: 98 additions & 0 deletions swap/contracts/Swap.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

import {SystemContract, IZRC20} from "@zetachain/toolkit/contracts/SystemContract.sol";
import {SwapHelperLib} from "@zetachain/toolkit/contracts/SwapHelperLib.sol";
import {BytesHelperLib} from "@zetachain/toolkit/contracts/BytesHelperLib.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.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
);

if (gasZRC20 == params.target) {
IZRC20(gasZRC20).approve(gatewayAddress, outputAmount + gasFee);
} else {
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 {}
}
119 changes: 119 additions & 0 deletions swap/contracts/SwapToAnyToken.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

import {SystemContract, IZRC20} from "@zetachain/toolkit/contracts/SystemContract.sol";
import {SwapHelperLib} from "@zetachain/toolkit/contracts/SwapHelperLib.sol";
import {BytesHelperLib} from "@zetachain/toolkit/contracts/BytesHelperLib.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.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";
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/IWZETA.sol";

contract SwapToAnyToken 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;
bool withdraw;
}

function onCrossChainCall(
zContext calldata context,
address zrc20,
uint256 amount,
bytes calldata message
) external virtual override {
Params memory params = Params({
target: address(0),
to: bytes(""),
withdraw: true
});

if (context.chainID == BITCOIN) {
params.target = BytesHelperLib.bytesToAddress(message, 0);
params.to = abi.encodePacked(
BytesHelperLib.bytesToAddress(message, 20)
);
if (message.length >= 41) {
params.withdraw = BytesHelperLib.bytesToBool(message, 40);
}
} else {
(
address targetToken,
bytes memory recipient,
bool withdrawFlag
) = abi.decode(message, (address, bytes, bool));
params.target = targetToken;
params.to = recipient;
params.withdraw = withdrawFlag;
}

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

if (params.withdraw) {
(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
);

if (params.withdraw) {
if (gasZRC20 == params.target) {
IZRC20(gasZRC20).approve(gatewayAddress, outputAmount + gasFee);
} else {
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
})
);
} else {
IWETH9(params.target).transfer(
address(uint160(bytes20(params.to))),
outputAmount
);
}
}

function onRevert(RevertContext calldata revertContext) external override {}
}
11 changes: 11 additions & 0 deletions swap/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[profile.default]
src = 'contracts'
out = 'out'
viaIR = true
libs = ['node_modules', 'lib']
test = 'test'
cache_path = 'cache_forge'
verbosity = 3

[dependencies]
forge-std = { version = "1.9.2" }
25 changes: 25 additions & 0 deletions swap/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import "./tasks/deploy";
import "@zetachain/localnet/tasks";
import "@nomicfoundation/hardhat-toolbox";
import "@zetachain/toolkit/tasks";

import { getHardhatConfigNetworks } from "@zetachain/networks";
import { HardhatUserConfig } from "hardhat/config";

const config: HardhatUserConfig = {
networks: {
...getHardhatConfigNetworks(),
},
solidity: {
compilers: [
{ version: "0.5.10" /** For create2 factory */ },
{ version: "0.6.6" /** For uniswap v2 router*/ },
{ version: "0.5.16" /** For uniswap v2 core*/ },
{ version: "0.4.19" /** For weth*/ },
{ version: "0.8.7" },
{ version: "0.8.26" },
],
},
};

export default config;
Loading

0 comments on commit 6763ecf

Please sign in to comment.