0% found this document useful (0 votes)
24 views7 pages

Solidity Functions and Variables Guide

The document provides an extensive overview of Solidity programming, covering variable initialization, function visibility, and gas usage. It also discusses smart contract interactions, testing methodologies, and key concepts in finite fields and elliptic curves. Additionally, it includes various commands for compiling, deploying, and interacting with smart contracts, as well as insights into computational complexity and algebraic structures.

Uploaded by

hiihello696970
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views7 pages

Solidity Functions and Variables Guide

The document provides an extensive overview of Solidity programming, covering variable initialization, function visibility, and gas usage. It also discusses smart contract interactions, testing methodologies, and key concepts in finite fields and elliptic curves. Additionally, it includes various commands for compiling, deploying, and interacting with smart contracts, as well as insights into computational complexity and algebraic structures.

Uploaded by

hiihello696970
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

In Solidity, when we don’t initialize a variable, it automatically gets


assigned to a faulty value.
2. In Solidity, there are 4 function view setting – private, public,
internal and external.

3. In Solidity, public functions can be accessed from anywhere, both


within the same contract and externally, including other contracts or
users. External functions can only be called from outside the
contract, either by users or other contracts, but not internally unless
using this. Internal functions are accessible within the same contract
and in contracts that inherit from it. Private functions are strictly
limited to the contract where they are defined and cannot be
accessed from inherited contracts or externally.
4. If we don’t set the function or variable view settings, it will
automatically take up the internal type.

