Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Blockchain 为什么我在阅读以太坊账户余额时会得到两个不同的答案?_Blockchain_Ethereum_Smartcontracts_Web3_Ganache - Fatal编程技术网

Blockchain 为什么我在阅读以太坊账户余额时会得到两个不同的答案?

Blockchain 为什么我在阅读以太坊账户余额时会得到两个不同的答案?,blockchain,ethereum,smartcontracts,web3,ganache,Blockchain,Ethereum,Smartcontracts,Web3,Ganache,我试着用两种不同的方式阅读一些以太坊测试账户的余额,得到了两个不同的答案,但不是所有账户的余额。更具体地说,我使用Ganache建立了一个测试以太坊网络,并使用连接到测试网络的Remix部署了以下智能合约。默认情况下,第一个帐户用于部署事务 pragma solidity ^0.6.0; contract Test { function balanceOf(address addr) external view returns (uint) { return

我试着用两种不同的方式阅读一些以太坊测试账户的余额,得到了两个不同的答案,但不是所有账户的余额。更具体地说,我使用Ganache建立了一个测试以太坊网络,并使用连接到测试网络的Remix部署了以下智能合约。默认情况下,第一个帐户用于部署事务

pragma solidity ^0.6.0;

contract Test {
    
    function balanceOf(address addr) external view returns (uint) {
        return addr.balance;
    }
    
}
在由Ganache提供的帐户上调用Remix中的
balanceOf
函数非常有效。然后,我使用最新的Web3.py库运行了以下Python代码,以便对所有帐户调用
balanceOf
函数

from web3 import Web3
import json

web3 = Web3(Web3.HTTPProvider('http://127.0.0.1:7545'))   # connecting to the Ganache Ethereum network

def toEther(balance):
    return web3.fromWei(balance, 'ether')

# connecting to the 'Test' contract
abi = json.loads('[ { "inputs": [ { "internalType": "address", "name": "addr", "type": "address" } ], "name": "balanceOf", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "stateMutability": "view", "type": "function" } ]')
contract_address = '0x3038871e10654B87858Be4952B26bd7607Cb0c8b'   # this will be different for you
contract = web3.eth.contract(abi=abi, address=contract_address)

# printing balances
for account in web3.eth.accounts: 
    print(f'balance of {account}:  {toEther(contract.functions.balanceOf(account).call())}  versus {toEther(web3.eth.getBalance(account))}')
如您所见,余额通过两种方式计算:

  • 通过调用生成第一个数字的合同的
    balanceOf
    函数
  • 通过调用web3的
    web3.eth.getBalance
    方法生成第二个数字。 结果是这样
  • 如您所见,合同的
    balanceOf
    函数为第一个地址生成了错误的结果。
    web3.eth.getBalance
    方法显示正确的结果(100-部署气体)。我在没有重新启动Ganache网络的情况下再次部署了合同,并得到了以下结果(仍然调用第一次部署,但这并不重要):

    正如您所看到的,由于第二次部署交易,第一个值意外地增加了燃气费(约0.002),而第二个值下降了相同的金额。最后,我使用第二个帐户而不是第一个帐户再次部署了该契约。同样,两个余额计算都给出了正确的结果,但第一个账户除外:

    balance of 0x76290BDDea6C156A7F364aBB952aE29D49C593bE:  180143885.0988325  versus 99.99598732
    balance of 0x3BFCAC461d922e3b4504BB5fc59dA5546a767617:  99.99799366  versus 99.99799366
    balance of 0xD2AA30E855eb3F9B64A14Bb1a4EE4C4Ad645dA58:  100  versus 100
    
    我是区块链技术的新手,我想知道是否有人能向我解释使用Web3的第一个帐户(仅!!!)的
    balanceOf
    函数的奇怪行为。正如我所说,在Remix中调用函数总是会得到正确的结果

    PS:这不是Python的问题。我使用Web3.js库做了同样的观察

    balance of 0x76290BDDea6C156A7F364aBB952aE29D49C593bE:  180143885.0988325  versus 99.99598732
    balance of 0x3BFCAC461d922e3b4504BB5fc59dA5546a767617:  100  versus 100
    balance of 0xD2AA30E855eb3F9B64A14Bb1a4EE4C4Ad645dA58:  100  versus 100
    ...
    
    balance of 0x76290BDDea6C156A7F364aBB952aE29D49C593bE:  180143885.0988325  versus 99.99598732
    balance of 0x3BFCAC461d922e3b4504BB5fc59dA5546a767617:  99.99799366  versus 99.99799366
    balance of 0xD2AA30E855eb3F9B64A14Bb1a4EE4C4Ad645dA58:  100  versus 100