Can we deploy same ERC20-token on different blockchains? - ethereum

I want to deploy my own ERC-20 token on different blockchains, so is there any possibility to deploy the same token contract on different blockchains. If we do that we can't give the same name and symbol for three blockchains. can anybody tell me what is the solution for this problem? Or can we deploy the contract with the same contract address on three blockchains?

can we deploy the contract with same contract address on three blockchains.
It depends on the network that you're deploying to and the address that you're deploying from. Assuming that the networks of your choice have the same 1) address format and 2) calculation of deployed contract address - then yes, you'll be able to deploy your contract on the same address on multiple networks.
For example Ethereum and BSC do have both of these features. But even though Tron network supports EVM-compatible smart contracts, it has a different way to calculate its addresses, so it won't be possible to deploy your contract on Tron network with the same address as on Ethereum or BSC.
The key to deploy the contract to the same address on multiple networks, is to deploy from the same address, and using the same params:
In case of the regular CREATE opcode, the transaction deploying the contract needs to have the same nonce (and the same from) value across all networks.
Or if you're using the CREATE2 opcode, you need to pass the same contract bytecode, the same salt (to the CREATE2), and again, you need to send the deploying transaction from the same address.
If we do that we can't give the same name and symbol for three blockchains
It is technically possible, so I'm assuming it's "just" a limitation of your business case or some tool that you're using, or a possible simple misunderstanding of how the ERC-20 standard works.
pragma solidity ^0.8;
contract MyToken {
string public constant name = "MyToken";
string public constant symbol = "MyT";
// TODO rest of your token contract source code
}

Related

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.

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].

Contract address collision

So from my understanding when creating a contract the two variables that are used in determining what the address of the contract will be are the msg.sender and the nonce value. So if I create two contracts in the same transaction such as I did with this code https://ropsten.etherscan.io/address/0xcb7d7e99e56406a675173f0ddbde7d8cc3392e5e#code
Why did it generate two contracts at two different addresses, what I though would happen is that they would generate at the same address and the one would simply overwrite the other or something like that.
You're understanding of the contract address determined by the address of the message creator and the nonce is correct. However, in the example you posted, msg.sender is the address of the Test contract.
These are the steps that occured:
You initiated the transaction to deploy Test from your external account (0x98081ce968e5643c15de9c024de96b18be8e5ace). According to the transaction information, the nonce of the account at that time was 639.
This resulted in the Test contract having an address of 0xcb7d7e99e56406a675173f0ddbde7d8cc3392e5e.
During the deployment of Test, the constructor then creates two new contracts through "internal transactions". Divert is deployed from the contract address at 0xcb7d7e99e56406a675173f0ddbde7d8cc3392e5e with nonce=1. OverRide is deployed from the same address with nonce=2.
You can view the details of the internal transaction here.

Upgrading smart contract in ethereum

I am trying to write upgradable smart contract in ethereum. Can anyone give an example of upgradable smart contract in ethereum and accessing data.
To write an upgradable smart contract I suggest you do the following (works for me):
Create a storage contract on which you will store all your map and variables. Add a modifier to the functions changing state. This modifier should require that an address must be present in a specific map (let’s call it authorized) to change the state of map or variable. Place the owner’s address in that map.
Write a function to authorize external address on the storage contract
Deploy another contract containing the logic of your app.
Autorise the logic contract on the storage contract.
Upon upgrade of the logic, deny access to the storage contract from the logic contract, deploy your upgraded logic and link the new contract to the storage.
Tadaa you now have an upgradable set of smart contracts.
Smart Contract cannot be replaced, but you can create a smart contract proxy to be able to replace the calling of all new Smart Contract functions (previous smart contracts cannot be removed on the main network ethereum).
Complete explanation and examples can be seen Here
You can implement follow Proxy Pattern
Blog
https://medium.com/nomic-labs-blog/malicious-backdoors-in-ethereum-proxies-62629adf3357
Smart contract upgradability is a state of the art area. Currently, there are a few solutions, but each of them has its own pros and cons. Here are the solutions with examples:
https://consensys.github.io/smart-contract-best-practices/software_engineering/

Is it possible to call a contract function just by making a transction to a address

Currently I'm trying to learn ethereum and smart contract. I read this tutorial: Dapps for beginners
I'm just wondering now, if I have to call everytime a function from a contract (as in the tutorial above) or is it possible that a specific function is executed when I just transfer some ethereum to that contract address?
Example:
I execute the code below, and the receiver address is also a address with a contract. One specific function should now be executed at the receiver function.
eth.sendTransaction({from:sender, to:receiver, value: amount})
You should create a nameless payable function in your smart contract.
This will then be the default function to execute if someone sends a raw transaction at your contract's address.
function() payable public {
}
Also, the other answer here states that you need to know the contract ABI to communicate with contracts but this is not true.
You need to know the contract address, the function name and the input and output parameter types. (You could use Web3's method.call or method.sendTransaction to send the encoded data in the transaction object and interact with the contract.)
The ABI may have this information, but the ABI is not required itself.
You can only communicate with contracts if you know the ABI which is the application binary interface.
In general, an ABI is the interface between two program modules, one of which is often at the level of machine code. The interface is the de facto method for encoding/decoding data into/out of the machine code. In ethereum, it's basicly how you can encode solidity contracts for the EVM and backwards how to read the data out of transactions.
If you have the JSON ABI of a contract, you still have to decide if you want to make calls or transactions. The difference between a call and a transaction is the following:
Transactions are created by your client, signed and broadcasted to the network. They will eventually alter the state of the blockchain, e.g., by manipulating balances or values in smart contracts.
Calls are only executed locally on your computer and not broadcasted to the network, e.g., a dry run.
Calls are usefull for debugging smart contracts as they do not cost transaction fees or gas.
So if you only send a transaction to a contract without using any interface, you wont be able to execute any code on the contract.