Skip to content

Commit

Permalink
Implement path_orderbook
Browse files Browse the repository at this point in the history
  • Loading branch information
lucca65 committed Mar 4, 2024
1 parent d48edff commit 196ec78
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ This is currently a work in progress and there is still a lot to do. Any help is

- [x] Account methods
- [x] Ledger methods
- [ ] Transaction methods
- [ ] Path and Order Book methods
- [x] Transaction methods
- [x] Path and Order Book methods
- [ ] Payment methods
- [ ] Server methods
- [ ] Clio methods
Expand Down
99 changes: 99 additions & 0 deletions lib/path_orderbook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,103 @@ defmodule XRPL.PathOrderbook do
Official RPC documentation https://xrpl.org/path-and-order-book-methods.html
"""

import 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
"""
def amm_info(_, _, _) do
raise "Not implemented. Requires amendments. More info: https://xrpl.org/resources/known-amendments/#amm"
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
"""
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]
})
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
"""
def deposit_authorized(source_account, destination_account, ledger_index \\ nil, ledger_hash \\ nil) do
params = %{
source_account: source_account,
destination_account: destination_account
}

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

post("/", %{
method: "deposit_authorized",
params: [params]
})
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
"""
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]
})
end
end
63 changes: 63 additions & 0 deletions test/path_orderbook_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
defmodule XRPL.PathOrderbookTest do
use ExUnit.Case

alias XRPL.PathOrderbook

describe "amm_info/2 and amm/3" do
test "use amm_account address" do
assert_raise RuntimeError, fn ->
PathOrderbook.amm_info(nil, nil, nil)
end
end

test "use asset and asset1" do
assert_raise RuntimeError, fn ->
PathOrderbook.amm_info(nil, nil, nil)
end
end
end

describe "book_offers/3" do
test "use taker_gets_currency, taker_pays_currency, taker_pays_issuer" do
assert {:ok, %Tesla.Env{status: 200}} =
PathOrderbook.book_offers("XRP", "USD", "rvYAfWj5gh67oV6fW32ZzP3Aw4Eubs59B")
end
end

describe "deposit_authorized/2" do
# Test failing in the original documentation as well
@tag :skip
test "send optional ledger_index" do
assert {:ok, %Tesla.Env{status: 200}} =
PathOrderbook.deposit_authorized(
"rEhxGqkqPPSxQ3P25J66ft5TwpzV14k2de",
"rsUiUMpnrgxQp24dJYZDhmV4bE3aBtQyt8",
"validated"
)
end
end

describe "nft_buy_offers/1" do
# Test failing in the original documentation as well
@tag :skip
test "uses ledger_index" do
assert {:ok, %Tesla.Env{status: 200}} =
PathOrderbook.nft_buy_offers(
"00090000D0B007439B080E9B05BF62403911301A7B1F0CFAA048C0A200000007",
"validated"
)
end
end

describe "nft_sell_offers/1" do
# Test failing in the original documentation as well
@tag :skip
test "uses ledger_index" do
assert {:ok, %Tesla.Env{status: 200}} =
PathOrderbook.nft_sell_offers(
"00090000D0B007439B080E9B05BF62403911301A7B1F0CFAA048C0A200000007",
"validated"
)
end
end
end
8 changes: 8 additions & 0 deletions test/payment_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule XRPL.PaymentTest do
use ExUnit.Case

alias XRPL.Payment

Check warning on line 4 in test/payment_test.exs

View workflow job for this annotation

GitHub Actions / Build and test

unused alias Payment

describe "" do
end
end

0 comments on commit 196ec78

Please sign in to comment.