Check our terms and conditions, privacy policies, and cookies policy.
Copyright 2024 © All rights Reserved. Designed by LACNet
Before deploying your smart contracts in the LACChain Networks, see this overview that explains the minor changes you will need to incorporate to them.
When blockchain development is carried out in Python it is common to use web3py. If that is your case, in the following guide we will explain the necessary changes and examples of how to deploy a contract and send a transaction using the Python programming language to the LACChain node.
We need to install the following libraries that we will use:
pip install web3
pip install py-solc-x
pip install eth_abi
pip install pycryptodome
In the following documentation we are going to use the following contract storage2 which we will deploy and send transactions.
To deploy a smart contract please create a new file called compile.py. Now, copy the following example in compile.py file:
# 1. Import solcx
import solcx
# 3. Compile contract
temp_file = solcx.compile_files(
'Storage2.sol',
output_values=['abi', 'bin'],
# solc_version='0.6.0'
)
# 4. Export contract data
abi = temp_file['Storage2.sol:Storage2']['abi']
bytecode = temp_file['Storage2.sol:Storage2']['bin']
Now create a new file called deploy.py. Now, copy the following example in deploy.py file:
# 1. Add imports
from compile import abi, bytecode
from web3 import Web3
from eth_abi import encode
# 2. Add the Web3 provider logic here:
provider_rpc = {
"development": "http://YOUR_NODE_RPC",
}
web3 = Web3(Web3.HTTPProvider(provider_rpc["development"]))
# 3. Create address variable
account_from = {
'private_key': '',
'address': '',
}
print(f'Attempting to deploy from account: { account_from["address"] }')
# 4. Create contract instance
expiration = 1731848762;
nodeAddress = ''
paramEncodeExtra=encode(['address','uint256'], [nodeAddress,expiration]).hex()
encodeAby = '0x'+ bytecode + paramEncodeExtra
nonce=web3.eth.get_transaction_count(web3.to_checksum_address(account_from["address"]))
# 5. Build constructor tx
construct_txn ={'value': 0,
'gas': 2612890,
'from': web3.to_checksum_address(account_from["address"]),
'nonce': nonce,
'gasPrice': 0,
'data': encodeAby}
# 6. Sign tx with PK
tx_create = web3.eth.account.sign_transaction(
construct_txn, account_from["private_key"]
)
# 7. Send tx and wait for receipt
tx_hash = web3.eth.send_raw_transaction(tx_create.rawTransaction)
tx_receipt = web3.eth.wait_for_transaction_receipt(tx_hash)
print(f"Contract deployed at address: { tx_receipt.contractAddress }")
It is important to mention that two parameters are added, the nodeAddress and the time expiration, these two parameters are in the part of “//Encode LACChain parameters” in the previous code. These two parameters are necessary to send the transaction to the network.
After copying the code, consider changing the following values:
Run the following to deploy the contract:
> python3 deploy.py
If everything is correct the Contract Address will show you
Attempting to deploy from account: 0x627306090abaB3A6e1400e9345bC60c78a8BEf57
Contract deployed at address: 0x970b06198B29007e93346ad23530a2bcceB29395
In this example we will send a transaction to the previously deployed contract, to change the value stored in the contract.
To send Transaction to smart contract deployed please create new file called send-tx.py. Now copy the following example:
# 1. Add imports
from web3 import Web3
import random
from eth_abi import encode
from Crypto.Hash import keccak
# 2. Add the Web3 provider logic here:
provider_rpc = {
"development": "http://YOUR_NODE_RPC",
}
web3 = Web3(Web3.HTTPProvider(provider_rpc["development"]))
# 3. Create variables
account_from = {
'private_key': '',
'address': '',
}
contract_address = ''
ramdomNumber = random.randint(1, 20)
print(
f"Calling the store by { ramdomNumber } function in contract at address: { contract_address }"
)
# 4. Create contract instance
nonce=web3.eth.get_transaction_count(web3.to_checksum_address(account_from["address"]))
# 5. Build store tx
expiration = 1731848762;
nodeAddress = ''
paramEncode=encode(['uint256'], [ramdomNumber]).hex()
paramEncodeExtra=encode(['address','uint256'], [nodeAddress,expiration]).hex()
k = keccak.new(digest_bits=256)
k.update(b'store(uint256)')
funcEncode=k.hexdigest()[:8]
encodeAby= '0x'+ funcEncode + paramEncode + paramEncodeExtra
increment_tx ={'value': 0,
'gas': 2612890,
'from': web3.to_checksum_address(account_from["address"]),
'nonce': nonce,
'gasPrice': 0,
'to': contract_address,
'data': encodeAby}
# 6. Sign tx with PK
tx_create = web3.eth.account.sign_transaction(increment_tx, account_from["private_key"])
# 7. Send tx and wait for receipt
tx_hash = web3.eth.send_raw_transaction(tx_create.rawTransaction)
tx_receipt = web3.eth.wait_for_transaction_receipt(tx_hash)
print(f"Tx successful with hash: { tx_receipt.transactionHash.hex() }")
Run the following to deploy the Transaction:
> python3 send-tx.py
If everything is correct, you will see the following lines in your console:
Calling the store by 7 function in contract at address: 0x970b06198B29007e93346ad23530a2bcceB29395
Tx successful with hash: 0xbe524f5d0bde4d46fb5ce6940dca40e32c6b2cb4e646a4bc43dd087e9468f036
In this example we will call to contract to send transaction to contract, to get the value stored in the contract.
To call to smart contract please create new file called get.py. Now copy the following example:
from web3 import Web3
from compile import abi
# 1. Add the Web3 provider logic here:
provider_rpc = {
"development": "http://YOUR_NODE_RPC",
}
web3 = Web3(Web3.HTTPProvider(provider_rpc["development"]))
contract_address = ''
print(f"Making a call to contract at address: { contract_address }")
# 2. Create contract instance
Storage = web3.eth.contract(address=contract_address, abi=abi)
# 3. Call Contract
asset = Storage.functions.retreive().call()
print(f"The current number stored is: { asset } ")
If everything is correct, you will see the following lines in your console:
Making a call to contract at address: 0x970b06198B29007e93346ad23530a2bcceB29395
The current number stored is: 7
Check our terms and conditions, privacy policies, and cookies policy.
Copyright 2024 © All rights Reserved. Designed by LACNet