Skip to content

Instantly share code, notes, and snippets.

@piokuc
Created March 16, 2020 16:13
Show Gist options
  • Save piokuc/0e89cfbde7d2ca45add5300f39e1d7c1 to your computer and use it in GitHub Desktop.
Save piokuc/0e89cfbde7d2ca45add5300f39e1d7c1 to your computer and use it in GitHub Desktop.
from casperlabs_client import CasperLabsClient, bundled_contract
from casperlabs_client.abi import ABI
import os
def read_hex_id(file_name):
with open(file_name) as f:
return f.read().strip()
REPO_DIR = f"{os.getenv('HOME')}/CasperLabs"
STORED_TRANSFER_WASM = f"{REPO_DIR}/execution-engine/target/wasm32-unknown-unknown/release/transfer_to_account_u512_stored.wasm"
PRIVATE_KEY = f"{REPO_DIR}/hack/docker/keys/faucet-account/account-private.pem"
PUBLIC_KEY_HEX = read_hex_id(f"{REPO_DIR}/hack/docker/keys/faucet-account/account-id-hex")
TARGET_ACCOUNT_HEX = read_hex_id(f"{REPO_DIR}/hack/docker/keys/account-0/account-id-hex")
PAYMENT_AMOUNT=10000000
client = CasperLabsClient()
common_args = dict(
from_addr=PUBLIC_KEY_HEX,
private_key=PRIVATE_KEY,
payment_amount=PAYMENT_AMOUNT,
)
print(" === Install stored contract")
deploy_hash = client.deploy(
session=STORED_TRANSFER_WASM,
session_args=ABI.args([ABI.string_value("target", "hash")]),
**common_args,
)
deploy_info = client.showDeploy(deploy_hash, wait_for_processed=True)
print(deploy_info)
# Retrieve contract hash of the stored transfer contract
q = client.queryState(deploy_info.processing_results[0].block_info.summary.block_hash.hex(), PUBLIC_KEY_HEX, "", keyType="address")
contract_hash = [k.key.hash.hash for k in q.account.named_keys if k.name == "transfer_to_account"][0]
print(f"contract_hash: {contract_hash.hex()}")
print(" === Call stored contract")
deploy_hash = client.deploy(
session_hash=contract_hash,
session_args=ABI.args([ABI.account("address", TARGET_ACCOUNT_HEX), ABI.big_int("amount", 123456)]),
**common_args,
)
deploy_info = client.showDeploy(deploy_hash, wait_for_processed=True)
print(deploy_info)
block_hash = deploy_info.processing_results[0].block_info.summary.block_hash.hex()
print("Balance:", client.balance(TARGET_ACCOUNT_HEX, block_hash))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment