Compare commits
3 Commits
28e20809c8
...
81d2e3e1e9
Author | SHA1 | Date | |
---|---|---|---|
|
81d2e3e1e9 | ||
|
dc4ed4341c | ||
|
5dcb6b5c08 |
48
dev/api.go
48
dev/api.go
@ -27,7 +27,7 @@ const (
|
|||||||
pubKeyLen int = 20
|
pubKeyLen int = 20
|
||||||
hashLen int = 32
|
hashLen int = 32
|
||||||
defaultDecimals = 6
|
defaultDecimals = 6
|
||||||
zeroAccount string = "0x0000000000000000000000000000000000000000"
|
zeroAddress string = "0x0000000000000000000000000000000000000000"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Tx struct {
|
type Tx struct {
|
||||||
@ -89,7 +89,12 @@ func NewDevAccountService(ctx context.Context, d string) *DevAccountService {
|
|||||||
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),
|
||||||
|
defaultAccount: zeroAddress,
|
||||||
}
|
}
|
||||||
|
acc := Account{
|
||||||
|
Address: zeroAddress,
|
||||||
|
}
|
||||||
|
svc.accounts[acc.Address] = acc
|
||||||
err := svc.db.Connect(ctx, d)
|
err := svc.db.Connect(ctx, d)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -117,6 +122,18 @@ func (das *DevAccountService) loadAccount(ctx context.Context, pubKey string, v
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (das *DevAccountService) loadTx(ctx context.Context, hsh string, v []byte) error {
|
||||||
|
var mytx Tx
|
||||||
|
|
||||||
|
err := json.Unmarshal(v, &mytx)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("malformed tx: %v", hsh)
|
||||||
|
}
|
||||||
|
das.txs[hsh] = mytx
|
||||||
|
das.txsTrack[mytx.Track] = hsh
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (das *DevAccountService) loadItem(ctx context.Context, k []byte, v []byte) error {
|
func (das *DevAccountService) loadItem(ctx context.Context, k []byte, v []byte) error {
|
||||||
var err error
|
var err error
|
||||||
s := string(k)
|
s := string(k)
|
||||||
@ -126,6 +143,8 @@ func (das *DevAccountService) loadItem(ctx context.Context, k []byte, v []byte)
|
|||||||
}
|
}
|
||||||
if ss[0] == "account" {
|
if ss[0] == "account" {
|
||||||
err = das.loadAccount(ctx, ss[1], v)
|
err = das.loadAccount(ctx, ss[1], v)
|
||||||
|
} else if ss[0] == "tx" {
|
||||||
|
err = das.loadTx(ctx, ss[1], v)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -258,12 +277,16 @@ func (das *DevAccountService) CreateAccount(ctx context.Context) (*models.Accoun
|
|||||||
|
|
||||||
das.accounts[pubKey] = acc
|
das.accounts[pubKey] = acc
|
||||||
das.accountsTrack[uid.String()] = pubKey
|
das.accountsTrack[uid.String()] = pubKey
|
||||||
das.balanceAuto(ctx, pubKey)
|
err = das.balanceAuto(ctx, pubKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
if das.defaultAccount == zeroAccount {
|
if das.defaultAccount == zeroAddress {
|
||||||
das.defaultAccount = pubKey
|
das.defaultAccount = pubKey
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logg.InfoCtxf(ctx, "account created", "account", acc)
|
||||||
|
|
||||||
return &models.AccountResult{
|
return &models.AccountResult{
|
||||||
PublicKey: pubKey,
|
PublicKey: pubKey,
|
||||||
@ -352,6 +375,15 @@ func (das *DevAccountService) VoucherData(ctx context.Context, address string) (
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (das *DevAccountService) saveTokenTransfer(ctx context.Context, mytx Tx) error {
|
||||||
|
k := "tx_" + mytx.Hsh
|
||||||
|
v, err := json.Marshal(mytx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return das.db.Put(ctx, []byte(k), v)
|
||||||
|
}
|
||||||
|
|
||||||
func (das *DevAccountService) TokenTransfer(ctx context.Context, amount, from, to, tokenAddress string) (*models.TokenTransferResponse, error) {
|
func (das *DevAccountService) TokenTransfer(ctx context.Context, amount, from, to, tokenAddress string) (*models.TokenTransferResponse, error) {
|
||||||
var b [hashLen]byte
|
var b [hashLen]byte
|
||||||
value, err := strconv.Atoi(amount)
|
value, err := strconv.Atoi(amount)
|
||||||
@ -362,7 +394,7 @@ func (das *DevAccountService) TokenTransfer(ctx context.Context, amount, from, t
|
|||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("sender account %v not found", from)
|
return nil, fmt.Errorf("sender account %v not found", from)
|
||||||
}
|
}
|
||||||
accTo, ok := das.accounts[from]
|
accTo, ok := das.accounts[to]
|
||||||
if !ok {
|
if !ok {
|
||||||
if !das.toAutoCreate {
|
if !das.toAutoCreate {
|
||||||
return nil, fmt.Errorf("recipient account %v not found, and not creating", from)
|
return nil, fmt.Errorf("recipient account %v not found, and not creating", from)
|
||||||
@ -390,7 +422,7 @@ 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{
|
mytx := Tx{
|
||||||
Hsh: hsh,
|
Hsh: hsh,
|
||||||
To: accTo.Address,
|
To: accTo.Address,
|
||||||
From: accFrom.Address,
|
From: accFrom.Address,
|
||||||
@ -399,6 +431,12 @@ func (das *DevAccountService) TokenTransfer(ctx context.Context, amount, from, t
|
|||||||
Track: uid.String(),
|
Track: uid.String(),
|
||||||
When: time.Now(),
|
When: time.Now(),
|
||||||
}
|
}
|
||||||
|
err = das.saveTokenTransfer(ctx, mytx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
das.txs[hsh] = mytx
|
||||||
|
logg.InfoCtxf(ctx, "token transfer created", "tx", mytx)
|
||||||
return &models.TokenTransferResponse{
|
return &models.TokenTransferResponse{
|
||||||
TrackingId: uid.String(),
|
TrackingId: uid.String(),
|
||||||
}, nil
|
}, nil
|
||||||
|
Loading…
Reference in New Issue
Block a user