-
Notifications
You must be signed in to change notification settings - Fork 49
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
Showing
16 changed files
with
9,178 additions
and
0 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,6 @@ | ||
.yarn | ||
artifacts | ||
cache | ||
coverage | ||
node_modules | ||
typechain-types |
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,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"), | ||
}, | ||
}, | ||
}, | ||
}; |
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,17 @@ | ||
node_modules | ||
.env | ||
coverage | ||
coverage.json | ||
typechain | ||
typechain-types | ||
dependencies | ||
|
||
# Hardhat files | ||
cache | ||
artifacts | ||
|
||
# Foundry files | ||
out | ||
cache_forge | ||
|
||
access_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,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. |
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,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. |
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,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 {} | ||
} |
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,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 {} | ||
} |
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,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" } |
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,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; |
Oops, something went wrong.