I have file test.js like there:
const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("UniswapExchange Contract", function () {
let uniswapExchange;
let owner;
let addr1;
beforeEach(async function () {
const UniswapExchange = await ethers.getContractFactory("UniswapExchange");
uniswapExchange = await UniswapExchange.deploy();
await uniswapExchange.waitForDeployment();
[owner, addr1] = await ethers.getSigners();
});
it("Should swap tokens", async function () {
const tokenInAddress = "0x7D1AfA7B718fb893dB30A3aBc0Cfc608AaCfeBB0";
const tokenOutAddress = "0x6B175474E89094C44Da98b954EedeAC495271d0F";
const amountIn = ethers.parseEther("1"); // Amount in wei
const tokenIn = await ethers.getContractAt("IERC20", tokenInAddress);
await tokenIn.transfer(uniswapExchange.address, amountIn);
await uniswapExchange.swapTokens(tokenInAddress, tokenOutAddress, amountIn);
const tokenOut = await ethers.getContractAt("IERC20", tokenOutAddress);
const ownerBalance = await tokenOut.balanceOf(owner.address);
expect(ownerBalance).to.not.equal(0);
});
it("Should withdraw tokens by the owner", async function () {
const tokenAddress = "0x6B175474E89094C44Da98b954EedeAC495271d0F"; // Replace with actual address
const amountToWithdraw = ethers.parseEther("1"); // Amount in wei
// Transfer tokens to the contract
const token = await ethers.getContractAt("IERC20", tokenAddress);
await token.transfer(uniswapExchange.address, amountToWithdraw);
// Call the withdrawTokens function
await uniswapExchange.connect(owner).withdrawTokens(tokenAddress, amountToWithdraw);
// Check token balance of the owner
const ownerBalance = await token.balanceOf(owner.address);
// Assert that the balance has increased
expect(ownerBalance).to.not.equal(0);
});
});
This is contract.sol file:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
interface IUniswapV2Router02 {
function swapExactTokensForTokens(
uint256 amountIn,
uint256 amountOutMin,
address[] calldata path,
address to,
uint256 deadline
) external returns (uint256[] memory amounts);
}
interface IERC20 {
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function approve(address spender, uint256 value) external returns (bool);
}
contract UniswapExchange {
address private constant UNISWAP_ROUTER_ADDRESS = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
address private owner;
// address private constant WETH_ADDRESS = <WETHAddress>; // Wrapped Ether Address
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "Only the owner can call this function");
_;
}
function swapTokens(
address tokenIn,
address tokenOut,
uint256 amountIn
// uint256 amountOutMin
) external {
address[] memory path = new address[](2);
path[0] = tokenIn;
path[1] = tokenOut;
IERC20(tokenIn).transfer(address(this), amountIn);
IERC20(tokenIn).approve(UNISWAP_ROUTER_ADDRESS, amountIn);
assembly {
let uniswapRouter := add(0, UNISWAP_ROUTER_ADDRESS)
let pathPtr := mload(0x40)
mstore(pathPtr, 0x20)
mstore(add(pathPtr, 0x20), 2)
mstore(add(pathPtr, 0x40), tokenIn)
mstore(add(pathPtr, 0x60), tokenOut)
let swapCallSuccess := call(
gas(),
uniswapRouter,
0,
pathPtr,
0x80,
0,
0
)
if eq(swapCallSuccess, 0) {
revert(0, 0)
}
}
uint256 swappedAmount = IERC20(tokenOut).balanceOf(address(this));
IERC20(tokenOut).transfer(msg.sender, swappedAmount);
}
function withdrawTokens(address token, uint256 amount) external onlyOwner{
IERC20(token).transfer(owner, amount);
}
}
Here is the error that occurs during the test:
-
UniswapExchange Contract
Should swap tokens:
TypeError: unsupported addressable value (argument=”target”, value=null, code=INVALID_ARGUMENT, version=6.7.0)
at makeError (node_modules/ethers/src.ts/utils/errors.ts:678:21)
at assert (node_modules/ethers/src.ts/utils/errors.ts:702:25)
at assertArgument (node_modules/ethers/src.ts/utils/errors.ts:714:5)
at resolveAddress (node_modules/ethers/src.ts/address/checks.ts:122:19)
at /Users/denys/Desktop/Solidity/Task/node_modules/ethers/src.ts/contract/contract.ts:166:60
at ParamType.#walkAsync (node_modules/ethers/src.ts/abi/fragments.ts:777:24)
at ParamType.walkAsync (node_modules/ethers/src.ts/abi/fragments.ts:795:24)
at /Users/denys/Desktop/Solidity/Task/node_modules/ethers/src.ts/contract/contract.ts:164:22
at Array.map ()
at resolveArgs (node_modules/ethers/src.ts/contract/contract.ts:163:37) -
UniswapExchange Contract
Should withdraw tokens by the owner:
TypeError: unsupported addressable value (argument=”target”, value=null, code=INVALID_ARGUMENT, version=6.7.0)
at makeError (node_modules/ethers/src.ts/utils/errors.ts:678:21)
at assert (node_modules/ethers/src.ts/utils/errors.ts:702:25)
at assertArgument (node_modules/ethers/src.ts/utils/errors.ts:714:5)
at resolveAddress (node_modules/ethers/src.ts/address/checks.ts:122:19)
at /Users/denys/Desktop/Solidity/Task/node_modules/ethers/src.ts/contract/contract.ts:166:60
at ParamType.#walkAsync (node_modules/ethers/src.ts/abi/fragments.ts:777:24)
at ParamType.walkAsync (node_modules/ethers/src.ts/abi/fragments.ts:795:24)
at /Users/denys/Desktop/Solidity/Task/node_modules/ethers/src.ts/contract/contract.ts:164:22
at Array.map ()
at resolveArgs (node_modules/ethers/src.ts/contract/contract.ts:163:37)
I put the addresses of tokens the main network from EhterScan. How I can solve this problem? Can somebody help me with this?
I created a new project and forked again, but nothing came out, again this problem. I even made mockups for my tokens, but there are some problems.