-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
48 lines (38 loc) · 1.34 KB
/
service.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
package accounts
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/liviudnicoara/egopay/pkg/convert"
"github.com/pkg/errors"
)
type AccountService interface {
CreateAccount(password string) (string, error)
GetAccount(address string, password string) (Account, error)
GetBalance(ctx context.Context, address string) (big.Float, error)
}
type accountService struct {
accountRepository AccountRepository
client *ethclient.Client
}
func NewAccountService(accountRepository AccountRepository, client *ethclient.Client) AccountService {
return &accountService{
accountRepository: accountRepository,
client: client,
}
}
func (s *accountService) CreateAccount(password string) (string, error) {
return s.accountRepository.CreateAccount(password)
}
func (s *accountService) GetAccount(address string, password string) (Account, error) {
return s.accountRepository.GetAccount(address, password)
}
func (s *accountService) GetBalance(ctx context.Context, address string) (big.Float, error) {
addr := common.HexToAddress(address)
balance, err := s.client.BalanceAt(ctx, addr, nil)
if err != nil {
return big.Float{}, errors.WithMessagef(err, "could not retrieve balance for address %s", address)
}
return convert.ConvertWEItoETH(balance), nil
}