Skip to content

Commit

Permalink
Finish refactor for path_orderbook
Browse files Browse the repository at this point in the history
  • Loading branch information
lucca65 committed Mar 8, 2024
1 parent 6620508 commit 09ec9e6
Show file tree
Hide file tree
Showing 6 changed files with 483 additions and 105 deletions.
29 changes: 28 additions & 1 deletion lib/ledger_entry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,17 @@ defmodule XRPL.LedgerEntry do
Official documentation: https://xrpl.org/ledger_entry.html#get-amm-object
"""
def amm(%{amm: amm} = params) when is_map(amm) do
xrpl("ledger_entry", "amm_map", params)
xrpl("ledger_entry", "amm_string", params)
end

def amm(%{amm: amm} = params) when is_binary(amm) do
xrpl("ledger_entry", "amm_object", params)
end

def amm(params) do
xrpl("ledger_entry", "amm_string", params)
end

def amm!(params), do: params |> amm() |> unwrap_or_raise()

defparams "amm_string" do
Expand Down Expand Up @@ -99,6 +103,10 @@ defmodule XRPL.LedgerEntry do
xrpl("ledger_entry", "directory_node_object", params)
end

def directory_node(params) do
xrpl("ledger_entry", "directory_node_string", params)
end

def directory_node!(params), do: params |> directory_node() |> unwrap_or_raise()

defparams "directory_node_string" do
Expand Down Expand Up @@ -134,6 +142,10 @@ defmodule XRPL.LedgerEntry do
xrpl("ledger_entry", "offer_object", params)
end

def offer(params) do
xrpl("ledger_entry", "offer_string", params)
end

def offer!(params), do: params |> offer() |> unwrap_or_raise()

defparams "offer_string" do
Expand Down Expand Up @@ -208,6 +220,10 @@ defmodule XRPL.LedgerEntry do
xrpl("ledger_entry", "escrow_object", params)
end

def escrow(params) do
xrpl("ledger_entry", "escrow_string", params)
end

def escrow!(params), do: params |> escrow() |> unwrap_or_raise()

defparams "escrow_string" do
Expand Down Expand Up @@ -261,6 +277,10 @@ defmodule XRPL.LedgerEntry do
xrpl("ledger_entry", "deposit_preauth_object", params)
end

def deposit_preauth(params) do
xrpl("ledger_entry", "deposit_preauth_string", params)
end

def deposit_preauth!(params), do: params |> deposit_preauth() |> unwrap_or_raise()

defparams "deposit_preauth_string" do
Expand Down Expand Up @@ -295,9 +315,14 @@ defmodule XRPL.LedgerEntry do
xrpl("ledger_entry", "ticket_object", params)
end

def ticket(params) do
xrpl("ledger_entry", "ticket_string", params)
end

def ticket!(params), do: params |> ticket() |> unwrap_or_raise()

defparams "ticket_string" do
required(:ticket, :string, format: XRPL.ledger_entry_regex())
optional(:ledger_index, :string, format: XRPL.ledger_index_regex())
optional(:ledger_hash, :string, format: XRPL.ledger_hash_regex())
optional(:binary, :boolean, default: false)
Expand All @@ -323,6 +348,8 @@ defmodule XRPL.LedgerEntry do
xrpl("ledger_entry", "nft_page", params)
end

def nft_page!(params), do: params |> nft_page() |> unwrap_or_raise()

defparams "nft_page" do
required(:nft_page, :string, format: XRPL.ledger_entry_regex())
optional(:ledger_index, :string, format: XRPL.ledger_index_regex())
Expand Down
229 changes: 158 additions & 71 deletions lib/path_orderbook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,102 +9,189 @@ defmodule XRPL.PathOrderbook do
Official RPC documentation https://xrpl.org/path-and-order-book-methods.html
"""

import XRPL
use XRPL

@doc """
The amm_info method retrieves information about an Automated Market Maker (AMM) account.
Official documentation: https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/path-and-order-book-methods/amm_info/#amm_info
Official documentation: https://xrpl.org/amm_info.html
"""
def amm_info(_, _, _) do
raise "Not implemented. Requires amendments. More info: https://xrpl.org/resources/known-amendments/#amm"
def amm_info(%{asset: asset, asset2: asset2} = params) when is_binary(asset) and is_binary(asset2) do
xrpl("amm_info", "amm_info_string_string", params)
end

def amm_info(%{asset: asset, asset2: asset2} = params) when is_map(asset) and is_binary(asset2) do
xrpl("amm_info", "amm_info_object_string", params)
end

def amm_info(%{asset: asset, asset2: asset2} = params) when is_binary(asset) and is_map(asset2) do
xrpl("amm_info", "amm_info_string_object", params)
end

