Skip to content

Commit 03dcd79

Browse files
committed
simplify code
1 parent a6806fe commit 03dcd79

File tree

3 files changed

+16
-23
lines changed

3 files changed

+16
-23
lines changed

internal/interpreter/interpreter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ func (st *programState) pushReceiverAddress(name string, sender Sender) {
392392
case VirtualAccount:
393393
// Here we have a debt from a virtual acc.
394394
// we don't want to emit that as a posting (but TODO check how does it interact with kept)
395-
senderAccountAddress.Pull(st.CurrentAsset, nil, Sender{
395+
senderAccountAddress.Pull(st.CurrentAsset, Sender{
396396
AccountAddress(name),
397397
sender.Amount,
398398
sender.Color,

internal/interpreter/virtual_account.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package interpreter
33
import (
44
"fmt"
55
"math/big"
6-
7-
"github.com/formancehq/numscript/internal/utils"
86
)
97

108
type VirtualAccount struct {
@@ -94,7 +92,7 @@ func send(
9492

9593
switch dest := destination.(type) {
9694
case AccountAddress:
97-
return source.Pull(asset, nil, Sender{
95+
return source.Pull(asset, Sender{
9896
Account: dest,
9997
Amount: amount,
10098
Color: color,
@@ -149,11 +147,7 @@ func (vacc *VirtualAccount) PullCredits(asset string) []Sender {
149147
//
150148
// If the overdraft is higher than 0 or unbounded, is possible that the pulled amount is higher than the virtual account's credits.
151149
// In this case, we'll add the pulled amount to the virtual account's debts.
152-
func (vacc *VirtualAccount) Pull(asset string, overdraft *big.Int, receiver Sender) []Posting {
153-
if overdraft == nil {
154-
overdraft = new(big.Int).Set(receiver.Amount)
155-
}
156-
150+
func (vacc *VirtualAccount) Pull(asset string, receiver Sender) []Posting {
157151
credits := vacc.getCredits(asset)
158152
pulled := credits.PullColored(receiver.Amount, receiver.Color)
159153

@@ -173,15 +167,14 @@ func (vacc *VirtualAccount) Pull(asset string, overdraft *big.Int, receiver Send
173167
}
174168

175169
// TODO it looks like we aren't using overdraft now. How's that possible?
176-
allowedDebt := utils.MinBigInt(remainingAmt, overdraft)
177-
if allowedDebt.Cmp(big.NewInt(0)) == 1 {
170+
if remainingAmt.Cmp(big.NewInt(0)) == 1 {
178171
// If we didn't pull enough and we're allowed to overdraft,
179172
// push the amount to debts WITHOUT emitting the corresponding postings (yet)
180173
debits := vacc.getDebits(asset)
181174
debits.Push(Sender{
182175
Account: receiver.Account,
183176
Color: receiver.Color,
184-
Amount: allowedDebt,
177+
Amount: remainingAmt,
185178
})
186179
}
187180

internal/interpreter/virtual_account_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestVirtualAccountReceiveAndThenPull(t *testing.T) {
1919
})
2020
require.Empty(t, postings)
2121

22-
postings = vacc.Pull("USD", big.NewInt(0), interpreter.Sender{
22+
postings = vacc.Pull("USD", interpreter.Sender{
2323
Account: interpreter.AccountAddress("dest"),
2424
Amount: big.NewInt(10),
2525
})
@@ -42,7 +42,7 @@ func TestVirtualAccountReceiveAndThenPullPartialAmount(t *testing.T) {
4242
})
4343
require.Empty(t, postings)
4444

45-
postings = vacc.Pull("USD", big.NewInt(0), interpreter.Sender{
45+
postings = vacc.Pull("USD", interpreter.Sender{
4646
Account: interpreter.AccountAddress("dest"),
4747
Amount: big.NewInt(1), // <- we're only pulling 1 out of 10
4848
})
@@ -64,7 +64,7 @@ func TestVirtualAccountPullFirst(t *testing.T) {
6464
vacc := interpreter.NewVirtualAccount()
6565

6666
// Now we pull first. Note the unbounded overdraft
67-
postings := vacc.Pull("USD", nil, interpreter.Sender{
67+
postings := vacc.Pull("USD", interpreter.Sender{
6868
Account: interpreter.AccountAddress("dest"),
6969
Amount: big.NewInt(10),
7070
})
@@ -90,7 +90,7 @@ func TestVirtualAccountPullFirstMixed(t *testing.T) {
9090
vacc := interpreter.NewVirtualAccount()
9191

9292
// 1 USD of debt
93-
vacc.Pull("USD", nil, interpreter.Sender{
93+
vacc.Pull("USD", interpreter.Sender{
9494
Account: interpreter.AccountAddress("lender"),
9595
Amount: big.NewInt(1),
9696
})
@@ -110,7 +110,7 @@ func TestVirtualAccountPullFirstMixed(t *testing.T) {
110110
}, postings)
111111

112112
// pull the rest
113-
postings = vacc.Pull("USD", nil, interpreter.Sender{
113+
postings = vacc.Pull("USD", interpreter.Sender{
114114
Account: interpreter.AccountAddress("dest"),
115115
Amount: big.NewInt(100),
116116
})
@@ -156,7 +156,7 @@ func TestVirtualAccountTransitiveWhenNotOverdraft(t *testing.T) {
156156
require.Equal(t, []Posting{
157157
{"src", "dest", amt, "USD"},
158158
},
159-
v1.Pull("USD", nil, interpreter.Sender{
159+
v1.Pull("USD", interpreter.Sender{
160160
Account: interpreter.AccountAddress("dest"),
161161
Amount: amt,
162162
}))
@@ -188,7 +188,7 @@ func TestVirtualAccountTransitiveWhenOverdraft(t *testing.T) {
188188
// => [{@src, @dest, 10}]
189189
require.Equal(t, []Posting{
190190
{"src", "dest", amt, "USD"},
191-
}, v1.Pull("USD", nil, interpreter.Sender{
191+
}, v1.Pull("USD", interpreter.Sender{
192192
Account: interpreter.AccountAddress("dest"),
193193
Amount: amt,
194194
}))
@@ -212,7 +212,7 @@ func TestVirtualAccountTransitiveWhenOverdraftAndPayLast(t *testing.T) {
212212
}))
213213

214214
// $v1 -> @dest (10 USD)
215-
require.Empty(t, v1.Pull("USD", nil, interpreter.Sender{
215+
require.Empty(t, v1.Pull("USD", interpreter.Sender{
216216
Account: interpreter.AccountAddress("dest"),
217217
Amount: amt,
218218
}))
@@ -254,7 +254,7 @@ func TestVirtualAccountTransitiveTwoSteps(t *testing.T) {
254254
}))
255255

256256
// $v2 -> @dest
257-
require.Empty(t, v2.Pull("USD", nil, interpreter.Sender{
257+
require.Empty(t, v2.Pull("USD", interpreter.Sender{
258258
Account: interpreter.AccountAddress("dest"),
259259
Amount: amt,
260260
}))
@@ -304,7 +304,7 @@ func TestVirtualAccountTransitiveTwoStepsPayFirst(t *testing.T) {
304304
// $v2 -> @dest
305305
require.Equal(t, []Posting{
306306
{"src", "dest", amt, "USD"},
307-
}, v2.Pull("USD", nil, interpreter.Sender{
307+
}, v2.Pull("USD", interpreter.Sender{
308308
Account: interpreter.AccountAddress("dest"),
309309
Amount: amt,
310310
}))
@@ -345,7 +345,7 @@ func TestCommutativeOrder(t *testing.T) {
345345
})
346346
},
347347
func() []Posting {
348-
return v2.Pull("USD", nil, interpreter.Sender{
348+
return v2.Pull("USD", interpreter.Sender{
349349
Account: interpreter.AccountAddress("dest"),
350350
Amount: amt,
351351
})

0 commit comments

Comments
 (0)