Blockchain Interview question - Solidity
Q: What is the difference between .call and .send?
.send sends a transaction and costs moeny while .call queries the state of the contract.
Q: What are some ways in which two contracts can interact?
A contract can invoke, create and inherit from another contract(s).
Q: Is Solidity statically or dynamically typed?
It is statically typed, which means that types are known at compilation.
Q: What is the equivalent to the Java "Class" in Solidity?
It's the Contract.
Q: What do you need in order to interact with a contract from a DApp?
The contract's ABI and bytecode.
Q: What is the very first thing you must specify in a Solidity file?
The version of Solidity compiler, which is specified as ^0.4.8. It is necessary because it prevents incompatibility errors which can be introduced when compiling with another version.
How do you set a limit on the ether balance of a contract and what happens when you try to send more ether to the limited contract?
pragma solidity ^0.4.19; contract yourContract{ uint256 public balanceLimit = 999; function () payable{ if (this.balance + msg.value > balanceLimit) { throw; } } }
Q: How to list accounts in web3 1.x?
web3.eth.getAccounts
Q: Do you know any solutions for scalability in Ethereum?
2-layer protocols. Possible solutions are state channels and plasma.
What's the difference between EVM and non-EVM calls?
EVM calls are smart contract method calls which trigger method execution and requires GAS. non-EVM calls are reading values public values. No need to GAS.
Q: Is sending one ether like this ".send({ value: 1 })" okay?
No, you send 1 wei. Transactions always work with wei.
Q: Can I do this: function doSomething(uint[] storage args) internal returns(uint[] storage data) {...}
Yes, you can force the arguments of a function to be of type storage. In this case if you do not pass a storage reference, the compiler will complain.
Q: Can I only import local files?
You can also import files using HTTP (even from Github), import "http://github.com/<owner>/<repo>/<path to the file>"
Q: What if I have a huge project, do I need to keep all my related contracts into a single file?
You can use import statement to import a file, import "./MyOtherContracts.sol";
Q: So in order to send 1 ether I have to mutiply the value by 1⁰¹⁸?
You can use the util method web3.utils.toWei(1, 'ether')
Q: What do I have to specify when calling ".send()"?
You must specify "from" which is the sender address. Everything else is optional.
QHow can you set the value of msg.val in a contract acct and not an external
acct? msg.val => msg.value = number of wei sent with the message
Q: Why would you use version web3 js 1.x instead of 0.2x.x?
Mainly because its async calls use promises instead of callbacks, which is preferred in the javascript world.
Q: What is the ABI used for?
ABI is a description of the public interface of a contract, which is used by DApps for invoking the contract.
Q: What is an instance of a contract?
An instance of a contract is the deployed contract on the blockchain.
Q: Why would you use BigNumber library?
Because Javascript does not handle big numbers correctly.
Q: Why is it necessary to always check if the web3 provider is set in the beginning of your web DApp code?
Because Metamask injects it and overwrites the any other web3 with its own.
Q: What is a DApp ?
Decentralized applications (dApps) are applications that run on a P2P network of computers rather than a single computer. dApps, have existed since the advent of P2P networks. They are a type of software program designed to exist on the Internet in a way that is not controlled by any single entity. Decentralized applications don't necessarily need to run on top of a blockchain network. BitTorrent, Popcorn Time, BitMessage, Tor, are all traditional dApps that run on a P2P network, but not on a Blockchain (which is a specific kind of P2P network). As opposed to simple smart contracts, in the classic sense of Bitcoin, that sends money from A to B, dApps have an unlimited number of participants on all sides of the market.
Q: Give some examples of DApps
Golem. The Golem project aims to create the first global market for idle computer power. Augur. Augur aims to combine the concept of prediction markets with the power of decentralised network to create a forecasting tool, for potential trading gains. Aragon Network aims to act as a digital jurisdiction that is extremely convenient for everyone to operate on.
Q: Explain Calldata
It can be thought of as the callstack. It is temporary, non-modifiable, and it stores EVM execution data.
Q: What does a contract consist of?
It consists mainly of storage variables, functions and events.
Q: Explain Memory
It is a temporary storage. The data is lost once the execution terminates. You can allocate complex datatypes like arrays and structs.
Q: What parts is the memory of an EVM divided into?
It is divided into Storage, Memory and Calldata
Q: What error will I get if I put multiple contract definitions into a single Solidity file?
It is perfectly fine to put multiple contract definitions into a single Solidity file.
Q: The only function of web3.eth.sendTransaction() is to send ethers to a specific address, is that correct?
No, you can also invoke contract methods.
Q: Take a look at the following code and explain which part of the code corresponds to which memory area: contract MyContract { // part 1 uint count; uint[] totalPoints; function localVars(){ // part 2 uint[] localArr; // part 3 uint[] memory memoryArr; // part 4 uint[] pointer = totalPoints; } }
Part 1 — Storage. Part 2 — Storage (array size points to the same location as counter) Part 3 — Memory. Part 4 — Reference to Storage.
Q: Give me a couple of differences between Java and Solidity.
Solidity supports multiple inheritance but doesn't support overloading
Q: What variables are stored in the Storage and Memory areas respectively?
State variables and local variables (which are references to the state variables) are stored in Storage. Function arguments are located in Memory area.
Q: What is the bytecode used for?
The EVM on each node requires bytecode in order to execute the contract code.
Q: What happens when you try to deploy a file with multiple contracts?
The compiler only deploys the last contract in that file and all other contracts are ignored.
Q: What types of functions are there?
There is a constructor, fallback function, constant functions and functions that modify the contract state.
Q: Explain Storage
Think of it as a database. Each contract manages its own Storage variables. It is a key-value datastore (256 bit key & value). The read and write are more costly in terms of gas used per execution.
Q: What does the front end use in order to connect to the backend (Smart Contracts)?
Web3 API libraries.
Q: How DApps are different from Smart Contracts ?
dApps are a 'blockchain enabled' website, where the Smart Contract is what allows it to connect to the blockchain. The easiest way to understand this is to understand how traditional websites operate. The traditional web application uses HTML, CSS and Javascript to render a page. It will also need to grab details from a database utilizing an API. When you go onto Facebook, the page will call an API to grab your personal data and display them on the page. Traditional websites: Front End → API → Database dApps are similar to a conventional web application. The front end uses the exact same technology to render the page. The one critical difference is that instead of an API connecting to a Database, you have a Smart Contract connecting to a blockchain. dApp enabled website: Front End → Smart Contract → Blockchain As opposed to traditional, centralized applications, where the backend code is running on centralized servers, dApps have their backend code running on a decentralized P2P network. Decentralized applications consist of the whole package, from backend to frontend. The smart contract is only one part of the dApp: Frontend (what you can see), and Backend (the logic in the background). A smart contract, on the other hand, consists only of the backend, and often only a small part of the whole dApp. That means if you want to create a decentralized application on a smart contract system, you have to combine several smart contracts and rely on 3rd party systems for the front-end.