Skip to content

Commit

Permalink
Merge pull request #1441 from zmcNotafraid/issue-1340-2
Browse files Browse the repository at this point in the history
Issue 1340 2
  • Loading branch information
zmcNotafraid authored May 16, 2023
2 parents 5a74532 + 6691120 commit f825557
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,39 @@ defmodule GodwokenExplorerWeb.API.DepositWithdrawalController do
end
end

def index(conn, %{"udt_id" => "0x" <> _, "export" => "true"} = params) do
case Repo.get_by(UDT, contract_address_hash: params["udt_id"]) do
%UDT{id: id} ->
data = DepositWithdrawalView.list_by_udt_id(id, nil)

DepositWithdrawalCsv.export(data)
|> Enum.reduce_while(
conn
|> put_resp_content_type("application/csv")
|> put_resp_header(
"content-disposition",
"attachment; filename=deposit_withdrawals.csv"
)
|> send_chunked(200),
fn chunk, conn ->
case Plug.Conn.chunk(
conn,
chunk
) do
{:ok, conn} ->
{:cont, conn}

{:error, :closed} ->
{:halt, conn}
end
end
)

nil ->
{:error, :not_found}
end
end

def index(conn, %{"udt_id" => _udt_id, "export" => "true"} = params) do
case Repo.get(UDT, params["udt_id"]) do
%UDT{id: id} ->
Expand Down Expand Up @@ -101,6 +134,17 @@ defmodule GodwokenExplorerWeb.API.DepositWithdrawalController do
end
end

def index(conn, %{"udt_id" => "0x" <> _} = params) do
case Repo.get_by(UDT, contract_address_hash: params["udt_id"]) do
%UDT{id: id} ->
data = DepositWithdrawalView.list_by_udt_id(id, conn.params["page"] || 1)
json(conn, data)

nil ->
{:error, :not_found}
end
end

def index(conn, %{"udt_id" => _udt_id} = params) do
case Repo.get(UDT, params["udt_id"]) do
%UDT{id: id} ->
Expand Down
1 change: 0 additions & 1 deletion lib/godwoken_indexer/fetcher/udt_balance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ defmodule GodwokenIndexer.Fetcher.UDTBalance do
alias GodwokenIndexer.Fetcher.UDTBalances
alias GodwokenExplorer.Account.{CurrentUDTBalance, UDTBalance}
alias GodwokenExplorer.Graphql.Workers.UpdateSmartContractCKB
alias GodwokenExplorer.UDT

import Ecto.Query

Expand Down
4 changes: 2 additions & 2 deletions native/molecule_parser/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion native/molecule_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ crate-type = ["dylib"]
rustler = "0.28.0"
molecule = { version = "0.7.5", default-features = false }
hex= "0.4.3"
ckb-hash = "0.109.0"
ckb-hash = "0.110.0"
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,87 @@ defmodule GodwokenExplorerWeb.API.DepositWithdrawalControllerTest do
}
end

test "export by udt_id and passed udt contract address hash", %{
conn: conn,
user: user,
udt: udt,
native_udt: native_udt,
deposit: deposit,
withdrawal: withdrawal,
block: block
} do
conn =
get(
conn,
~p"/api/deposit_withdrawals?udt_id=#{to_string(native_udt.contract_address_hash)}&export=true"
)

parsed_withdrawal = %{
"block_hash" => to_string(block.hash),
"block_number" => block.number,
"ckb_lock_hash" => nil,
"eth_address" => to_string(user.eth_address),
"layer1_block_number" => withdrawal.layer1_block_number,
"layer1_output_index" => withdrawal.layer1_output_index,
"layer1_tx_hash" => to_string(withdrawal.layer1_tx_hash),
"owner_lock_hash" => to_string(withdrawal.owner_lock_hash),
"script_hash" => to_string(withdrawal.l2_script_hash),
"sudt_script_hash" => to_string(udt.script_hash),
"timestamp" => withdrawal.timestamp |> DateTime.to_iso8601(),
"type" => "withdrawal",
"udt_icon" => udt.icon,
"udt_id" => udt.id,
"udt_name" => udt.name,
"udt_symbol" => udt.symbol,
"value" => withdrawal.amount |> balance_to_view(udt.decimal),
"state" => "pending",
"capacity" => withdrawal.capacity |> Decimal.to_string(),
"udt_decimal" => udt.decimal
}

