Skip to content

Instantly share code, notes, and snippets.

@njgheorghita
Last active January 10, 2019 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save njgheorghita/cc1bd25d7ae90c85751bd31e8d50a0f5 to your computer and use it in GitHub Desktop.
Save njgheorghita/cc1bd25d7ae90c85751bd31e8d50a0f5 to your computer and use it in GitHub Desktop.
- # Solidity source code
- contract_source_code = '''
- pragma solidity ^0.4.21;
-
- contract Greeter {
-     string public greeting;
-
-     function Greeter() public {
-         greeting = 'Hello';
-     }
-
-     function setGreeting(string _greeting) public {
-         greeting = _greeting;
-     }
- 
-     function greet() view public returns (string) {
-         return greeting;
-     }
- }
- '''

- compiled_sol = compile_source(contract_source_code) # Compiled source code
- contract_interface = compiled_sol['<stdin>:Greeter']

# web3.py instance
w3 = Web3(Web3.EthereumTesterProvider())

# set pre-funded account as sender
w3.eth.defaultAccount = w3.eth.accounts[0]

# Instantiate and deploy contract
- Greeter = w3.eth.contract(abi=contract_interface['abi'], bytecode=contract_interface['bin'])
+ Greeter = w3.pm.from_registry('trusted.registry.eth', 'Greeter')

# Submit the transaction that deploys the contract
tx_hash = Greeter.constructor().transact()

# Wait for the transaction to be mined, and get the transaction receipt
tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)

# Create the contract instance with the newly-deployed address
- greeter = w3.eth.contract(
-     address=tx_receipt.contractAddress,
-     abi=contract_interface['abi'],
- )
+ greeter = Greeter.get_contract_instance(address)

# Display the default greeting from the contract
print('Default contract greeting: {}'.format(
    greeter.functions.greet().call()
))

print('Setting the greeting to Nihao...')
tx_hash = greeter.functions.setGreeting('Nihao').transact()

# Wait for transaction to be mined...
w3.eth.waitForTransactionReceipt(tx_hash)

# Display the new greeting value
print('Updated contract greeting: {}'.format(
    greeter.functions.greet().call()
))

# When issuing a lot of reads, try this more concise reader:
reader = ConciseContract(greeter)
assert reader.greet() == "Nihao"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment