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

Update the multioutput example to support Bitcoin #113

Merged
merged 22 commits into from
Apr 4, 2024
Merged
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "omnichain/multioutput/lib/forge-std"]
path = omnichain/multioutput/lib/forge-std
url = https://github.com/foundry-rs/forge-std
5 changes: 5 additions & 0 deletions omnichain/multioutput/.gitignore
Original file line number Diff line number Diff line change
@@ -9,4 +9,9 @@ typechain-types
cache
artifacts

# Foundry files
out/
cache_forge/
lib/

access_token
94 changes: 0 additions & 94 deletions omnichain/multioutput/contracts/MultiOutput.sol

This file was deleted.

153 changes: 153 additions & 0 deletions omnichain/multioutput/contracts/Multioutput.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
// 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/BytesHelperLib.sol";
import "@zetachain/toolkit/contracts/SwapHelperLib.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract Multioutput is zContract, Ownable {
error NoAvailableTransfers();
error InvalidRecipient();
error FetchingBTCZRC20Failed();

event Withdrawal(address, uint256, bytes);

SystemContract public immutable systemContract;

uint256 constant BITCOIN = 18332;

constructor(address systemContractAddress) {
systemContract = SystemContract(systemContractAddress);
}

modifier onlySystem() {
require(
msg.sender == address(systemContract),
"Only system contract can call this function"
);
_;
}

function onCrossChainCall(
zContext calldata context,
address zrc20,
uint256 amount,
bytes calldata message
) external virtual override onlySystem {
address btcToken = systemContract.gasCoinZRC20ByChainId(BITCOIN);
if (btcToken == address(0)) revert FetchingBTCZRC20Failed();

(
address evmRecipient,
bytes memory btcRecipient,
address[] memory destinationTokens
) = parseMessage(context.chainID, message);

uint256 totalTransfers = destinationTokens.length;
if (totalTransfers == 0) revert NoAvailableTransfers();

uint256 amountToTransfer = amount / totalTransfers;
uint256 leftOver = amount - amountToTransfer * totalTransfers;

uint256 lastTransferIndex = destinationTokens[
destinationTokens.length - 1
] == zrc20
? destinationTokens.length - 2
: destinationTokens.length - 1;

for (uint256 i; i < destinationTokens.length; i++) {
address targetZRC20 = destinationTokens[i];
if (targetZRC20 == zrc20) continue;

if (lastTransferIndex == i) {
amountToTransfer += leftOver;
}

bytes memory recipient = abi.encodePacked(
BytesHelperLib.addressToBytes(evmRecipient)
);

if (targetZRC20 == btcToken) {
if (btcRecipient.length == 0) revert InvalidRecipient();
recipient = abi.encodePacked(btcRecipient);
}

_doSwapAndWithdraw(zrc20, amountToTransfer, targetZRC20, recipient);
}
}

function _doSwapAndWithdraw(
address zrc20,
uint256 amountToTransfer,
address targetZRC20,
bytes memory recipient
) internal {
(address gasZRC20, uint256 gasFee) = IZRC20(targetZRC20)
.withdrawGasFee();

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

uint256 outputAmount = SwapHelperLib._doSwap(
systemContract.wZetaContractAddress(),
systemContract.uniswapv2FactoryAddress(),
systemContract.uniswapv2Router02Address(),
zrc20,
amountToTransfer - inputForGas,
targetZRC20,
0
);

IZRC20(gasZRC20).approve(targetZRC20, gasFee);
IZRC20(targetZRC20).withdraw(recipient, outputAmount);
}

function parseMessage(
uint256 chainID,
bytes calldata message
) public pure returns (address, bytes memory, address[] memory) {
address evmRecipient;
bytes memory btcRecipient;
address[] memory destinationTokens;
if (chainID == BITCOIN) {
evmRecipient = BytesHelperLib.bytesToAddress(message, 0);
uint256 numTokens = message.length / 20 - 1;
destinationTokens = new address[](numTokens);
for (uint256 i = 0; i < numTokens; i++) {
destinationTokens[i] = BytesHelperLib.bytesToAddress(
message,
20 + i * 20
);
}
} else {
(
address evmAddress,
bytes memory btcAddress,
bytes memory targetTokens
) = abi.decode(message, (address, bytes, bytes));

btcRecipient = btcAddress;
evmRecipient = evmAddress;

uint256 numTokens = targetTokens.length / 32;
destinationTokens = new address[](numTokens);
for (uint256 i = 0; i < numTokens; i++) {
destinationTokens[i] = BytesHelperLib.bytesMemoryToAddress(
targetTokens,
i * 32
);
}
}

return (evmRecipient, btcRecipient, destinationTokens);
}
}
7 changes: 7 additions & 0 deletions omnichain/multioutput/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[profile.default]
src = 'contracts'
out = 'out'
viaIR = true
libs = ['node_modules', 'lib']
test = 'test'
cache_path = 'cache_forge'
2 changes: 1 addition & 1 deletion omnichain/multioutput/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import "./tasks/interact";
import "./tasks/deploy";
import "./tasks/destination";
import "@nomicfoundation/hardhat-toolbox";
import "@zetachain/toolkit/tasks";
import "@nomicfoundation/hardhat-foundry";

import { getHardhatConfigNetworks } from "@zetachain/networks";
import { HardhatUserConfig } from "hardhat/config";
1 change: 1 addition & 0 deletions omnichain/multioutput/lib/forge-std
Submodule forge-std added at 87a2a0
Loading