Web3AptosEx.Aptos.SmartContractParser (web3_aptos_ex v1.4.2)
Link to this section Summary
Functions
Parses the given binary
as parse.
module hello_blockchain::message {
use std::error; use std::signer; use std::string; use aptos_framework::account; use aptos_framework::event;
Link to this section Functions
parse(binary, opts \\ [])
@spec parse(binary(), keyword()) :: {:ok, [term()], rest, context, line, byte_offset} | {:error, reason, rest, context, line, byte_offset} when line: {pos_integer(), byte_offset}, byte_offset: pos_integer(), rest: binary(), reason: String.t(), context: map()
Parses the given binary
as parse.
Returns {:ok, [token], rest, context, position, byte_offset}
or
{:error, reason, rest, context, line, byte_offset}
where position
describes the location of the parse (start position) as {line, offset_to_start_of_line}
.
To column where the error occurred can be inferred from byte_offset - offset_to_start_of_line
.
options
Options
:byte_offset
- the byte offset for the whole binary, defaults to 0:line
- the line and the byte offset into that line, defaults to{1, byte_offset}
:context
- the initial context value. It will be converted to a map
parse_code(contract_code)
module hello_blockchain::message {
use std::error; use std::signer; use std::string; use aptos_framework::account; use aptos_framework::event;
//:!:>resource struct MessageHolder has key {
message: string::String,
message_change_events: event::EventHandle<MessageChangeEvent>,
} //<:!:resource
struct MessageChangeEvent has drop, store {
from_message: string::String,
to_message: string::String,
}
/// There is no message present const ENO_MESSAGE: u64 = 0;
#[view] public fun get_message(addr: address): string::String acquires MessageHolder {
assert!(exists<MessageHolder>(addr), error::not_found(ENO_MESSAGE));
borrow_global<MessageHolder>(addr).message
}
public entry fun set_message(account: signer, message: string::String) acquires MessageHolder {
let account_addr = signer::address_of(&account);
if (!exists<MessageHolder>(account_addr)) {
move_to(&account, MessageHolder {
message,
message_change_events: account::new_event_handle<MessageChangeEvent>(&account),
})
} else {
let old_message_holder = borrow_global_mut<MessageHolder>(account_addr);
let from_message = old_message_holder.message;
event::emit_event(&mut old_message_holder.message_change_events, MessageChangeEvent {
from_message,
to_message: copy message,
});
old_message_holder.message = message;
}
}
#[test(account = @0x1)] public entry fun sender_can_set_message(account: signer) acquires MessageHolder {
let addr = signer::address_of(&account);
aptos_framework::account::create_account_for_test(addr);
set_message(account, string::utf8(b"Hello, Blockchain"));
assert!(
get_message(addr) == string::utf8(b"Hello, Blockchain"),
ENO_MESSAGE
);
} }