Skip to content

Feat/virtual account #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions internal/analysis/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const FnVarOriginBalance = "balance"
const FnVarOriginOverdraft = "overdraft"
const FnVarOriginGetAsset = "get_asset"
const FnVarOriginGetAmount = "get_amount"
const FnVarOriginVirtual = "virtual"

var Builtins = map[string]FnCallResolution{
FnSetTxMeta: StatementFnCallResolution{
Expand Down Expand Up @@ -114,6 +115,18 @@ var Builtins = map[string]FnCallResolution{
},
},
},
FnVarOriginVirtual: VarOriginFnCallResolution{
Params: []string{},
Return: TypeAccount,
Docs: "create a virtual account",
VersionConstraints: []VersionClause{
{
// TODO flag
Version: parser.NewVersionInterpreter(0, 0, 17),
// FeatureFlag: flags.ExperimentalGetAmountFunctionFeatureFlag,
},
},
},
}

type Diagnostic struct {
Expand Down
8 changes: 4 additions & 4 deletions internal/interpreter/args_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestParseValid(t *testing.T) {

p := NewArgsParser([]Value{
NewMonetaryInt(42),
AccountAddress("user:001"),
Account{Repr: AccountAddress("user:001")},
})
a1 := parseArg(p, parser.Range{}, expectNumber)
a2 := parseArg(p, parser.Range{}, expectAccount)
Expand All @@ -47,16 +47,16 @@ func TestParseValid(t *testing.T) {
require.NotNil(t, a1, "a1 should not be nil")
require.NotNil(t, a2, "a2 should not be nil")

require.Equal(t, *a1, *big.NewInt(42))
require.Equal(t, *a2, "user:001")
require.Equal(t, *big.NewInt(42), *a1)
require.Equal(t, Account{AccountAddress("user:001")}, *a2)
}

func TestParseBadType(t *testing.T) {
t.Parallel()

p := NewArgsParser([]Value{
NewMonetaryInt(42),
AccountAddress("user:001"),
Account{Repr: AccountAddress("user:001")},
})
parseArg(p, parser.Range{}, expectMonetary)
parseArg(p, parser.Range{}, expectAccount)
Expand Down
16 changes: 13 additions & 3 deletions internal/interpreter/batch_balances_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ func (st *programState) findBalancesQueriesInStatement(statement parser.Statemen
if err != nil {
return err
}
st.batchQuery(*account, *asset, nil)

if account, ok := account.Repr.(AccountAddress); ok {
st.batchQuery(string(account), *asset, nil)
}

return nil

case *parser.SendStatement:
Expand Down Expand Up @@ -95,7 +99,10 @@ func (st *programState) findBalancesQueries(source parser.Source) InterpreterErr
return err
}

st.batchQuery(*account, st.CurrentAsset, color)
if account, ok := account.Repr.(AccountAddress); ok {
st.batchQuery(string(account), st.CurrentAsset, color)
}

return nil

case *parser.SourceOverdraft:
Expand All @@ -113,7 +120,10 @@ func (st *programState) findBalancesQueries(source parser.Source) InterpreterErr
return err
}

st.batchQuery(*account, st.CurrentAsset, color)
if account, ok := account.Repr.(AccountAddress); ok {
st.batchQuery(string(account), st.CurrentAsset, color)
}

return nil

case *parser.SourceInorder:
Expand Down
2 changes: 1 addition & 1 deletion internal/interpreter/evaluate_expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (st *programState) divOp(rng parser.Range, left parser.ValueExpr, right par

func castToString(v Value, rng parser.Range) (string, InterpreterError) {
switch v := v.(type) {
case AccountAddress:
case Account:
return v.String(), nil
case String:
return v.String(), nil
Expand Down
12 changes: 8 additions & 4 deletions internal/interpreter/function_exprs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func overdraft(

// TODO more precise args range location
p := NewArgsParser(args)
account := parseArg(p, r, expectAccount)
account := parseArg(p, r, expectAccountAddress) // TODO also handle virtual account
asset := parseArg(p, r, expectAsset)
err = p.parse()
if err != nil {
Expand Down Expand Up @@ -53,7 +53,7 @@ func meta(
) (string, InterpreterError) {
// TODO more precise location
p := NewArgsParser(args)
account := parseArg(p, rng, expectAccount)
account := parseArg(p, rng, expectAccountAddress)
key := parseArg(p, rng, expectString)
err := p.parse()
if err != nil {
Expand Down Expand Up @@ -86,7 +86,7 @@ func balance(
) (*Monetary, InterpreterError) {
// TODO more precise args range location
p := NewArgsParser(args)
account := parseArg(p, r, expectAccount)
account := parseArg(p, r, expectAccountAddress)
asset := parseArg(p, r, expectAsset)
err := p.parse()
if err != nil {
Expand All @@ -102,7 +102,7 @@ func balance(

if balance.Cmp(big.NewInt(0)) == -1 {
return nil, NegativeBalanceError{
Account: *account,
Account: Account{AccountAddress(*account)},
Amount: *balance,
}
}
Expand Down Expand Up @@ -155,3 +155,7 @@ func getAmount(

return mon.Amount, nil
}

func virtual() Value {
return Account{Repr: NewVirtualAccount()}
}
2 changes: 1 addition & 1 deletion internal/interpreter/function_statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func setTxMeta(st *programState, r parser.Range, args []Value) InterpreterError

func setAccountMeta(st *programState, r parser.Range, args []Value) InterpreterError {
p := NewArgsParser(args)
account := parseArg(p, r, expectAccount)
account := parseArg(p, r, expectAccountAddress)
key := parseArg(p, r, expectString)
meta := parseArg(p, r, expectAnything)
err := p.parse()
Expand Down
68 changes: 55 additions & 13 deletions internal/interpreter/funds_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
)

type Sender struct {
Name string
Amount *big.Int
Color string
Account AccountValue
Amount *big.Int
Color string
}

type stack[T any] struct {
Expand Down Expand Up @@ -76,15 +76,15 @@
continue
}

if first.Name != second.Name || first.Color != second.Color {
if first.Account != second.Account || first.Color != second.Color {
return
}

s.senders = &stack[Sender]{
Head: Sender{
Name: first.Name,
Color: first.Color,
Amount: new(big.Int).Add(first.Amount, second.Amount),
Account: first.Account,
Color: first.Color,
Amount: new(big.Int).Add(first.Amount, second.Amount),
},
Tail: s.senders.Tail.Tail,
}
Expand Down Expand Up @@ -152,19 +152,19 @@
case 1: // more than enough
s.senders = &stack[Sender]{
Head: Sender{
Name: available.Name,
Color: available.Color,
Amount: new(big.Int).Sub(available.Amount, requiredAmount),
Account: available.Account,
Color: available.Color,
Amount: new(big.Int).Sub(available.Amount, requiredAmount),
},
Tail: s.senders,
}
fallthrough

case 0: // exactly the same
out = append(out, Sender{
Name: available.Name,
Color: available.Color,
Amount: new(big.Int).Set(requiredAmount),
Account: available.Account,
Color: available.Color,
Amount: new(big.Int).Set(requiredAmount),
})
return out
}
Expand All @@ -186,3 +186,45 @@

return fs
}

// Treat this stack as debts and use the sender to repay debt.
// Return the sender updated with the left amt (and the emitted postings)
func (s *fundsStack) RepayWithSender(asset string, credit Sender) ([]Posting, Sender) {
// clone the amount so that we can modify it
credit.Amount = new(big.Int).Set(credit.Amount)

var postings []Posting

// Take away the debt that the credit allows for
clearedDebt := s.PullColored(credit.Amount, credit.Color)
for _, receiver := range clearedDebt {
switch creditAccount := credit.Account.(type) {
case VirtualAccount:

// pulled := creditAccount.Pull(asset, nil, credit)
// fmt.Printf("PULLED: %#v", credit)

panic("TODO handle vacc in credit scenario")

Check warning on line 207 in internal/interpreter/funds_stack.go

View check run for this annotation

Codecov / codecov/patch

internal/interpreter/funds_stack.go#L202-L207

Added lines #L202 - L207 were not covered by tests

case AccountAddress:
credit.Amount.Sub(credit.Amount, receiver.Amount)

switch receiverAccount := receiver.Account.(type) {
case AccountAddress:
postings = append(postings, Posting{
Source: string(creditAccount),
Destination: string(receiverAccount),
Amount: receiver.Amount,
Asset: coloredAsset(asset, &credit.Color),
})

case VirtualAccount:
panic("TODO repay vacc")

Check warning on line 222 in internal/interpreter/funds_stack.go

View check run for this annotation

Codecov / codecov/patch

internal/interpreter/funds_stack.go#L221-L222

Added lines #L221 - L222 were not covered by tests
}
}

}

return postings, credit

}
Loading