-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnippets.json
138 lines (138 loc) · 5.05 KB
/
snippets.json
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
{
"requests": [
{
"action": "addObject",
"body": {
"title": "How to mark a function as private in Solidity",
"author": "thomasvds",
"updated_at": "Jan 05, 2018",
"language": "solidity",
"code": [
"function _addToArray(uint _number) private {",
" numbers.push(_number);",
"}"
],
"advice": "use the <span class=\"code-token\">private</span> modifier, and a starting underscore <span class=\"code-token\">_</span> as a convention to emphasize the private nature of your function."
}
},
{
"action": "addObject",
"body": {
"title": "How to listen to a contract event in JavaScript",
"author": "thomasvds",
"updated_at": "Jan 05, 2018",
"language": "javascript",
"code": [
"Contract.MyEvent(function(error, result) {",
" if (error) return;",
" console.log(result.args);",
"});"
],
"advice": "the <span class='code-token'>result</span> object contains both the event data as defined in the contract event code, under the <span class='code-token'>args</span> object, as well as event metadata."
}
},
{
"action": "addObject",
"body": {
"title": "How to load a contract in JavaScript",
"author": "thomasvds",
"updated_at": "Jan 05, 2018",
"language": "javascript",
"code": [
"const MyContract = web3.eth.contract(abi);",
"const Contract = MyContract.at(address);"
],
"advice": "the <span class='code-token'>abi</span> is the Application Binary Interface of the contract. If you compile with Remix for example, you'll find it in the details of the contract. <span class='code-token'>address</span> is the address of your contract once it has been deployed on the Blockchain. Once these commands have been run, you can access your contract public functions like with any JS object."
}
},
{
"action": "addObject",
"body": {
"title": "How to declare a Struct in Solidity",
"author": "thomasvds",
"updated_at": "Jan 05, 2018",
"language": "solidity",
"code": [
"struct Person {",
" string name;",
" uint age;",
"}"
],
"advice": "Don't forget the uppercase in your struct name. After declaration, your struct can be used like other types."
}
},
{
"action": "addObject",
"body": {
"title": "How to declare a dynamic array in Solidity",
"author": "thomasvds",
"updated_at": "Jan 05, 2018",
"language": "solidity",
"code": [
"string[] public names;"
],
"advice": "the first token indicates the type of the objects the array will store. Using the empty brackets indicates this array can size indefinitely."
}
},
{
"action": "addObject",
"body": {
"title": "How to create a new contract in Solidity",
"author": "thomasvds",
"updated_at": "Jan 05, 2018",
"language": "solidity",
"code": [
"pragma solidity ^0.4.19;",
"",
"contract MyContract {",
" // contract functions and variables go here",
"}"
],
"advice": "the <span class='code-token'>pragma</span> statement tells the compiler which version of the Solidity language you are using. Find the latest on the <a href='https://github.com/ethereum/solidity/releases'>GitHub repo</a> of Solidity."
}
},
{
"action": "addObject",
"body": {
"title": "How to declare a mapping in Solidity",
"author": "thomasvds",
"updated_at": "Jan 05, 2018",
"language": "solidity",
"code": [
"mapping (address => uint) balances;"
],
"advice": "be advised that mappings cannot be iterated on. Their data can only be retrieved if you know the corresponding index."
}
},
{
"action": "addObject",
"body": {
"title": "How to create and emit an event in Solidity",
"author": "thomasvds",
"updated_at": "Jan 05, 2018",
"language": "solidity",
"code": [
"event MyEvent(string myMessage);",
"",
"function myFunction(string _message) public {",
" MyEvent(_message);",
"}"
],
"advice": "listening to your contract events will let you know when certain transactions have actually occured, giving you a feedback after you've called one of your contract functions."
}
},
{
"action": "addObject",
"body": {
"title": "How to convert Ether to Wei in JavaScript",
"author": "thomasvds",
"updated_at": "Jan 08, 2018",
"language": "JavaScript",
"code": [
"web3.utils.toWei('1', 'ether');"
],
"advice": "<span class='code-token'>wei</span> are the smallest unit, and you should always make calculations in wei and convert only for display reasons."
}
}
]
}