5. In Solidity, view functions can read blockchain state (e.g., variables


or balances) but cannot modify it, making them ideal for retrieving
data. Pure functions neither read nor modify blockchain state,
relying solely
on input
parameters
for
computations.
Both are gas-
free when
called off-chain but consume gas if used in on-chain transactions.
6. In a function, arguments and functions should be started with _. If
the function is private, then the function name should also be
started with _.
7. Storing strings in storage is not allowed. It can be stored in memory.
8. Virtual and override keywords are used to show inheritance. Virtual
keyword is used in parent contract specifying that it can be override
in any other derived contract. Override is in derived contract and
can only be applied on a function with virtual keyword.
9. If in any contract we used contract A is B, then A can access all the
functionality and variables of contract B.
10. Payable function is used to make any function payable.
11. [Link] refers to the amount gwei sent in the transaction.
12. ABI stands for Application Binary Interface. It serves as a
standard way for contracts to interact with external entities like
other contracts or applications, such as decentralized applications
(dApps) or web interfaces.
13. [Link] is used to take record of the sender of the
amount.
14. In Solidity, amounts can be withdrawn using three primary
methods: (1) transfer: a simple and limited option that sends a
fixed amount of Ether to an address and automatically reverts if it
fails (also reverts the leftover gas) (gas limit: 2300), (2) send:
similar to transfer, but it returns a boolean indicating success or
failure instead of reverting, requiring explicit handling of failures (it
also don’t revert any gas in case of failure), and (3) call: the most
flexible and recommended method, which allows sending Ether
along with calling a function on the recipient address and returns a
boolean success status and data, making it suitable for interacting
with contracts or overcoming transfer and send's gas limitations.
(does not revert the leftover gas).
15. Whatever we write in constructor in Solidity, gets executed
immediately as soon as the contract is employed.
16. We can add modifier in a contract which will be implemented
in the functions we want.
17. In Solidity, constant variables are fixed and set when you write
the code (e.g., uint constant X = 10;), while immutable variables are
set only once during the contract’s deployment in the constructor.
After being set, both cannot be changed, and they save gas because
their values are stored in the contract code, not in storage.
18. We can make a custom error in Solidity using keyword error
outside the contract and then can use it in the contract using the
keyword revert.
19. Fallback() is executed when a non-existent function is called
on the contract. It is required to be marked external and no name
or arguments. It can’t return anything and can be defined one per
contract. If fnot marked payable, it will throw exception on receiving
ether. It’s main use is to directly send the ETH to the contract. It can
also take data in form of bytes. Receive is exact same as the
fallback except the fact that it is mandatory to mark receive as
payable and it can’t take data.
20. When both fallback and receive are present in the contract
and if the contract gets only ETH, then it will take it via receive. But
if it receives both data and ETH, then it will take it via fallback.
21. In Solidity, a script typically refers to an external program or
script (written in tools like Hardhat, Foundry, or Truffle) that interacts
with smart contracts. These scripts automate tasks like deploying
contracts, testing functionality, interacting with deployed contracts,
or simulating transactions. Scripts are essential because they
streamline the deployment process, enable reproducible and
automated interactions with contracts, and facilitate testing and
debugging by simulating real-world scenarios. This helps developers
ensure the reliability and functionality of their smart contracts
efficiently.
22. ```
forge build
```
command which should be run before compilation
23. ```
forge create SimpleStorage –interactive
```
command to compile a .sol file
24. ```
cast wallet import defaultKey –interactive
```
command to encrypt a private key
25. ```
forge script script/[Link]
```
command to run the script file on a local blockchain which will not
produce a broadcast .json file
26. ```
forge script script/[Link] --rpc-url
[Link] --broadcast --private-key
0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a80
4cdab365a(this is a fake private key)
```
command to run the script file on anvil resulting in creation of
a .json file
27. ```
forge script script/[Link] --rpc-url
[Link] --account defaultKey --sender
0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266 –broadcast
```
command to run the script using encrypted private key. The address
here is the address where the private key is stored. This encryption
follows ERC : 2335 protocol.
28. ```
cast send 0x5fbdb2315678afecb367f032d93f642f64180aa3
"store(uint256)" 69 --rpc-url [Link] --account
defaultKey 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
```
command used to interact with the contract in terminal. The store
here is a function in the contract. The send is used when we want to
send any data and call is used to callback a stored data. The ‘69’ is
the data that we want to send. When we use call we don’t put any
arguments in the function and don’t need to give the private key.
29. ```
cast --to-base
0x0000000000000000000000000000000000000000000000000000
000000000045 dec
```
used to convert hex to decimal
30. In Solidity, we use memory with strings when they are being
used temporarily like when they are used in a function.
31. We use -vv option to make the console effective in a test file.
32. We can do 4 types of test – Unit (testing a part of the code),
Integration (testing how our code works with other parts of the
code), forked (testing our code on a simulated real environment)
and staging (testing our code in a real environment that is not
production).
33. ```
forge test --fork-url
[Link] -
vvvvvvvv
```
command to run forked tests on a test file with detailed viewing.
34. [Link]() is a cheat code used in test file which only
passes when the line beneath it fails.
35. We can generate address like address(index) but the index
must be uint160 because address also consists of 160 bits.
36. A storage is a place where the global variables are stored in a
contract. These variables are stored in a 32-bit format. These
variables are stored in form of a number array.
37. For arrays and mapping, only their length is stored in this
storage. Their elements are stored with help of hashing.
38. Constant and immutable variables are not stored in this
storage. Rather they act as a pointer to the value assigned to them.
39. We can see the storage of a contract by the command
```
forge inspect “contract_name” storageLayout
```
40. The gas depends on the op codes we use because they cost
some gas everytime we use them. Just for reference, storing and
reading from storage costs a lot.
41. A makefile is used to make shortcuts.
42. ERCs are subset of EIPs. The former has interfaces while the
latter ones don’t.
43. Pure functions don’t read from the blockchain whereas view
function reads from the blockchain.
44. Uniform Resource Identifier (URI) identifies a piece of data on
a webpage or anything similar.
45. Uniform Resource Locator (URL) just locates this data.
46. A tokenURI function retuurns the metadata fot the nft which
specifies all the properties of it. This metadata is unique for each
tokenID.
47. We can’t compare strings in solidity because they are array of
bytes. To compare them, we first convert them to bytes and then
apply abiencoded keccak256 hacking onto them.
48. assertEq is not designed to compare strings. The later is done
with assert.
49. Base64 -I relative_path_of_file is used to encrypt any file.
50. [Link] encodes the data in computer readable form.
[Link] does the same but removes the zero that are not
doing anything but only taking space.
51. [Link] is used to decode string.
52. P problems are easy to solve and easy to verify.
53. PSPACE problems are complex to solve and complex to verify.
54. NP problems are complex to solve and easy to verify, for example, a sudoku problem.
55. P is a subset of NP and NP are a subset of PSPACE.
56. It is assumed that if we have the computation power to solve or verify the problem
in the outer subset then we will have the same for inner subset.
57. Currently, research is going on to prove NP = P.
58. A finite field P is a field containing numbers from 0 to P-1 and allows addition and
multiplication and all the calculations are done along with P. The order of field and
characteristic is P (when P Is prime).
59. In a prime finite field, a and p-a are each other’s additive inverse and 1 and p-1 are
itself multiplicative inverse.
60. According to Fermat’s theorem a^p (mod p) = a.
61. a^(p-2) is the multiplicative inverse of a.
62. 5/6 = 5 * multiplicative_inverse(6).
63. No two elements can be multiplied together to obtain zero in a finite field unless one of the
elements is zero itself.
64. ```
def legendre_symbol(a, p):
65. return pow(a, (p - 1) // 2, p)
66.
67. def tonelli_shanks(a, p):
68. if legendre_symbol(a, p) != 1:
69. return None # No solution
70.
71. # Factor p-1 as Q * 2^S
72. Q, S = p - 1, 0
73. while Q % 2 == 0:
74. Q //= 2
75. S += 1
76.
77. # Find a quadratic non-residue z
78. z = 2
79. while legendre_symbol(z, p) != p - 1:
80. z += 1
81.
82. # Initialize values
83. M, c, t, R = S, pow(z, Q, p), pow(a, Q, p), pow(a, (Q + 1) // 2, p)
84.
85. while t != 1:
86. i, temp = 0, t
87. while temp != 1:
88. temp = pow(temp, 2, p)
89. i += 1
90. if i == M:
91. return None # Should not happen if a is a QR
92.
93. b = pow(c, 2 ** (M - i - 1), p)
94. R, c, t, M = (R * b) % p, (b * b) % p, (t * b * b) % p, i
95.
96. return R
```
97. A primitive root is the root for which every power from 1 to p-1 raised on that number
results in a distinct number in the field.
98. A magma is a set having a closed binary operator. A semi group is a magma which is
associative. A monoid is a semi group which contains identity element. A group is a monoid
in which every element has an inverse. Without an identity element there is no meaning of
inverse. An abelian group is commutative. A group can be finite or cyclic. If a group is cyclic
then it is abelian.
99. A primitive root in a field is by powers of which we can generate any other number in the
field.
100. Any line except for a vertical one will intersect an elliptic curve at three points if it
intersects at two. Two of those points could be the same point if one of the points of
intersection is a tangent point.
101. The inverse of an elliptic curve point is the negative of the y value of the pair.
102. When the order of the curve matches the order of the finite field every operation
you do in the finite field has a homomorphic equivalent in the elliptic curve.
103. The field modulus is the modulo we do the curve over. The curve order is the
number of points on the curve.
104. Ethereum uses bnn128 elliptic curve.
105. A Rank 1 Constraint System (R1CS) is an arithmetic circuit with the requirement that
each equality constraint has one multiplication (and no restriction on the number of
additions).
106. Our witness vector will be 1Xn and all the other ones will be mXn where n is the
number of variables and m is the number of constraints.

You might also like