classHelloBlockchainClient(RestClient):defpublish_module(self,account_from: Account,module_hex:str) ->str:"""Publish a new module to the blockchain within the specified account""" payload ={"type":"module_bundle_payload","modules": [{"bytecode":f"0x{module_hex}"}, ],} txn_request = self.generate_transaction(account_from.address(), payload) signed_txn = self.sign_transaction(account_from, txn_request) res = self.submit_transaction(signed_txn)returnstr(res["hash"])
pubstructHelloBlockchainClient {pub rest_client:RestClient,}implHelloBlockchainClient {/// Represents an account as well as the private, public key-pair for the Aptos blockchain.pubfnnew(url:String) -> Self { Self { rest_client:RestClient::new(url), } }/// Publish a new module to the blockchain within the specified accountpubfnpublish_module(&self, account_from:&mutAccount, module_hex:&str) ->String {let payload = serde_json::json!({"type":"module_bundle_payload","modules": [{"bytecode":format!("0x{}", module_hex)}], });let txn_request = self.rest_client.generate_transaction(&account_from.address(), payload);let signed_txn = self.rest_client.sign_transaction(account_from, txn_request);let res = self.rest_client.submit_transaction(&signed_txn); res.get("hash").unwrap().as_str().unwrap().to_string() }
classHelloBlockchainClientextendsRestClient {/** Publish a new module to the blockchain within the specified account */asyncpublishModule(accountFrom:Account, moduleHex:string):Promise<string> {constpayload= {"type":"module_bundle_payload","modules": [ {"bytecode":`0x${moduleHex}`}, ], };consttxnRequest=awaitthis.generateTransaction(accountFrom.address(), payload);constsignedTxn=awaitthis.signTransaction(accountFrom, txnRequest);constres=awaitthis.submitTransaction(accountFrom, signedTxn);return res["hash"]; }
=== Addresses ===
Alice: a52671f10dc3479b09d0a11ce47694c0
Bob: ec6ec14e4abe10aaa6ad53b0b63a1806
=== Initial Balances ===
Alice: 10000000
Bob: 10000000
Update the module with Alice's address, build, copy to the provided path, and press enter.
=== Testing Alice ===
Publishing...
Initial value: None
Setting the message to "Hello, Blockchain"
New value: Hello, Blockchain
=== Testing Bob ===
Initial value: None
Setting the message to "Hello, Blockchain"
New value: Hello, Blockchain
结果表明,Alice 和 Bob 从没有资源变成了一个 message 设置为“Hello, Blockchain”的地址。