Add auto disperse capability in devapi

This commit is contained in:
lash 2025-01-12 18:11:20 +00:00
parent 14d9a66cfe
commit 174093e271
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 46 additions and 0 deletions

View File

@ -9,14 +9,20 @@ import (
"time"
"github.com/gofrs/uuid"
"git.defalsify.org/vise.git/logging"
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
)
var (
logg = logging.NewVanilla().WithDomain("sarafu-api.devapi")
)
const (
pubKeyLen int = 20
hashLen int = 32
defaultDecimals = 6
zeroAccount string = "0x0000000000000000000000000000000000000000"
)
type tx struct {
@ -57,6 +63,9 @@ type DevAccountService struct {
txs map[string]tx
txsTrack map[string]string
toAutoCreate bool
autoVouchers []string
autoVoucherValue map[string]int
defaultAccount string
// accountsSession map[string]string
}
@ -69,9 +78,21 @@ func NewDevAccountService() *DevAccountService {
vouchersAddress: make(map[string]string),
txs: make(map[string]tx),
txsTrack: make(map[string]string),
autoVoucherValue: make(map[string]int),
}
}
func (das *DevAccountService) WithAutoVoucher(ctx context.Context, v voucher, value int) *DevAccountService {
err := das.AddVoucher(v)
if err != nil {
logg.ErrorCtxf(ctx, "cannot add autovoucher %s: %v", v, err)
return das
}
das.autoVouchers = append(das.autoVouchers, v.symbol)
das.autoVoucherValue[v.symbol] = value
return das
}
func (das *DevAccountService) AddVoucher(v voucher) error {
if v.symbol == "" {
return fmt.Errorf("cannot add empty sym voucher")
@ -105,6 +126,23 @@ func (das *DevAccountService) CheckBalance(ctx context.Context, publicKey string
}, nil
}
func (das *DevAccountService) balanceAuto(ctx context.Context, pubKey string) error {
for _, v := range(das.autoVouchers) {
voucher, ok := das.vouchers[v]
if !ok {
return fmt.Errorf("balance auto voucher set but not resolved: %s", v)
}
value, ok := das.autoVoucherValue[v]
if !ok {
value = 0
}
_, err := das.TokenTransfer(ctx, strconv.Itoa(value), das.defaultAccount, pubKey, voucher.address)
if err != nil {
return err
}
}
return nil
}
func (das *DevAccountService) CreateAccount(ctx context.Context) (*models.AccountResult, error) {
var b [pubKeyLen]byte
@ -125,6 +163,11 @@ func (das *DevAccountService) CreateAccount(ctx context.Context) (*models.Accoun
address: pubKey,
}
das.accountsTrack[uid.String()] = pubKey
das.balanceAuto(ctx, pubKey)
if das.defaultAccount == zeroAccount {
das.defaultAccount = pubKey
}
return &models.AccountResult{
PublicKey: pubKey,
TrackingId: uid.String(),

1
go.mod
View File

@ -3,6 +3,7 @@ module git.grassecon.net/grassrootseconomics/sarafu-api
go 1.23.4
require (
git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250112121325-9e4c65c8b4d1
github.com/gofrs/uuid v4.4.0+incompatible
github.com/grassrootseconomics/eth-custodial v1.3.0-beta

2
go.sum
View File

@ -1,3 +1,5 @@
git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d h1:bPAOVZOX4frSGhfOdcj7kc555f8dc9DmMd2YAyC2AMw=
git.defalsify.org/vise.git v0.2.3-0.20250103172917-3e190a44568d/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck=
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250112121325-9e4c65c8b4d1 h1:RfU5/WFfPxDptlkyx4MT+4YmO79sY6HvIngUq5uwQPU=
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250112121325-9e4c65c8b4d1/go.mod h1:E6W7ZOa7ZvVr0Bc5ot0LNSwpSPYq4hXlAIvEPy3AJ7U=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=