Example with emitting event contains provided number:
script {use 0x1::Event; fun main(account:&signer, a:u64) {Event::emit<u64>(account, a); }}
Or you you can emit event from your module:
module MyEvent {use 0x1::Event;structMyStruct { value:u64 } public fun my_event(account:&signer, a:u64) {Event::emit(account, MyStruct { value: a }); }}
Signer
Signer module allows to work with the signer type. To get address of signer:
script {use 0x1::Signer; fun main(sender:&signer) {let _ =Signer::address_of(sender); }}
Signer type is required for functions which work with resources, address of signer could be useful in case of resource related functions: borrow_global, borrow_global_mut, exists, move_from.
Account module allows to work with user balances: get balances, deposit coins/tokens to balances, withdraw them to deposit in another module, etc.
Also, it creates an account, if the account doesn't exist yet, and related data, like event handlers for sending/receiving payments.
A lot of different methods can be used to send tokens from account A to account B, as these one-line methods:
script {use 0x1::Account;use 0x1::XFI; fun main(sender:&signer, payee: address, amount:u128, metadata: vector<u8>) {// Move XFI from sender account to payee.Account::pay_from_sender<XFI::T>(sender, payee, amount);// Again move XFI, but with metadata.Account::pay_from_sender_with_metadata<XFI::T>(sender, payee, amount, metadata); }}
Also, you can just withdraw from sender balance and deposit to payee:
script {use 0x1::Account;use 0x1::XFI; fun main(sender:&signer, payee: address, amount:u128) {// Move XFI from sender account to payee.let xfi =Account::withdraw_from_sender<XFI::T>(sender, amount);// Again move XFI, but with metadata.Account::deposit(sender, payee, xfi); }}
For the rest of the features of Account module look at account.move.
Dfinance
Dfinance module allows you to work with coins balances, get coins info, also register new tokens, etc.
First of all, Dfinance module presents type for all balances in the system, it's Dfinance::T:
resource structT<Coin> { value:u128}
The value field contains information about actual balance for specific coin/token, e.g.:
script {use 0x1::Account;use 0x1::XFI; fun main(sender:&signer, amount:u128) {// Use XFI::T to get Dfinance::T<XFI::T> contains balance.let xfi :0x1::Dfinance::T<XFI::T> =Account::withdraw_from_sender<XFI::T>(sender, amount);Account::deposit_to_sender(sender, xfi); }}
Also, you can create an empty coin:
module BankXFI {use 0x1::Dfinance;use 0x1::XFI; resource structT { balance:Dfinance::T<XFI::T>, } public fun create(account:&signer) { move_to<T>(account, T { balance:Dfinance::zero<XFI::T>() }) }}
Get denom, decimals, and actual value:
script {use 0x1::Dfinance;use 0x1::Account;use 0x1::XFI; fun main(sender:&signer, amount:u128) {let xfi =Account::withdraw_from_sender<XFI::T>(sender, amount);// Get denom vector<8>.let _ =Dfinance::denom<XFI::T>();// Get value of withdrawed xfi.let value =Dfinance::value(&xfi);assert(amount == value, 101);Account::deposit_to_sender(sender, xfi); }}