0

this is my contract code

pragma solidity ^0.4.4;
contract IamHongKongTokenBack_00{ 

  // 現在我錢包的地址+Eth量
function SeeMyAddAndETHVar() public constant returns (address, uint) {
    address _myadd = msg.sender;
    return ( _myadd,  _myadd.balance);
}   

// 查看其他地址的Eth量
function SeeSheEthVar(address _InputAdd) public constant returns (address, uint) {
    address _SheAc = _InputAdd;
    return (_SheAc, _SheAc.balance);
}   


}

the SeeMyAddAndETHVar is use see my address and ETH amount

and SeeSheEthVar is use input address and see this address and ETH amount

If i use 0X123... address go to this contract now i use the SeeSheEthVar Input address 0X789... i can see the correct address and correct ETH amount

but use SeeMyAddAndETHVar i will see the correct address and error ETH amount

so strange When I change use 0X789... address go to this contract i will can see 0X123... correct address and correct ETH amount but 0X789... address is both error ETH amount enter image description here

May I ask where my problem is Thanks for answer~!!

0

EDIT : Working use case of the msg.sender.balance on a call value. User pay gaz (thus sending a tx) to store data in the contract storage, then can call freely the last value saved for his account. It return correct balance for me even with metamask, which prove the issue I'm trying to explain you, which you need to send a tx to use the msg.sender.balance (with metamask for sure, maybe other supplier it work):

pragma solidity ^0.4.4;
contract IamHongKongTokenBack_00{ 


//balance stored on call of saveBalance
mapping(address => uint256)  lastBalance;

function saveBalance() public  {
    lastBalance[msg.sender] = msg.sender.balance;
}   


function fetchBalance(address _address) constant public returns (uint256) {
    return lastBalance[_address];
}   


}

Using msg.sender.balance and trying to return it in a constant function seems to bug.

I myself tried with remix and metamask and it's not working as it should.

But if you use in a function that cost fees (call) and let's say you store the msg.sender.balance in a event or require it over a sum, it will work because a tx object was supplied.

Check that:

return msg.sender.balance not acting as expected

https://github.com/ethereum/solidity/issues/2325

https://github.com/ethereum/go-ethereum/issues/14989

Example of use that work for me in a "callable" function (which cost gaz to execute, modify the storage of my contract):

require(msg.sender.balance >= 0.01 ether);

This work for me, which mean if I have >= 0.01 eth in my account calling the function it execute, if not then it revert and don't execute. Which mean the "msg.sender.balance" really work as it should and contain there the correct balance of the sender.

Result of your code using metamask as web3 supplier:

0: address: 0xc544[...]
1: uint256: 115792089237316195423570985008687907853269984665640564039457434007913129639935

My actual balance is 4.424 ETH so it would be supposed to return 4424000000000000000 WEI, not this random string.

  • (call) is need a gas? – i don't like tell you hehe Mar 22 '18 at 22:08
  • Yup. Calling as a constant with metamask don't return the correct balance but as I said, using in a callable function and use it as a requirement or storing it in an event work. – btc4cash Mar 22 '18 at 22:14
  • thank you comments!! Maybe you could tell me a specific code? i am sorry because i am a beginner i don't know how to do this – i don't like tell you hehe Mar 22 '18 at 22:17
  • Just did in my answer! – btc4cash Mar 22 '18 at 22:20
  • 1
    thank you very much again yes it is work although it can't like the normal wallet but it is valuable ~!!! – i don't like tell you hehe Mar 23 '18 at 10:32
0

Honestly I don't understand what's the question but I tried to decrypt it and seems like you're having troubles getting the balance in case you call the function SeeMyAddAndETHVar. Anyway I've tried your contract on remix and the behaviour is correct

call to IamHongKongTokenBack_00.SeeMyAddAndETHVar

{
    "0": "address: 0x14723a09acff6d2a60dcdf7aa4aff308fddc160c",
    "1": "uint256: 99999999999997000000"
}
call to IamHongKongTokenBack_00.SeeSheEthVar
{
    "0": "address: 0x14723a09acff6d2a60dcdf7aa4aff308fddc160c",
    "1": "uint256: 99999999999997000000"
}
  • thank you comments!! saw you comments so i test in remix VM network this is work , can display correct ETH amount but in private network and ethernet network it is not work – i don't like tell you hehe Mar 22 '18 at 15:14
  • Are you using metamask as a supplier? @mirg – btc4cash Mar 22 '18 at 22:22
  • No you are not , I added result using metamask as a web3 supplier – btc4cash Mar 22 '18 at 22:25
  • The issue with this code is behavior of msg.sender.balance is not correct using metamask and a constant function, as my links show. But it can still be used in a callable function where gaz is spent and actual TX supplied. – btc4cash Mar 22 '18 at 22:27

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.