sarafu-api/dev/api.go

188 lines
4.6 KiB
Go
Raw Normal View History

package dev
import (
"context"
"encoding/json"
"fmt"
"math/rand"
"strconv"
2025-01-12 18:14:13 +01:00
"time"
"github.com/gofrs/uuid"
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
2025-01-12 18:02:08 +01:00
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
)
const (
pubKeyLen int = 20
)
2025-01-12 18:14:13 +01:00
type tx struct {
hsh string
to string
from string
voucher string
value int
when time.Time
}
type account struct {
2025-01-12 18:02:08 +01:00
track string
nonce int
2025-01-12 18:02:08 +01:00
defaultVoucher string
balances map[string]int
2025-01-12 18:14:13 +01:00
txs []tx
}
2025-01-12 18:02:08 +01:00
type voucher struct {
2025-01-12 18:18:55 +01:00
name string
2025-01-12 18:02:08 +01:00
address string
symbol string
decimals int
2025-01-12 18:18:55 +01:00
sink string
commodity string
location string
2025-01-12 18:02:08 +01:00
}
var (
vouchers = make(map[string]voucher)
2025-01-12 18:18:55 +01:00
vouchersAddress = make(map[string]string)
2025-01-12 18:02:08 +01:00
)
type DevAccountService struct {
accounts map[string]account
accountsTrack map[string]string
// accountsSession map[string]string
}
func NewDevAccountService() *DevAccountService {
return &DevAccountService{
accounts: make(map[string]account),
accountsTrack: make(map[string]string),
//accountsSession: make(map[string]string),
}
}
func (das *DevAccountService) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResult, error) {
acc, ok := das.accounts[publicKey]
if !ok {
return nil, fmt.Errorf("account not found (publickey): %v", publicKey)
}
2025-01-12 18:02:08 +01:00
if acc.defaultVoucher == "" {
return nil, fmt.Errorf("no default voucher set for: %v", publicKey)
}
bal, ok := acc.balances[acc.defaultVoucher]
if !ok {
return nil, fmt.Errorf("balance not found for default token %s pubkey %v", acc.defaultVoucher, publicKey)
}
return &models.BalanceResult {
2025-01-12 18:02:08 +01:00
Balance: strconv.Itoa(bal),
Nonce: json.Number(strconv.Itoa(acc.nonce)),
}, nil
}
func (das *DevAccountService) CreateAccount(ctx context.Context) (*models.AccountResult, error) {
var b [pubKeyLen]byte
uid, err := uuid.NewV4()
if err != nil {
return nil, err
}
c, err := rand.Read(b[:])
if err != nil {
return nil, err
}
if c != pubKeyLen {
return nil, fmt.Errorf("short read: %d", c)
}
pubKey := fmt.Sprintf("0x%x", b)
2025-01-12 18:02:08 +01:00
das.accounts[pubKey] = account{
track: uid.String(),
}
das.accountsTrack[uid.String()] = pubKey
return &models.AccountResult{
PublicKey: pubKey,
TrackingId: uid.String(),
}, nil
}
2025-01-12 18:02:08 +01:00
func (das *DevAccountService) TrackAccountStatus(ctx context.Context, publicKey string) (*models.TrackStatusResult, error) {
var ok bool
_, ok = das.accounts[publicKey]
if !ok {
return nil, fmt.Errorf("account not found (publickey): %v", publicKey)
}
return &models.TrackStatusResult{
Active: true,
}, nil
}
func (das *DevAccountService) FetchVouchers(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
var holdings []dataserviceapi.TokenHoldings
acc, ok := das.accounts[publicKey]
if !ok {
return nil, fmt.Errorf("account not found (publickey): %v", publicKey)
}
for k, v := range(acc.balances) {
voucher, ok := vouchers[k]
if !ok {
return nil, fmt.Errorf("voucher has balance but object not found: %v", k)
}
holdings = append(holdings, dataserviceapi.TokenHoldings{
ContractAddress: voucher.address,
TokenSymbol: voucher.symbol,
TokenDecimals: strconv.Itoa(voucher.decimals),
Balance: strconv.Itoa(v),
})
}
return holdings, nil
}
2025-01-12 18:14:13 +01:00
func (das *DevAccountService) FetchTransactions(ctx context.Context, publicKey string) ([]dataserviceapi.Last10TxResponse, error) {
var lasttx []dataserviceapi.Last10TxResponse
acc, ok := das.accounts[publicKey]
if !ok {
return nil, fmt.Errorf("account not found (publickey): %v", publicKey)
}
for i, v := range(acc.txs) {
if i == 10 {
break
}
voucher, ok := vouchers[v.voucher]
if !ok {
return nil, fmt.Errorf("voucher %s in tx list but not found in voucher list", v.voucher)
}
lasttx = append(lasttx, dataserviceapi.Last10TxResponse{
Sender: v.from,
Recipient: v.to,
TransferValue: strconv.Itoa(v.value),
ContractAddress: voucher.address,
TxHash: v.hsh,
DateBlock: v.when,
TokenSymbol: voucher.symbol,
TokenDecimals: strconv.Itoa(voucher.decimals),
})
}
return lasttx, nil
}
2025-01-12 18:18:55 +01:00
func (das *DevAccountService) VoucherData(ctx context.Context, address string) (*models.VoucherDataResult, error) {
sym, ok := vouchersAddress[address]
if !ok {
return nil, fmt.Errorf("voucher address %v not found", address)
}
voucher, ok := vouchers[sym]
if !ok {
return nil, fmt.Errorf("voucher address %v found but does not resolve", address)
}
return &models.VoucherDataResult{
TokenName: voucher.name,
TokenSymbol: voucher.symbol,
TokenDecimals: voucher.decimals,
SinkAddress: voucher.sink,
TokenCommodity: voucher.commodity,
TokenLocation: voucher.location,
}, nil
}