0

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: enter image description here

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: enter image description here

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: enter image description here

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

  • You should accept answers to previous questions of yours if you're expecting help in future questions! – goodvibration May 18 at 8:31
  • @goodvibration: I'm sorry. I had forgotten to mark your last answer to me. i did it. You are the one who has always helped me in this forum. I hope I didn't offend you. – Alireza May 18 at 9:02
  • There is not a single transaction in the code that you've posted in those two screenshots (and BTW, please post them as plain text (formatted as code), so that people here will be able to refer to that information). You should show in code how you "transferred ether to B ,with pay()". – goodvibration May 18 at 9:18
  • @goodvibration: I transferred ether to B with ax1.pay.sendTransaction('address of B',{value:'1000000000000000000', gasPrice:200}) in Truffle console. – Alireza May 18 at 9:37
  • This transaction is executed via the account specified in the from field in the options object that you pass to sendTransaction. Since you haven't specified this field, I would guess that it takes accounts[0] by default. You can verify this by looking into the balance of that account and see that it has decreased by how much you've expected the balance of contract A to decrease. – goodvibration May 18 at 9:41
0

Problem 1: In order to send some ether to B from the balance of A, using ax1.pay.sendTransaction('address of B',{value:'1000000000000000000', gasPrice:200} is inappropriate. Whenever, we use {from:'...', value:'...' , gasPrice:..., gas:...} in .sendTransaction(...), in fact, we are telling to A "decrease as much as is determined in value:'...' from balance of the account corresponds tofrom:'...' and add it to the balance of B" . If from:'...' is not determined the accounts[0] in Ganache will be considered. But, if we want to decrease the balance of A as much as value:'...' and add it to the balance of B, should modify pay() function in A and method of transaction sending in Truffle console. For that, we add an other parameter to pay() and set it as argument of .transfer():

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

.
.
.

  function pay(address payable recipient, uint val) external payable{
        recipient.transfer(val);
        A(recipient).cash(val, tx.gasprice);
        receiver=recipient;
        sentValue=val;
        gasPrice=tx.gasprice;
        emit Payment(recipient, val);
    }

.
.
.

}

And in Truffle console:

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

Now, one can see that the balance of A will be decreased as much as determined with val. But, it must be noted that by this way val can not be more than 999999999999999 wei

Problem 2:

In Truffle, just the interaction between a wallet account and a contract account is recorded in the ledger.

I hope this answer can help.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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