Skip to content

Commit

Permalink
Merge pull request #82 from multiversx/refactor-python-scripts
Browse files Browse the repository at this point in the history
Refactored python scripts
  • Loading branch information
iulianpascalau authored Sep 18, 2024
2 parents 5cfe567 + 9a543fb commit 37f2ae4
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 140 deletions.
46 changes: 20 additions & 26 deletions examples/contracts/adder/adder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,35 @@
import time
from pathlib import Path

from multiversx_sdk_core import TokenComputer, TransactionComputer, AddressFactory, Address, ContractQueryBuilder
from multiversx_sdk_core.transaction_factories import SmartContractTransactionsFactory, TransactionsFactoryConfig
from multiversx_sdk_network_providers import ProxyNetworkProvider
from multiversx_sdk_network_providers.transactions import TransactionOnNetwork
from multiversx_sdk_wallet import UserPEM, UserSigner
from multiversx_sdk import UserSecretKey
from multiversx_sdk.core import Address, ContractQueryBuilder
from multiversx_sdk.core import SmartContractTransactionsFactory, TransactionsFactoryConfig
from multiversx_sdk.network_providers import ProxyNetworkProvider
from multiversx_sdk.network_providers.transactions import TransactionOnNetwork

SIMULATOR_URL = "http://localhost:8085"
GENERATE_BLOCKS_URL = f"{SIMULATOR_URL}/simulator/generate-blocks"
GENERATE_BLOCKS_UNTIL_EPOCH_REACHED_URL = f"{SIMULATOR_URL}/simulator/generate-blocks-until-epoch-reached"
GENERATE_BLOCKS_UNTIL_TX_PROCESSED = f"{SIMULATOR_URL}/simulator/generate-blocks-until-transaction-processed"


def main():
# create a network provider
provider = ProxyNetworkProvider(SIMULATOR_URL)

pem = UserPEM.from_file(Path("../../wallets/wallet.pem"))
key = UserSecretKey.generate()
address = key.generate_public_key().to_address("erd")
print(f"working with the generated address: {address.to_bech32()}")

# call proxy faucet
address = pem.public_key.to_address("erd")
data = {"receiver": f"{address.to_bech32()}"}
provider.do_post(f"{SIMULATOR_URL}/transaction/send-user-funds", data)

# generate 20 blocks to pass an epoch and the smart contract deploys to be enabled
provider.do_post(f"{GENERATE_BLOCKS_URL}/20", {})
# generate blocks until smart contract deploys are enabled
provider.do_post(f"{GENERATE_BLOCKS_UNTIL_EPOCH_REACHED_URL}/1", {})

config = TransactionsFactoryConfig(provider.get_network_config().chain_id)

sc_factory = SmartContractTransactionsFactory(config, TokenComputer())
sc_factory = SmartContractTransactionsFactory(config)

bytecode = Path("./adder.wasm").read_bytes()
deploy_transaction = sc_factory.create_transaction_for_deploy(
Expand All @@ -45,20 +47,15 @@ def main():

# set nonce
deploy_transaction.nonce = provider.get_account(address).nonce

# sign transaction
user_signer = UserSigner(pem.secret_key)
tx_computer = TransactionComputer()
deploy_transaction.signature = user_signer.sign(tx_computer.compute_bytes_for_signing(deploy_transaction))
deploy_transaction.signature = b"dummy"

# send transaction
tx_hash = provider.send_transaction(deploy_transaction)
print(f"deploy tx hash: {tx_hash}")

time.sleep(0.5)

# execute 1 block
provider.do_post(f"{GENERATE_BLOCKS_URL}/1", {})
# generate enough blocks until the transaction is completed
provider.do_post(f"{GENERATE_BLOCKS_UNTIL_TX_PROCESSED}/{tx_hash}", {})

# get transaction with status
tx_from_network = provider.get_transaction(tx_hash, with_process_status=True)
Expand All @@ -78,16 +75,15 @@ def main():
)

call_transaction.nonce = provider.get_account(address).nonce
call_transaction.signature = user_signer.sign(tx_computer.compute_bytes_for_signing(call_transaction))
call_transaction.signature = b"dummy"

# send transaction
tx_hash = provider.send_transaction(call_transaction)
print(f"sc call tx hash: {tx_hash}")

time.sleep(0.5)

# execute 1 block
provider.do_post(f"{GENERATE_BLOCKS_URL}/1", {})
# generate enough blocks until the transaction is completed
provider.do_post(f"{GENERATE_BLOCKS_UNTIL_TX_PROCESSED}/{tx_hash}", {})

