2022-04-28 03:54:56 +01:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package storjscan
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-06-17 00:29:31 +01:00
|
|
|
"encoding/json"
|
|
|
|
"time"
|
2022-04-28 03:54:56 +01:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
2022-06-01 22:59:47 +01:00
|
|
|
"go.uber.org/zap"
|
2022-04-28 03:54:56 +01:00
|
|
|
|
|
|
|
"storj.io/common/uuid"
|
|
|
|
"storj.io/storj/private/blockchain"
|
|
|
|
"storj.io/storj/satellite/payments"
|
2022-06-17 00:29:31 +01:00
|
|
|
"storj.io/storj/satellite/payments/billing"
|
2022-04-28 03:54:56 +01:00
|
|
|
)
|
|
|
|
|
2022-06-01 22:59:47 +01:00
|
|
|
// ErrService is storjscan service error class.
|
|
|
|
var ErrService = errs.Class("storjscan service")
|
2022-04-28 03:54:56 +01:00
|
|
|
|
2022-06-17 00:29:31 +01:00
|
|
|
type storjscanMetadata struct {
|
|
|
|
ReferenceID string
|
|
|
|
Wallet string
|
|
|
|
BlockNumber int64
|
|
|
|
LogIndex int
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensures that storjscan implements payments.DepositWallets.
|
2022-04-28 03:54:56 +01:00
|
|
|
var _ payments.DepositWallets = (*Service)(nil)
|
|
|
|
|
2022-06-17 00:29:31 +01:00
|
|
|
// ensures that storjscan implements billing.PaymentType.
|
|
|
|
var _ billing.PaymentType = (*Service)(nil)
|
|
|
|
|
2022-06-01 22:59:47 +01:00
|
|
|
// Service exposes API to interact with storjscan payments provider.
|
2022-04-28 03:54:56 +01:00
|
|
|
type Service struct {
|
2022-06-01 22:59:47 +01:00
|
|
|
log *zap.Logger
|
|
|
|
walletsDB WalletsDB
|
|
|
|
paymentsDB PaymentsDB
|
|
|
|
client *Client
|
2022-04-28 03:54:56 +01:00
|
|
|
}
|
|
|
|
|
2022-06-01 22:59:47 +01:00
|
|
|
// NewService creates new storjscan service instance.
|
|
|
|
func NewService(log *zap.Logger, walletsDB WalletsDB, paymentsDB PaymentsDB, client *Client) *Service {
|
2022-04-28 03:54:56 +01:00
|
|
|
return &Service{
|
2022-06-01 22:59:47 +01:00
|
|
|
log: log,
|
|
|
|
walletsDB: walletsDB,
|
|
|
|
paymentsDB: paymentsDB,
|
|
|
|
client: client,
|
|
|
|
}
|
2022-04-28 03:54:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Claim gets a new crypto wallet and associates it with a user.
|
|
|
|
func (service *Service) Claim(ctx context.Context, userID uuid.UUID) (_ blockchain.Address, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2022-08-15 15:41:19 +01:00
|
|
|
wallet, err := service.walletsDB.GetWallet(ctx, userID)
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
return wallet, nil
|
|
|
|
case errs.Is(err, billing.ErrNoWallet):
|
|
|
|
// do nothing and continue
|
|
|
|
default:
|
|
|
|
return blockchain.Address{}, err
|
|
|
|
}
|
|
|
|
|
2022-06-01 22:59:47 +01:00
|
|
|
address, err := service.client.ClaimNewEthAddress(ctx)
|
2022-04-28 03:54:56 +01:00
|
|
|
if err != nil {
|
2022-06-01 22:59:47 +01:00
|
|
|
return blockchain.Address{}, ErrService.Wrap(err)
|
2022-04-28 03:54:56 +01:00
|
|
|
}
|
|
|
|
err = service.walletsDB.Add(ctx, userID, address)
|
2022-06-06 13:42:45 +01:00
|
|
|
if err != nil {
|
2022-06-01 22:59:47 +01:00
|
|
|
return blockchain.Address{}, ErrService.Wrap(err)
|
2022-06-06 13:42:45 +01:00
|
|
|
}
|
2022-09-28 17:59:18 +01:00
|
|
|
service.log.Info("STORJ token wallet claimed",
|
|
|
|
zap.String("userid", userID.String()),
|
|
|
|
zap.String("wallet address", address.Hex()))
|
2022-06-06 13:42:45 +01:00
|
|
|
|
|
|
|
return address, nil
|
2022-04-28 03:54:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the crypto wallet address associated with the given user.
|
|
|
|
func (service *Service) Get(ctx context.Context, userID uuid.UUID) (_ blockchain.Address, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2022-06-17 00:29:31 +01:00
|
|
|
address, err := service.walletsDB.GetWallet(ctx, userID)
|
2022-06-01 22:59:47 +01:00
|
|
|
return address, ErrService.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Payments retrieves payments for specific wallet.
|
|
|
|
func (service *Service) Payments(ctx context.Context, wallet blockchain.Address, limit int, offset int64) (_ []payments.WalletPayment, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
cachedPayments, err := service.paymentsDB.ListWallet(ctx, wallet, limit, offset)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrService.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var walletPayments []payments.WalletPayment
|
|
|
|
for _, pmnt := range cachedPayments {
|
|
|
|
walletPayments = append(walletPayments, payments.WalletPayment{
|
|
|
|
From: pmnt.From,
|
|
|
|
To: pmnt.To,
|
|
|
|
TokenValue: pmnt.TokenValue,
|
2022-06-16 14:26:27 +01:00
|
|
|
USDValue: pmnt.USDValue,
|
2022-06-01 22:59:47 +01:00
|
|
|
Status: pmnt.Status,
|
|
|
|
BlockHash: pmnt.BlockHash,
|
|
|
|
BlockNumber: pmnt.BlockNumber,
|
|
|
|
Transaction: pmnt.Transaction,
|
|
|
|
LogIndex: pmnt.LogIndex,
|
|
|
|
Timestamp: pmnt.Timestamp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return walletPayments, nil
|
2022-04-28 03:54:56 +01:00
|
|
|
}
|
2022-06-17 00:29:31 +01:00
|
|
|
|
|
|
|
// Source defines the billing transaction source for storjscan payments.
|
|
|
|
func (service *Service) Source() string {
|
2023-03-28 02:42:26 +01:00
|
|
|
return billing.StorjScanSource
|
2022-06-17 00:29:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Type defines the billing transaction type for storjscan payments.
|
|
|
|
func (service *Service) Type() billing.TransactionType {
|
|
|
|
return billing.TransactionTypeCredit
|
|
|
|
}
|
|
|
|
|
2022-08-15 15:41:19 +01:00
|
|
|
// GetNewTransactions returns the storjscan payments since the given block number and index as billing transactions type.
|
2022-06-17 00:29:31 +01:00
|
|
|
func (service *Service) GetNewTransactions(ctx context.Context, _ time.Time, lastPaymentMetadata []byte) ([]billing.Transaction, error) {
|
|
|
|
|
|
|
|
var latestMetadata storjscanMetadata
|
2022-08-15 15:41:19 +01:00
|
|
|
if lastPaymentMetadata == nil {
|
|
|
|
latestMetadata = storjscanMetadata{}
|
|
|
|
} else if err := json.Unmarshal(lastPaymentMetadata, &latestMetadata); err != nil {
|
2022-06-17 00:29:31 +01:00
|
|
|
service.log.Error("error retrieving metadata from latest recorded billing payment")
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
newCachedPayments, err := service.paymentsDB.ListConfirmed(ctx, latestMetadata.BlockNumber, latestMetadata.LogIndex)
|
|
|
|
if err != nil {
|
|
|
|
return []billing.Transaction{}, ErrService.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var billingTXs []billing.Transaction
|
|
|
|
for _, payment := range newCachedPayments {
|
|
|
|
userID, err := service.walletsDB.GetUser(ctx, payment.To)
|
|
|
|
if err != nil {
|
|
|
|
service.log.Error("error retrieving user ID associated with wallet", zap.String("Wallet Address", payment.To.Hex()))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata, err := json.Marshal(storjscanMetadata{
|
|
|
|
ReferenceID: payment.Transaction.Hex(),
|
|
|
|
Wallet: payment.To.Hex(),
|
|
|
|
BlockNumber: payment.BlockNumber,
|
|
|
|
LogIndex: payment.LogIndex,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
service.log.Error("error populating metadata", zap.String("Reference ID", payment.Transaction.Hex()), zap.String("Wallet", payment.To.Hex()))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
billingTXs = append(billingTXs, billing.Transaction{
|
|
|
|
UserID: userID,
|
|
|
|
Amount: payment.USDValue,
|
|
|
|
Description: "Storj token deposit",
|
|
|
|
Source: service.Source(),
|
|
|
|
Status: payments.PaymentStatusConfirmed,
|
|
|
|
Type: service.Type(),
|
|
|
|
Metadata: metadata,
|
|
|
|
Timestamp: payment.Timestamp,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return billingTXs, nil
|
|
|
|
}
|