<!-- livebook:{"default_language":"erlang","file_entries":[{"name":"logo_web3_aptos_ex.png","type":"attachment"},{"name":"web3_aptos_ex.png","type":"attachment"}]} -->
# Web3AptosEx
```elixir
Mix.install([:poison, :jason, {:ecto, "~> 3.11", override: true}, {:web3_aptos_ex, "~>1.4.2"}])
```
## Section
[](https://livebook.dev/run?url=https%3A%2F%2Fgithub.com%2FNonceGeek%2Fweb3_aptos_ex%2Fblob%2Fmain%2FREADME.livemd)
## 0x00 Description

Aptos SDK impl in elixir!

See the using example in:
> https://github.com/NonceGeek/move_sdk_ex_liveview_example
## 0x01 Implementation
* [x] Aptos
* [x] RPC Implementation
* [x] Read Resource
* [x] Send Transaction
* [x] Chain Interactor
* [x] Smart Contract Parser
* [ ] Mutiple Code Generator
## 0x02 Preparation
> Test Smart Contract Lists:
>
> * hello_blockchain: [0xcd6e69ff3c22db037584fb1650f7ca75df721fb0143690fb33f2f3bd0c1fe5bd](https://explorer.aptoslabs.com/account/0xcd6e69ff3c22db037584fb1650f7ca75df721fb0143690fb33f2f3bd0c1fe5bd/modules)
## 0x03 Installation
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `web3_aptos_ex` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[
{:web3_aptos_ex, "~> 1.4.2"}
]
end
```
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/web3_aptos_ex](https://hexdocs.pm/web3_aptos_ex).
## 0x04 Aptos
**Namespace:** Web3AptosEx.Aptos
<!-- livebook:{"break_markdown":true} -->
### 4.1 Create a Connect
```elixir
import Web3AptosEx.Aptos
alias Web3AptosEx.Aptos
{:ok, client} = Aptos.RPC.connect("https://fullnode.testnet.aptoslabs.com/v1")
client
```
### 4.2 Create Account
Aptos using the Ed25519 Algorithm for the Account.
```elixir
priv = Web3AptosEx.Crypto.generate_priv()
{:ok, account} = Aptos.Account.from_private_key(priv)
account
```
```elixir
{:ok, res} = Aptos.get_faucet(client, account)
Process.sleep(5000)
{:ok, account_ol} = Aptos.load_account(client, account)
account_ol
# it will works if account is actived(has any APT)
```
### 4.3 Read Resource of Acct
#### 4.3.1 Read the resources of the Acct
```elixir
Aptos.RPC.get_resources(client, "0x3")
```
#### 4.3.2 Read resource by resource_type
```elixir
Web3AptosEx.Aptos.RPC.get_resource(client, "0x3", "0x1::account::Account")
```
#### 4.3.2 Read the item in Table
💡 You could get handle in Explorer:
> https://explorer.aptoslabs.com/account/0x2df41622c0c1baabaa73b2c24360d205e23e803959ebbcb0e5b80462165893ed/resources
```elixir
Web3AptosEx.Aptos.RPC.get_table_item(
client,
"0xe30b2ff6a515aa04729522512d50147d5cdd52ee3741c26ff1b97306b0b2b148",
"0x1::string::String",
"0x65f4a0954aa6e68d2381ff98b7676df2fe57beee3ca37a4a8a57fa621c1db872::addr_info::AddrInfo",
"0x73c7448760517E3E6e416b2c130E3c6dB2026A1d"
)
```
### 4.4 Send Tx
```elixir
{:ok, f} = ~a"0x1::coin::transfer<CoinType>(address, u64)"
payload = Aptos.call_function(f, ["0x1::aptos_coin::AptosCoin"], [account_ol.address, 100])
Aptos.submit_txn_with_auto_acct_updating(client, account_ol, payload)
```
### 4.5 Smart Contract Code Parser
Parse the smart contract code, it is the basis for generating call-codes for mutiple langugage, and the embedding for the smart contract to vector dataset!
> https://ai.movedid.build/
```erlang
# TODO: make it run in livebook
payload = " module hello_blockchain::message {\n use std::error;\n use std::signer;\n use std::string;\n use aptos_framework::account;\n use aptos_framework::event;\n\n //:!:>resource\n struct MessageHolder has key {\n message: string::String,\n message_change_events: event::EventHandle<MessageChangeEvent>,\n }\n //<:!:resource\n\n struct MessageChangeEvent has drop, store {\n from_message: string::String,\n to_message: string::String,\n }\n\n /// There is no message present\n const ENO_MESSAGE: u64 = 0;\n\n #[view]\n public fun get_message(addr: address): string::String acquires MessageHolder {\n assert!(exists<MessageHolder>(addr), error::not_found(ENO_MESSAGE));\n borrow_global<MessageHolder>(addr).message\n }\n\n public entry fun set_message(account: signer, message: string::String)\n acquires MessageHolder {\n let account_addr = signer::address_of(&account);\n if (!exists<MessageHolder>(account_addr)) {\n move_to(&account, MessageHolder {\n message,\n message_change_events: account::new_event_handle<MessageChangeEvent>(&account),\n })\n } else {\n let old_message_holder = borrow_global_mut<MessageHolder>(account_addr);\n let from_message = old_message_holder.message;\n event::emit_event(&mut old_message_holder.message_change_events, MessageChangeEvent {\n from_message,\n to_message: copy message,\n });\n old_message_holder.message = message;\n }\n }\n\n #[test(account = @0x1)]\n public entry fun sender_can_set_message(account: signer) acquires MessageHolder {\n let addr = signer::address_of(&account);\n aptos_framework::account::create_account_for_test(addr);\n set_message(account, string::utf8(b\"Hello, Blockchain\"));\n\n assert!(\n get_message(addr) == string::utf8(b\"Hello, Blockchain\"),\n ENO_MESSAGE\n );\n }\n }"
{:ok, tokens, _} = :smart_move_leex.string(String.to_charlist(payload))
:smart_move_yecc.parse(tokens)
```
## 0x06 Contributing
Bug report or pull request are welcome.
## 0x07 Make a pull request
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
Please write unit test with your code if necessary.
## 0x08 License
web3_aptos_ex is available as open source under the terms of the [MIT License](./LICENSE.md).
## 0x09 Distributed Rules
<!-- distributed_rules -->
Contributors:
```
pool, 20%
https://github.com/leeduckgo, 30%
https://github.com/zven21, 50%
```
<!-- / distributed_rules -->