# query
builder = ContractQueryBuilder(
Expand All @@ -107,14 +103,12 @@ def main():


def extract_contract_address(tx: TransactionOnNetwork) -> Address:
factory = AddressFactory("erd")
for event in tx.logs.events:
if event.identifier != "SCDeploy":
continue

return factory.create_from_hex(event.topics[0].hex())
return Address.new_from_hex(event.topics[0].hex(), "erd")


if __name__ == "__main__":
main()

33 changes: 16 additions & 17 deletions examples/contracts/issue-esdt-with-contract/issue-with-contract.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
import base64
import sys
import time
from pathlib import Path

from multiversx_sdk_core import TokenComputer, AddressFactory, Address
from multiversx_sdk_core.transaction_factories import SmartContractTransactionsFactory, TransactionsFactoryConfig
from multiversx_sdk_network_providers import ProxyNetworkProvider
from multiversx_sdk_network_providers.transactions import TransactionOnNetwork
from multiversx_sdk_wallet import UserPEM
from multiversx_sdk import UserSecretKey
from multiversx_sdk.core import Address
from multiversx_sdk.core import SmartContractTransactionsFactory, TransactionsFactoryConfig
from multiversx_sdk.network_providers import ProxyNetworkProvider
from multiversx_sdk.network_providers.transactions import TransactionOnNetwork

SIMULATOR_URL = "http://localhost:8085"
GENERATE_BLOCKS_URL = f"{SIMULATOR_URL}/simulator/generate-blocks"
GENERATE_BLOCKS_UNTIL_EPOCH_REACHED_URL = f"{SIMULATOR_URL}/simulator/generate-blocks-until-epoch-reached"
GENERATE_BLOCKS_UNTIL_TX_PROCESSED = f"{SIMULATOR_URL}/simulator/generate-blocks-until-transaction-processed"


def main():
# create a network provider
provider = ProxyNetworkProvider(SIMULATOR_URL)

pem = UserPEM.from_file(Path("../../wallets/wallet.pem"))
key = UserSecretKey.generate()
address = key.generate_public_key().to_address("erd")
print(f"working with the generated address: {address.to_bech32()}")

# call proxy faucet
address = pem.public_key.to_address("erd")
data = {"receiver": f"{address.to_bech32()}"}
provider.do_post(f"{SIMULATOR_URL}/transaction/send-user-funds", data)

# generate 20 blocks to pass an epoch and the smart contract deploys to be enabled
provider.do_post(f"{GENERATE_BLOCKS_URL}/20", {})
# generate blocks until smart contract deploys & ESDTs are enabled
provider.do_post(f"{GENERATE_BLOCKS_UNTIL_EPOCH_REACHED_URL}/1", {})

config = TransactionsFactoryConfig(provider.get_network_config().chain_id)

sc_factory = SmartContractTransactionsFactory(config, TokenComputer())
sc_factory = SmartContractTransactionsFactory(config)

bytecode = Path("./contract.wasm").read_bytes()
deploy_transaction = sc_factory.create_transaction_for_deploy(
Expand All @@ -52,9 +54,8 @@ def main():
print(f"deploy tx hash: {tx_hash}")

time.sleep(0.5)

# execute 1 block
provider.do_post(f"{GENERATE_BLOCKS_URL}/1", {})
# generate enough blocks until the transaction is completed
provider.do_post(f"{GENERATE_BLOCKS_UNTIL_TX_PROCESSED}/{tx_hash}", {})

# get transaction with status
tx_from_network = provider.get_transaction(tx_hash, with_process_status=True)
Expand Down Expand Up @@ -101,14 +102,12 @@ def get_processed_status(provider, tx_hash):


def extract_contract_address(tx: TransactionOnNetwork) -> Address:
factory = AddressFactory("erd")
for event in tx.logs.events:
if event.identifier != "SCDeploy":
continue

return factory.create_from_hex(event.topics[0].hex())
return Address.new_from_hex(event.topics[0].hex(), "erd")


if __name__ == "__main__":
main()

42 changes: 21 additions & 21 deletions examples/contracts/wrappedegld/wrapped-egld.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import json
import sys
import time
from pathlib import Path

from multiversx_sdk_core import AddressFactory, TokenComputer, TransactionComputer
from multiversx_sdk_core.transaction_factories import TransactionsFactoryConfig, SmartContractTransactionsFactory
from multiversx_sdk_network_providers import ProxyNetworkProvider
from multiversx_sdk_wallet import UserPEM, UserSigner
from multiversx_sdk import UserSecretKey
from multiversx_sdk.core import Address
from multiversx_sdk.core import TransactionsFactoryConfig, SmartContractTransactionsFactory
from multiversx_sdk.network_providers import ProxyNetworkProvider

SIMULATOR_URL = "http://localhost:8085"
GENERATE_BLOCKS_URL = f"{SIMULATOR_URL}/simulator/generate-blocks"
GENERATE_BLOCKS_UNTIL_EPOCH_REACHED_URL = f"{SIMULATOR_URL}/simulator/generate-blocks-until-epoch-reached"
GENERATE_BLOCKS_UNTIL_TX_PROCESSED = f"{SIMULATOR_URL}/simulator/generate-blocks-until-transaction-processed"
WRAPPED_EGLD_TOKEN = "WEGLD-bd4d79"


def main():
# create a network provider
provider = ProxyNetworkProvider(SIMULATOR_URL)

pem = UserPEM.from_file(Path("../../wallets/wallet.pem"))
key = UserSecretKey.generate()
address = key.generate_public_key().to_address("erd")
print(f"working with the generated address: {address.to_bech32()}")

# call proxy faucet
address = pem.public_key.to_address("erd")
data = {"receiver": f"{address.to_bech32()}"}
provider.do_post(f"{SIMULATOR_URL}/transaction/send-user-funds", data)

# generate 20 blocks to pass an epoch and the smart contract deploys to be enabled
provider.do_post(f"{GENERATE_BLOCKS_URL}/20", {})
# generate blocks until ESDTs are enabled
provider.do_post(f"{GENERATE_BLOCKS_UNTIL_EPOCH_REACHED_URL}/1", {})

# set state for wrapped egld contract and system account on shard 1
# load JSON data from the file
Expand All @@ -34,11 +35,12 @@ def main():

provider.do_post(f"{SIMULATOR_URL}/simulator/set-state", json_data)

factory = AddressFactory("erd")
wrapp_contract_address = factory.create_from_bech32("erd1qqqqqqqqqqqqqpgqhe8t5jewej70zupmh44jurgn29psua5l2jps3ntjj3")
wrapp_contract_address = Address.new_from_bech32(
"erd1qqqqqqqqqqqqqpgqhe8t5jewej70zupmh44jurgn29psua5l2jps3ntjj3"
)

config = TransactionsFactoryConfig(provider.get_network_config().chain_id)
sc_factory = SmartContractTransactionsFactory(config, TokenComputer())
sc_factory = SmartContractTransactionsFactory(config)
amount_egld = 5000000000000000000
call_transaction = sc_factory.create_transaction_for_execute(
sender=address,
Expand All @@ -50,22 +52,20 @@ def main():
)

call_transaction.nonce = provider.get_account(address).nonce
# sign transaction
user_signer = UserSigner(pem.secret_key)
tx_computer = TransactionComputer()
call_transaction.signature = user_signer.sign(tx_computer.compute_bytes_for_signing(call_transaction))
call_transaction.signature = b"dummy"

# send transaction
tx_hash = provider.send_transaction(call_transaction)
print(f"wrapp egld tx hash: {tx_hash}")

time.sleep(1)
time.sleep(0.05)
# generate enough blocks until the transaction is completed
provider.do_post(f"{GENERATE_BLOCKS_UNTIL_TX_PROCESSED}/{tx_hash}", {})

# execute 5 block ( transaction needs to be executed on source, block on source has to be finalized...)
provider.do_post(f"{GENERATE_BLOCKS_URL}/5", {})
amount = provider.get_fungible_token_of_account(address, WRAPPED_EGLD_TOKEN)
if amount.balance != amount_egld:
sys.exit(f"amount of token from balance is no equal with the initial supply: actual-{amount.balance}, expected-{amount_egld}")
sys.exit(f"amount of token from balance is no equal with the initial supply: "
f"actual-{amount.balance}, expected-{amount_egld}")

print("transaction was executed, initial address received the wrapped egld token")

Expand Down
32 changes: 14 additions & 18 deletions examples/esdt/issue-fungible.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
import sys

from multiversx_sdk_network_providers import ProxyNetworkProvider
from multiversx_sdk_network_providers.transactions import TransactionOnNetwork
from multiversx_sdk_core.transaction_factories import TokenManagementTransactionsFactory, TransactionsFactoryConfig
from multiversx_sdk_core import TransactionComputer
from multiversx_sdk_wallet import UserPEM, UserSigner
from pathlib import Path
from multiversx_sdk import UserSecretKey
from multiversx_sdk.network_providers import ProxyNetworkProvider
from multiversx_sdk.network_providers.transactions import TransactionOnNetwork
from multiversx_sdk.core import TokenManagementTransactionsFactory, TransactionsFactoryConfig

SIMULATOR_URL = "http://localhost:8085"
GENERATE_BLOCKS_URL = f"{SIMULATOR_URL}/simulator/generate-blocks"
GENERATE_BLOCKS_UNTIL_TX_PROCESSED = f"{SIMULATOR_URL}/simulator/generate-blocks-until-transaction-processed"
GENERATE_BLOCKS_UNTIL_EPOCH_REACHED_URL = f"{SIMULATOR_URL}/simulator/generate-blocks-until-epoch-reached"


def main():
# create a network provider
provider = ProxyNetworkProvider(SIMULATOR_URL)

pem = UserPEM.from_file(Path("../wallets/wallet.pem"))
key = UserSecretKey.generate()
address = key.generate_public_key().to_address("erd")
print(f"working with the generated address: {address.to_bech32()}")

# call proxy faucet
address = pem.public_key.to_address("erd")
data = {"receiver": f"{address.to_bech32()}"}
provider.do_post(f"{SIMULATOR_URL}/transaction/send-user-funds", data)

# generate 20 blocks to pass an epoch and the ESDT contract to be enabled
provider.do_post(f"{GENERATE_BLOCKS_URL}/20", {})
# generate blocks until ESDTs are enabled
provider.do_post(f"{GENERATE_BLOCKS_UNTIL_EPOCH_REACHED_URL}/1", {})

# create transaction config and factory
config = TransactionsFactoryConfig(provider.get_network_config().chain_id)
Expand All @@ -47,13 +46,9 @@ def main():
)

# set issue cost and nonce
tx.amount = 50000000000000000 #0.05 EGLD
tx.amount = 50000000000000000 # 0.05 EGLD
tx.nonce = provider.get_account(address).nonce

# sign transaction
user_signer = UserSigner(pem.secret_key)
tx_computer = TransactionComputer()
tx.signature = user_signer.sign(tx_computer.compute_bytes_for_signing(tx))
tx.signature = b"dummy"

# send transaction
tx_hash = provider.send_transaction(tx)
Expand All @@ -73,7 +68,8 @@ def main():
token_identifier_string = extract_token_identifier(tx_from_network)
amount = provider.get_fungible_token_of_account(address, token_identifier_string)
if amount.balance != initial_supply:
sys.exit(f"amount of token from balance is no equal with the initial supply: actual-{amount.balance}, expected-{initial_supply}")
sys.exit(f"amount of token from balance is no equal with the initial supply: "
f"actual-{amount.balance}, expected-{initial_supply}")

print("transaction was executed and tokens were created")

Expand Down
12 changes: 6 additions & 6 deletions examples/generateBlocks/epoch-reached.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys

from multiversx_sdk_network_providers import ProxyNetworkProvider
from multiversx_sdk.network_providers import ProxyNetworkProvider

SIMULATOR_URL = "http://localhost:8085"
GENERATE_BLOCKS_UNTIL_EPOCH_REACHED_URL = f"{SIMULATOR_URL}/simulator/generate-blocks-until-epoch-reached"
Expand All @@ -11,16 +11,16 @@ def main():
# create a network provider
provider = ProxyNetworkProvider(SIMULATOR_URL)

targetEpoch = 10
target_epoch = 10
# generate blocks until we reach the target epoch
provider.do_post(f"{GENERATE_BLOCKS_UNTIL_EPOCH_REACHED_URL}/{targetEpoch}", {})
provider.do_post(f"{GENERATE_BLOCKS_UNTIL_EPOCH_REACHED_URL}/{target_epoch}", {})

network_status = provider.get_network_status() # will default to metachain

if network_status.epoch_number < targetEpoch:
sys.exit(f"epoch {targetEpoch} not reached")
if network_status.epoch_number < target_epoch:
sys.exit(f"epoch {target_epoch} not reached")

print(f"successfully created blocks and epoch {targetEpoch} was reached")
print(f"successfully created blocks and epoch {target_epoch} was reached")


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 37f2ae4

Please sign in to comment.