简体   繁体   中英

Account balance zero when trying to make an transaction on private network

EDIT: solved, was caused by declaring this.web3 instead of web3

I'm running a private network with truffle and I'm facing this weird problem where at Metamask and Mist show that I have balance of 100 eth. When I try actually to make an transaction it says that the account has ZERO eth. I have a contract that is fairly complex and I'm not sure what is the problem.

I have tried restarting truffle and recompiling and remigrating the contracts a few times and I have reinstalled metamask multiple times. When I tried Mist, it also does this.

I'm fairly new at smart contracts, but I'm trying to create a somewhat big application with it. I code the contracts with Solidity and I have tested the contracts on remix without problems and I can manipulate the contracts via truffle just fine. I think it may be because of the front.. I'm programming it with react. This is the specific part of code in js that I call the function. It's just a plain auth mechanism. I use truffle-contract and this.database is a Contract.

authAddress(sotu, password) {
    return new Promise ((resolve, reject) => {
        this.database.deployed().then(instance => {
            return instance.authAddress(sotu, password);
        }).then(res => {
            resolve();
        }).catch(error => {
            reject(error);
        });
    });
}

The solidity counterpart is follows:

function authAddress(string sotu, string password) public {
    //Check if already logged in
    require(authenticatedAddresses[msg.sender] == address(0));
    Person p = persons[keccak256(sotu)];
    p.login(keccak256(password), msg.sender);
    authenticatedAddresses[msg.sender] = p;
}

I'm still learning these things, but I'm a bit bummed that this problem exists and nobody else seems to have countered this as far as I have tried to search. Have I missed something completely?

EDIT:

Here's tbe web3 provider init:

    try {
        if(web3 != null) {
            this.web3 = new Web3(web3.currentProvider);
        }
    } catch (error) {
        this.web3 = null;
    }

I try to make the site only available if you have metamask or such alike. I'd like to to wrap the web3 to a class, but if that screws things uo let me know.

My databasehandler init

initDatabaseHandler() {
    return new Promise((resolve, reject) => {
        this.databaseHandler = new DatabaseHandler();
        this.databaseHandler.loadContract(this.state.coinbase, this.web3.currentProvider)
        .then(() => {
            this.databaseHandler.getAccountInfo()
            .then(info => {
                this.setState({userinfo: info}, resolve());
            }).catch(err => {
                console.log(err);
            });
        })
        .catch(err => reject(err));

    });
}

And the abstracted contract loader:

loadContract(coinbase, provider) {
    return new Promise((resolve, reject) => {
        this.database = new Contract(Database);
        this.database.setProvider(provider);
        this.database.web3.eth.defaultAccount = coinbase;

        this.person = new Contract(Person);
        this.person.setProvider(provider);
        this.person.web3.eth.defaultAccount = coinbase;

        console.log(this.database.web3.eth.defaultAccount);
        resolve();

    });
}

解决,是由于声明了this.web3而不是web3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM