-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
188 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule Tezex.Application do | ||
# See https://hexdocs.pm/elixir/Application.html | ||
# for more information on OTP Applications | ||
@moduledoc false | ||
|
||
use Application | ||
|
||
@impl true | ||
def start(_type, _args) do | ||
children = [{Finch, name: Tezex.Finch}] | ||
|
||
# See https://hexdocs.pm/elixir/Supervisor.html | ||
# for other strategies and supported options | ||
opts = [strategy: :one_for_one, name: Tezex.Supervisor] | ||
Supervisor.start_link(children, opts) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
defmodule Tezex.Rpc do | ||
alias Tezex.ForgeOperation | ||
alias Tezex.Rpc | ||
alias Tezex.Transaction | ||
|
||
@type t() :: %__MODULE__{ | ||
endpoint: binary(), | ||
chain_id: binary(), | ||
headers: Finch.Request.headers(), | ||
opts: Finch.request_opts() | ||
} | ||
|
||
defstruct [:endpoint, chain_id: "main", headers: [], opts: []] | ||
|
||
def send_contract_operation( | ||
%Rpc{} = rpc, | ||
public_key_hash, | ||
counter, | ||
contract, | ||
amount, | ||
fee, | ||
storage_limit, | ||
gas_limit, | ||
entrypoint, | ||
parameters | ||
) do | ||
parameters = | ||
cond do | ||
not is_nil(parameters) -> %{entrypoint: entrypoint || "default", value: parameters} | ||
not is_nil(entrypoint) -> %{entrypoint: entrypoint, value: []} | ||
true -> nil | ||
end | ||
|
||
tx = %Transaction{ | ||
source: public_key_hash, | ||
destination: contract, | ||
amount: Integer.to_string(amount), | ||
storage_limit: Integer.to_string(storage_limit), | ||
gas_limit: Integer.to_string(gas_limit), | ||
counter: Integer.to_string(counter), | ||
fee: Integer.to_string(fee), | ||
kind: "transaction", | ||
parameters: parameters | ||
} | ||
end | ||
|
||
def send_operation(%Rpc{} = rpc, operations, signer, offset) do | ||
{:ok, block_head} = get_block_at_offset(rpc, offset) | ||
block_hash = binary_part(block_head["hash"], 0, 51) | ||
|
||
forged_operation_group = | ||
ForgeOperation.forge_operation_group(%{"branch" => block_hash, "contents" => operations}) | ||
end | ||
|
||
def get_counter_for_account(%Rpc{} = rpc, address) do | ||
with {:ok, n} <- get(rpc, "/blocks/head/context/contracts/#{address}/counter"), | ||
{n, ""} <- Integer.parse(n) do | ||
n | ||
end | ||
end | ||
|
||
def get_block(%Rpc{} = rpc, hash \\ "head") do | ||
get(rpc, "/blocks/#{hash}") | ||
end | ||
|
||
def get_block_at_offset(%Rpc{} = rpc, offset) do | ||
if offset <= 0 do | ||
get_block(rpc) | ||
end | ||
|
||
{:ok, head} = get_block(rpc) | ||
get(rpc, "/blocks/#{head["header"]["level"] - offset}") | ||
end | ||
|
||
def get(%Rpc{} = rpc, path) do | ||
url = | ||
URI.parse(rpc.endpoint) | ||
|> URI.append_path("/chains/#{rpc.chain_id}") | ||
|> URI.append_path(path) | ||
|> URI.to_string() | ||
|
||
Finch.build(:get, url, rpc.headers) | ||
|> Finch.request(Tezex.Finch, rpc.opts) | ||
|> case do | ||
{:ok, %Finch.Response{status: 200, body: body}} -> Jason.decode(body) | ||
{:error, _} = err -> err | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
defmodule Tezex.Transaction do | ||
alias Tezex.Transaction | ||
|
||
@type t() :: %__MODULE__{ | ||
source: binary() | nil, | ||
destination: binary(), | ||
fee: pos_integer() | nil, | ||
counter: integer() | nil, | ||
gas_limit: pos_integer() | nil, | ||
amount: pos_integer() | nil, | ||
destination: binary(), | ||
storage_limit: pos_integer() | nil, | ||
parameters: map() | nil | ||
} | ||
|
||
defstruct [ | ||
:kind, | ||
:destination, | ||
:source, | ||
fee: 0, | ||
counter: 0, | ||
gas_limit: 0, | ||
amount: 0, | ||
storage_limit: 0, | ||
parameters: nil | ||
] | ||
|
||
defimpl Jason.Encoder do | ||
def encode(%Transaction{parameters: nil} = transaction, opts) do | ||
transaction | ||
|> Map.from_struct() | ||
|> Map.drop([:parameters]) | ||
|> encode(opts) | ||
end | ||
|
||
def encode(%Transaction{} = transaction, opts) do | ||
transaction | ||
|> Map.from_struct() | ||
|> encode(opts) | ||
end | ||
|
||
def encode(value, opts) do | ||
value | ||
|> Map.put(:kind, "transaction") | ||
|> Map.update!(:fee, &to_string/1) | ||
|> Map.update!(:gas_limit, &to_string/1) | ||
|> Map.update!(:amount, &to_string/1) | ||
|> Map.update!(:storage_limit, &to_string/1) | ||
|> Map.update!(:counter, &to_string/1) | ||
|> Jason.Encode.map(opts) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule Tezex.RpcTest do | ||
use ExUnit.Case, async: true | ||
|
||
alias Tezex.Rpc | ||
doctest Rpc | ||
|
||
test "get_counter_for_account" do | ||
# mock | ||
# counter = | ||
# Rpc.get_counter_for_account( | ||
# %Rpc{endpoint: "https://rpc.tzkt.io/mainnet/"}, | ||
# "tz1LKpeN8ZSSFNyTWiBNaE4u4sjaq7J1Vz2z" | ||
# ) | ||
|
||
# assert is_integer(counter) | ||
end | ||
end |