@@ -202,19 +202,19 @@ Given the following solidity source file stored at ``contract.sol``.
202
202
.. code-block :: javascript
203
203
204
204
contract StoreVar {
205
-
205
+
206
206
uint8 public _myVar;
207
207
event MyEvent (uint indexed _var);
208
-
208
+
209
209
function setVar (uint8 _var ) public {
210
210
_myVar = _var;
211
211
MyEvent (_var);
212
212
}
213
-
213
+
214
214
function getVar () public view returns (uint8) {
215
215
return _myVar;
216
216
}
217
-
217
+
218
218
}
219
219
220
220
The following example demonstrates a few things:
@@ -229,45 +229,45 @@ The following example demonstrates a few things:
229
229
import sys
230
230
import time
231
231
import pprint
232
-
232
+
233
233
from web3.providers.eth_tester import EthereumTesterProvider
234
234
from web3 import Web3
235
235
from solc import compile_source
236
-
236
+
237
237
238
238
def compile_source_file (file_path ):
239
239
with open (file_path, ' r' ) as f:
240
240
source = f.read()
241
-
241
+
242
242
return compile_source(source)
243
-
244
-
243
+
244
+
245
245
def deploy_contract (w3 , contract_interface ):
246
246
tx_hash = w3.eth.contract(
247
247
abi = contract_interface[' abi' ],
248
248
bytecode = contract_interface[' bin' ]).deploy()
249
-
249
+
250
250
address = w3.eth.getTransactionReceipt(tx_hash)[' contractAddress' ]
251
251
return address
252
-
253
-
252
+
253
+
254
254
w3 = Web3(EthereumTesterProvider())
255
-
255
+
256
256
contract_source_path = ' contract.sol'
257
257
compiled_sol = compile_source_file(' contract.sol' )
258
-
258
+
259
259
contract_id, contract_interface = compiled_sol.popitem()
260
-
260
+
261
261
address = deploy_contract(w3, contract_interface)
262
262
print (" Deployed {0} to: {1} \n " .format(contract_id, address))
263
-
263
+
264
264
store_var_contract = w3.eth.contract(
265
265
address = address,
266
266
abi = contract_interface[' abi' ])
267
-
267
+
268
268
gas_estimate = store_var_contract.functions.setVar(255 ).estimateGas()
269
269
print (" Gas estimate to transact with setVar: {0} \n " .format(gas_estimate))
270
-
270
+
271
271
if gas_estimate < 100000 :
272
272
print (" Sending transaction to setVar(255)\n " )
273
273
tx_hash = store_var_contract.functions.setVar(255 ).transact()
0 commit comments