def amm_info(%{asset: asset, asset2: asset2} = params) when is_map(asset) and is_map(asset2) do
xrpl("amm_info", "amm_info_object_object", params)
end

def amm_info(_) do
{:error, :invalid_params}
end

def amm_info!(params), do: params |> amm_info() |> unwrap_or_raise()

defparams "amm_info_string_string" do
optional(:account, :string, format: XRPL.account_address_regex())
optional(:amm_account, :string, format: XRPL.account_address_regex())
optional(:asset, :string, format: XRPL.currency_regex())
optional(:asset2, :string, format: XRPL.currency_regex())
end

defparams "amm_info_object_string" do
optional(:account, :string, format: XRPL.account_address_regex())
optional(:amm_account, :string, format: XRPL.account_address_regex())

optional(:asset, :map) do
required(:currency, :string, format: XRPL.currency_regex())
optional(:issuer, :string, format: XRPL.account_address_regex())
end

optional(:asset2, :string)
end

defparams "amm_info_string_object" do
optional(:account, :string, format: XRPL.account_address_regex())
optional(:amm_account, :string, format: XRPL.account_address_regex())

optional(:asset, :string)

optional(:asset2, :map) do
required(:currency, :string, format: XRPL.currency_regex())
optional(:issuer, :string, format: XRPL.account_address_regex())
end
end

defparams "amm_info_object_object" do
optional(:account, :string, format: XRPL.account_address_regex())
optional(:amm_account, :string, format: XRPL.account_address_regex())

optional(:asset, :map) do
required(:currency, :string, format: XRPL.currency_regex())
optional(:issuer, :string, format: XRPL.account_address_regex())
end

optional(:asset2, :map) do
required(:currency, :string, format: XRPL.currency_regex())
optional(:issuer, :string, format: XRPL.account_address_regex())
end
end

@doc """
The book_offers method retrieves a list of Offers between two currencies, also known as an order book.
The response omits unfunded Offers and reports how much of each remaining Offer's total is currently funded.
Official documentation: https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/path-and-order-book-methods/book_offers/#book_offers
Official documentation: https://xrpl.org/book_offers.html
"""
def book_offers(
taker_gets_currency,
taker_pays_currency,
taker_pays_issuer,
ledger_hash \\ nil,
ledger_index \\ nil,
limit \\ nil,
taker \\ nil
) do
params = %{
taker_gets: %{currency: taker_gets_currency},
taker_pays: %{currency: taker_pays_currency, issuer: taker_pays_issuer}
}

params = if taker, do: Map.put(params, :taker, taker), else: params
params = if ledger_hash, do: Map.put(params, :ledger_hash, ledger_hash), else: params
params = if ledger_index, do: Map.put(params, :ledger_index, ledger_index), else: params
params = if limit, do: Map.put(params, :limit, limit), else: params

post("/", %{
method: "book_offers",
params: [params]
})
def book_offers(params) do
xrpl("book_offers", params)
end

def book_offers!(params), do: params |> book_offers() |> unwrap_or_raise()

defparams "book_offers" do
required(:taker_gets, :map) do
required(:currency, :string, format: XRPL.currency_regex())
optional(:issuer, :string, format: XRPL.account_address_regex())
end

required(:taker_pays, :map) do
required(:currency, :string, format: XRPL.currency_regex())
optional(:issuer, :string, format: XRPL.account_address_regex())
end

optional(:ledger_hash, :string, format: XRPL.ledger_hash_regex())
optional(:ledger_index, :string, format: XRPL.ledger_index_regex())
optional(:limit, :integer)
optional(:taker, :string, format: XRPL.account_address_regex())
end

@doc """
The deposit_authorized command indicates whether one account is authorized to send payments directly to another. See Deposit Authorization for information on how to require authorization to deliver money to your account.
Official documentation: https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/path-and-order-book-methods/deposit_authorized/#deposit_authorized
Official documentation: https://xrpl.org/deposit_authorized.html
"""
def deposit_authorized(source_account, destination_account, ledger_index \\ nil, ledger_hash \\ nil) do
params = %{
source_account: source_account,
destination_account: destination_account
}
def deposit_authorized(params) do
xrpl("deposit_authorized", params)
end

params = if ledger_hash, do: Map.put(params, :ledger_hash, ledger_hash), else: params
params = if ledger_index, do: Map.put(params, :ledger_index, ledger_index), else: params
def deposit_authorized!(params), do: params |> deposit_authorized() |> unwrap_or_raise()

