0

I am deploying a contract factory via code (Web3js.). In the contract factory, I wish to determine my address automatically. How do I do this?

2
  • 1
    Which IDE are you using?
    – Ad-h0c
    Sep 9 at 7:28
  • Remix and Webstorm.
    – Boris K
    Sep 9 at 8:39

1 Answer 1

1

Generally you can get accounts by the command

web3.eth.getAccounts();

And if you want to get the first account you can use the following command.

web3.eth.getAccounts().then(e => let account1=e[0]; 
console.log(account1));

Since you are using web3.js, I am assuming you are using truffle and ganache. So generally first account is default deployment address in the truffle.

Edit:

//SPDX-License-Identifier: MIT

pragma solidity ^0.8.10;


contract getAddress {

    address public deployer; //Solidity compiler automatically generates getter function for public variables.

    constructor()    {
        deployer = msg.sender; //msg.sender gives the address of the current user. Since we are declaring in the constructor it is akin to the owner of the contract and also deployer.
    }


    function getContractAddress() public view returns(address) {
        return address(this); //address(this) gives the address of the contract.
    } 
}
2
  • I want to do this right in the Solidity code, though.
    – Boris K
    Sep 9 at 8:39
  • 1
    You mentioned web3js. So I answered in web3.js. Anyways I am gonna add solidity script to get deployer address and the address of a contract.
    – Ad-h0c
    Sep 9 at 9:50

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.