Skip to content

Commit

Permalink
feat: upgrade sdk to use bigints
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Jul 18, 2023
1 parent 495a207 commit d601133
Show file tree
Hide file tree
Showing 41 changed files with 366 additions and 248 deletions.
6 changes: 4 additions & 2 deletions components/fctl/cmd/ledger/internal/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io"
"math/big"
"time"

fctl "github.com/formancehq/fctl/pkg"
Expand Down Expand Up @@ -69,9 +70,10 @@ func PrintExpandedTransaction(out io.Writer, transaction ExpandedTransaction) er
tableData = append(tableData, []string{"Account", "Asset", "Movement", "Final balance"})
for account, postCommitVolume := range transaction.PostCommitVolumes {
for asset, volumes := range postCommitVolume {
movement := *volumes.Balance - *(transaction.PreCommitVolumes)[account][asset].Balance
movement := big.NewInt(0)
movement = movement.Sub(volumes.Balance, (transaction.PreCommitVolumes)[account][asset].Balance)
movementStr := fmt.Sprint(movement)
if movement > 0 {
if movement.Cmp(big.NewInt(0)) > 0 {
movementStr = "+" + movementStr
}
tableData = append(tableData, []string{
Expand Down
3 changes: 2 additions & 1 deletion components/fctl/cmd/ledger/send.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ledger

import (
"math/big"
"strconv"

"github.com/formancehq/fctl/cmd/ledger/internal"
Expand Down Expand Up @@ -106,7 +107,7 @@ func (c *SendController) Run(cmd *cobra.Command, args []string) (fctl.Renderable
Metadata: metadata,
Postings: []shared.Posting{
{
Amount: amount,
Amount: big.NewInt(amount),
Asset: asset,
Destination: destination,
Source: source,
Expand Down
3 changes: 2 additions & 1 deletion components/fctl/cmd/wallets/credit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package wallets

import (
"fmt"
"math/big"
"strconv"

"github.com/formancehq/fctl/cmd/wallets/internal"
Expand Down Expand Up @@ -119,7 +120,7 @@ func (c *CreditWalletController) Run(cmd *cobra.Command, args []string) (fctl.Re
CreditWalletRequest: &shared.CreditWalletRequest{
Amount: shared.Monetary{
Asset: asset,
Amount: int64(amount),
Amount: big.NewInt(int64(amount)),
},
Metadata: metadata,
Sources: sources,
Expand Down
3 changes: 2 additions & 1 deletion components/fctl/cmd/wallets/debit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package wallets

import (
"fmt"
"math/big"
"strconv"

"github.com/formancehq/fctl/cmd/wallets/internal"
Expand Down Expand Up @@ -128,7 +129,7 @@ func (c *DebitWalletController) Run(cmd *cobra.Command, args []string) (fctl.Ren
DebitWalletRequest: &shared.DebitWalletRequest{
Amount: shared.Monetary{
Asset: asset,
Amount: amount,
Amount: big.NewInt(amount),
},
Pending: &pending,
Metadata: metadata,
Expand Down
3 changes: 2 additions & 1 deletion components/fctl/cmd/wallets/holds/confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package holds

import (
"fmt"
"math/big"

fctl "github.com/formancehq/fctl/pkg"
"github.com/formancehq/formance-sdk-go/pkg/models/operations"
Expand Down Expand Up @@ -78,7 +79,7 @@ func (c *ConfirmController) Run(cmd *cobra.Command, args []string) (fctl.Rendera
request := operations.ConfirmHoldRequest{
HoldID: args[0],
ConfirmHoldRequest: &shared.ConfirmHoldRequest{
Amount: &amount,
Amount: big.NewInt(amount),
Final: &final,
},
}
Expand Down
16 changes: 8 additions & 8 deletions components/ledger/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ paths:
description: Filter accounts by their balance (default operator is gte)
schema:
type: integer
format: int64
format: bigint
example: 2400
- name: balanceOperator
in: query
Expand Down Expand Up @@ -1070,14 +1070,14 @@ components:
type: object
additionalProperties:
type: integer
format: int64
format: bigint
minimum: 0
example: { COIN: { input: 100, output: 0 } }
balances:
type: object
additionalProperties:
type: integer
format: int64
format: bigint
example:
COIN: 100

Expand All @@ -1097,7 +1097,7 @@ components:
type: object
additionalProperties:
type: integer
format: int64
format: bigint
example:
USD: 100
EUR: 12
Expand All @@ -1107,7 +1107,7 @@ components:
properties:
amount:
type: integer
format: int64
format: bigint
minimum: 0
example: 100
asset:
Expand Down Expand Up @@ -1298,13 +1298,13 @@ components:
properties:
input:
type: integer
format: int64
format: bigint
output:
type: integer
format: int64
format: bigint
balance:
type: integer
format: int64
format: bigint
required:
- input
- output
Expand Down
30 changes: 30 additions & 0 deletions components/orchestration/internal/schema/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package schema

import (
"fmt"
"math/big"
"reflect"
"regexp"
"strconv"
Expand Down Expand Up @@ -40,6 +41,35 @@ func mapObjectField(ctx Context, raw any, spec reflect.Value, tag tag) error {
if raw == nil {
return nil
}
if _, isBigInt := spec.Interface().(*big.Int); isBigInt {
switch json := raw.(type) {
case string:
interpolated := interpolate(ctx, json)
if interpolated == "" {
interpolated = tag.defaultValue
}
bigIntValue, ok := big.NewInt(0).SetString(interpolated, 10)
if !ok {
return fmt.Errorf("unable to parse '%s' as big int", interpolated)
}
spec.Set(reflect.ValueOf(bigIntValue))
case float64:
spec.Set(reflect.ValueOf(big.NewInt(int64(json))))
case nil:
defaultValue := tag.defaultValue
if defaultValue == "" {
return nil
}
bigIntValue, ok := big.NewInt(0).SetString(defaultValue, 10)
if !ok {
panic(fmt.Errorf("unable to parse '%s' as big int", defaultValue))
}
spec.Set(reflect.ValueOf(bigIntValue))
default:
return fmt.Errorf("expected big int or interpolated string but was %T", json)
}
return nil
}
spec.Set(reflect.New(spec.Type().Elem()))
return mapObjectField(ctx, raw, spec.Elem(), tag)
case reflect.String:
Expand Down
15 changes: 10 additions & 5 deletions components/orchestration/internal/workflow/stages/send/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package send

import (
"fmt"
"math/big"
"strings"

"github.com/formancehq/formance-sdk-go/pkg/models/shared"
Expand Down Expand Up @@ -71,7 +72,7 @@ func runPaymentToWallet(ctx workflow.Context, source *PaymentSource, destination
}
if amount == nil {
amount = &shared.Monetary{
Amount: payment.InitialAmount,
Amount: big.NewInt(payment.InitialAmount),
Asset: payment.Asset,
}
}
Expand All @@ -94,7 +95,7 @@ func savePayment(ctx workflow.Context, paymentID string) (*shared.Payment, error
reference := paymentAccountName(paymentID)
_, err = activities.CreateTransaction(internal.InfiniteRetryContext(ctx), internalLedger, shared.PostTransaction{
Postings: []shared.Posting{{
Amount: payment.InitialAmount,
Amount: big.NewInt(payment.InitialAmount),
Asset: payment.Asset,
Destination: paymentAccountName(paymentID),
Source: "world",
Expand Down Expand Up @@ -122,7 +123,7 @@ func runPaymentToAccount(ctx workflow.Context, source *PaymentSource, destinatio
}
if amount == nil {
amount = &shared.Monetary{
Amount: payment.InitialAmount,
Amount: big.NewInt(payment.InitialAmount),
Asset: payment.Asset,
}
}
Expand Down Expand Up @@ -194,8 +195,10 @@ func runWalletToPayment(ctx workflow.Context, source *WalletSource, destination
if err != nil {
return err
}

v := amount.Amount.Int64()
if err := activities.StripeTransfer(internal.InfiniteRetryContext(ctx), shared.StripeTransferRequest{
Amount: &amount.Amount,
Amount: &v,
Asset: &amount.Asset,
Destination: &stripeConnectID,
}); err != nil {
Expand Down Expand Up @@ -358,8 +361,10 @@ func runAccountToPayment(ctx workflow.Context, source *LedgerAccountSource, dest
if err != nil {
return err
}

v := amount.Amount.Int64()
if err := activities.StripeTransfer(internal.InfiniteRetryContext(ctx), shared.StripeTransferRequest{
Amount: &amount.Amount,
Amount: &v,
Asset: &amount.Asset,
Destination: &stripeConnectID,
}); err != nil {
Expand Down
Loading

1 comment on commit d601133

@vercel
Copy link

@vercel vercel bot commented on d601133 Jul 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.