mirror of
https://github.com/hanzo-fi/stack.git
synced 2026-10-30 20:34:43 +00:00
54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package wallet
|
|
|
|
import (
|
|
"math/big"
|
|
"net/http"
|
|
|
|
"github.com/formancehq/ledger/pkg/core/assets"
|
|
|
|
"github.com/formancehq/go-libs/time"
|
|
|
|
"github.com/formancehq/go-libs/metadata"
|
|
)
|
|
|
|
var DefaultCreditSource = NewLedgerAccountSubject("world")
|
|
|
|
type CreditRequest struct {
|
|
Amount Monetary `json:"amount"`
|
|
Metadata metadata.Metadata `json:"metadata"`
|
|
Sources Subjects `json:"sources"`
|
|
Reference string `json:"reference"`
|
|
Balance string `json:"balance"`
|
|
Timestamp *time.Time `json:"timestamp"`
|
|
}
|
|
|
|
func (c *CreditRequest) Bind(r *http.Request) error {
|
|
return nil
|
|
}
|
|
|
|
func (c CreditRequest) Validate() error {
|
|
if err := c.Sources.Validate(); err != nil {
|
|
return err
|
|
}
|
|
if c.Amount.Amount.Cmp(big.NewInt(0)) < 0 {
|
|
return ErrNegativeAmount
|
|
}
|
|
if !assets.IsValid(c.Amount.Asset) {
|
|
return newErrInvalidAsset(c.Amount.Asset)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type Credit struct {
|
|
CreditRequest
|
|
WalletID string `json:"walletID"`
|
|
}
|
|
|
|
func (c Credit) destinationAccount(chart *Chart) string {
|
|
if c.Balance == "" {
|
|
return chart.GetMainBalanceAccount(c.WalletID)
|
|
}
|
|
return chart.GetBalanceAccount(c.WalletID, c.Balance)
|
|
}
|