Is smartcontract the only way to transfer ether in Ethereum network? - blockchain

I am new to smartcontract and start writing some demo code in Solidity to work with Ropsten testing network. I see that I can use send/transfer method in the contract to transfer ether to other accounts. One question about it is that is contract the only way to transfer ether? Can I transfer ether without a contract?
I understand the concept of the contract but not sure whether it is a required thing in Ethereum. When I deploy a contract to the network, it will be sitting in a new block and all transactions generated from this contract are in the same block? Am I understand correctly?

You do not need a smart contract to sign or send a transaction. The transferring of ETH is built into the protocol. This can be accomplished by using libraries such as web3 or ethers if you're a developer. If you're not a developer, this can be accomplished by using tools such as Metamask.
Steps to send a transaction using ethers.js:
Instantiate your provider and signer using ethers.providers. and Signer.
Then sign a transaction using signer.signTransaction.
Use provider to send the transaction using provider.sendTransaction.
References:
Ethers.js: https://docs.ethers.io/v5/api/
web3.js: https://web3js.readthedocs.io/en/v1.5.2/
metamask: https://metamask.io

Related

Can WalletConnect be used to send any cryptocurrency from wallet to wallet, and how does it work?

I couldn't understand from reading WalletConnect docs if it can be used to transfer any cryptocurrency from wallet to wallet. The documentation mentions that we can connect a wallet to a Dapp. But what it really means is not clear. If WalletConnect can be used to transfer crypto from wallet to wallet, how does it work exactly under the hood? My understanding is that you can also pass some offchain transaction id along with the transfer - how is it possible with different blockchains such as Bitcoin and Ethereum?

Can I check the NFTs in a virtual machine wallet in remix?

I am learning about NFT minting by using remix to run test code on testnets. Now the function seems to be working as the ETH balance is reduced by the amount of mint price I set, but I can't check the NFTs the given wallet was holding. Is there a way to view it somewhere to check if the NFT is successfully minted?
UPDATE: I see that people can use test nets with their own wallet to receive those NFTs and view them on platforms like Opensea. But is it possible to do the same with VM?
Whether you are using a virtual machine or not, should not have the ability to use Remix or access any testnets. So it is possible. Remix and Ethereum wallets use HTTP to communicate with JSON-RPC nodes that allow your wallets, transactions and other to function.

Is there some relation between window.ethereum injected by metamask and web3.js? Can we use both?

I'm learning to program DApps and a question that came up is the difference between the API injected by Metamask and web3.js. As I understand it, MetaMask injects an object that has its own API, which has nothing to do with the web3.js library. Is this correct? And can I still use the web3.js library with MetaMask? In the latter case, how would I do it? If anyone can explain further about this difference, I would be grateful. Thanks!
web3 is an older implementation which still some clients might use,
window.ethereum is the new api. since some clients still use web3 we should still check for it.
Metamask connects you to the blockchain. Blockchain consists of blocks of data and these blocks of data are stored on nodes. Without nodes, you cannot access blockchain data.
MetaMask allows connectivity to the Ethereum blockchain through the infrastructure available at Infura ( https://infura.io ). This allows users to interact with the blockchain without having to install any node locally. infura connects to
to different nodes in a different environment, such as mainnet, rinkeby, ropsten, etc.
web3js or web3py libraries are not related to metamask web3. Metamask provides a provider to web3.js library. This provider tells web3.js library which network we are going to communicate with and then connects to a node in that network.
Once you get the provider, you instantiate a web3 instance:
const web3 = new Web3(provider);
then you create a contract instance
contract = new web3.eth.Contract(
Contract.abi,
Contract.address
);
Now with contract.methods you can call the methods on the contract. Also web3js library provides too many util functions like hashing, converting units etc

Does deploy a contract mean create a new block in blockchain?

I am using Ethereum network to deploy smart contract. The contract is written in Solidity and I am using Remix IDE to do the deploy and testing. I am using testing network at the moment.
What I don't understand is that when I deploy a smart contract to Ethereum network, does it create a new block on the blockchain? My contract may send transaction to different account when it is running. Whether this transaction is inside the block of the smart contract? Once I deploy the contract, it should be running inside the network. Whenever there is a method call on the contract, does it always generate a transaction in the block?
There's a slight difference between live networks (e.g. Ethereum mainnet or Ropsten testnet) and emulators (such as Remix VM or Ganache).
When you want to deploy a contract, its compiled bytecode is passed as the data field of a transaction that doesn't have a recipient (the to field is omitted).
On a live network, the transaction is broadcasted to the network, and waiting in the mempool to be mined.
When a miner mines this transaction, the EVM on their machine calculates the state changes that were made during its execution (in other words, the contract deployment) and broadcasts the state changes (e.g. storing the contract bytecode under the newly created address storage) to the rest of the network.
However, each instance of an EVM emulator usually runs on one machine (e.g. in your browser in case of Remix) and doesn't have access to a network of miners. Plus it would be ineffective to wait for an external miner to mine your transaction every time during development.
So when you send a transaction on an emulator, it's locally executed and wrapped in a new block right away.
So to answer your questions:
Does deploy a contract mean create a new block in blockchain?
live networks - no
emulators - yes
Whenever there is a method call on the contract, does it always generate a transaction in the block?
There's a difference between a transaction (read-write) and a call (read-only). For transactions, see above. Read-only calls are not mined at all (so they are free), and they are only invoked on the machine of your node provider (or in the emulator).

Run solidity/deploy ethereum smart contract on a website

Does anyone know if I can deploy my own smart contract or run/host a solidity code via my own website? I am following this Youtube tutorial by Decypher Media.
If I'm correct, the testrpc address localhost:8545 means that I am deploying the contract as being part of a private blockchain that is only local to my pc, right? This is the case for development. So if I would want to actually deploy the contract as an actual Ethereum node or to the Ethereum blockchain itself, would I just have to replace the localhost:8545 with the actual URL? I am not totally familiar with this yet, but I am really trying to develop something on Ethereum.
I am looking at creating a contract that sends a token to a dynamic number of eth addresses (i.e. today I may have to send tokens to 10 eth addresses, tomorrow I may do it for 15). So, instead of firing the contract for each token which would be time-consuming, I would like to do it automatically/seamlessly. But I want to be able to run my solidity code and handle the contract through my own website instead of running it on the ethereum wallet application or somewhere else.
Anyone, please share some ideas on how we can achieve this, or maybe a summary/outline of the steps that we would need to take to achieve it. Any help would be great! Thanks!
You have many possibilities to achieve this. It depends what you really want to do.
1 - With testrpc :
The easiest way to do this is to run testrpc on a server, for example a free aws-server or azure and expose the testrpc on internet. Then you can replace localhost:8545 by :8545 or using a dns to by-pass the ip.
2 - With a private Ethereum Blockchain :
If you want a real private Ethereum Blockchain, you can build a private node on a aws/azure/google server and expose this node to internet. See this, to learn a bit more : https://blockgeeks.com/two-node-setup-of-a-private-ethereum/
3 - Use a ethereum public network
You can send your smart contract to the public ethereum testnet and then use Metamask and web3js to connect your website to the ethereum testnet. More information for Metamask and how to deploy a contract with Metamask : https://karl.tech/learning-solidity-part-1-deploy-a-contract/ and here https://citywebconsultants.co.uk/blog/blockchain/introducing-ethereum-development-part-1-metamask-and-web3