parsed_deposit = %{
"block_hash" => nil,
"block_number" => nil,
"ckb_lock_hash" => to_string(deposit.ckb_lock_hash),
"eth_address" => to_string(user.eth_address),
"layer1_block_number" => deposit.layer1_block_number,
"layer1_output_index" => deposit.layer1_output_index,
"layer1_tx_hash" => to_string(deposit.layer1_tx_hash),
"owner_lock_hash" => nil,
"script_hash" => to_string(deposit.script_hash),
"sudt_script_hash" => nil,
"timestamp" => deposit.timestamp |> DateTime.to_iso8601(),
"type" => "deposit",
"udt_icon" => udt.icon,
"udt_id" => udt.id,
"udt_name" => udt.name,
"udt_symbol" => udt.symbol,
"value" => deposit.amount |> balance_to_view(udt.decimal),
"state" => "succeed",
"capacity" => deposit.capacity |> Decimal.to_string(),
"udt_decimal" => udt.decimal
}

assert response(conn, 200) ==
"Type,Value,UDT Symbol,Capacity,UnixTimestamp,Address,Layer1 TxnHash,Block Number\r\n" <>
"withdrawal," <>
"#{parsed_withdrawal["value"]}," <>
"#{parsed_withdrawal["udt_symbol"]}," <>
"#{parsed_withdrawal["capacity"]}," <>
"#{parsed_withdrawal["timestamp"]}," <>
"#{parsed_withdrawal["eth_address"]}," <>
"#{parsed_withdrawal["layer1_tx_hash"]}," <>
"#{parsed_withdrawal["block_number"]}\r\n" <>
"deposit," <>
"#{parsed_deposit["value"]}," <>
"#{parsed_deposit["udt_symbol"]}," <>
"#{parsed_deposit["capacity"]}," <>
"#{parsed_deposit["timestamp"]}," <>
"#{parsed_deposit["eth_address"]}," <>
"#{parsed_deposit["layer1_tx_hash"]}," <>
"#{parsed_deposit["block_number"]}\r\n"
end

test "export by udt_id", %{
conn: conn,
user: user,
Expand Down Expand Up @@ -457,6 +538,74 @@ defmodule GodwokenExplorerWeb.API.DepositWithdrawalControllerTest do
"#{parsed_deposit["block_number"]}\r\n"
end

test "udt is native and passed udt's contract address hash", %{
conn: conn,
deposit: deposit,
user: user,
udt: udt,
withdrawal: withdrawal,
block: block,
native_udt: native_udt
} do
conn =
get(
conn,
~p"/api/deposit_withdrawals?udt_id=#{to_string(native_udt.contract_address_hash)}"
)

assert json_response(conn, 200) ==
%{
"data" => [
%{
"block_hash" => to_string(block.hash),
"block_number" => block.number,
"ckb_lock_hash" => nil,
"eth_address" => to_string(user.eth_address),
"layer1_block_number" => withdrawal.layer1_block_number,
"layer1_output_index" => withdrawal.layer1_output_index,
"layer1_tx_hash" => to_string(withdrawal.layer1_tx_hash),
"owner_lock_hash" => to_string(withdrawal.owner_lock_hash),
"script_hash" => to_string(withdrawal.l2_script_hash),
"sudt_script_hash" => to_string(udt.script_hash),
"timestamp" => withdrawal.timestamp |> DateTime.to_iso8601(),
"type" => "withdrawal",
"udt_icon" => udt.icon,
"udt_id" => udt.id,
"udt_name" => udt.name,
"udt_symbol" => udt.symbol,
"value" => withdrawal.amount |> balance_to_view(udt.decimal),
"state" => "pending",
"capacity" => withdrawal.capacity |> Decimal.to_string(),
"udt_decimal" => udt.decimal
},
%{
"block_hash" => nil,
"block_number" => nil,
"ckb_lock_hash" => to_string(deposit.ckb_lock_hash),
"eth_address" => to_string(user.eth_address),
"layer1_block_number" => deposit.layer1_block_number,
"layer1_output_index" => deposit.layer1_output_index,
"layer1_tx_hash" => to_string(deposit.layer1_tx_hash),
"owner_lock_hash" => nil,
"script_hash" => to_string(deposit.script_hash),
"sudt_script_hash" => nil,
"timestamp" => deposit.timestamp |> DateTime.to_iso8601(),
"type" => "deposit",
"udt_icon" => udt.icon,
"udt_id" => udt.id,
"udt_name" => udt.name,
"udt_symbol" => udt.symbol,
"value" => deposit.amount |> balance_to_view(udt.decimal),
"state" => "succeed",
"capacity" => deposit.capacity |> Decimal.to_string(),
"udt_decimal" => udt.decimal
}
],
"page" => "1",
"total_count" => "2"
}
end

test "udt is native", %{
conn: conn,
deposit: deposit,
Expand Down

0 comments on commit f825557

Please sign in to comment.