# Would \`msg.sender.balance\` change in a tx or a block?

**URL:** <https://forum.soliditylang.org/t/would-msg-sender-balance-change-in-a-tx-or-a-block/384>\
**Category:** Uncategorized\
**Created:** [July 14, 2021, 1:44pm UTC](https://forum.soliditylang.org/t/would-msg-sender-balance-change-in-a-tx-or-a-block/384 "2021-07-14T13:44:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxareo](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/maxareo/32/276_2.png) [@maxareo](https://forum.soliditylang.org/u/maxareo)\
**Post date:** [July 14, 2021, 1:44pm UTC](https://forum.soliditylang.org/t/would-msg-sender-balance-change-in-a-tx-or-a-block/384/1 "2021-07-14T13:44:20Z")

</div>

Hey guys, this may or may not be directly related to the Solidity language, but it does depend on how Solidity handles the mechanism of a blockchain.

I’m trying to understand various aspects of a micro-process of a transaction such as how msg.sender’s balance is changed prior to and after a transaction is mined. Is the balance updated after a transaction is processed or after the block is mined? If two transactions request for the balance of one of the two senders, would they get the same number? To be more specific, suppose TX 1: A-\>B, TX 2: A-\>C, and in both TX 1&2, the balance of A is read as some input parameter, would it be the same for the two TXs Thanks.

---

<div class="post-metadata">

**Author:** ![cameel](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/cameel/32/34_2.png) [@cameel](https://forum.soliditylang.org/u/cameel)\
**Post date:** [July 15, 2021, 8:46pm UTC](https://forum.soliditylang.org/t/would-msg-sender-balance-change-in-a-tx-or-a-block/384/2 "2021-07-15T20:46:15Z")

</div>

In a tx. Transactions within a block have a strict order and each one works on the state as the previous transaction left it. Each transaction has the whole blockchain for its exclusive use for the short time it runs.

> suppose TX 1: A-\>B, TX 2: A-\>C, and in both TX 1&2, the balance of A is read as some input parameter, would it be the same for the two TXs

There’s no parallel execution in the EVM. When TX 1 changes `A` to `B` then TX 2 will see `B` so it cannot do `A->C`. It will do `B->C`. But presumably it won’t just blindly overwrite it - in most cases you would read the old value first and update it.

---

<div class="post-metadata">

**Author:** ![maxareo](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/maxareo/32/276_2.png) [@maxareo](https://forum.soliditylang.org/u/maxareo)\
**Post date:** [July 16, 2021, 1:29am UTC](https://forum.soliditylang.org/t/would-msg-sender-balance-change-in-a-tx-or-a-block/384/3 "2021-07-16T01:29:44Z")

</div>

@cameel Thanks for the explanation. “Each transaction has the whole blockchain for its exclusive use for the short time it runs.” This sentence nailed it.

The reason I raised this up is I found out `msg.sender.balance` is not changing within a function given below. The function `gasleft()` does return changing values, but not `msg.sender.balance`.

Since every transaction makes a state change, and `msg.sender.balance` is not changed within the function/transaction, it must be updated right after the transaction is finished, is that right? That means, there is a moment after finishing the previous transaction and before starting the next transaction, state variables like 'msg.sender.balance` are updated. Is this what is happening on a micro-level?

I was thinking about using `msg.sender.balance` as a source of randomness, so figuring out when it is changed, either at the end of, after the transaction or at the end of the block, would help a lot. Thanks.

```auto
// SPDX-License-Identifier: MIT

pragma solidity 0.8.4;

import "hardhat/console.sol";

contract Test {

    uint256 val;

    function doSth() public {
        console.logUint(msg.sender.balance);
        console.logUint(gasleft());

        for (uint256 i = 0; i < 30; i++) {
            val += i;
        }

        console.logUint(gasleft());
        console.logUint(msg.sender.balance);
    }
}

```

---

<div class="post-metadata">

**Author:** ![cameel](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/cameel/32/34_2.png) [@cameel](https://forum.soliditylang.org/u/cameel)\
**Post date:** [July 21, 2021, 8:11pm UTC](https://forum.soliditylang.org/t/would-msg-sender-balance-change-in-a-tx-or-a-block/384/4 "2021-07-21T20:11:20Z")

</div>

It’s not that `msg.sender.balance` does not get updated during a transaction. It does. If you send ether to it, you should see a difference. It’s just that you do not “buy gas as you go”. It’s deducted from balance as one lump sum before the transaction runs. And afterwards some gas may be refunded if the transaction did not actually use it all.

Also note that gas cost is not deducted from `msg.sender.balance` but from `tx.origin.balance`. These two addresses are the same inside the first function call in the transaction but if that function then calls another contract, they will be different. `tx.origin` is the EOA that initiated the whole transaction while `msg.sender` is the address (contract or EOA) that called the current function.

---

<div class="post-metadata">

**Author:** ![maxareo](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.soliditylang.org/maxareo/32/276_2.png) [@maxareo](https://forum.soliditylang.org/u/maxareo)\
**Post date:** [August 1, 2021, 3:33am UTC](https://forum.soliditylang.org/t/would-msg-sender-balance-change-in-a-tx-or-a-block/384/5 "2021-08-01T03:33:52Z")

</div>

Hey @cameel, this part is a bit confusing. A blockchain executes transactions linearly and “Each transaction has the whole blockchain for its exclusive use for the short time it runs.” Doesn’t that mean in the process of running through this `doSth()` function, no second transaction can happen? Doesn’t that also mean `msg.sender.balance` or `tx.origin.balance` would not change because of another transaction?
