Categories
Cryptocurrency Ethereum Mastering Development

Problem: Payment between smart contracts without reducing the balance of the sender contract

I work on Truffle. I have two smart contracts A and B. Codes of them are same. A should transfer some ether to B and store some information about this payment. I funded A with METAMASK. But, whenever i try a transaction from A to B , with

ax1.pay.sendTransaction('address of B',{value:'1000000000000000000', gasPrice:200})

In Truffle console, there are two problems: 1- balance of B increases but balance of A does not decrease. 2- This transaction does not be recorded in the leger.

A:

pragma solidity ^0.5.12;
import "./B.sol";
contract A{

    event Payment(address recipient, uint value);
    event Receive(address sender, uint value);

    address sender;
    address payable receiver;
    uint sentValue;
    uint receiptValue;
    uint gasPrice;

    function pay(address payable recipient) external payable{\\Transfers some ether to B
        recipient.transfer(msg.value);
        B(recipient).cash(msg.value, tx.gasprice);
        receiver=recipient;
        sentValue=msg.value;
        gasPrice=tx.gasprice;
        emit Payment(recipient, msg.value);
    }

    function payView() public view returns(address, string memory, string memory, string memory){\\Shows some data about the last transfer.
        return (receiver, uint2str(sentValue), uint2str(gasPrice), uint2str(address(this).balance));
    }

    function cash(uint cashValue, uint GasPrice) external payable{\\Records some data about the last received transaction
        sender=msg.sender;
        receiptValue=cashValue;
        gasPrice=GasPrice;
        emit Receive(msg.sender, cashValue);
    }

    function cashView() public view returns(address, string memory, string memory, string memory){\\Shows some data about the last received transaction
        return (sender, uint2str(receiptValue), uint2str(gasPrice), uint2str(address(this).balance));
    }

    function uint2str(uint _i) internal pure returns (string memory ) {
        if (_i == 0) {
                return "0";
         }
        uint j = _i;
        uint len;
        while (j != 0) {
                len++;
              j /= 10;
        }
        bytes memory bstr = new bytes(len);
        uint k = len - 1;
        while (_i != 0) {
             bstr[k--] = byte(uint8(48 + _i % 10));
             _i /= 10;
         }
        return string(bstr);
    }

    function() external payable{}

}

B:

pragma solidity ^0.5.12;
import "./A.sol";
contract B{


    event Payment(address recipient, uint value);
    event Receive(address sender, uint value);

    address sender;
    address payable receiver;
    uint sentValue;
    uint receiptValue;
    uint gasPrice;

    function pay(address payable recipient) external payable{\\Transfers some ether to A
        recipient.transfer(msg.value);
        A(recipient).cash(msg.value, tx.gasprice);
        receiver=recipient;
        sentValue=msg.value;
        gasPrice=tx.gasprice;
        emit Payment(recipient, msg.value);
    }

    function payView() public view returns(address, string memory, string memory, string memory){\\Shows some data about the last transfer.
        return (receiver, uint2str(sentValue), uint2str(gasPrice), uint2str(address(this).balance));
    }

    function cash(uint cashValue, uint GasPrice) external payable{\\Records some data about the last received transaction
        sender=msg.sender;
        receiptValue=cashValue;
        gasPrice=GasPrice;
        emit Receive(msg.sender, cashValue);
    }

    function cashView() public view returns(address, string memory, string memory, string memory){\\Shows some data about the last received transaction
        return (sender, uint2str(receiptValue), uint2str(gasPrice), uint2str(address(this).balance));
    }

    function uint2str(uint _i) internal pure returns (string memory ) {
        if (_i == 0) {
                return "0";
         }
        uint j = _i;
        uint len;
        while (j != 0) {
                len++;
              j /= 10;
        }
        bytes memory bstr = new bytes(len);
        uint k = len - 1;
        while (_i != 0) {
             bstr[k--] = byte(uint8(48 + _i % 10));
             _i /= 10;
         }
        return string(bstr);
    }
    function() external payable{}
}

Define A and B after deployment and funding Awith METAMASK:

After that, when i transferred ether to B ,with pay() function in A, i tried to see the result with payView() in A and cashView() in B:

As you can see the balance of A didn’t decrease. This transaction was recorded in block 68 but when i see the block, it says a transaction occured between my wallet and A not between A and B:

What are the reasons for these two things? How can i see the transaction in the ledger and the decreased balance on that.

Leave a Reply

Your email address will not be published. Required fields are marked *