What would be the value of msg.sender in a function called inside a contract constructor? - constructor

I have read that during a contract deployment, inside the constructor, the contract address is not defined yet. So if this constructor was calling a function of another contract, what would this function get as the msg.sender ?
Thanks!

The constructor is what is ran when the contract is launched on the blockchain. msg.sender is the variable pointer that is the wallet address that is launching or interacting with the contract.

So I tested my question on a virtual network and it seems that during the deployment of a contract, functions called inside the constructor will receive a different address than the final address after deployment, and this deployment address cannot be detected as a being a contract's address contrary to the final address.

Related

Can functions with the onlyOwner modifier be called by the owner if they are set with visibility external instead of public?

I have some functions that I'm trying to save gas on.
onlyOwner modifier is self explanatory.
This is related to a previous question that I had, where I noticed that address(this) is different from msg.sender in a given contract. Upon further reading I get the impression that address(this) is a hash of msg.sender, but the two are not considered the same. Is this correct?
Since address(this) is related to the owner address, is the owner address (msg.sender, set within the contract's constructor) considered different enough from the contract address address(this) so as to be considered external visibility?
// can the owner (set in the constructor) call this function once it is deployed on the blockchain?
function setXYZ(address _address) external onlyOwner returns (bool success) {
XYZ[_address] = 4;
return true;
}
Seems to work when I test it within my IDE, but I don't know if there is a difference in implementations between my test environment and the blockchain environment.
Upon further reading I get the impression that address(this) is a hash of msg.sender, but the two are not considered the same. Is this correct?
address(this) returns address of the currently executed contract. When a contract is deployed, it's assigned an address which is determined by hash of "the deployer address combined with other data". Once the contract address is assigned, it never changes.
On the other hand, msg.sender reflects address of whoever is currently executing the contract. So if Alice executes a function, msg.sender is her address - and if Bob executes the same function, msg.sender reflects his address.
As the OpenZeppelin Ownable code shows, the deployer address (msg.sender in constructor) becomes the initial owner.
And the onlyOwner modifier validates whether the current msg.sender is the owner. If they match - all good. If the currrent msg.sender is different from the owner, it reverts the transaction.
Seems to work when I test it within my IDE, but I don't know if there is a difference in implementations between my test environment and the blockchain environment.
This code will work the same on all EVM networks.
There are few very small differences between the EVM implementation in emulators and live networks (as well as between different live networks). But they mostly affect low-level features such as ordering of transactions in a block or gas usage optimizations.

How do I access a plain Ether transaction event in a Solidity Ethereum smart contract dApp?

When a user sends some Ether to a smart contract/dApp address, is there a way I can access that transaction in my smart contract? Like a built-in method that's invoked, that I can add some code to?
My ideal work flow, if it's possible:
User sends Ether to my dApp address.
A function is invoked in my smart contract code.
Within that function, I can create some logic, like add their address to some state, etc.
Ideally, I want to avoid a user having to invoke a particular public function of the contract, but instead just send Ether to the dApp address.
If the user has to invoke a particular function, I have to start thinking about services like MEW, or I have to build a web2 frontend app that integrates with the MetaMask browser extension or something. This seems like a lot of work, but is this the way it has to be?
There is the receive() special function that gets executed when you send ETH to the contract address and not specify any function to execute (i.e. the data field of the transaction is empty).
You can get the sender address through the msg.sender global variable and the amount through msg.value.
pragma solidity ^0.8;
contract MyContract {
mapping (address => uint256) contributions;
receive() external payable {
contributions[msg.sender] += msg.value;
}
}

How can i store a contract address

I have create a Voting Dapp using web3j and ganache, but their is a small problem. I decide to deploy a voting contract every time a new election begin, and there will be a administrator who control the deploy of the contract and the begin and end of the election. So when the voting contract is deployed, only can the administrator get the contract contract address. how can I send the address to normal citizens so they can call the smart contract.
I thought of storing the contract address in a normal database, but if the database is hacked or destroyed, the whole dapp would crash.
Is there any function in web3j that I can store a string in the blockchain itself?
ps. I saw the web3j.dbputString function but I have no idea how to use it.
private String deployContract(Web3j web3j, Credentials credentials) throws Exception {
return Election.deploy(web3j, credentials,GAS_PRICE,GAS_LIMIT).send().getContractAddress();
}
here is the contract deployed segment.
A common design pattern is called Factory. You can have a smart contract that deploys other contracts. This will generate an event that is visible to the user. See Uniswap Factory.
Alternative you can just make the contract constructor to evit an emit and that is caught by the user frontend.

How do Ethereum ERC-20 Contracts for Tokens Interact

Learning Solidity along with token development.
I'm having a hard time understanding how tokens with multiple smart contracts interact with each other.
I've read through other tokens with multiple contracts on GitHub and reviewed contracts on OpenZeppelin but they all appear somewhat orphaned.
Let's say we developed a token that has a supply and tracks wallet address to amount using a map. The contract is released and is given an address. Admin or privilege methods are protected via owner address validation. Then we release a second contract that needs to make apply a fee for transactions.
How does the second (token transaction fees) contract interact with the data stored on the first (token contract)? Does the second contract need to also verify ownership?
Any contract on Ethereum can interact with any other contract by using Interfaces. You can call methods from the second contract on the first contract for an ERC20 token as below:
Define ERC20 Interface
interface ERC20 {
function balanceOf(address account) external view returns (uint256);
}
Use the interface and token contract address to call methods on the first contract:
ERC20(tokenContractAddress).balanceOf(0xabcd....);
The similar approach can be used for any contracts

Why msg.sender has the address of the account which has deployed the contract (and not the one's which interacts with it)?

I am currently developing a dapp on ethereum. From what I know msg.sender should has the value of the account who interact with it. However, it keeps the value of the deployer's account.
I am using metamask and solidity ^0.4.24. I am deploying the contact using truffle framework and also Ganache GUI as my virtual node
function getMe() public view returns(address){
return msg.sender;
}
So, I expect this code to return the hash of the account which is interacting with the contract but instead I am taking back the address of the acount which deployed it
msg.sender contains the value of the address of the caller. You must be deploying and interacting from the same account, probably using accounts[0].