Add auto disperse capability in devapi
This commit is contained in:
parent
14d9a66cfe
commit
174093e271
43
dev/api.go
43
dev/api.go
@ -9,14 +9,20 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gofrs/uuid"
|
"github.com/gofrs/uuid"
|
||||||
|
"git.defalsify.org/vise.git/logging"
|
||||||
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
||||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
logg = logging.NewVanilla().WithDomain("sarafu-api.devapi")
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
pubKeyLen int = 20
|
pubKeyLen int = 20
|
||||||
hashLen int = 32
|
hashLen int = 32
|
||||||
defaultDecimals = 6
|
defaultDecimals = 6
|
||||||
|
zeroAccount string = "0x0000000000000000000000000000000000000000"
|
||||||
)
|
)
|
||||||
|
|
||||||
type tx struct {
|
type tx struct {
|
||||||
@ -57,6 +63,9 @@ type DevAccountService struct {
|
|||||||
txs map[string]tx
|
txs map[string]tx
|
||||||
txsTrack map[string]string
|
txsTrack map[string]string
|
||||||
toAutoCreate bool
|
toAutoCreate bool
|
||||||
|
autoVouchers []string
|
||||||
|
autoVoucherValue map[string]int
|
||||||
|
defaultAccount string
|
||||||
// accountsSession map[string]string
|
// accountsSession map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,9 +78,21 @@ func NewDevAccountService() *DevAccountService {
|
|||||||
vouchersAddress: make(map[string]string),
|
vouchersAddress: make(map[string]string),
|
||||||
txs: make(map[string]tx),
|
txs: make(map[string]tx),
|
||||||
txsTrack: make(map[string]string),
|
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 {
|
func (das *DevAccountService) AddVoucher(v voucher) error {
|
||||||
if v.symbol == "" {
|
if v.symbol == "" {
|
||||||
return fmt.Errorf("cannot add empty sym voucher")
|
return fmt.Errorf("cannot add empty sym voucher")
|
||||||
@ -105,6 +126,23 @@ func (das *DevAccountService) CheckBalance(ctx context.Context, publicKey string
|
|||||||
}, nil
|
}, 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) {
|
func (das *DevAccountService) CreateAccount(ctx context.Context) (*models.AccountResult, error) {
|
||||||
var b [pubKeyLen]byte
|
var b [pubKeyLen]byte
|
||||||
@ -125,6 +163,11 @@ func (das *DevAccountService) CreateAccount(ctx context.Context) (*models.Accoun
|
|||||||
address: pubKey,
|
address: pubKey,
|
||||||
}
|
}
|
||||||
das.accountsTrack[uid.String()] = pubKey
|
das.accountsTrack[uid.String()] = pubKey
|
||||||
|
das.balanceAuto(ctx, pubKey)
|
||||||
|
|
||||||
|
if das.defaultAccount == zeroAccount {
|
||||||
|
das.defaultAccount = pubKey
|
||||||
|
}
|
||||||
return &models.AccountResult{
|
return &models.AccountResult{
|
||||||
PublicKey: pubKey,
|
PublicKey: pubKey,
|
||||||
TrackingId: uid.String(),
|
TrackingId: uid.String(),
|
||||||
|
1
go.mod
1
go.mod
@ -3,6 +3,7 @@ module git.grassecon.net/grassrootseconomics/sarafu-api
|
|||||||
go 1.23.4
|
go 1.23.4
|
||||||
|
|
||||||
require (
|
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
|
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250112121325-9e4c65c8b4d1
|
||||||
github.com/gofrs/uuid v4.4.0+incompatible
|
github.com/gofrs/uuid v4.4.0+incompatible
|
||||||
github.com/grassrootseconomics/eth-custodial v1.3.0-beta
|
github.com/grassrootseconomics/eth-custodial v1.3.0-beta
|
||||||
|
2
go.sum
2
go.sum
@ -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 h1:RfU5/WFfPxDptlkyx4MT+4YmO79sY6HvIngUq5uwQPU=
|
||||||
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250112121325-9e4c65c8b4d1/go.mod h1:E6W7ZOa7ZvVr0Bc5ot0LNSwpSPYq4hXlAIvEPy3AJ7U=
|
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=
|
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||||
|
Loading…
Reference in New Issue
Block a user