New contracts visibility - solidity

first of all, excuse me because I am starting to learn Solidity programming, and this question is surely trivial for most of you, but I haven't found any answer yet.
When I create a simple smart contract from within another one (using "new"), and I try to check the new contract visibility, I cannot find it on etherscan (Rinkeby), even though I can interact with it from within Remix IDE. Is there any reason for that?
Thank you very much in advance!!

First, how do you know which address the new contract has? You can try to log it via event emitting and Remix will show it on the console.
Secondly, on which network are you deploying your contract? By default Remix use an EVM VM that some mimic a fake network, it is not a public test net, just something that runs locally in your browser, meaning, you can not see in etherscan.
To achieve this, you have to choose "injected web3" in the environment dropdown during the deployment process.
There are a lot of gotchas but here is a good guide on how to connect your metamastk testnet.

when you create Contract that Creates other Contracts,It doesn't create or what you are trying to say that it doesn't deploying any
new contract on Rinkeby.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Account {
address public owner;
constructor(address _owner) {
owner = _owner;
}
}
contract AccountFactory{
function createAccount(address _owner) public {
Account account = new Account(_owner);
}
}
When you are deploying the written smart contract both Account and AccountFactory will be deployed you will be able to find it on (Rinkeby)
Therefore each time you hit the public createAccount function it's just making an transaction on with interact with the deployed Account smart contract
which will definitely not create Account smart contract again for each function call.
Have a look HERE

Thank you all!
For example, if this piece of code
SimpleStorage[] public simpleStorageArray;
function createSimpleStorageContract() public {
SimpleStorage simpleStorage = new SimpleStorage();
simpleStorageArray.push(simpleStorage);
}
is suposed to "deploy" a new instance of a contract (storing their addresses in an array), each time I call the function, so we can interact with to store or retrieve values, or whatever, my question came because I figured out that the address of the new "deployed" contract (contained in simpleStorageArray) should be found in Etherscan, but it actually does not.

Related

I have deployed a smart contract using the Remix editor on the Mumbai testnet. Do I still need hardhat in order to interact with the smart contract?

I have deployed a smart contract using the remix editor. I need to know that if I have to have the hardhat extension as well.
I want a user to be able to set up their profile using the UI with some info like availability, profile picture hourly rate etc. In remix I have been able to achieve this by adding a new instance after deploying the smart contract. I am not too sure how I(or someone else) would be able to interact with the smart contract using the UI. I am intending use Moralis and the Web3uikit I also want the pictures to be uploaded to IPFS aswell.
No, hardhat is used to compile, test and deploy smart contracts, on your own local hardhat blockchain instance, or a testnet or mainnet. If you have already deployed the contract to a blockchain then you don't need hardhat anymore.
If you verified the contract, you should be able to interact with it via https://mumbai.polygonscan.com/ > search your contract's address > contract tab.
Yet, from what I can grasp from your question, I think what you'll need to do is create a website that would be used as an interface for the contract.
You do not need hardhat.
If you want to interact with a smart contract from your UI, you will need to connect a wallet like metamask from the application's UI. Using JS libraries like etherJs or web3JS you can connect and interact with the smart contract.
For example using etherjs:
async function payUser (amount){
//connect to metamask
const { ethereum } = window;
//if ethereum is not found, it means that a user does not
//have metamask installed on their browser
if (!ethereum) {
return;
}
//Get wallet provider and signer
const provider = new ethers.providers.Web3Provider(ethereum);
const signer = provider.getSigner();
//contract initialization: create an instance of the //contract
const contractInstance = new ethers.Contract(contractAddress, abi, signer);
//Interact with the contract using appropriate methods
const transaction = await
contractInstance.pay(ethers.utils.parseEther(amount))
}

How do i automatically make a function start in solidity without putting it in a constructor

function changeOwnership() public {
require(lastpresence < block.timestamp -30 seconds);
_transferOwnership(#ANYADDRESS);
lastpresence = block.timestamp;
}
is it possible to make this function start automatically without me pressing the chaneOwnership function
There is no way to do it using only solidity and your contract. The solidity was designed in a way where there is no nondeterministic behavior inside the contract - thus, automatic function execution is forbidden.
But there is a way to do it if you use not only solidity but also some other solutions.
You can create a service(on the backend, frontend, or even mobile) that periodically calls the needed function via the RPC API of your contract and blockchain RPC endpoint.
Some solidity oracles also provide such a capability - for example, you can do it via chainlink

How to change old implementation contract address on etherscan?

I wrote upgradable smart contract using solidity and upgraded contract several time.
When I upgraded smart contract, implementation contract address was changed.
But under that address, the old implementation contract address still remains.
https://rinkeby.etherscan.io/address/0x245dBBE31f33569D3d7F1e0df10c93547c44065D#readProxyContract
How to change or hide this old address?
Particularly you can't hide anything once it is stored on the Blockchain. The old address will still be visible.
But if you want that calling to the old contract should fail, you can simply create a self destruct function inside your smart contract and call it when you have updated the smart contract and deployed it with a new address.
Tip -
Always have smart contracts with the self destruct functionality
Whenever you update your smart contract i.e deploy it to a new address, call the self destruct function on the old contract address for it to be destroyed.
Syntax for self destruct -
contract YourContract {
// State variables
// Some functions
function destruct(address addr) ownerOnly {
selfdestruct(addr);
}
// The above function sends all ether from the contract to the specified address
}

Solidity - How can I start to understand the bytecode in order to call a function of a deployed smart contract?

I can go through the opcodes for the parts of the bytecode, but I don't understand how to figure out how to call a deployed contract. Somewhat new to ethereum and solidiy, any help is appreciated. For example:
0x60806040526040518060400160405280600c81526020017f696e697469616c697365643100000000000000000000000000000000000000008152506000908051906020019061004f9291906100ae565b506040518...
You realy don't need to understand this. To call deployed contract you need to have either it's code or abi and address.
Then you create your contract and call it through that.
Like this
import "contractYouWantToInteractWith.sol";
contract Interact {
contractYouWantToInteractWith public contract = contractYouWantToInteractWith(addressOfTheContract);
// Then just call on it like this
function interact() public {
contract.FUNCTIONFROMTHECONTRACT(inputs);
}
}

How to avoid hardcoding contract address in Solidity

When looking at other team's smart contracts, I often see code like this:
address constant public token = address(0xabc123...);
Where the hexadecimal number is the address of an earlier-deployed smart contract. Coming from JS and C++ background, I'm not a fan of this because it effectively hardcodes what should be in a configuration file directly into the smart contract code. Several questions come to mind when I see code like this:
What if I want to deploy this to another EVM-compatible network?
What if I want to deploy this to testnet?
I'm still relatively new to Solidity, so it's possible I'm missing some feature of truffle that allows me to insert these strings at the time of deployment, but I didn't see this mentioned in any of the tutorials I went through. I would much rather have something like a JSON configuration file for testnet/mainnet/L2-chain/etc instead of having N versions of the same file with minor differences. How should I handle these cases?
You can have a variable (instead of a constant) containing the token address. And this variable can be set from a constructor.
So you can effectively pass the value from an environment variable to the contract constructor, to the contract storage.
Example:
.env
TOKEN_ADDRESS=0x123
deploy.js using Truffle (docs) for example
MyContract.new(process.env.TOKEN_ADDRESS)
You can also use Hardhat (docs) or any other library allowing you to deploy to any network depending on the config.
MyContract.sol
pragma solidity ^0.8;
contract MyContract {
address token;
constructor(address _token) {
token = _token;
}
}