storj/satellite/payments/storjscan/service.go
Yaroslav Vorobiov 6d95e34e39 satellite/payments: add payments method for deposit wallets
Add payments method to payments to DepositWallets interface.
Exposes payments retrieval API for a particular wallet to
other systems such as console billing API.

Change-Id: Ifcb3a35514aab50be00f6360007954980b5d8b38
2022-07-12 11:25:30 +00:00

91 lines
2.5 KiB
Go

// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package storjscan
import (
"context"
"github.com/zeebo/errs"
"go.uber.org/zap"
"storj.io/common/uuid"
"storj.io/storj/private/blockchain"
"storj.io/storj/satellite/payments"
)
// ErrService is storjscan service error class.
var ErrService = errs.Class("storjscan service")
// ensures that Wallets implements payments.Wallets.
var _ payments.DepositWallets = (*Service)(nil)
// Service exposes API to interact with storjscan payments provider.
type Service struct {
log *zap.Logger
walletsDB WalletsDB
paymentsDB PaymentsDB
client *Client
}
// NewService creates new storjscan service instance.
func NewService(log *zap.Logger, walletsDB WalletsDB, paymentsDB PaymentsDB, client *Client) *Service {
return &Service{
log: log,
walletsDB: walletsDB,
paymentsDB: paymentsDB,
client: client,
}
}
// 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)
address, err := service.client.ClaimNewEthAddress(ctx)
if err != nil {
return blockchain.Address{}, ErrService.Wrap(err)
}
err = service.walletsDB.Add(ctx, userID, address)
if err != nil {
return blockchain.Address{}, ErrService.Wrap(err)
}
return address, nil
}
// 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)
address, err := service.walletsDB.Get(ctx, userID)
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,
Status: pmnt.Status,
BlockHash: pmnt.BlockHash,
BlockNumber: pmnt.BlockNumber,
Transaction: pmnt.Transaction,
LogIndex: pmnt.LogIndex,
Timestamp: pmnt.Timestamp,
})
}
return walletPayments, nil
}