How to get some ether in my local Geth account? - ethereum

I have set up Geth and created some account with no balance in them. So I cannot make any transaction as it costs Gas fee. How to create account with some initial balance so that I can test my contracts.
I have created account using the following command:
> personal.newAccount()

Assuming that you're running a private network and not connecting to an already existing public network (e.g. Ropsten, Rinkeby, Kovan, ...).
You can add the pre-funded accounts to your genesis.json, under the alloc property. Example from the linked documentation:
"alloc": {
"7df9a875a174b3bc565e6424a0050ebc1b2d1d82": { "balance": "300000" },
"f41c74c9ae680c1aa78f42e5647a62f353b7bdde": { "balance": "400000" }
}
These accounts will be funded in the first block, so you'll need to re-init your network in order to fund the accounts.
If you want to fund your accounts on a public network, you can't just re-init the whole public network. But there are so called faucets for testnet networks, that give you some small amount of testnet ETH. Example: https://faucet.metamask.io/ (requires MetaMask wallet extension to be installed in your browser).

Related

How to get the tokens the user has imported to his Metamask using the ethereum API?

With Metamask, users have to manually import tokens to their account for these to show up in the Assets section. This is has a neat security feature as dust attack and other "spam" tokens are out of sight from the user. You wallet is usually full of spam as well.
Now I want to relay these user intentions (import of a token) to a custom dApp and filter the tokens according to what the user has imported on his Metamask.
i.e can you access the tokens user has imported to his Metamask and thus see the "whitelisted" contract addresses on users Metamask using the ehtereum or other API?
I have more or less read through the Metamask API docs but didn't find anything.
Only hint I found was in this article saying it cannot be done.
unfortunately we can not get all the tokens the wallet has access to, we need to know first the smart contract addresses.
To best of my knowledge it cannot be done, but what you can do is access the tokens that the user has in their wallet (Not the ones imported into metamask)
Using the Moralis Web3 SDK you can retrieve all token balances of a current user or specified address using:
const balances = await Moralis.Web3API.account.getTokenBalances({address: "any_address"});
And you get an object with the number of tokens and the array of token objects
Example:
[
{
"token_address": "0x2d30ca6f024dbc1307ac8a1a44ca27de6f797ec22ef20627a1307243b0ab7d09",
"name": "Kylin Network",
"symbol": "KYL",
"logo": "https://cdn.moralis.io/eth/0x67b6d479c7bb412c54e03dca8e1bc6740ce6b99c.png",
"thumbnail": "https://cdn.moralis.io/eth/0x67b6d479c7bb412c54e03dca8e1bc6740ce6b99c_thumb.png",
"decimals": "18",
"balance": "123456789"
},
{other tokens}
]
To filter out the spam token you could iterate over the result and query the user allowance for each of the token contracts. We could suppose that the tokens with allowance != 0 are the token are not spam

Can we deploy same ERC20-token on different blockchains?

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
}

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

Ethereum Blockchain - whitelist ip for smart contracts

I'm looking to build a ethereum private blockchain, and will like to only restrict a particular user or ip address that can upload smart contracts to our chain.
Is that possible?