Skip to content

Commit

Permalink
update to toolkit v8-rc1
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed May 9, 2024
1 parent c5cf646 commit ca0b53f
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 46 deletions.
12 changes: 4 additions & 8 deletions omnichain/swap/contracts/Swap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import "@zetachain/toolkit/contracts/SwapHelperLib.sol";
import "@zetachain/toolkit/contracts/BytesHelperLib.sol";

contract Swap is zContract {
SystemContract public immutable systemContract;
SystemContract public systemContract;
uint256 constant BITCOIN = 18332;

constructor(address systemContractAddress) {
Expand Down Expand Up @@ -49,19 +49,15 @@ contract Swap is zContract {
.withdrawGasFee();

uint256 inputForGas = SwapHelperLib.swapTokensForExactTokens(
systemContract.wZetaContractAddress(),
systemContract.uniswapv2FactoryAddress(),
systemContract.uniswapv2Router02Address(),
systemContract,
zrc20,
gasFee,
gasZRC20,
amount
);

uint256 outputAmount = SwapHelperLib._doSwap(
systemContract.wZetaContractAddress(),
systemContract.uniswapv2FactoryAddress(),
systemContract.uniswapv2Router02Address(),
uint256 outputAmount = SwapHelperLib.swapExactTokensForTokens(
systemContract,
zrc20,
amount - inputForGas,
targetTokenAddress,
Expand Down
42 changes: 15 additions & 27 deletions omnichain/swap/contracts/SwapExtended.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,13 @@ import "@zetachain/toolkit/contracts/SwapHelperLib.sol";
import "@zetachain/toolkit/contracts/BytesHelperLib.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract SwapExtended is zContract {
contract SwapZeta is zContract {
SystemContract public systemContract;
address public wzeta;
address public factory;
address public router;

uint256 constant BITCOIN = 18332;

constructor(address systemContractAddress) {
systemContract = SystemContract(systemContractAddress);
wzeta = systemContract.wZetaContractAddress();
factory = systemContract.uniswapv2FactoryAddress();
router = systemContract.uniswapv2Router02Address();
}

modifier onlySystem() {
Expand All @@ -36,55 +30,49 @@ contract SwapExtended is zContract {
uint256 amount,
bytes calldata message
) external virtual override onlySystem {
address targetTokenAddress;
bytes memory recipientAddress;
address target;
bytes memory to;

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

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

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

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

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

if (!isTargetZeta) {
IZRC20(gasZRC20).approve(targetTokenAddress, gasFee);
IZRC20(targetTokenAddress).withdraw(recipientAddress, outputAmount);
IZRC20(gasZRC20).approve(target, gasFee);
IZRC20(target).withdraw(to, outputAmount);
}
}
}
2 changes: 1 addition & 1 deletion omnichain/swap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@types/node": ">=12.0.0",
"@typescript-eslint/eslint-plugin": "^5.59.9",
"@typescript-eslint/parser": "^5.59.9",
"@zetachain/toolkit": "7.0.0",
"@zetachain/toolkit": "8.0.0-rc1",
"axios": "^1.3.6",
"chai": "^4.2.0",
"dotenv": "^16.0.3",
Expand Down
19 changes: 13 additions & 6 deletions omnichain/swap/tasks/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { getAddress } from "@zetachain/protocol-contracts";
import { getAddress, ParamChainName } from "@zetachain/protocol-contracts";
import { task } from "hardhat/config";
import { HardhatRuntimeEnvironment } from "hardhat/types";

const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
if (hre.network.name !== "zeta_testnet") {
const network = hre.network.name as ParamChainName;

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

Expand All @@ -16,20 +18,25 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
);
}

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

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

const isTestnet = network === "zeta_testnet";
const zetascan = isTestnet ? "athens.explorer" : "explorer";
const blockscout = isTestnet ? "zetachain-athens-3" : "zetachain";

if (args.json) {
console.log(JSON.stringify(contract));
} else {
console.log(`🔑 Using account: ${signer.address}
🚀 Successfully deployed contract on ZetaChain.
🚀 Successfully deployed contract on ${network}.
📜 Contract address: ${contract.address}
🌍 Explorer: https://athens3.explorer.zetachain.com/address/${contract.address}
🌍 ZetaScan: https://${zetascan}.zetachain.com/address/${contract.address}
🌍 Blockcsout: https://${blockscout}.blockscout.com/address/${contract.address}
`);
}
};
Expand Down
8 changes: 4 additions & 4 deletions omnichain/swap/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1736,10 +1736,10 @@
resolved "https://registry.yarnpkg.com/@zetachain/protocol-contracts/-/protocol-contracts-7.0.0-rc1.tgz#588483d1ec70e572b7e40e84ef5b34282b0ab375"
integrity sha512-vgS+Pjh4MysOyw8WbqTQVBsHJYqKvMcdV7cNVqxaTJd/dl2ak7NNvsIeaeUnxQrp8XfQol2B8GXJpVLM6MK/dg==

"@zetachain/toolkit@7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@zetachain/toolkit/-/toolkit-7.0.0.tgz#7301ccab40c37d3fd8fcc347f99c831ec31b1bd3"
integrity sha512-5cOJVBIEcosF2A2TJbNGFfh4Bob8UcodQII1RdHRstqvV3toZ18r1gVWpLGQ8w2N6T2FCPE8ueN4Q5zH68q20Q==
"@zetachain/toolkit@8.0.0-rc1":
version "8.0.0-rc1"
resolved "https://registry.yarnpkg.com/@zetachain/toolkit/-/toolkit-8.0.0-rc1.tgz#f7ef34393e63a7e33c2e7b27c307b9423972151f"
integrity sha512-V0uqIQgfYDCg2Ni4ccy6Il3IdOjgOZ5nISTURidq4wocLDAPQHl0yoos9GoVqL5QYa49WLFFEL8lRXouU0qz3g==
dependencies:
"@inquirer/prompts" "^2.1.1"
"@inquirer/select" "1.1.3"
Expand Down

0 comments on commit ca0b53f

Please sign in to comment.