Skip to content

Instantly share code, notes, and snippets.

@normanlmfung
Last active October 21, 2022 07:54
Show Gist options
  • Save normanlmfung/43d6a65bfa2fbb6888d540d8ceb1e131 to your computer and use it in GitHub Desktop.
Save normanlmfung/43d6a65bfa2fbb6888d540d8ceb1e131 to your computer and use it in GitHub Desktop.
first contract interation with aptos
import time
from typing import Optional
from aptos_sdk.account import Account
from aptos_sdk.account_address import AccountAddress
from aptos_sdk.bcs import Serializer
from aptos_sdk.client import FaucetClient, RestClient
from aptos_sdk.transactions import (
EntryFunction,
TransactionArgument,
TransactionPayload,
)
FAUCET_URL = "https://tap.devnet.prod.gcp.aptosdev.com"
NODE_URL = "https://fullnode.devnet.aptoslabs.com/v1"
class HelloBlockchainClient(RestClient):
def get_message(
self, contract_address: str, account_address: AccountAddress
) -> Optional[str]:
return self.account_resource(
account_address, f"0x{contract_address}::message::MessageHolder"
)
def set_message(self, contract_address: str, sender: Account, message: str) -> str:
payload = EntryFunction.natural(
f"0x{contract_address}::message",
"set_message",
[],
[TransactionArgument(message, Serializer.str)],
)
signed_transaction = self.create_single_signer_bcs_transaction(
sender, TransactionPayload(payload)
)
return self.submit_bcs_transaction(signed_transaction)
if __name__ == "__main__":
contract_address = "xxxxx"
alice = Account.generate()
print("\n=== Addresses ===")
print(f"Alice: {alice.address()}")
rest_client = HelloBlockchainClient(NODE_URL)
faucet_client = FaucetClient(FAUCET_URL, rest_client)
faucet_client.fund_account(alice.address(), 1_000_000_000)
print("\n=== Testing Alice ===")
print(
f"Initial value: {rest_client.get_message(contract_address, alice.address())}"
)
for i in range(100):
start = time.time()
txn_hash = rest_client.set_message(contract_address, alice, f"Hello, Blockchain {i}")
rest_client.wait_for_transaction(txn_hash)
finish = time.time()
elapsed = (finish - start) * 1000 # aptos the "Solana killer"? Lets put it to the test!!
new_msg = rest_client.get_message(contract_address, alice.address())
print(f"elapsed (ms): {elapsed}, new_msg: {new_msg}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment