Interacting with Elixir Aptos | Move dApp Getting Started Quickly

NonceGeekDAO has a longstanding focus on the Move ecosystem and the Elixir functional programming language.

https://github.com/NonceGeek/

web3_move_ex is the Elixir SDK for the pan-Move ecosystem developed by NonceGeekDAO, which has now completed its implementation of theAptosof basic support.

The case for this post can be seen:

https://github.com/NonceGeek/web3_move_ex_example

Aptos List of Completed Features:

  • Acct Operations (account-related operations)
  • Read Resource
  • Read Events
  • Call Func
  • Func Parser (a function that parses a string format such as0xdea79e568e00066f60fbfe6ac6d8a9ef2fabbeadc6aae1ec9158d50f6efe4ac8::addr_aggregator::create_addr_aggregator(u64,vector))

0x01 Installation

Create a new sample project:

$ mix new web3_move_ex_example
$ # Or clone an existing github repo.
$ git clone https://github.com/NonceGeek/web3_move_ex_example.git

existmix.exs Add the latest version of theweb3_move_ex::

defp deps do
    [
{:web3_move_ex, "~> 0.4.0"}
    ]
end

Open the interactive interface:

$ iex-S mix

0x02 Account Related

We can generate accounts by randomizing seeds:

alias Web3MoveEx.Aptos
{:ok, acct} = Aptos.generate_keys()

It can also be generated with a private key:

{:ok, acct} = Aptos.generate_keys("0xffc207d0666ca82eac4e2238d0cf15f963a03ec6a9daa617dc035b7228de1f28")

Create a connection to the node's client::

{:ok, client} = Aptos.connect(:testnet)

Inject some into the account faucet::

Aptos.get_faucet(client, acct)

Get the balance:

Aptos.get_balance(client, acct)

You can see that getting faucet was successful:

iex(10)> Aptos.get_balance(client, acct)
%{
  coin: %{value: "100000000"},
  deposit_events: %{
    counter: "1",
    guid: %{
      id: %{
        addr: "0xe59f44953723d5a8e26df9d3d4a613a2da3ff9f7dc4214dc281e10a244f39e3f",
        creation_num: "2"
      }
    }
  },
  creation_num: "2" } }, frozen: false,
  withdraw_events: %{
    counter: "0",
    guid: %{
      id: %{
        addr: "0xe59f44953723d5a8e26df9d3d4a613a2da3ff9f7dc4214dc281e10a244f39e3f",
        creation_num: "3"
      }
    }
  }
}

We can package the series of steps we just took into a single function:

def gen_acct_and_get_faucet(network_type) do
  {:ok, acct} = Aptos.generate_keys()
  {:ok, client} = Aptos.connect(network_type)
  {:ok, _res} = Aptos.get_faucet(client, acct)
  Process.sleep(2000) # wait 2 seconds for successful transaction
  %{res: Aptos.get_balance(client, acct), acct: acct}
end

The result of the function execution:

iex(7)> %{acct: acct} = Web3MoveExExample.gen_acct_and_get_faucet(:testnet)
%{
  acct: #Account,
  res: %{
    coin: %{value: "100000000"},
    deposit_events: %{
      counter: "1",
      guid: %{
        id: %{
          addr: "0xedf8dcb761e7ee7884e5a94d1f9b330f7a3ed09317bebe163738c31dc67e446f",
          creation_num: "2"
        }
      }
    },
    creation_num: "2" } }, frozen: false,
    withdraw_events: %{
      counter: "0",
      guid: %{
        id: %{
          addr: "0xedf8dcb761e7ee7884e5a94d1f9b330f7a3ed09317bebe163738c31dc67e446f",
          creation_num: "3"
        }
      }
    }
  }
}

0x03 Calling Functions and Reading Events

We are based on MoveDIDcontract on a test network as an example, demonstrating how to pass theweb3_move_exto make contract calls.

MoveDID Repo: https://github.com/NonceGeek/MoveDID

Smart Contract on Testnet:

0x06195d43edde4b1cd3a96f7838686b9b12b51023cd388dfb21f123b350f4ec46

We can generate func by func_string:

import Web3MoveEx.
addr= "0x06195d43edde4b1cd3a96f7838686b9b12b51023cd388dfb21f123b350f4ec46" # contract address
init_func_str = "#{addr}::init::init(u64, string)"
{:ok, init_func} = ~a "#{init_func_str}"

The results are as follows:

iex(5)> {:ok, init_func} = ~a "#{init_func_str}"
{:ok.
 %Web3MoveEx.Aptos.Types.Function{
   address: <>,
   address_encoded: "0x06195d43edde4b1cd3a96f7838686b9b12b51023cd388dfb21f123b350f4ec46",
   is_entry: true,
   
   name: "init",
   param_names: [],
   
   return: [],
   type_params: [],
   visibility: :public
 }}

You can check by the way.eventsfunction, follow the contract logic and add a newDIDIt will be in thecontract_ownerThe corresponding event is generated under

Aptos.build_event_path(client, addr, "#{addr}::addr_aggregator::CreateAddrAggregatorEventSet", "create_addr_aggregator_events")
# This function generates an address that can be accessed directly with a browser.
Aptos.get_events(client, addr, "#{addr}::addr_aggregator::CreateAddrAggregatorEventSet", "create_addr_aggregator_events")
# This function gets the Event results.

Result:

iex(13)> Aptos.get_events(client, addr, "#{addr}::addr_aggregator::CreateAddrAggregatorEventSet", "create_addr_aggregator_events")
{:ok, []}

generating payload::

payload = Aptos.call_function(f, [], [1, "testAcct"])

Submit a transaction:

Aptos.submit_txn_with_auto_acct_updating(client, acct, payload)
# Method 0x01: Automatically updating acct's information
# Aptos.submit_txn(client, acct, payload)
# Method 0x02: do not automatically update acct information

Implementation results:

iex(15)> Aptos.submit_txn_with_auto_acct_updating(client, acct, payload)
{:ok.
 %{
   expiration_timestamp_secs: "1679451173",
   gas_unit_price: "1000",
   hash: "0x931c2821df3e6c9c40bf00d5ed7e20aa1172a053d3c3d658a9f7bd31f17d91f6",
   max_gas_amount: "2000",
   payload: %{
     arguments: ["1", "testAcct"],
     function: "0x6195d43edde4b1cd3a96f7838686b9b12b51023cd388dfb21f123b350f4ec46::init::init",
     type: "entry_function_payload",
     type_arguments: []
   },
   sender: "0xedf8dcb761e7ee7884e5a94d1f9b330f7a3ed09317bebe163738c31dc67e446f",
   sequence_number: "0",
   signature: %{
     public_key: "0x361c4395cc87cf951dc5f879bdf2c2f91df3078c9a2ee4b5bf9892243a24a5fb",
     signature: " 0x94f9cbf17c5095e76ea30a88d6683c9951fbfa3b1e261ca6b4722fac985946dcc0f5e7a8c03e6b322f39b102874484b620cf0943c672eadd4fd9b3505e65f804 ",
     type: "ed25519_signature"
   }
 }}

Viewed on a browser, the transaction was executed successfully:

We can also pass thecheck_tx_res_by_hashView:

Aptos.check_tx_res_by_hash(client, hash) # return true or false

We can similarly set thecall_fun_initprocess is encapsulated as a function:

 def call_func_init(client, acct, contract_addr, did_type, description) do
    {:ok, f} = gen_func_init(contract_addr)
    payload = Aptos.call_function(f, [], [did_type, description])
    {:ok, %{hash: hash} = tx} = Aptos.submit_txn_with_auto_acct_updating(client, acct, payload)
    Process.sleep(2000) # takes 2 seconds to wait for the transaction to succeed
    res = Aptos.check_tx_res_by_hash(client, hash)
    %{res: res, tx: tx}
  res: res, tx: tx}

  def gen_func_init(contract_addr) do
    init_func_str = "#{contract_addr}::init::init(u64, string)"
    ~a "#{init_func_str}"
  end

Implementation results:

iex(22)> Web3MoveExExample.call_func_init(client, acct, addr, 1, "test Acct")
%{
  res: true,
  tx: %{
    expiration_timestamp_secs: "1679475296",
    expiration_timestamp_secs: "1679475296", gas_unit_price: "1000",
    hash: "0x73687a7e4c953f3cf13a768fe03560a61c06615fa8c089888f23cd3ccd8dc6e8",
    max_gas_amount: "2000",
    payload: %{
      arguments: ["1", "test Acct"],
      function: "0x6195d43edde4b1cd3a96f7838686b9b12b51023cd388dfb21f123b350f4ec46::init::init",
      type: "entry_function_payload",
      type_arguments: []
    },
    sender: "0xc193cd62bab00790d40265505ce894552c14a3693aca9bc2310ee0ecdc53780b",
    sequence_number: "0",
    signature: %{
      public_key: "0x6bfa0d62dd63ea8a4291ffec10fc448ab554188d7765a9faf1b0992dbb199c38",
      signature: " 0xc8a9d4368a0cc2daff3add7162745990d3bd9c33ec6f1112b5dfe45cdd25d4fdf5d19a361048b67081e3ce9c242c93266af4736d8c86541906e08c2ef387ef05 ",
      type: "ed25519_signature"
    }
  }
}

At this point, execute the get_eventsYou will see that events was generated successfully:

iex(16)> Aptos.get_events(client, addr, "#{addr}::addr_aggregator::CreateAddrAggregatorEventSet", "create_addr_aggregator_events")
{:ok.
 [
   %{
     data: %{
       description: "testAcct",
       key_addr: "0xedf8dcb761e7ee7884e5a94d1f9b330f7a3ed09317bebe163738c31dc67e446f",
       type: "1"
     },
     guid: %{
       account_address: "0x6195d43edde4b1cd3a96f7838686b9b12b51023cd388dfb21f123b350f4ec46",
       creation_number: "4"
     },
     creation_number: "4" }, sequence_number: "0",
     type: "0x6195d43edde4b1cd3a96f7838686b9b12b51023cd388dfb21f123b350f4ec46::addr_aggregator::CreateAddrAggregatorEvent",
     version: "473098605"
   }
 ]}

0x04 Viewing Resources

Open your browser to access it:

https://explorer.aptoslabs.com/account/0xedf8dcb761e7ee7884e5a94d1f9b330f7a3ed09317bebe163738c31dc67e446f/resources

This resource is also available to us through the web3_move_exGetting to:

Aptos.get_resource(client, account.address_hex, "#{addr}::addr_aggregator::AddrAggregator")

Results:

iex(20)> Aptos.get_resource(client, "0xedf8dcb761e7ee7884e5a94d1f9b330f7a3ed09317bebe163738c31dc67e446f", "#{addr}::addr_ aggregator::AddrAggregator")
{:ok.
 %{
   data: %{
     add_addr_events: %{
       counter: "0",
       guid: %{
         id: %{
           addr: "0xedf8dcb761e7ee7884e5a94d1f9b330f7a3ed09317bebe163738c31dc67e446f",
           creation_num: "4"
         }
       }
     },
     addr_infos_map: %{
       handle: "0xbae0b5ae8c373db15ca2f71a18f92ed3ed363e5730cf3f8a90791c04ff6908da"
     },
     addrs: [],
     delete_addr_events: %{
       counter: "0",
       guid: %{
         id: %{
           addr: "0xedf8dcb761e7ee7884e5a94d1f9b330f7a3ed09317bebe163738c31dc67e446f",
           creation_num: "7"
         }
       }
     },
     description: "testAcct",
     key_addr: "0xedf8dcb761e7ee7884e5a94d1f9b330f7a3ed09317bebe163738c31dc67e446f",
     max_id: "0",
     modified_counter: "0",
     type: "1",
     update_addr_events: %{
       counter: "0",
       guid: %{
         id: %{
           addr: "0xedf8dcb761e7ee7884e5a94d1f9b330f7a3ed09317bebe163738c31dc67e446f",
           creation_num: "6"
         }
       }
     },
     update_addr_signature_events: %{
       counter: "0",
       guid: %{
         id: %{
           addr: "0xedf8dcb761e7ee7884e5a94d1f9b330f7a3ed09317bebe163738c31dc67e446f",
           creation_num: "5"
         }
       }
     }
   },
   type: "0x6195d43edde4b1cd3a96f7838686b9b12b51023cd388dfb21f123b350f4ec46::addr_aggregator::AddrAggregator"
 }}

All of the above content is reproduced from the Internet, does not represent the position of AptosNews, is not investment advice, investment risk, the market need to be cautious, such as infringement, please contact the administrator to delete.

Like (0)
Donate WeChat Sweep WeChat Sweep Alipay Sweep Alipay Sweep
Previous July 21, 2023 at 12:18 pm
Next July 21, 2023 at 12:37 pm

Related posts

Leave a Reply

Please Login to Comment
WeChat Sweep
Baidu Sweep

Subscribe to AptosNews

Subscribe to AptosNews to stay on top of Aptos.


This will close in 0 seconds

This site has no investment advice, investment risk, the market needs to be cautious.