From 0ebfa2416b1354a92ab33bdbdaa2c55df50127b2 Mon Sep 17 00:00:00 2001 From: Samuel Date: Wed, 22 Jun 2022 15:13:55 +0200 Subject: [PATCH] Set NFT id to 0 for fungible --- .../validation_stamp/ledger_operations.ex | 32 +++++++++++++------ .../explorer/transaction_details.html.leex | 25 +++++++++++---- 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/lib/archethic/transaction_chain/transaction/validation_stamp/ledger_operations.ex b/lib/archethic/transaction_chain/transaction/validation_stamp/ledger_operations.ex index a32dfec7e3..4f2b3915ea 100644 --- a/lib/archethic/transaction_chain/transaction/validation_stamp/ledger_operations.ex +++ b/lib/archethic/transaction_chain/transaction/validation_stamp/ledger_operations.ex @@ -65,7 +65,6 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation ...> ) %LedgerOperations{ unspent_outputs: [ - %UnspentOutput{from: "@NFT2", amount: 100_000_000, type: {:NFT, "@NFT2", 0}}, %UnspentOutput{from: "@NFT2", amount: 100_000_000, type: {:NFT, "@NFT2", 1}}, %UnspentOutput{from: "@NFT2", amount: 100_000_000, type: {:NFT, "@NFT2", 2}}, %UnspentOutput{from: "@NFT2", amount: 100_000_000, type: {:NFT, "@NFT2", 3}}, @@ -74,7 +73,8 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation %UnspentOutput{from: "@NFT2", amount: 100_000_000, type: {:NFT, "@NFT2", 6}}, %UnspentOutput{from: "@NFT2", amount: 100_000_000, type: {:NFT, "@NFT2", 7}}, %UnspentOutput{from: "@NFT2", amount: 100_000_000, type: {:NFT, "@NFT2", 8}}, - %UnspentOutput{from: "@NFT2", amount: 100_000_000, type: {:NFT, "@NFT2", 9}} + %UnspentOutput{from: "@NFT2", amount: 100_000_000, type: {:NFT, "@NFT2", 9}}, + %UnspentOutput{from: "@NFT2", amount: 100_000_000, type: {:NFT, "@NFT2", 10}} ] } """ @@ -114,7 +114,7 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation properties |> Enum.with_index() |> Enum.map(fn {_item_properties, index} -> - %UnspentOutput{from: address, amount: 1 * @unit_uco, type: {:NFT, address, index}} + %UnspentOutput{from: address, amount: 1 * @unit_uco, type: {:NFT, address, index + 1}} end) end @@ -329,26 +329,28 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation ] } + # When non-fungible tokens are used as input but want to consume only a single input + iex> %LedgerOperations{ ...> transaction_movements: [ - ...> %TransactionMovement{to: "@Bob4", amount: 100_000_000, type: {:NFT, "@CharlieNFT", 1}} + ...> %TransactionMovement{to: "@Bob4", amount: 100_000_000, type: {:NFT, "@CharlieNFT", 2}} ...> ], ...> fee: 40_000_000 ...> } |> LedgerOperations.consume_inputs("@Alice2", [ ...> %UnspentOutput{from: "@Charlie1", amount: 200_000_000, type: :UCO}, - ...> %UnspentOutput{from: "@CharlieNFT", amount: 100_000_000, type: {:NFT, "@CharlieNFT", 0}}, ...> %UnspentOutput{from: "@CharlieNFT", amount: 100_000_000, type: {:NFT, "@CharlieNFT", 1}}, - ...> %UnspentOutput{from: "@CharlieNFT", amount: 100_000_000, type: {:NFT, "@CharlieNFT", 2}} + ...> %UnspentOutput{from: "@CharlieNFT", amount: 100_000_000, type: {:NFT, "@CharlieNFT", 2}}, + ...> %UnspentOutput{from: "@CharlieNFT", amount: 100_000_000, type: {:NFT, "@CharlieNFT", 3}} ...> ]) %LedgerOperations{ fee: 40_000_000, transaction_movements: [ - %TransactionMovement{to: "@Bob4", amount: 100_000_000, type: {:NFT, "@CharlieNFT", 1}} + %TransactionMovement{to: "@Bob4", amount: 100_000_000, type: {:NFT, "@CharlieNFT", 2}} ], unspent_outputs: [ %UnspentOutput{from: "@Alice2", amount: 160_000_000, type: :UCO}, - %UnspentOutput{from: "@CharlieNFT", amount: 100_000_000, type: {:NFT, "@CharlieNFT", 0}}, - %UnspentOutput{from: "@CharlieNFT", amount: 100_000_000, type: {:NFT, "@CharlieNFT", 2}} + %UnspentOutput{from: "@CharlieNFT", amount: 100_000_000, type: {:NFT, "@CharlieNFT", 1}}, + %UnspentOutput{from: "@CharlieNFT", amount: 100_000_000, type: {:NFT, "@CharlieNFT", 3}} ] } """ @@ -392,10 +394,20 @@ defmodule Archethic.TransactionChain.Transaction.ValidationStamp.LedgerOperation acc recv_amount when recv_amount - amount_to_spend > 0 -> + fixed_amount_to_spend = + case nft_id do + 0 -> + amount_to_spend + + _ -> + # We prevent part non-fungible token to be spent + trunc(amount_to_spend / @unit_uco) * @unit_uco + end + [ %UnspentOutput{ from: change_address, - amount: recv_amount - amount_to_spend, + amount: recv_amount - fixed_amount_to_spend, type: {:NFT, nft_address, nft_id} } | acc diff --git a/lib/archethic_web/templates/explorer/transaction_details.html.leex b/lib/archethic_web/templates/explorer/transaction_details.html.leex index 5ad1273a0e..8bc49789fa 100644 --- a/lib/archethic_web/templates/explorer/transaction_details.html.leex +++ b/lib/archethic_web/templates/explorer/transaction_details.html.leex @@ -333,9 +333,14 @@ <%= if movement.amount > 0 do %> (<%= format_full_usd_amount(movement.amount, @uco_price_at_time[:usd], @uco_price_now[:usd]) %>) <% end %> - <% {:NFT, nft_address, _nft_id} -> %> + <% {:NFT, nft_address, nft_id} -> %> <%= to_float(movement.amount) %> - NFT + NFT + <%= if nft_id >= 1 do %> + (#<%= nft_id %>) + <% end %> + + <%= link to: Routes.live_path(@socket, ArchethicWeb.TransactionDetailsLive, Base.encode16(nft_address)) do %> <%= Base.encode16(:binary.part(nft_address, 0, 13)) %>... <% end %> @@ -370,9 +375,13 @@ <%= if unspent_output.amount > 0 do %> (<%= format_full_usd_amount(unspent_output.amount, @uco_price_at_time[:usd], @uco_price_now[:usd]) %>) <% end %> - <% {:NFT, nft_address, _nft_id} -> %> + <% {:NFT, nft_address, nft_id} -> %> <%= to_float(unspent_output.amount) %> - NFT + NFT + <%= if nft_id >= 1 do %> + (#<%= nft_id %>) + <% end %> + <%= link to: Routes.live_path(@socket, ArchethicWeb.TransactionDetailsLive, Base.encode16(nft_address)) do %> <%= Base.encode16(:binary.part(nft_address, 0, 13)) %>... <% end %> @@ -509,9 +518,13 @@ <%= if input.reward? do %> Mining reward <% end %> - <% {:NFT, nft_address, _nft_id} -> %> + <% {:NFT, nft_address, nft_id} -> %>
-
NFT
+
NFT + <%= if nft_id >= 1 do %> + (#<%= nft_id %>) + <% end %> +
<%= link to: Routes.live_path(@socket, ArchethicWeb.TransactionDetailsLive, Base.encode16(nft_address)) do %> <%= Base.encode16(:binary.part(nft_address, 0, 6)) %>...