0
function createTransaction(
        uint256 _merchantId,
        uint256 _tokenAmount,
        address _tokenAddress
    ) public onlyActiveMerchants(_merchantId) {
        IERC20 token = IERC20(_tokenAddress);
        require(token.balanceOf(msg.sender) >= _tokenAmount, "not enough token balance for transaction");
        require(supported_Currency[_tokenAddress], "not supported currency");
        Merchant storage userById = User_By_Id[_merchantId];
        uint256 transactionId = userById.transction_Counter;
        address merchantCurrency = userById.specified_Currency;
        //forward funds ,maintain transaction data and merchant balances;
        console.log("hello 1");

        //uniswap interaction remains
        Transaction memory newTransaction = Transaction({
            from: msg.sender,
            token_address: _tokenAddress,
            block_Number: block.number,
            amount: _tokenAmount,
            transaction_Id: transactionId,
            to: _merchantId
        });
        console.log("hello2");

        //focus on this lines logic might have to move it down
        userById.transactions_all.push(newTransaction);
        console.log("hello 3");

        userById.transction_Counter++;
        console.log("hello 4");

        if (_tokenAddress == merchantCurrency) {
            console.log("hello 5");
            require(token.allowance(msg.sender, address(this)) >= _tokenAmount, "allowance not set");
            console.log("hello 6");

            // token.transferFrom(msg.sender, address(this), _tokenAmount);
            bool transferSuccess = token.transferFrom(msg.sender, address(this), _tokenAmount);
            console.log("hello 6.5");
            require(transferSuccess, "Token transfer failed");
            console.log("hello 7");

            token_Balances[_merchantId][_tokenAddress] += _tokenAmount;
            console.log("finished same token block");

            // return (transferSuccess, amounts);
        } else if (_tokenAddress != merchantCurrency) {
            console.log("inside swap block");

            require(uniswap_Contract != IUniswapV2Router02(address(0)), "uniswap contract invalid");
            address[] memory path;
            path = new address[](2);
            path[0] = address(_tokenAddress);
            path[1] = address(merchantCurrency);
            console.log("right before the swap function call");
            uint[] memory amounts = IUniswapV2Router02(uniswap_Contract).swapExactTokensForTokens(
                _tokenAmount,
                0,
                path,
                address(this),
                block.timestamp + 300
            ); //deadline to be considered  and also the amount of output token's to be recieved
            if (amounts.length == 0) {
                // Handle Uniswap swap failure (revert or throw error)
                revert("Uniswap swap failed");
            }

            token_Balances[_merchantId][_tokenAddress] += amounts[1];
            // return (true, amounts);
        }
    }
7
  • You're getting that error after commenting out the last line i.e., return amounts; or before commenting ? Commented May 7 at 17:57
  • before commenting
    – Anwar
    Commented May 8 at 7:35
  • So, are you getting any error after commenting that too ? Commented May 8 at 8:01
  • yup the same error
    – Anwar
    Commented May 8 at 8:14
  • when added console statements to the contract ``` token.transferFrom(msg.sender, address(this), _tokenAmount); ``` this line is the culprit but i don't know why it is behaving like this because in interface of erc20 the transferFrom function does retruns the boolean value
    – Anwar
    Commented May 8 at 8:45

2 Answers 2

0

If it is with return amounts you will need to add returns value to function definition. public onlyActiveMerchants(_merchantId) returns(uint256) Can you provide exact error details otherwise.

2
  • yeah sure below are the error details ) CrypoVerse should create a transaction for a user: Error: could not coalesce error (error={ "code": -32603, "data": { "data": "0x", "message": "Error: Transaction reverted: function returned an unexpected amount of data", "txHash": "0x7a7542322c71e7fb49d15814c7ebd607c52033370dd502e615171c3887c0234b" }, "message": "Error: Transaction reverted: function returned an unexpected amount of data" }, payload={ "id": 43, "jsonrpc": "2.0", "method": "eth_sendTransaction", "params":
    – Anwar
    Commented May 8 at 7:35
  • And, if you’re adding returns(uint256) in your function definition (as Piyush mentioned), then what error are you getting? Commented May 8 at 8:22
0

i found a solution to my problem. i was testing with the usdt token in my smart contract and was getting this error as per suggetions from the above responses the error was definitely related to the return values.

the usdt isn't 100% erc20 compliant there transfer function doesnt return bool value like most of the ERC20 i utilized a safeErc20 library so by doing that even if other token's with same problems used it won't be able to break our protocol linking a article below for understanding and solution.*

https://medium.com/@JohnnyTime/why-you-should-always-use-safeerc20-94f44aa852d8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.