Skip to content

Commit 7febbf9

Browse files
authored
Merge pull request #10 from PagoPlus/fix/transactions-to-itself
[FIX] Transactions to itself
2 parents 17a32a9 + ad759e0 commit 7febbf9

File tree

4 files changed

+118
-4
lines changed

4 files changed

+118
-4
lines changed

lib/numscriptex.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ defmodule Numscriptex do
5656
5757
```elixir
5858
iex> Numscriptex.version()
59-
%{numscriptex: "v0.2.1", numscript_wasm: "v0.0.2"}
59+
%{numscriptex: "v0.2.2", numscript_wasm: "v0.0.2"}
6060
```
6161
"""
6262
@spec version() :: %{numscriptex: binary(), numscript_wasm: binary()}

lib/numscriptex/balances.ex

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,13 @@ defmodule Numscriptex.Balances do
129129

130130
defp calculate_final_balance(balance, posting, initial_balance) do
131131
same_asset? = posting["asset"] == balance["asset"]
132-
source? = posting["source"] === balance["account"]
133-
destination? = posting["destination"] === balance["account"]
132+
source? = posting["source"] == balance["account"]
133+
destination? = posting["destination"] == balance["account"]
134134

135135
cond do
136+
source? and destination? and same_asset? ->
137+
initial_balance
138+
136139
source? and same_asset? ->
137140
initial_balance - posting["amount"]
138141

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ defmodule Numscriptex.MixProject do
22
use Mix.Project
33

44
@source_url "https://github.com/PagoPlus/numscriptex"
5-
@version "0.2.1"
5+
@version "0.2.2"
66

77
def project do
88
[

test/numscriptex_test.exs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,117 @@ defmodule NumscriptexTest do
739739
]
740740
end
741741

742+
test "transactions to itself" do
743+
balances = %{"user" => %{"USD/2" => 1000}}
744+
struct = build_run_struct(balances, %{}, %{})
745+
746+
script = """
747+
send [USD/2 1000] (
748+
source = @user
749+
destination = @user
750+
)
751+
"""
752+
753+
assert {:ok, result} = Numscriptex.run(script, struct)
754+
755+
assert result.postings == [
756+
%{
757+
amount: 1000,
758+
asset: "USD/2",
759+
destination: "user",
760+
source: "user"
761+
}
762+
]
763+
764+
assert result.balances == [
765+
%{
766+
account: "user",
767+
asset: "USD/2",
768+
final_balance: 1000,
769+
initial_balance: 1000
770+
}
771+
]
772+
end
773+
774+
test "transactions to itself and another destination" do
775+
balances = %{"user" => %{"USD/2" => 1000}, "user2" => %{"USD/2" => 1000}}
776+
struct = build_run_struct(balances, %{}, %{})
777+
778+
script = """
779+
send [USD/2 1000] (
780+
source = @user
781+
destination = {
782+
62% to @user
783+
27% to @user2
784+
remaining to @user3
785+
}
786+
)
787+
"""
788+
789+
assert {:ok, result} = Numscriptex.run(script, struct)
790+
791+
assert result.postings == [
792+
%{
793+
amount: 620,
794+
asset: "USD/2",
795+
destination: "user",
796+
source: "user"
797+
},
798+
%{
799+
amount: 270,
800+
asset: "USD/2",
801+
destination: "user2",
802+
source: "user"
803+
},
804+
%{
805+
source: "user",
806+
destination: "user3",
807+
amount: 110,
808+
asset: "USD/2"
809+
}
810+
]
811+
812+
assert result.balances == [
813+
%{
814+
account: "user",
815+
asset: "USD/2",
816+
final_balance: 620,
817+
initial_balance: 1000
818+
},
819+
%{
820+
account: "user2",
821+
asset: "USD/2",
822+
final_balance: 1270,
823+
initial_balance: 1000
824+
},
825+
%{
826+
account: "user3",
827+
asset: "USD/2",
828+
final_balance: 110,
829+
initial_balance: 0
830+
}
831+
]
832+
end
833+
834+
test "save a minimum amount from source" do
835+
balances = %{"user" => %{"USD/2" => 1000}}
836+
struct = build_run_struct(balances, %{}, %{})
837+
838+
script = """
839+
// Now that we are saving $5 from the total amount of $10,
840+
// the user will only have $5 to spend, thus failing the transaction
841+
// given that the user will need $5,01.
842+
save [USD/2 500] from @user
843+
send [USD/2 501] (
844+
source = @user
845+
destination = @dest
846+
)
847+
"""
848+
849+
assert {:error, error} = Numscriptex.run(script, struct)
850+
assert error.details == "Not enough funds. Needed [USD/2 501] (only [USD/2 500] available)"
851+
end
852+
742853
test "with insufficient amount" do
743854
script = """
744855
send [USD/2 100] (

0 commit comments

Comments
 (0)