Enable autovoucher

This commit is contained in:
lash 2025-01-12 18:35:10 +00:00
parent 174093e271
commit ce308ef0c2
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746

View File

@ -2,6 +2,7 @@ package dev
import ( import (
"context" "context"
"crypto/sha1"
"encoding/json" "encoding/json"
"fmt" "fmt"
"math/rand" "math/rand"
@ -82,27 +83,36 @@ func NewDevAccountService() *DevAccountService {
} }
} }
func (das *DevAccountService) WithAutoVoucher(ctx context.Context, v voucher, value int) *DevAccountService { func (das *DevAccountService) WithAutoVoucher(ctx context.Context, symbol string, value int) *DevAccountService {
err := das.AddVoucher(v) err := das.AddVoucher(ctx, symbol)
if err != nil { if err != nil {
logg.ErrorCtxf(ctx, "cannot add autovoucher %s: %v", v, err) logg.ErrorCtxf(ctx, "cannot add autovoucher %s: %v", symbol, err)
return das return das
} }
das.autoVouchers = append(das.autoVouchers, v.symbol) das.autoVouchers = append(das.autoVouchers, symbol)
das.autoVoucherValue[v.symbol] = value das.autoVoucherValue[symbol] = value
return das return das
} }
func (das *DevAccountService) AddVoucher(v voucher) error { func (das *DevAccountService) AddVoucher(ctx context.Context, symbol string) error {
if v.symbol == "" { if symbol == "" {
return fmt.Errorf("cannot add empty sym voucher") return fmt.Errorf("cannot add empty sym voucher")
} }
v, ok := das.vouchers[v.symbol] v, ok := das.vouchers[symbol]
if ok { if ok {
return fmt.Errorf("already have voucher with symbol %s", v.symbol) return fmt.Errorf("already have voucher with symbol %s", v.symbol)
} }
das.vouchers[v.symbol] = v h := sha1.New()
das.vouchersAddress[v.address] = v.symbol h.Write([]byte(symbol))
z := h.Sum(nil)
address := fmt.Sprintf("0x%x", z)
das.vouchers[symbol] = voucher{
name: symbol,
symbol: symbol,
address: address,
}
das.vouchersAddress[address] = symbol
logg.InfoCtxf(ctx, "added dev voucher", "symbol", symbol, "address", address)
return nil return nil
} }