-
Notifications
You must be signed in to change notification settings - Fork 6
/
solidity_cheatsheet.html
90 lines (86 loc) · 5.11 KB
/
solidity_cheatsheet.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>Solidity Reference</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
/* The extra [class] is a hack that increases specificity enough to
override a similar rule in reveal.js */
ul.task-list[class]{list-style: none;}
ul.task-list li input[type="checkbox"] {
font-size: inherit;
width: 0.8em;
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
.display.math{display: block; text-align: center; margin: 0.5rem auto;}
</style>
<link rel="stylesheet" href="../markdown.css" />
<script>
function openTab(evt, tabName) {
// Declare all variables
var i, tabcontent, tablinks;
// Get all elements with class="tabcontent" and hide them
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
// Get all elements with class="tablinks" and remove the class "active"
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tablinks.length; i++) {
tablinks[i].className = tablinks[i].className.replace(" active", "");
}
// Show the current tab, and add an "active" class to the button that opened the tab
document.getElementById(tabName).style.display = "block";
evt.currentTarget.className += " active";
}
function insertCopyLink(text) {
document.write("<span class=\"copylink copy_img\" onclick=\"navigator.clipboard.writeText('" + text.replace(/\"/g,"\\'") + "')\"></span>");
}
</script>
</head>
<body>
<h1 id="solidity-reference">Solidity Reference</h1>
<p><a href="index.html">Go up to the main CCC docs page</a> (<a href="index.md">md</a>)</p>
<p>This document is meant to be a series of reminders about Solidity programming tasks, all in one place.</p>
<h3 id="spdx-license-line">SPDX license line</h3>
<p>The first line of your program, it might look like:</p>
<pre><code>// SPDX-License-Identifier: Unlicensed</code></pre>
<p>The fill list can be found <a href="https://spdx.org/licenses/">here</a>. For this class, you can use any one you want. A few examples:</p>
<ul>
<li><code>Unlicensed</code>: there is no attached license. This will silence the SPDX missing warning.</li>
<li><code>GPL</code> or <code>GPL-3.0-only</code> or <code>GPL-3.0-or-later</code>: the <a href="https://spdx.org/licenses/GPL-3.0-or-later.html">GNU Public License</a> which means that anybody who uses that code must release it under a similar license (which, in practice, is pretty much always the GPL); this generally prevents commercial usage unless they publicly release their code</li>
<li><code>MIT</code>: the <a href="https://spdx.org/licenses/MIT.html">MIT license</a> which means anybody can use this code, personal or commercial, for any reason, and they do not have to release their derived code</li>
</ul>
<h3 id="paying-from-a-contract">Paying from a contract</h3>
<pre><code>(bool success, ) = payable(a).call{value: v}("");
require(success, "Failed to transfer ETH");</code></pre>
<h3 id="getting-the-balance-of-a-contract">Getting the balance of a contract</h3>
<p>This only pertains to the spring 2022 semester; outside that semester, just call <code>address(this).balance</code>.</p>
<p>Having a contract get it’s own balance should be easy – just <code>address(this).balance</code>. But for the spring 2022 semester, that is not working due to a misconfiguration in how the blockchain was setup – it complains about <code>SELFBALANCE</code> being an unknown opcode. So we need a work-around: a contract has to ask something <em>else</em> to get it’s own balance.</p>
<p>Consider this contract:</p>
<pre><code>// SPDX-License-Identifier: Unlicensed
pragma solidity ^0.8.16;
contract GetBalance {
function getBalance(address a) public view returns (uint) {
return a.balance;
}
}</code></pre>
<p>Once deployed, you can call the <code>getBalance()</code> method to get one’s own balance. This is deployed at address <code>0x1E2c5E21c3b9cFD67bedF28e04fDd295AecbC500</code> <span class="copylink" onclick="navigator.clipboard.writeText('0x1E2c5E21c3b9cFD67bedF28e04fDd295AecbC500')">⎘</span> on the course blockchain.</p>
<p>If you need to be able to switch between this code on the course blockchain, and Remix (in which you can just call <code>address(this).balance</code>), you can use the following method:</p>
<pre><code>// This is SUCH AND UGLY HACK, but it will work for the spring 2022 semester
function getSelfBalance() public view returns (uint) {
if ( block.number > 70000 )
return GetBalance(0x1E2c5E21c3b9cFD67bedF28e04fDd295AecbC500).getBalance(address(this));
else
return address(this).balance;
}</code></pre>
</body>
</html>