Add json serialization instructions

This commit is contained in:
lash 2025-01-12 18:45:57 +00:00
parent ce308ef0c2
commit c5aba2ac8e
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746

View File

@ -26,42 +26,42 @@ const (
zeroAccount string = "0x0000000000000000000000000000000000000000" zeroAccount string = "0x0000000000000000000000000000000000000000"
) )
type tx struct { type Tx struct {
hsh string Track string `json: "track"`
to string Hsh string `json:"hash"`
from string To string `json:"to"`
voucher string From string `json: "from"`
value int Voucher string `json: "voucher"`
when time.Time Value int `json: "value"`
track string When time.Time `json: "when"`
} }
type account struct { type Account struct {
track string Track string `json: "track"`
address string Address string `json: "address"`
nonce int Nonce int `json: "nonce"`
defaultVoucher string DefaultVoucher string `json: "defaultVoucher"`
balances map[string]int Balances map[string]int `json: "balances"`
txs []string Txs []string `json: "txs"`
} }
type voucher struct { type Voucher struct {
name string Name string `json: "name"`
address string Address string `json: "address"`
symbol string Symbol string `json: "symbol"`
decimals int Decimals int `json: "decimals"`
sink string Sink string `json: "sink"`
commodity string Commodity string `json: "commodity"`
location string Location string `json: "location"`
} }
type DevAccountService struct { type DevAccountService struct {
accounts map[string]account accounts map[string]Account
accountsTrack map[string]string accountsTrack map[string]string
accountsAlias map[string]string accountsAlias map[string]string
vouchers map[string]voucher vouchers map[string]Voucher
vouchersAddress map[string]string vouchersAddress map[string]string
txs map[string]tx txs map[string]Tx
txsTrack map[string]string txsTrack map[string]string
toAutoCreate bool toAutoCreate bool
autoVouchers []string autoVouchers []string
@ -72,12 +72,12 @@ type DevAccountService struct {
func NewDevAccountService() *DevAccountService { func NewDevAccountService() *DevAccountService {
return &DevAccountService{ return &DevAccountService{
accounts: make(map[string]account), accounts: make(map[string]Account),
accountsTrack: make(map[string]string), accountsTrack: make(map[string]string),
accountsAlias: make(map[string]string), accountsAlias: make(map[string]string),
vouchers: make(map[string]voucher), vouchers: make(map[string]Voucher),
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), autoVoucherValue: make(map[string]int),
} }
@ -100,16 +100,16 @@ func (das *DevAccountService) AddVoucher(ctx context.Context, symbol string) err
} }
v, ok := das.vouchers[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)
} }
h := sha1.New() h := sha1.New()
h.Write([]byte(symbol)) h.Write([]byte(symbol))
z := h.Sum(nil) z := h.Sum(nil)
address := fmt.Sprintf("0x%x", z) address := fmt.Sprintf("0x%x", z)
das.vouchers[symbol] = voucher{ das.vouchers[symbol] = Voucher{
name: symbol, Name: symbol,
symbol: symbol, Symbol: symbol,
address: address, Address: address,
} }
das.vouchersAddress[address] = symbol das.vouchersAddress[address] = symbol
logg.InfoCtxf(ctx, "added dev voucher", "symbol", symbol, "address", address) logg.InfoCtxf(ctx, "added dev voucher", "symbol", symbol, "address", address)
@ -123,16 +123,16 @@ func (das *DevAccountService) CheckBalance(ctx context.Context, publicKey string
if !ok { if !ok {
return nil, fmt.Errorf("account not found (publickey): %v", publicKey) return nil, fmt.Errorf("account not found (publickey): %v", publicKey)
} }
if acc.defaultVoucher == "" { if acc.DefaultVoucher == "" {
return nil, fmt.Errorf("no default voucher set for: %v", publicKey) return nil, fmt.Errorf("no default voucher set for: %v", publicKey)
} }
bal, ok := acc.balances[acc.defaultVoucher] bal, ok := acc.Balances[acc.DefaultVoucher]
if !ok { if !ok {
return nil, fmt.Errorf("balance not found for default token %s pubkey %v", acc.defaultVoucher, publicKey) return nil, fmt.Errorf("balance not found for default token %s pubkey %v", acc.DefaultVoucher, publicKey)
} }
return &models.BalanceResult { return &models.BalanceResult {
Balance: strconv.Itoa(bal), Balance: strconv.Itoa(bal),
Nonce: json.Number(strconv.Itoa(acc.nonce)), Nonce: json.Number(strconv.Itoa(acc.Nonce)),
}, nil }, nil
} }
@ -146,7 +146,7 @@ func (das *DevAccountService) balanceAuto(ctx context.Context, pubKey string) er
if !ok { if !ok {
value = 0 value = 0
} }
_, err := das.TokenTransfer(ctx, strconv.Itoa(value), das.defaultAccount, pubKey, voucher.address) _, err := das.TokenTransfer(ctx, strconv.Itoa(value), das.defaultAccount, pubKey, voucher.Address)
if err != nil { if err != nil {
return err return err
} }
@ -168,9 +168,9 @@ func (das *DevAccountService) CreateAccount(ctx context.Context) (*models.Accoun
return nil, fmt.Errorf("short read: %d", c) return nil, fmt.Errorf("short read: %d", c)
} }
pubKey := fmt.Sprintf("0x%x", b) pubKey := fmt.Sprintf("0x%x", b)
das.accounts[pubKey] = account{ das.accounts[pubKey] = Account{
track: uid.String(), Track: uid.String(),
address: pubKey, Address: pubKey,
} }
das.accountsTrack[uid.String()] = pubKey das.accountsTrack[uid.String()] = pubKey
das.balanceAuto(ctx, pubKey) das.balanceAuto(ctx, pubKey)
@ -201,15 +201,15 @@ func (das *DevAccountService) FetchVouchers(ctx context.Context, publicKey strin
if !ok { if !ok {
return nil, fmt.Errorf("account not found (publickey): %v", publicKey) return nil, fmt.Errorf("account not found (publickey): %v", publicKey)
} }
for k, v := range(acc.balances) { for k, v := range(acc.Balances) {
voucher, ok := das.vouchers[k] voucher, ok := das.vouchers[k]
if !ok { if !ok {
return nil, fmt.Errorf("voucher has balance but object not found: %v", k) return nil, fmt.Errorf("voucher has balance but object not found: %v", k)
} }
holdings = append(holdings, dataserviceapi.TokenHoldings{ holdings = append(holdings, dataserviceapi.TokenHoldings{
ContractAddress: voucher.address, ContractAddress: voucher.Address,
TokenSymbol: voucher.symbol, TokenSymbol: voucher.Symbol,
TokenDecimals: strconv.Itoa(voucher.decimals), TokenDecimals: strconv.Itoa(voucher.Decimals),
Balance: strconv.Itoa(v), Balance: strconv.Itoa(v),
}) })
} }
@ -222,24 +222,24 @@ func (das *DevAccountService) FetchTransactions(ctx context.Context, publicKey s
if !ok { if !ok {
return nil, fmt.Errorf("account not found (publickey): %v", publicKey) return nil, fmt.Errorf("account not found (publickey): %v", publicKey)
} }
for i, v := range(acc.txs) { for i, v := range(acc.Txs) {
mytx := das.txs[v] mytx := das.txs[v]
if i == 10 { if i == 10 {
break break
} }
voucher, ok := das.vouchers[mytx.voucher] voucher, ok := das.vouchers[mytx.Voucher]
if !ok { if !ok {
return nil, fmt.Errorf("voucher %s in tx list but not found in voucher list", mytx.voucher) return nil, fmt.Errorf("voucher %s in tx list but not found in voucher list", mytx.Voucher)
} }
lasttx = append(lasttx, dataserviceapi.Last10TxResponse{ lasttx = append(lasttx, dataserviceapi.Last10TxResponse{
Sender: mytx.from, Sender: mytx.From,
Recipient: mytx.to, Recipient: mytx.To,
TransferValue: strconv.Itoa(mytx.value), TransferValue: strconv.Itoa(mytx.Value),
ContractAddress: voucher.address, ContractAddress: voucher.Address,
TxHash: mytx.hsh, TxHash: mytx.Hsh,
DateBlock: mytx.when, DateBlock: mytx.When,
TokenSymbol: voucher.symbol, TokenSymbol: voucher.Symbol,
TokenDecimals: strconv.Itoa(voucher.decimals), TokenDecimals: strconv.Itoa(voucher.Decimals),
}) })
} }
return lasttx, nil return lasttx, nil
@ -255,12 +255,12 @@ func (das *DevAccountService) VoucherData(ctx context.Context, address string) (
return nil, fmt.Errorf("voucher address %v found but does not resolve", address) return nil, fmt.Errorf("voucher address %v found but does not resolve", address)
} }
return &models.VoucherDataResult{ return &models.VoucherDataResult{
TokenName: voucher.name, TokenName: voucher.Name,
TokenSymbol: voucher.symbol, TokenSymbol: voucher.Symbol,
TokenDecimals: voucher.decimals, TokenDecimals: voucher.Decimals,
SinkAddress: voucher.sink, SinkAddress: voucher.Sink,
TokenCommodity: voucher.commodity, TokenCommodity: voucher.Commodity,
TokenLocation: voucher.location, TokenLocation: voucher.Location,
}, nil }, nil
} }
@ -303,14 +303,14 @@ func (das *DevAccountService) TokenTransfer(ctx context.Context, amount, from, t
return nil, fmt.Errorf("tx hash short read: %d", c) return nil, fmt.Errorf("tx hash short read: %d", c)
} }
hsh := fmt.Sprintf("0x%x", b) hsh := fmt.Sprintf("0x%x", b)
das.txs[hsh] = tx{ das.txs[hsh] = Tx{
hsh: hsh, Hsh: hsh,
to: accTo.address, To: accTo.Address,
from: accFrom.address, From: accFrom.Address,
voucher: voucher.symbol, Voucher: voucher.Symbol,
value: value, Value: value,
track: uid.String(), Track: uid.String(),
when: time.Now(), When: time.Now(),
} }
return &models.TokenTransferResponse{ return &models.TokenTransferResponse{
TrackingId: uid.String(), TrackingId: uid.String(),
@ -327,6 +327,6 @@ func (das *DevAccountService) CheckAliasAddress(ctx context.Context, alias strin
return nil, fmt.Errorf("alias %s found but does not resolve", alias) return nil, fmt.Errorf("alias %s found but does not resolve", alias)
} }
return &dataserviceapi.AliasAddress{ return &dataserviceapi.AliasAddress{
Address: acc.address, Address: acc.Address,
}, nil }, nil
} }