-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
transaction.go
55 lines (48 loc) · 1.27 KB
/
transaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package internal
import (
"context"
"errors"
"fmt"
"math/big"
"strconv"
"strings"
formance "github.com/formancehq/formance-sdk-go/v2"
"github.com/formancehq/formance-sdk-go/v2/pkg/models/operations"
)
func TransactionIDOrLastN(ctx context.Context, ledgerClient *formance.Formance, ledger, id string) (*big.Int, error) {
if strings.HasPrefix(id, "last") {
id = strings.TrimPrefix(id, "last")
sub := int64(0)
if id != "" {
var err error
sub, err = strconv.ParseInt(id, 10, 64)
if err != nil {
return nil, err
}
}
pageSize := int64(1)
request := operations.ListTransactionsRequest{
Ledger: ledger,
PageSize: &pageSize,
}
response, err := ledgerClient.Ledger.ListTransactions(ctx, request)
if err != nil {
return nil, err
}
if response.StatusCode >= 300 {
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
}
if len(response.TransactionsCursorResponse.Cursor.Data) == 0 {
return nil, errors.New("no transaction found")
}
return response.TransactionsCursorResponse.Cursor.Data[0].Txid.Sub(
response.TransactionsCursorResponse.Cursor.Data[0].Txid,
big.NewInt(sub),
), nil
}
v, ok := big.NewInt(0).SetString(id, 10)
if !ok {
return nil, fmt.Errorf("invalid bigint: %s", id)
}
return v, nil
}