0

ApprovalContract.sol

pragma solidity ^0.5.1;

    contract ApprovalContract{
    address public sender;
    address payable public reciever;
    address public constant approver=0xe05cE3f89b5Ab28d6d2Ac8A503473Cd5A23e616a;

    function deposit (address payable _receiver)external payable{

        require (msg.value > 0);
        sender=msg.sender;
        reciever=_receiver;
    }

    function viewApprover () external pure returns(address) {
        return approver;
    }

    function approve () external payable {

        require (msg.sender==approver);
        reciever.transfer(address(this).balance);
    }
    }

2_deploy_contract.js

const ApprovalContract = artifacts.require("ApprovalContract");

module.exports = function(deployer) {
  deployer.deploy(ApprovalContract);
}

For testing, ApprovalContract.js

const ApprovalContract = artifacts.require('ApprovalContract');

console.log("something");
  contract('ApprovalContract', function(accounts) {

    it('initiates contract', async function() {

      const contract = await ApprovalContract.deployed();
      const approver = await contract.approver.call();
      assert.equal(approver, 0x0feeede47d471276377ed83e3ef749995fc0ebe2, "approvers don't match");
    });
    it('takes a deposit', async function () {
      const contract = await ApprovalContract.deployed();
      await contract.deposit(accounts[0], { value: 1e+18, from: accounts[1] });
      assert.equal(web3.eth.getBalance(contract.address), 1e+18, "amount did not match");
    });
    it('makes the transaction when approved, approver: ' + accounts[2], async function () {
      const contract = await ApprovalContract.deployed();
      await contract.deposit(accounts[0], { value: 1e+18, from: accounts[1] });
      await contract.approve({ from: accounts[2] });
      assert.equal(web3.eth.getBalance(contract.address), 0, "didn't transfer ether");
    });
});

truffle compile,truffle migrate run successfully. When I do truffle test,this error comes-

1) Contract: ApprovalContract
   takes a deposit:
 AssertionError: amount did not match: expected {} to equal 1000000000000000000
  at Context.<anonymous> (test/ApprovalContract.js:15:14)
  at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:189:7)

2) Contract: ApprovalContract

makes the transaction when approved, approver: 0xAD90b2dd2A7E8091349A96e50c4112E1EA0159fE:
 Error: Returned error: VM Exception while processing transaction: revert
  at Object.ErrorResponse (/home/neha/.nvm/versions/node/v8.16.0/lib/node_modules/truffle/build/webpack:/~/web3-core-requestmanager/~/web3-core-helpers/src/errors.js:29:1)
  • Should be await contract.approver().call() (parenthesis after the approver). – goodvibration Jul 11 at 13:45
  • And choose a different variable name for contract. This symbol is already in use by the Truffle testing infrastructure (you can see it appearing at the top of your test file). – goodvibration Jul 11 at 13:46
  • You don't need to specify a file path for artifacts.require, just the contract's name (and note that, for example, if the contract inside ApprovalContract.sol is not named ApprovalContract, then your require statement will fail to find it). – goodvibration Jul 11 at 13:48
  • 0xe05cE3f89b5Ab28d6d2Ac8A503473Cd5A23e616 is larger than javascript's Number.MAX_SAFE_INTEGER. Use "0xe05cE3f89b5Ab28d6d2Ac8A503473Cd5A23e616" instead (also because approver().call() will return a string anyway, so you may as well compare a string with a string). – goodvibration Jul 11 at 13:50
  • Debug your code and find which line causes the error. You can do this by adding a console.log before and after every line in your test file. – goodvibration Jul 11 at 13:52
0

You have two problems in your code:

  1. getBalance is async. For it to work you need to wait for the result:

    const balance = await web3.eth.getBalance(contract.address);
    
  2. Js does not have large numbers so web3 wraps them in a BN object. To compare with other numbers is better to compare them as strings since equal do not understand how to compare BN objects and numbers:

    assert.equal(
        balance.toString(),
        web3.utils.toBN(1e+18).toString(),
        "amount did not match"
    );
    

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.