0

Here is my Login.sol

pragma solidity ^0.4.2;

contract Login {
   event LoginAttempt(address sender, string challenge);

   function Login (string challenge) public {
       LoginAttempt(msg.sender, challenge);
   }

}

Here is my server.js

const LoginContract = new web3.eth.Contract(abiLogin);

// LoginAttempt is the name of the event that signals logins in the
// Login contract. This is specified in the login.sol file.

loginAttempt = LoginContract.LoginAttempt({});

challenges = {};
successfulLogins = {};

loginAttempt.watch({}, '', function(error, event) {
    if(error) {
        console.log(error);
        return;
    }

    console.log(event);

    const sender = event.args.sender.toLowerCase();

    // If the challenge sent through Ethereum matches the one we generated,
    // mark the login attempt as valid, otherwise ignore it.
    if(challenges[sender] === event.args.challenge) {
        successfulLogins[sender] = true;
    }
});

I don't know what is wrong with my code, I followed the instruction from https://auth0.com/blog/an-introduction-to-ethereum-and-smart-contracts-part-2/

But for some reason, my contract's event is unable to be watched. Can someone help me solve this issue?

EDIT 1 Since I need to deploy the contract, I changed my code above into these things below

let LoginContract = new web3.eth.Contract(abiLogin);

web3.eth.personal.unlockAccount('6ded1c5b448819a6cde4293e33fbe54583ef5c52', 'bank')
.then(result => {
    util.log(`>>>>> contractApi - Is bank account unlocked ? ${result}`);
    util.log('>>>>> contractApi - Ready to deploy Login contract');

    LoginContract.deploy({
        data: '0x'+binLogin,
        arguments: ['']
    })
    .send({
        from: '6ded1c5b448819a6cde4293e33fbe54583ef5c52',
        gas: gas
    })
    .on('receipt', receipt => {
        util.log(`>>>>> contractApi - Login Contract sucessfully deployed @ address: ${receipt.contractAddress}`);
        // LoginAttempt is the name of the event that signals logins in the
        // Login contract. This is specified in the login.sol file.

        const loginContract = LoginContract.at(`${receipt.contractAddress}`)

        loginAttempt = loginContract.LoginAttempt();

        challenges = {};
        successfulLogins = {};

        loginAttempt.watch((error, event) => {
            if(error) {
                console.log(error);
                return;
            }

            console.log(event);

            const sender = event.args.sender.toLowerCase();

            // If the challenge sent through Ethereum matches the one we generated,
            // mark the login attempt as valid, otherwise ignore it.
            if(challenges[sender] === event.args.challenge) {
                successfulLogins[sender] = true;
            }
        });

        secret = process.env.JWT_SECRET || "my super secret passcode";

        util.log('>>>>> setup - Completed !!!')
    });
}, error => {
    util.log(`***** contractApi - Bank account unlock error - ${error}`);
});

But a new error occurs, TypeError: LoginContract.at is not a function. What should I do in this case? Does this issue related with web3 module installation?

0

You missed a line which is also explained in the tutorial, and the most important:

const loginContract = LoginContract.at(process.env.LOGIN_CONTRACT_ADDRESS ||
                      '0xf7b06365e9012592c8c136b71c7a2475c7a94d71');

which this line you actually point at the contract instance in that specific address. You can get the contract address (0xf7...) when you deploy your contract (truffle might be helpful).

| 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.