post("/", %{
method: "deposit_authorized",
params: [params]
})
defparams "deposit_authorized" do
required(:source_account, :string, format: XRPL.account_address_regex())
required(:destination_account, :string, format: XRPL.account_address_regex())
optional(:ledger_hash, :string, format: XRPL.ledger_hash_regex())
optional(:ledger_index, :string, format: XRPL.ledger_index_regex())
end

@doc """
The nft_buy_offers method returns a list of buy offers for a given NFToken object.
Official documentation: https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/path-and-order-book-methods/nft_buy_offers/#nft_buy_offers
Official documentation: https://xrpl.org/nft_buy_offers.html
"""
def nft_buy_offers(nft_id, ledger_index \\ nil, ledger_hash \\ nil, limit \\ nil, maker \\ nil) do
params = %{
nft_id: nft_id
}

params = if ledger_hash, do: Map.put(params, :ledger_hash, ledger_hash), else: params
params = if ledger_index, do: Map.put(params, :ledger_index, ledger_index), else: params
params = if limit, do: Map.put(params, :limit, limit), else: params
params = if maker, do: Map.put(params, :maker, maker), else: params

post("/", %{
method: "nft_buy_offers",
params: [params]
})
end

def nft_sell_offers(nft_id, ledger_index \\ nil, ledger_hash \\ nil, limit \\ nil, taker \\ nil) do
params = %{
nft_id: nft_id
}

params = if ledger_hash, do: Map.put(params, :ledger_hash, ledger_hash), else: params
params = if ledger_index, do: Map.put(params, :ledger_index, ledger_index), else: params
params = if limit, do: Map.put(params, :limit, limit), else: params
params = if taker, do: Map.put(params, :taker, taker), else: params

post("/", %{
method: "nft_sell_offers",
params: [params]
})
def nft_buy_offers(params) do
xrpl("nft_buy_offers", params)
end

def nft_buy_offers!(params), do: params |> nft_buy_offers() |> unwrap_or_raise()

defparams "nft_buy_offers" do
required(:nft_id, :string)
optional(:ledger_hash, :string, format: XRPL.ledger_hash_regex())
optional(:ledger_index, :string, format: XRPL.ledger_index_regex())
optional(:limit, :integer)
optional(:marker, :string)
end

@doc """
The nft_sell_offers method returns a list of sell offers for a given NFToken object.
Official documentation: https://xrpl.org/nft_sell_offers.html
"""
def nft_sell_offers(params) do
xrpl("nft_sell_offers", params)
end

def nft_sell_offers!(params), do: params |> nft_sell_offers() |> unwrap_or_raise()

defparams "nft_sell_offers" do
required(:nft_id, :string)
optional(:ledger_hash, :string, format: XRPL.ledger_hash_regex())
optional(:ledger_index, :string, format: XRPL.ledger_index_regex())
optional(:limit, :integer)
optional(:marker, :string)
end

@doc """
The ripple_path_find method is a simplified version of the path_find method that provides a single response with a payment path you can use right away. It is available in both the WebSocket and JSON-RPC APIs. However, the results tend to become outdated as time passes. Instead of making multiple calls to stay updated, you should instead use the path_find method to subscribe to continued updates where possible.
Although the rippled server tries to find the cheapest path or combination of paths for making a payment, it is not guaranteed that the paths returned by this method are, in fact, the best paths.
Official documentation: https://xrpl.org/ripple_path_find.html
"""
def ripple_path_find(params) do
xrpl("ripple_path_find", params)
end

def ripple_path_find!(params), do: params |> ripple_path_find() |> unwrap_or_raise()

defparams "ripple_path_find" do
required(:source_account, :string, format: XRPL.account_address_regex())
required(:destination_account, :string, format: XRPL.account_address_regex())

required(:destination_amount, :map) do
required(:currency, :string, format: XRPL.currency_regex())
optional(:issuer, :string, format: XRPL.account_address_regex())
required(:value, :string)
end

optional(:source_currencies, :list)
end
end
14 changes: 8 additions & 6 deletions lib/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule XRPL.Transaction do
Official RPC documentation https://xrpl.org/transaction-methods.html
"""

import XRPL
use XRPL

@doc """
A submit-only request includes the following parameters:
Expand All @@ -16,11 +16,13 @@ defmodule XRPL.Transaction do
Official documentation: https://xrpl.org/docs/references/http-websocket-apis/public-api-methods/transaction-methods/submit/#submit
"""
def submit(tx_blob, fail_hard \\ false) do
post("/", %{
method: "submit",
params: [%{tx_blob: tx_blob, fail_hard: fail_hard}]
})
def submit(params) do
xrpl("submit", params)
end

def submit!(params), do: params |> submit() |> unwrap_or_raise()

defparams "submit" do
end

@doc """
Expand Down
Loading

0 comments on commit 09ec9e6

Please sign in to comment.