Skip to content

Commit

Permalink
Documentation update for contract creation
Browse files Browse the repository at this point in the history
  • Loading branch information
gabedonnan committed Feb 12, 2024
1 parent f2e7603 commit 6d1d847
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion docs/source/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -656,4 +656,48 @@ These objects generate functions based on those contained in the ABI so users ca
data=abi.swap_owners(address_one, address_two) # Can be called using snake_case for extra pythonic-ness
)
It is not currently possible to automatically get ABI or un-compiled contract data without external block explorers
It is not currently possible to automatically get ABI or un-compiled contract data without external block explorers

Deploying a Smart Contract
==========================

Deploying a contract is much the same as sending any regular transaction with some small changes.

.. code-block:: python
:linenos:
import asyncio
import solcx # Solidity compiler
from eth_account import Account
from pythereum import EthRPC, Transaction
async def my_first_contract(contract_bytecode: str):
# Create an arbitrary account wallet
acct = Account.create()
tx = Transaction(
from_address=acct.address,
to_address=None, # The to address is 0x80, which is equivalent to None
value=0, # Contracts must be sent to this address to publish them
chain_id=1,
nonce=0,
max_fee_per_gas=0,
max_priority_fee_per_gas=0,
data=contract_bytecode,
)
async with EthRPC(ENDPOINT_URL, pool_size=1) as rpc:
gas_price = await rpc.estimate_gas(tx)
tx["gas"] = gas_price
signed_tx = acct.sign_transaction(tx).rawTransaction
print(await rpc.send_raw_transaction(signed_tx)) # Print contract address
if __name__ == "__main__":
bytecode = solcx.compile_files( # Compile some solidity code with solcx
["foo.sol"],
output_values=["abi", "bin-runtime"],
solc_version="0.7.0"
)["<stdin>:foo"]["bin"]
asyncio.run(my_first_transaction(bytecode))

0 comments on commit 6d1d847

Please sign in to comment.