Skip to content

Commit

Permalink
Set NFT id to 0 for fungible
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel committed Jun 22, 2022
1 parent d2e88a4 commit 0ebfa24
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}},
Expand All @@ -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}}
]
}
"""
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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}}
]
}
"""
Expand Down Expand Up @@ -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
Expand Down
25 changes: 19 additions & 6 deletions lib/archethic_web/templates/explorer/transaction_details.html.leex
Original file line number Diff line number Diff line change
Expand Up @@ -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) %>
<span class="tag is-success is-light ml-2">NFT</span>
<span class="tag is-success is-light ml-2">NFT
<%= if nft_id >= 1 do %>
(#<%= nft_id %>)
<% end %>

</span>
<%= link to: Routes.live_path(@socket, ArchethicWeb.TransactionDetailsLive, Base.encode16(nft_address)) do %>
<span><%= Base.encode16(:binary.part(nft_address, 0, 13)) %>...</span>
<% end %>
Expand Down Expand Up @@ -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) %>
<span class="tag is-success is-light ml-2">NFT</span>
<span class="tag is-success is-light ml-2">NFT
<%= if nft_id >= 1 do %>
(#<%= nft_id %>)
<% end %>
</span>
<%= link to: Routes.live_path(@socket, ArchethicWeb.TransactionDetailsLive, Base.encode16(nft_address)) do %>
<span><%= Base.encode16(:binary.part(nft_address, 0, 13)) %>...</span>
<% end %>
Expand Down Expand Up @@ -509,9 +518,13 @@
<%= if input.reward? do %>
<span class="tag is-info is-light">Mining reward</span>
<% end %>
<% {:NFT, nft_address, _nft_id} -> %>
<% {:NFT, nft_address, nft_id} -> %>
<div class="columns">
<div class="column is-2-desktop"><span class="tag is-success is-light">NFT</span></div>
<div class="column is-2-desktop"><span class="tag is-success is-light">NFT
<%= if nft_id >= 1 do %>
(#<%= nft_id %>)
<% end %>
</span></div>
<div class="column is-4-desktop">
<%= link to: Routes.live_path(@socket, ArchethicWeb.TransactionDetailsLive, Base.encode16(nft_address)) do %>
<span class="is-hidden-mobile"><%= Base.encode16(:binary.part(nft_address, 0, 6)) %>...</span>
Expand Down

0 comments on commit 0ebfa24

Please sign in to comment.