class HelloBlockchainClient(RestClient):
def publish_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)
return str(res["hash"])
pub struct HelloBlockchainClient {
pub rest_client: RestClient,
}
impl HelloBlockchainClient {
/// Represents an account as well as the private, public key-pair for the Aptos blockchain.
pub fn new(url: String) -> Self {
Self {
rest_client: RestClient::new(url),
}
}
/// Publish a new module to the blockchain within the specified account
pub fn publish_module(&self, account_from: &mut Account, 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()
}
class HelloBlockchainClient extends RestClient {
/** Publish a new module to the blockchain within the specified account */
async publishModule(accountFrom: Account, moduleHex: string): Promise<string> {
const payload = {
"type": "module_bundle_payload",
"modules": [
{"bytecode": `0x${moduleHex}`},
],
};
const txnRequest = await this.generateTransaction(accountFrom.address(), payload);
const signedTxn = await this.signTransaction(accountFrom, txnRequest);
const res = await this.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”的地址。