Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ZetaChain.omniChainSwap() function #135

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions contracts/OnlySystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ pragma solidity ^0.8.7;
import "@zetachain/protocol-contracts/contracts/zevm/SystemContract.sol";

contract OnlySystem {
address internal systemContractAddress;

error OnlySystemContract(string);

modifier onlySystem(SystemContract systemContract) {
if (msg.sender != address(systemContract)) {
constructor(address _systemContractAddress) {
systemContractAddress = _systemContractAddress;
}

modifier onlySystem() {
if (msg.sender != systemContractAddress) {
revert OnlySystemContract(
"Only system contract can call this function"
);
Expand Down
79 changes: 79 additions & 0 deletions contracts/ZetaChain.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;

import "./OnlySystem.sol";
import "./BytesHelperLib.sol";
import "./SwapHelperLib.sol";
import "@zetachain/protocol-contracts/contracts/zevm/SystemContract.sol";
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/IWZETA.sol";

abstract contract ZetaChain is zContract, OnlySystem {
SystemContract public systemContract;
mapping(uint256 => address) private systemContracts;

constructor() OnlySystem(_initializeSystemContracts()) {
address systemContractAddress = systemContracts[block.chainid];
systemContract = SystemContract(systemContractAddress);
}

function _initializeSystemContracts() private returns (address) {
systemContracts[7000] = 0x91d18e54DAf4F677cB28167158d6dd21F6aB3921;
systemContracts[7001] = 0xEdf1c3275d13489aCdC6cD6eD246E72458B8795B;
return systemContracts[block.chainid];
}

function omniChainSwap(
zContext calldata context,
address zrc20,
uint256 amount,
bytes calldata message
) internal virtual {
address target;
bytes memory to;

if (context.chainID == 18332) {
target = BytesHelperLib.bytesToAddress(message, 0);
to = abi.encodePacked(BytesHelperLib.bytesToAddress(message, 20));
} else {
(address targetToken, bytes memory recipient) = abi.decode(
message,
(address, bytes)
);
target = targetToken;
to = recipient;
}

address wzeta = systemContract.wZetaContractAddress();
bool isTargetZeta = target == wzeta;
uint256 inputForGas;
address gasZRC20;
uint256 gasFee;

if (!isTargetZeta) {
(gasZRC20, gasFee) = IZRC20(target).withdrawGasFee();

inputForGas = SwapHelperLib.swapTokensForExactTokens(
systemContract,
zrc20,
gasFee,
gasZRC20,
amount
);
}

uint256 outputAmount = SwapHelperLib.swapExactTokensForTokens(
systemContract,
zrc20,
isTargetZeta ? amount : amount - inputForGas,
target,
0
);

if (isTargetZeta) {
IWETH9(wzeta).transfer(address(uint160(bytes20(to))), outputAmount);
} else {
IZRC20(gasZRC20).approve(target, gasFee);
IZRC20(target).withdraw(to, outputAmount);
}
}
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,6 @@
"spinnies": "^0.5.1",
"tiny-secp256k1": "^2.2.3",
"ws": "^8.13.0"
}
}
},
"packageManager": "[email protected]+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72"
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;

import "@zetachain/protocol-contracts/contracts/zevm/SystemContract.sol";
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/zContract.sol";
import "@zetachain/toolkit/contracts/OnlySystem.sol";
import "@zetachain/toolkit/contracts/ZetaChain.sol";

contract {{contractName}} is zContract, OnlySystem {
SystemContract public systemContract;

constructor(address systemContractAddress) {
systemContract = SystemContract(systemContractAddress);
}
contract {{contractName}} is ZetaChain {
constructor() ZetaChain() {}

function onCrossChainCall(
zContext calldata context,
address zrc20,
uint256 amount,
bytes calldata message
) external virtual override onlySystem(systemContract) {
) external override onlySystem {
{{#if arguments.pairsWithDataLocation}}
({{#each arguments.pairsWithDataLocation}}{{#if @index}}, {{/if}}{{this.[1]}} {{this.[0]}}{{/each}}) = abi.decode(
message,
Expand Down
4 changes: 1 addition & 3 deletions packages/tasks/templates/omnichain/tasks/deploy.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
);
}

const systemContract = getAddress("systemContract", network);

const factory = await hre.ethers.getContractFactory(args.name);
const contract = await factory.deploy(systemContract);
const contract = await factory.deploy();
await contract.deployed();

const isTestnet = network === "zeta_testnet";
Expand Down
2 changes: 2 additions & 0 deletions typechain-types/@openzeppelin/contracts/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/* Autogenerated file. Do not edit manually. */
/* tslint:disable */
/* eslint-disable */
import type * as access from "./access";
export type { access };
import type * as token from "./token";
export type { token };
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
/* eslint-disable */
import type * as interfaces from "./interfaces";
export type { interfaces };
import type * as tools from "./tools";
export type { tools };
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/* eslint-disable */
import type * as zetaInterfacesSol from "./ZetaInterfaces.sol";
export type { zetaInterfacesSol };
export type { ZetaInteractorErrors } from "./ZetaInteractorErrors";
Loading
Loading