Skip to content

Commit

Permalink
feat: make swap example compatible with Bitcoin (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev authored Oct 9, 2023
1 parent abb7a57 commit 12c6d3a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
39 changes: 36 additions & 3 deletions omnichain/swap/contracts/Swap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import "@zetachain/protocol-contracts/contracts/zevm/SystemContract.sol";
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/zContract.sol";

import "@zetachain/toolkit/contracts/SwapHelperLib.sol";
import "@zetachain/toolkit/contracts/BytesHelperLib.sol";

contract Swap is zContract {
error SenderNotSystemContract();
error WrongGasContract();
error NotEnoughToPayGasFee();

SystemContract public immutable systemContract;
uint256 constant BITCOIN = 18332;

constructor(address systemContractAddress) {
systemContract = SystemContract(systemContractAddress);
Expand All @@ -24,8 +28,29 @@ contract Swap is zContract {
if (msg.sender != address(systemContract)) {
revert SenderNotSystemContract();
}
(address targetZRC20, bytes32 recipient, uint256 minAmountOut) = abi
.decode(message, (address, bytes32, uint256));

uint32 targetChainID;
bytes memory recipient;
uint256 minAmountOut;

if (context.chainID == BITCOIN) {
targetChainID = BytesHelperLib.bytesToUint32(message, 0);
recipient = BytesHelperLib.bytesToBech32Bytes(message, 4);
} else {
(
uint32 targetChainID_,
bytes32 recipient_,
uint256 minAmountOut_
) = abi.decode(message, (uint32, bytes32, uint256));
targetChainID = targetChainID_;
recipient = abi.encodePacked(recipient_);
minAmountOut = minAmountOut_;
}

address targetZRC20 = systemContract.gasCoinZRC20ByChainId(
targetChainID
);

uint256 outputAmount = SwapHelperLib._doSwap(
systemContract.wZetaContractAddress(),
systemContract.uniswapv2FactoryAddress(),
Expand All @@ -35,6 +60,14 @@ contract Swap is zContract {
targetZRC20,
minAmountOut
);
SwapHelperLib._doWithdrawal(targetZRC20, outputAmount, recipient);

(address gasZRC20, uint256 gasFee) = IZRC20(targetZRC20)
.withdrawGasFee();

if (gasZRC20 != targetZRC20) revert WrongGasContract();
if (gasFee >= outputAmount) revert NotEnoughToPayGasFee();

IZRC20(targetZRC20).approve(targetZRC20, gasFee);
IZRC20(targetZRC20).withdraw(recipient, outputAmount - gasFee);
}
}
2 changes: 1 addition & 1 deletion omnichain/swap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"@types/node": ">=12.0.0",
"@typescript-eslint/eslint-plugin": "^5.59.9",
"@typescript-eslint/parser": "^5.59.9",
"@zetachain/toolkit": "^2.1.2",
"@zetachain/toolkit": "^2.2.4",
"axios": "^1.3.6",
"chai": "^4.2.0",
"dotenv": "^16.0.3",
Expand Down
9 changes: 6 additions & 3 deletions omnichain/swap/tasks/interact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
const [signer] = await hre.ethers.getSigners();
console.log(`🔑 Using account: ${signer.address}\n`);

const targetZRC20 = getAddress("zrc20", args.destination);
const targetChainID = hre.config.networks[args.destination]?.chainId;
if (targetChainID === undefined) {
throw new Error("Invalid destination network");
}
const minAmountOut = BigNumber.from("0");

const data = prepareData(
args.contract,
["address", "bytes32", "uint256"],
[targetZRC20, args.recipient, minAmountOut]
["uint32", "bytes32", "uint256"],
[targetChainID, args.recipient, minAmountOut]
);
const to = getAddress("tss", hre.network.name as any);
const value = parseEther(args.amount);
Expand Down
8 changes: 4 additions & 4 deletions omnichain/swap/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1690,10 +1690,10 @@
resolved "https://registry.yarnpkg.com/@zetachain/protocol-contracts/-/protocol-contracts-2.1.0.tgz#775b4eee7c85d115232dece121cbfc798fde6b63"
integrity sha512-xmG6p8DizIk0h7Tr8Lt6UMG0ejrfRrPx0qH9ze3enwblo7W+eGP12oQ7djanYu50B0pNhh9z7xy/IYxKa9wD0Q==

"@zetachain/toolkit@^2.1.2":
version "2.1.2"
resolved "https://registry.yarnpkg.com/@zetachain/toolkit/-/toolkit-2.1.2.tgz#f83b57d323eab2f54961565b8bd15279eab964e7"
integrity sha512-eKZDUKWCf/h5RRvQsP0rG2qB0kKaw9GvpSylZwetx8+NckhKwQi52sUisX7sjHVqjDziTSdsagkrl4HmiPJ0pQ==
"@zetachain/toolkit@^2.2.4":
version "2.2.4"
resolved "https://registry.yarnpkg.com/@zetachain/toolkit/-/toolkit-2.2.4.tgz#4c80d160fdac1ebe9058c4f626934056476982f7"
integrity sha512-njh+owCjXAMe3PVvgm/5zIjErUqx87M/SciJXolAHYnbnKm85/gVwyguQu3mpHFQ5sIYBU2YN/W7HoMn6kVG1g==
dependencies:
"@inquirer/prompts" "^2.1.1"
"@inquirer/select" "1.1.3"
Expand Down

0 comments on commit 12c6d3a

Please sign in to comment.