-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate to 0.8.9
218 lines (162 loc) · 5.58 KB
/
update to 0.8.9
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
Updating Your Inbox Project to Solc v0.8.9
This lecture will walk you through all of the changes needed to bring your project up to date with the latest Solc v0.8.9.
Environment Setup
Due to expected dependency conflicts with old installed versions, it would be best to create a brand-new project.
In your terminal of choice, run the following:
mkdir inbox-updated
cd inbox-updated
npm init -y
npm install solc@0.8.9 web3 mocha ganache @truffle/hdwallet-provider
Update your test script in the package.json file to be "test": "mocha"
Copy your contracts directory (containing Inbox.sol), test directory (containing Inbox.test.js), compile.js, and deploy.js into the new inbox-updated project directory.
Here is the package.json file for reference (your versions should not be lower than those specified below):
{
"name": "inbox-updated",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"author": "",
"license": "ISC",
"dependencies": {
"@truffle/hdwallet-provider": "^2.1.12",
"ganache": "^7.8.0",
"mocha": "^10.2.0",
"solc": "^0.8.9",
"web3": "^4.0.2"
}
}
Inbox.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
contract Inbox {
string public message;
constructor(string memory initialMessage) {
message = initialMessage;
}
function setMessage(string memory newMessage) public {
message = newMessage;
}
}
Outline of changes:
Update the pragma version at the top of the contract file to ^0.8.9
Refactor the Inbox constructor to use the new constructor keyword - Source.
Specify the data location of the variables to be memory - Source.
Remove the public keyword from the constructor - Source.
Add an SPDX identifier to the top of the contract (will address compilation warnings) - Source.
compile.js
const path = require('path');
const fs = require('fs');
const solc = require('solc');
const inboxPath = path.resolve(__dirname, 'contracts', 'Inbox.sol');
const source = fs.readFileSync(inboxPath, 'utf8');
const input = {
language: 'Solidity',
sources: {
'Inbox.sol': {
content: source,
},
},
settings: {
outputSelection: {
'*': {
'*': ['*'],
},
},
},
};
module.exports = JSON.parse(solc.compile(JSON.stringify(input))).contracts[
'Inbox.sol'
].Inbox;
Outline of changes:
Add the expected JSON formatted input, specifying the language, sources, and outputSelection - Source.
Update the export to provide the expected JSON formatted output - Source
Note - the output is structured differently so the accessors have changed slightly. If you have a doubt you can add a console.log to view this structure:
console.log(JSON.parse(solc.compile(JSON.stringify(input))).contracts);
This will return the following:
{
'Inbox.sol': {
Inbox: {
abi: [Array],
devdoc: [Object],
evm: [Object],
ewasm: [Object],
metadata: '{...}',
storageLayout: [Object],
userdoc: [Object]
}
}
}
Inbox.test.js
const assert = require('assert');
const ganache = require('ganache');
const { Web3 } = require('web3');
const web3 = new Web3(ganache.provider());
const { abi, evm } = require('../compile');
let accounts;
let inbox;
beforeEach(async () => {
// Get a list of all accounts
accounts = await web3.eth.getAccounts();
inbox = await new web3.eth.Contract(abi)
.deploy({
data: evm.bytecode.object,
arguments: ['Hi there!'],
})
.send({ from: accounts[0], gas: '1000000' });
});
describe('Inbox', () => {
it('deploys a contract', () => {
assert.ok(inbox.options.address);
});
it('has a default message', async () => {
const message = await inbox.methods.message().call();
assert.equal(message, 'Hi there!');
});
it('can change the message', async () => {
await inbox.methods.setMessage('bye').send({ from: accounts[0] });
const message = await inbox.methods.message().call();
assert.equal(message, 'bye');
});
});
Outline of changes:
Update the import to destructure the abi (formerly the interface) and the evm (bytecode)
const { abi, evm } = require('../compile');
Pass the abi to the contract object
inbox = await new web3.eth.Contract(abi)
Assign the bytecode to the data property of the deploy method:
.deploy({
data: evm.bytecode.object,
deploy.js
const HDWalletProvider = require('@truffle/hdwallet-provider');
const { Web3 } = require('web3');
const { abi, evm } = require('./compile');
provider = new HDWalletProvider(
'YOUR_MNEMONIC',
'YOUR_INFURA_URL'
);
const web3 = new Web3(provider);
const deploy = async () => {
const accounts = await web3.eth.getAccounts();
console.log('Attempting to deploy from account', accounts[0]);
const result = await new web3.eth.Contract(abi)
.deploy({ data: evm.bytecode.object, arguments: ['Hi there!'] })
.send({ gas: '1000000', from: accounts[0] });
console.log('Contract deployed to', result.options.address);
provider.engine.stop();
};
deploy();
Outline of changes:
Update the import to use the newer @truffle/hdwallet-provider module.
Update the import to destructure the abi (formerly the interface) and the evm (bytecode)
const { abi, evm } = require('./compile');
Pass the abi to the contract object:
const result = await new web3.eth.Contract(abi)
Assign the bytecode to the data property of the deploy method:
.deploy({
data: evm.bytecode.object,
Call provider.engine.stop() to prevent deployment from hanging in the terminal - Source
Completed Code
Completed project with all changes described above can be found attached to this lecture note as a zip file.