README.livemd

<!-- livebook:{"persist_outputs":true} -->

# Web3MoveEx

```elixir
Mix.install([:poison, :web3_move_ex])
```

## Description

Move(Starcoin) SDK impl in elixir!

See the using example in:

> https://github.com/WeLightProject/move_sdk_ex_liveview_example

## Implementation

* [x] Starcoin
  * [x] Readonly Function
  * [x] Read Resource
  * [x] Send Transaction
  * [x] Deploy Contract
  * [x] Chain Interactor
* [ ] Aptos
* [ ] Sui

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `web3_move_ex` to your list of dependencies in `mix.exs`:

```elixir
def deps do
  [
    {:web3_move_ex, "~> 0.1.0"}
  ]
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_move_ex](https://hexdocs.pm/web3_move_ex).

## Livebook Little CookBook

## 0x01 Create a Connect

```elixir
alias Web3MoveEx.Starcoin
# Create a Connect to local node
client_local = Starcoin.Client.connect()
IO.puts(inspect(client_local))
# Create a Connect to Barnard node
client_barnard = Starcoin.Client.connect("https://barnard-seed.starcoin.org")
IO.puts(inspect(client_barnard))

client_mainnet = Starcoin.Client.connect("https://main-seed.starcoin.org")
IO.puts(inspect(client_mainnet))
```

<!-- livebook:{"output":true} -->

```
%Web3MoveEx.Starcoin.Client{chain_id: 254, endpoint: "http://localhost:9851"}
%Web3MoveEx.Starcoin.Client{chain_id: 251, endpoint: "https://barnard-seed.starcoin.org"}
%Web3MoveEx.Starcoin.Client{chain_id: 1, endpoint: "https://main-seed.starcoin.org"}
```

<!-- livebook:{"output":true} -->

```
:ok
```

## 0x02 Create Account

Starcoin using the **Ed25519 Algorithm** for the Account.

```elixir
{:ok, %{address_hex: address_hex}} =
  payload =
  Starcoin.Account.from_private_key(
    0xA95ED7F73DE144BC8F64C0266BE38ED6AF0903762E2503D3C7C71CDA69EEA99A
  )
```

<!-- livebook:{"output":true} -->

```
{:ok,
 %Web3MoveEx.Starcoin.Account{
   address: <<17, 104, 232, 143, 252, 92, 236, 83, 179, 152, 180, 45, 97, 136, 91, 187>>,
   address_hex: "0x1168e88ffc5cec53b398b42d61885bbb",
   public_key: <<182, 132, 185, 94, 183, 12, 78, 210, 69, 176, 65, 101, 54, 159, 176, 213, 17, 104,
     232, 143, 252, 92, 236, 83, 179, 152, 180, 45, 97, 136, 91, 187>>,
   sequence_number: 0,
   signing_key: %Web3MoveEx.Starcoin.SigningKey{
     private_key: <<169, 94, 215, 247, 61, 225, 68, 188, 143, 100, 192, 38, 107, 227, 142, 214, 175,
       9, 3, 118, 46, 37, 3, 211, 199, 199, 28, 218, 105, 238, 169, 154>>,
     public_key: <<216, 214, 6, 64, 220, 252, 62, 18, 115, 98, 182, 218, 97, 153, 40, 125, 44, 236,
       69, 88, 194, 34, 54, 251, 3, 120, 233, 36, 185, 157, 10, 42>>
   }
 }}
```

## 0x03 Deploy Contract

#### 3.1 Starcoin Console Ways

Using the `my_counter` as Example:

```
$ git clone git@github.com:WeLightProject/Web3-dApp-Camp.git
$ cd Web3-dApp-Camp/move-dapp/my-counter
```

Modify the address in `move.toml` to the address that you would like to deploy your contract.

```rust
[package]
name = "my_counter"
version = "0.0.1"

[addresses]
StarcoinFramework = "0x1"
MyAddr = [YOUR ADDR] 

[dependencies]
StarcoinFramework = {git = "https://github.com/starcoinorg/starcoin-framework.git", rev="9ea61ce1429070c945002b26ef5f5ea6b72884e4"}
```

Compile:

```bash
$ mpm release
```

> 💡 [How to install mpm?](https://starcoinorg.github.io/starcoin-cookbook/docs/move/prepare-move-env)

Open Console, the command will start a dev node local yet:

```bash
$ starcoin -d /Users/liaohua/starcoin_data -n dev console
```

Get-coin on local network:

```
starcoin% dev get-coin -v 100STC
```

Deploy Contract with starcoin:

```
starcoin% dev deploy [path to blob] -s [addr] -b
```

Deploy Contract with Elixir SDK:

💡Remember to get some faucet first:

```
starcoin% account transfer -r 0x52bfdf8638e3658bb9f00cc04ca98bdd -v 100000000000
```

Operate in `iex`:

```elixir
client = Web3MoveEx.Starcoin.Client.connect()

payload = Web3MoveEx.Starcoin.Transaction.script_function("0x52bfdf8638e3658bb9f00cc04ca98bdd", "MyCounter", "init_counter", [], [])

module_path = "./test/starcoin/transaction/fixtures/MyCounter.mv""
options = [private_key: "0xdebb41434877520254a4551a80b78dcfdce9bc3d576b13b66c3b343d515c0a8b"]

Web3MoveEx.Starcoin.deploy_contract(client, payload, module_path, options)
```

**Additional Tips:**

* Get Faucet on barnard:
  https://faucet.starcoin.org/barnard

* Connect Testnet by Console:

```bash
$ starcoin --connect ws://barnard.seed.starcoin.org:9870 --local-account-dir ~/.starcoin/barnard/account_vaults console
```

## 0x04 Read Resource

```elixir
Web3MoveEx.Starcoin.Caller.Contract.get_resource(
  client,
  "<you_addresss>",
  "<deploy_address>::MyCounter::Counter"
)

{:ok,
 %{
   id: 1,
   jsonrpc: "2.0",
   result: %{
     abilities: 12,
     type_: "<deploy_address>::MyCounter::Counter",
     value: [["value", %{U64: "0"}]]
   }
 }}
```

## 0x05 Run ReadOnly Function in MOVE

```elixir
/ / TODO
```

## 0x06 Run Writed Function in MOVE

```elixir
client = Web3MoveEx.Starcoin.Client.connect()

payload =
  Web3MoveEx.Starcoin.Transaction.Function.call(
    client,
    "<deploy_address>::MyCounter::incr_counter",
    [],
    []
  )

options = [private_key: "<you_private_key>"]

Web3MoveEx.Starcoin.submit_txn(client, payload, options)
# when get resource return 1
```