Skip to content

Commit 86daad0

Browse files
authored
feat: add v2 txs endpoints (#552)
1 parent d765a25 commit 86daad0

File tree

5 files changed

+250
-212
lines changed

5 files changed

+250
-212
lines changed

README.md

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,15 @@ We currently provide hosted infrastructure at https://mainnet.aeternity.io/mdw/
165165
## HTTP endpoints
166166

167167
```
168-
GET /tx/:hash - returns transaction by hash
169-
GET /txi/:index - returns transaction by index (0 .. last transaction index)
170-
GET /txs/count - returns total number of transactions (last transaction index + 1)
171-
GET /txs/count/:id - returns counts of transactions per transaction field for given id
172-
GET /txs/:scope_type/:range - returns transactions bounded by scope/range where query is in query string
173-
GET /txs/:direction - returns transactions from beginning (forward) or end (backward), query is in query string
174-
175-
GET /v2/blocks/:hash - returns block by hash
176-
GET /v2/blocks/:kbi - returns key block by integer index
177-
GET /v2/blocks/:kbi/:mbi - returns micro block by integer indices
178-
GET /v2/blocks - returns generation blocks
168+
GET /v2/txs/:hash_or_index - returns transaction by hash or index
169+
GET /v2/txs/count - returns total number of transactions (last transaction index + 1)
170+
GET /v2/txs/count/:id - returns counts of transactions per transaction field for given id
171+
GET /v2/txs - returns transactions in any direction
172+
173+
GET /v2/blocks/:hash - returns block by hash
174+
GET /v2/blocks/:kbi - returns key block by integer index
175+
GET /v2/blocks/:kbi/:mbi - returns micro block by integer indices
176+
GET /v2/blocks - returns generation blocks
179177
180178
GET /name/:id - returns name information by hash or plain name
181179
GET /name/auction/:id - returns name information for auction, by hash or plain name
@@ -193,14 +191,9 @@ GET /names/active/:scope_type/:range - returns active names for continuation
193191
GET /names - returns all names (active and expired) ordered by (optional) query parameters
194192
GET /names/:scope_type/:range - returns all names for continuation link
195193
196-
GET /oracle/:id - returns oracle information by hash
194+
GET /v2/oracles/:id - returns oracle information by hash
197195
198-
GET /oracles/inactive - returns expired oracles ordered by expiration height
199-
GET /oracles/inactive/gen/:range - returns expired oracles for continuation link
200-
GET /oracles/active - returns active oracles ordered by expiration height
201-
GET /oracles/active/gen/:range - returns active oracles for continuation link
202-
GET /oracles - returns all oracles from newest (active) to oldest (expired)
203-
GET /oracles/gen/:range - returns all oracles for continuation link
196+
GET /v2/oracles - returns expired oracles ordered by expiration height, filtered by active/inactive state and scope
204197
205198
GET /aex9/by_name - returns AEX9 tokens, filtered by token name
206199
GET /aex9/by_symbol - returns AEX9 tokens, filtered by token symbol
@@ -303,7 +296,7 @@ Type groups for the transactions listed above are:
303296

304297
`type` parameter:
305298
```
306-
$ curl -s "https://mainnet.aeternity.io/mdw/txs/forward?type=channel_create&limit=1" | jq '.'
299+
$ curl -s "https://mainnet.aeternity.io/mdw/txs?direction=forward&type=channel_create&limit=1" | jq '.'
307300
{
308301
"data": [
309302
{
@@ -340,7 +333,7 @@ $ curl -s "https://mainnet.aeternity.io/mdw/txs/forward?type=channel_create&limi
340333

341334
`type_group` parameter:
342335
```
343-
$ curl -s "https://mainnet.aeternity.io/mdw/txs/forward?type_group=oracle&limit=1" | jq '.'
336+
$ curl -s "https://mainnet.aeternity.io/mdw/txs?direction=forward&type_group=oracle&limit=1" | jq '.'
344337
{
345338
"data": [
346339
{
@@ -395,7 +388,7 @@ With generic ids, it is possible to select also `create`/`register` transactions
395388
###### Examples
396389

397390
```
398-
$ curl -s "https://mainnet.aeternity.io/mdw/txs/forward?contract=ct_2AfnEfCSZCTEkxL5Yoi4Yfq6fF7YapHRaFKDJK3THMXMBspp5z&limit=2" | jq '.'
391+
$ curl -s "https://mainnet.aeternity.io/mdw/txs?direction=forward&contract=ct_2AfnEfCSZCTEkxL5Yoi4Yfq6fF7YapHRaFKDJK3THMXMBspp5z&limit=2" | jq '.'
399392
{
400393
"data": [
401394
{
@@ -484,7 +477,7 @@ $ curl -s "https://mainnet.aeternity.io/mdw/txs/forward?contract=ct_2AfnEfCSZCTE
484477
```
485478

486479
```
487-
$ curl -s "https://mainnet.aeternity.io/mdw/txs/forward?oracle=ok_24jcHLTZQfsou7NvomRJ1hKEnjyNqbYSq2Az7DmyrAyUHPq8uR&limit=1" | jq '.'
480+
$ curl -s "https://mainnet.aeternity.io/mdw/txs?direction=forward&oracle=ok_24jcHLTZQfsou7NvomRJ1hKEnjyNqbYSq2Az7DmyrAyUHPq8uR&limit=1" | jq '.'
488481
{
489482
"data": [
490483
{
@@ -521,7 +514,7 @@ $ curl -s "https://mainnet.aeternity.io/mdw/txs/forward?oracle=ok_24jcHLTZQfsou7
521514
```
522515

523516
```
524-
$ curl -s "https://mainnet.aeternity.io/mdw/txs/forward?channel=ch_22usvXSjYaDPdhecyhub7tZnYpHeCEZdscEEyhb2M4rHb58RyD&limit=2" | jq '.'
517+
$ curl -s "https://mainnet.aeternity.io/mdw/txs?direction=forward&channel=ch_22usvXSjYaDPdhecyhub7tZnYpHeCEZdscEEyhb2M4rHb58RyD&limit=2" | jq '.'
525518
{
526519
"data": [
527520
{
@@ -654,7 +647,7 @@ For example, for a GAMetaTx with inner SpendTx, one might request with the follo
654647

655648
with provided transaction type (`name_transfer`):
656649
```
657-
$ curl -s "https://mainnet.aeternity.io/mdw/txs/forward?name_transfer.recipient_id=ak_idkx6m3bgRr7WiKXuB8EBYBoRqVsaSc6qo4dsd23HKgj3qiCF&limit=1" | jq '.'
650+
$ curl -s "https://mainnet.aeternity.io/mdw/txs?direction=forward&name_transfer.recipient_id=ak_idkx6m3bgRr7WiKXuB8EBYBoRqVsaSc6qo4dsd23HKgj3qiCF&limit=1" | jq '.'
658651
{
659652
"data": [
660653
{
@@ -685,7 +678,7 @@ $ curl -s "https://mainnet.aeternity.io/mdw/txs/forward?name_transfer.recipient_
685678

686679
freestanding field `from_id`, and via `jq` extracting only tx_index and transaction type:
687680
```
688-
curl -s "https://mainnet.aeternity.io/mdw/txs/backward?from_id=ak_ozzwBYeatmuN818LjDDDwRSiBSvrqt4WU7WvbGsZGVre72LTS&limit=5" | jq '.data | .[] | [.tx_index, .tx.type]'
681+
curl -s "https://mainnet.aeternity.io/mdw/v2/txs?from_id=ak_ozzwBYeatmuN818LjDDDwRSiBSvrqt4WU7WvbGsZGVre72LTS&limit=5" | jq '.data | .[] | [.tx_index, .tx.type]'
689682
[
690683
98535,
691684
"ChannelForceProgressTx"
@@ -733,7 +726,7 @@ retrieve a new page of results.
733726

734727
getting the first transaction:
735728
```
736-
$ curl -s "https://mainnet.aeternity.io/mdw/txs/forward?account=ak_E64bTuWTVj9Hu5EQSgyTGZp27diFKohTQWw3AYnmgVSWCnfnD&limit=1" | jq '.'
729+
$ curl -s "https://mainnet.aeternity.io/mdw/v2/txs?direction=forward&account=ak_E64bTuWTVj9Hu5EQSgyTGZp27diFKohTQWw3AYnmgVSWCnfnD&limit=1" | jq '.'
737730
{
738731
"data": [
739732
{
@@ -765,7 +758,7 @@ $ curl -s "https://mainnet.aeternity.io/mdw/txs/forward?account=ak_E64bTuWTVj9Hu
765758

766759
getting the next transaction by prepending host (https://mainnet.aeternity.io/mdw) to the continuation-URL from last request:
767760
```
768-
$ curl -s "https://mainnet.aeternity.io/mdw/txs/forward?account=ak_E64bTuWTVj9Hu5EQSgyTGZp27diFKohTQWw3AYnmgVSWCnfnD&cursor=1779354&limit=1" | jq '.'
761+
$ curl -s "https://mainnet.aeternity.io/mdw/v2/txs?direction=forward&account=ak_E64bTuWTVj9Hu5EQSgyTGZp27diFKohTQWw3AYnmgVSWCnfnD&cursor=1779354&limit=1" | jq '.'
769762
{
770763
"data": [
771764
{
@@ -811,7 +804,7 @@ If `type` or `type_group` is provided, the transaction in the result set must be
811804

812805
transactions where each transaction contains both accounts, no matter at which field:
813806
```
814-
$ curl -s "https://mainnet.aeternity.io/mdw/txs/backward?account=ak_24jcHLTZQfsou7NvomRJ1hKEnjyNqbYSq2Az7DmyrAyUHPq8uR&account=ak_zUQikTiUMNxfKwuAfQVMPkaxdPsXP8uAxnfn6TkZKZCtmRcUD&limit=1" | jq '.'
807+
$ curl -s "https://mainnet.aeternity.io/mdw/v2/txs?account=ak_24jcHLTZQfsou7NvomRJ1hKEnjyNqbYSq2Az7DmyrAyUHPq8uR&account=ak_zUQikTiUMNxfKwuAfQVMPkaxdPsXP8uAxnfn6TkZKZCtmRcUD&limit=1" | jq '.'
815808
{
816809
"data": [
817810
{
@@ -844,7 +837,7 @@ $ curl -s "https://mainnet.aeternity.io/mdw/txs/backward?account=ak_24jcHLTZQfso
844837

845838
spend transactions between sender and recipient (transaction type = spend is deduced from the fields):
846839
```
847-
$ curl -s "https://mainnet.aeternity.io/mdw/txs/forward?sender_id=ak_26dopN3U2zgfJG4Ao4J4ZvLTf5mqr7WAgLAq6WxjxuSapZhQg5&recipient_id=ak_r7wvMxmhnJ3cMp75D8DUnxNiAvXs8qcdfbJ1gUWfH8Ufrx2A2&limit=1" | jq '.'
840+
$ curl -s "https://mainnet.aeternity.io/mdw/v2/txs?direction=forward&sender_id=ak_26dopN3U2zgfJG4Ao4J4ZvLTf5mqr7WAgLAq6WxjxuSapZhQg5&recipient_id=ak_r7wvMxmhnJ3cMp75D8DUnxNiAvXs8qcdfbJ1gUWfH8Ufrx2A2&limit=1" | jq '.'
848841
{
849842
"data": [
850843
{
@@ -876,7 +869,7 @@ $ curl -s "https://mainnet.aeternity.io/mdw/txs/forward?sender_id=ak_26dopN3U2zg
876869

877870
name related transactions for account:
878871
```
879-
$ curl -s "https://mainnet.aeternity.io/mdw/txs/forward?account=ak_E64bTuWTVj9Hu5EQSgyTGZp27diFKohTQWw3AYnmgVSWCnfnD&type_group=name" | jq '.'
872+
$ curl -s "https://mainnet.aeternity.io/mdw/v2/txs?direction=forward&account=ak_E64bTuWTVj9Hu5EQSgyTGZp27diFKohTQWw3AYnmgVSWCnfnD&type_group=name" | jq '.'
880873
{
881874
"data": [
882875
{
@@ -1184,14 +1177,14 @@ $ curl -s "https://mainnet.aeternity.io/mdw/txi/10000000" | jq '.'
11841177
#### All transactions
11851178

11861179
```
1187-
$ curl -s "https://mainnet.aeternity.io/mdw/txs/count" | jq '.'
1180+
$ curl -s "https://mainnet.aeternity.io/mdw/v2/txs/count" | jq '.'
11881181
11921825
11891182
```
11901183

11911184
#### Transactions by type/field for ID
11921185

11931186
```
1194-
$ curl -s "https://mainnet.aeternity.io/mdw/txs/count/ak_24jcHLTZQfsou7NvomRJ1hKEnjyNqbYSq2Az7DmyrAyUHPq8uR" | jq '.'
1187+
$ curl -s "https://mainnet.aeternity.io/mdw/v2/txs/count/ak_24jcHLTZQfsou7NvomRJ1hKEnjyNqbYSq2Az7DmyrAyUHPq8uR" | jq '.'
11951188
{
11961189
"channel_create_tx": {
11971190
"responder_id": 74
@@ -3579,11 +3572,7 @@ $ curl -s "https://mainnet.aeternity.io/mdw/oracle/ok_R7cQfVN15F5ek1wBSYaMRjW2Xb
35793572
35803573
### Listing oracles
35813574
3582-
There are 3 paginable endpoints for listing oracles:
3583-
3584-
- `/oracles` - for listing ALL oracles (`active` and `inactive`)
3585-
- `/oracles/inactive` - for listing `inactive` oracles (expired)
3586-
- `/oracles/active` - for listing `active` oracles
3575+
There is only one paginable endpoints for listing oracles: `/v2/oracles` - for listing oracles, with filters that include the `scope` (e.g. `gen:100-200`) or state (`active` or `inactive`).
35873576
35883577
They are ordered by expiration height and support parameter `direction` (with options `forward` and `backward`).
35893578
@@ -3668,7 +3657,7 @@ $ curl -s "https://mainnet.aeternity.io/mdw/oracles?direction=forward&limit=1&ex
36683657
#### Inactive oracles
36693658
36703659
```
3671-
$ curl -s "https://mainnet.aeternity.io/mdw/oracles/inactive?limit=1" | jq '.'
3660+
$ curl -s "https://mainnet.aeternity.io/mdw/v2/oracles?state=inactive&limit=1" | jq '.'
36723661
{
36733662
"data": [
36743663
{
@@ -3685,15 +3674,15 @@ $ curl -s "https://mainnet.aeternity.io/mdw/oracles/inactive?limit=1" | jq '.'
36853674
"register": 15198855
36863675
}
36873676
],
3688-
"next": "/oracles/inactive?cursor=507223-ok_26QSujxMBhg67YhbgvjQvsFfGdBrK9ddG4rENEGUq2EdsyfMTC&direction=backward&expand=false&limit=1",
3677+
"next": "/v2/oracles?state=inactive&cursor=507223-ok_26QSujxMBhg67YhbgvjQvsFfGdBrK9ddG4rENEGUq2EdsyfMTC&direction=backward&expand=false&limit=1",
36893678
"prev": null
36903679
}
36913680
```
36923681
36933682
#### Active oracles
36943683
36953684
```
3696-
$ curl -s "https://mainnet.aeternity.io/mdw/oracles/active?limit=1&expand" | jq '.'
3685+
$ curl -s "https://mainnet.aeternity.io/mdw/v2/oracles?state=active&limit=1&expand" | jq '.'
36973686
{
36983687
"data": [
36993688
{
@@ -3737,7 +3726,7 @@ $ curl -s "https://mainnet.aeternity.io/mdw/oracles/active?limit=1&expand" | jq
37373726
}
37383727
}
37393728
],
3740-
"next": "/oracles/active?cursor=1289003-ok_f9vDQvr1cFAQAesYA16vjvBX9TFeWUB4Gb7WJkwfYSkL1CpDx&direction=backward&expand=true&limit=1",
3729+
"next": "/v2/oracles?state=active&cursor=1289003-ok_f9vDQvr1cFAQAesYA16vjvBX9TFeWUB4Gb7WJkwfYSkL1CpDx&direction=backward&expand=true&limit=1",
37413730
"prev": null
37423731
}
37433732
```
@@ -4264,6 +4253,7 @@ This is a list of the exceptions together with the changes that need to be done:
42644253
* `/names/auctions` - Can now be accessed via `/v2/names/auctions`
42654254
* `/names/auctions/:scope_type/:range` - Can now be accessed via `/v2/auctions?scope=gen:10-100` (or `?scope=txi:1000-2000`).
42664255
* `/names/search/:prefix` - The prefix is no longer part of the path, but a query parameter instead (`?prefix=...`).
4256+
* `/oracles/:state/:scope_scope/:range`, `/oracles/:scope_scope/:range` - Can now all be accessed via `/v2/oracles?state=inactive&scope=gen:100-200`.
42674257
42684258
## Websocket interface
42694259

lib/ae_mdw/txs.ex

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ defmodule AeMdw.Txs do
1313
alias AeMdw.Db.Model.Tx
1414
alias AeMdw.Db.Model.Type
1515
alias AeMdw.Db.Name
16-
alias AeMdw.Db.Util
16+
alias AeMdw.Db.Util, as: DbUtil
17+
alias AeMdw.Error
1718
alias AeMdw.Error.Input, as: ErrInput
1819
alias AeMdw.Log
1920
alias AeMdw.Node
@@ -83,10 +84,10 @@ defmodule AeMdw.Txs do
8384
end
8485
end
8586

86-
defp first_gen_to_txi(first_gen, :forward), do: Util.gen_to_txi(first_gen)
87-
defp first_gen_to_txi(first_gen, :backward), do: Util.gen_to_txi(first_gen + 1) - 1
88-
defp last_gen_to_txi(last_gen, :forward), do: Util.gen_to_txi(last_gen + 1) - 1
89-
defp last_gen_to_txi(last_gen, :backward), do: Util.gen_to_txi(last_gen)
87+
defp first_gen_to_txi(first_gen, :forward), do: DbUtil.gen_to_txi(first_gen)
88+
defp first_gen_to_txi(first_gen, :backward), do: DbUtil.gen_to_txi(first_gen + 1) - 1
89+
defp last_gen_to_txi(last_gen, :forward), do: DbUtil.gen_to_txi(last_gen + 1) - 1
90+
defp last_gen_to_txi(last_gen, :backward), do: DbUtil.gen_to_txi(last_gen)
9091

9192
# The purpose of this function is to generate the streams that will be then used as input for
9293
# Collection.merge/2 function. The function is divided into three clauses. There's an explanation
@@ -334,16 +335,10 @@ defmodule AeMdw.Txs do
334335
tx
335336
end
336337

337-
@spec fetch(txi(), add_spendtx_details?()) :: {:ok, tx()} | :not_found
338-
def fetch(txi, add_spendtx_details?) do
339-
case Database.fetch(@table, txi) do
340-
{:ok, tx} -> {:ok, render(tx, add_spendtx_details?)}
341-
:not_found -> :not_found
342-
end
343-
end
338+
@spec fetch(txi() | tx_hash(), add_spendtx_details?()) :: {:ok, tx()} | {:error, Error.t()}
339+
def fetch(tx_hash, add_spendtx_details? \\ true)
344340

345-
@spec fetch_by_hash(tx_hash()) :: {:ok, tx()} | :not_found
346-
def fetch_by_hash(tx_hash) do
341+
def fetch(tx_hash, add_spendtx_details?) when is_binary(tx_hash) do
347342
mb_hash = :aec_db.find_tx_location(tx_hash)
348343

349344
case :aec_chain.get_header(mb_hash) do
@@ -353,15 +348,22 @@ defmodule AeMdw.Txs do
353348
|> Blocks.fetch_txis_from_gen()
354349
|> Stream.map(&Database.fetch!(@table, &1))
355350
|> Enum.find_value(
356-
:not_found,
351+
{:error, ErrInput.NotFound.exception(value: tx_hash)},
357352
fn
358-
Model.tx(id: ^tx_hash) = tx -> {:ok, render(tx, true)}
353+
Model.tx(id: ^tx_hash) = tx -> {:ok, render(tx, add_spendtx_details?)}
359354
_tx -> nil
360355
end
361356
)
362357

363-
:error ->
364-
:not_found
358+
:not_found ->
359+
{:error, ErrInput.NotFound.exception(value: tx_hash)}
360+
end
361+
end
362+
363+
def fetch(txi, add_spendtx_details?) do
364+
case Database.fetch(@table, txi) do
365+
{:ok, tx} -> {:ok, render(tx, add_spendtx_details?)}
366+
:not_found -> {:error, ErrInput.NotFound.exception(value: txi)}
365367
end
366368
end
367369

0 commit comments

Comments
 (0)