Skip to content

Commit

Permalink
fix: negative balances not allowed even with mapping defined (#425)
Browse files Browse the repository at this point in the history
* fix: negative balances not allowed even with mapping defined

* fix: keep old error messages for backward compatibility
  • Loading branch information
gfyrag authored Jan 31, 2023
1 parent 2e108d8 commit 871d69e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
65 changes: 56 additions & 9 deletions pkg/api/controllers/transaction_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,9 @@ func TestPostTransactions(t *testing.T) {
expectedStatusCode: http.StatusBadRequest,
expectedErr: sharedapi.ErrorResponse{
ErrorCode: apierrors.ErrInsufficientFund,
ErrorMessage: "[INSUFFICIENT_FUND] account had insufficient funds",
Details: apierrors.EncodeLink("account had insufficient funds"),
ErrorMessage: "balance.insufficient.TOK",
ErrorCodeDeprecated: apierrors.ErrInsufficientFund,
ErrorMessageDeprecated: "[INSUFFICIENT_FUND] account had insufficient funds",
ErrorMessageDeprecated: "balance.insufficient.TOK",
},
},
{
Expand Down Expand Up @@ -593,11 +592,61 @@ func TestPostTransactions(t *testing.T) {
ErrorMessageDeprecated: "cannot pass a timestamp prior to the last transaction:",
},
},
{
name: "mapping with postings",
payload: []controllers.PostTransaction{
{
Postings: core.Postings{
{
Source: "negativebalances:bar",
Destination: "world",
Amount: core.NewMonetaryInt(1000),
Asset: "TOK",
},
},
Timestamp: timestamp3,
},
},
expectedStatusCode: http.StatusOK,
expectedRes: sharedapi.BaseResponse[[]core.ExpandedTransaction]{
Data: &[]core.ExpandedTransaction{{
Transaction: core.Transaction{
TransactionData: core.TransactionData{
Postings: core.Postings{
{
Source: "negativebalances:bar",
Destination: "world",
Amount: core.NewMonetaryInt(1000),
Asset: "TOK",
},
},
Timestamp: timestamp3,
},
},
}},
},
},
}

internal.RunTest(t, fx.Invoke(func(lc fx.Lifecycle, api *api.API) {
lc.Append(fx.Hook{
OnStart: func(ctx context.Context) error {
internal.SaveMapping(t, api, core.Mapping{
Contracts: []core.Contract{{
Name: "negative balances",
Account: "negativebalances:*",
Expr: core.ExprOr{
&core.ExprGte{
Op1: core.VariableExpr{Name: "balance"},
Op2: core.ConstantExpr{Value: 0},
},
&core.ExprLte{
Op1: core.VariableExpr{Name: "balance"},
Op2: core.ConstantExpr{Value: 0},
},
},
}},
})
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for i := 0; i < len(tc.payload)-1; i++ {
Expand Down Expand Up @@ -2003,10 +2052,9 @@ func TestPostTransactionsBatch(t *testing.T) {
internal.Decode(t, rsp.Body, &err)
require.EqualValues(t, sharedapi.ErrorResponse{
ErrorCode: apierrors.ErrInsufficientFund,
ErrorMessage: "[INSUFFICIENT_FUND] account had insufficient funds",
Details: apierrors.EncodeLink("account had insufficient funds"),
ErrorMessage: "balance.insufficient.COIN",
ErrorCodeDeprecated: apierrors.ErrInsufficientFund,
ErrorMessageDeprecated: "[INSUFFICIENT_FUND] account had insufficient funds",
ErrorMessageDeprecated: "balance.insufficient.COIN",
}, err)
})

Expand Down Expand Up @@ -2053,10 +2101,9 @@ func TestPostTransactionsBatch(t *testing.T) {
internal.Decode(t, rsp.Body, &err)
require.EqualValues(t, sharedapi.ErrorResponse{
ErrorCode: apierrors.ErrInsufficientFund,
ErrorMessage: "[INSUFFICIENT_FUND] account had insufficient funds",
Details: apierrors.EncodeLink("account had insufficient funds"),
ErrorMessage: "balance.insufficient.GEM",
ErrorCodeDeprecated: apierrors.ErrInsufficientFund,
ErrorMessageDeprecated: "[INSUFFICIENT_FUND] account had insufficient funds",
ErrorMessageDeprecated: "balance.insufficient.GEM",
}, err)
})

Expand Down
2 changes: 1 addition & 1 deletion pkg/core/numscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TxsToScriptsData(txsData ...TransactionData) []ScriptData {
if !ok {
panic(fmt.Sprintf("source %s not found", p.Source))
}
sb.WriteString(fmt.Sprintf("\tsource = $%s\n", src.name))
sb.WriteString(fmt.Sprintf("\tsource = $%s allowing unbounded overdraft\n", src.name))
}
if p.Destination == WORLD {
sb.WriteString("\tdestination = @world\n")
Expand Down

0 comments on commit 871d69e

Please sign in to comment.