0

This is my cantract code which I was using in truffle

pragma solidity 0.5.16;

contract Test {
    function getAdd() public view returns(address){
        address add = address(this);
        return add;
    }
}

This is my js Test code

const Test = artifacts.require('../contracts/Test.sol');

contract('Test',(accounts)=>{

    it('Testing Testing',async()=>{
        let ins =await Test.deployed();
        let ans =await ins.getAdd.call();
        console.log(ans)
    })
})

and I am using ganache for my local blockchain

when I run truffle test command it gives me the result 0x4cB6949669C8631fb2f99Bbe890B99B7DbF64685

but in ganache contract section the deployed contract address is 0xc48Cda939749b1Da32E6853184D595a30c5accCf

I don't know what is going wrong here as address(this) should have return my contract address but it is not working as expected

Steps I have done are:-

  1. truffle init
  2. wrote the contract
  3. wrote migration file

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

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

truffle compile 5. truffle migrate 6. truffle test

I dont know why I am getting wrong address in that but when i tried it on remix it was working please help!!

2
  • Just FYI, you don't need await ins.getAdd.call(), you can simply use ins.address. Jun 9, 2020 at 14:51
  • but why address (this) is not working.? Jun 9, 2020 at 17:17

1 Answer 1

0

I think if you

console.log(ins.address, ans);

... you will find they are the same.

If so, the issue is confusion about the instance of the contract that deployed() is using. It is the most recent, on this network, that the deployer knows about, but not necessarily what you expect. It gets updated with each deployer.deploy().

Hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.