Skip to content

Commit

Permalink
Fix multioutput (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev authored Apr 15, 2024
1 parent 51f37dc commit eaf0b23
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
1 change: 1 addition & 0 deletions omnichain/multioutput/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@ethersproject/abi": "^5.4.7",
"@ethersproject/providers": "^5.4.7",
"@nomicfoundation/hardhat-chai-matchers": "^1.0.0",
"@nomicfoundation/hardhat-foundry": "^1.1.1",
"@nomicfoundation/hardhat-network-helpers": "^1.0.0",
"@nomicfoundation/hardhat-toolbox": "^2.0.0",
"@nomiclabs/hardhat-ethers": "^2.0.0",
Expand Down
68 changes: 61 additions & 7 deletions omnichain/multioutput/tasks/interact.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,68 @@
import { task } from "hardhat/config";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { parseEther } from "@ethersproject/units";
import { parseUnits } from "@ethersproject/units";
import { getAddress } from "@zetachain/protocol-contracts";
import ERC20Custody from "@zetachain/protocol-contracts/abi/evm/ERC20Custody.sol/ERC20Custody.json";
import { prepareData } from "@zetachain/toolkit/client";
import { utils, ethers } from "ethers";
import ERC20 from "@openzeppelin/contracts/build/contracts/ERC20.json";

const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
const [signer] = await hre.ethers.getSigners();

const data = prepareData(args.contract, ["address"], [args.recipient]);
const to = getAddress("tss", hre.network.name);
const value = parseEther(args.amount);
const destinationTokens = args.targetToken.split(",");

const tx = await signer.sendTransaction({ data, to, value });
let bitcoinAddress = "";
let data;
if (args.btcRecipient) {
bitcoinAddress = args.btcRecipient;
}

const bitcoinAddressBytes = utils.solidityPack(
["bytes"],
[utils.toUtf8Bytes(bitcoinAddress)]
);

const tokensBytes = ethers.utils.concat(
destinationTokens.map((address) =>
utils.defaultAbiCoder.encode(["address"], [address])
)
);

data = prepareData(
args.contract,
["address", "bytes", "bytes"],
[args.recipient, bitcoinAddressBytes, tokensBytes]
);

let tx;

if (args.token) {
const custodyAddress = getAddress("erc20Custody", hre.network.name as any);
if (!custodyAddress) {
throw new Error(
`No ERC20 Custody contract found for ${hre.network.name} network`
);
}

const custodyContract = new ethers.Contract(
custodyAddress,
ERC20Custody.abi,
signer
);
const tokenContract = new ethers.Contract(args.token, ERC20.abi, signer);
const decimals = await tokenContract.decimals();
const value = parseUnits(args.amount, decimals);
const approve = await tokenContract.approve(custodyAddress, value);
await approve.wait();

tx = await custodyContract.deposit(signer.address, args.token, value, data);
tx.wait();
} else {
const value = parseUnits(args.amount, 18);
const to = getAddress("tss", hre.network.name as any);
tx = await signer.sendTransaction({ data, to, value });
}

if (args.json) {
console.log(JSON.stringify(tx, null, 2));
Expand All @@ -20,12 +71,15 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {

console.log(`🚀 Successfully broadcasted a token transfer transaction on ${hre.network.name} network.
📝 Transaction hash: ${tx.hash}
`);
`);
}
};

task("interact", "Interact with the contract", main)
.addParam("contract", "The address of the withdraw contract on ZetaChain")
.addParam("amount", "Amount of tokens to send")
.addOptionalParam("token", "The address of the token to send")
.addFlag("json", "Output in JSON")
.addParam("recipient");
.addParam("recipient")
.addOptionalParam("btcRecipient", "The bitcoin address to send to")
.addParam("targetToken");
7 changes: 7 additions & 0 deletions omnichain/multioutput/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,13 @@
deep-eql "^4.0.1"
ordinal "^1.0.3"

"@nomicfoundation/hardhat-foundry@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@nomicfoundation/hardhat-foundry/-/hardhat-foundry-1.1.1.tgz#db72b1f33f9cfaecc27e67f69ad436f8710162d6"
integrity sha512-cXGCBHAiXas9Pg9MhMOpBVQCkWRYoRFG7GJJAph+sdQsfd22iRs5U5Vs9XmpGEQd1yEvYISQZMeE68Nxj65iUQ==
dependencies:
chalk "^2.4.2"

"@nomicfoundation/hardhat-network-helpers@^1.0.0":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@nomicfoundation/hardhat-network-helpers/-/hardhat-network-helpers-1.0.8.tgz#e4fe1be93e8a65508c46d73c41fa26c7e9f84931"
Expand Down

0 comments on commit eaf0b23

Please sign in to comment.