Compare commits
No commits in common. "0037d3273892a5ec5f3fa23c41424967037fc93c" and "bc9d365fbbe59efe3e126ec09fc317f11a6e73b6" have entirely different histories.
0037d32738
...
bc9d365fbb
93
dev/api.go
93
dev/api.go
@ -15,7 +15,6 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
pubKeyLen int = 20
|
pubKeyLen int = 20
|
||||||
hashLen int = 32
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type tx struct {
|
type tx struct {
|
||||||
@ -25,16 +24,14 @@ type tx struct {
|
|||||||
voucher string
|
voucher string
|
||||||
value int
|
value int
|
||||||
when time.Time
|
when time.Time
|
||||||
track string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type account struct {
|
type account struct {
|
||||||
track string
|
track string
|
||||||
address string
|
|
||||||
nonce int
|
nonce int
|
||||||
defaultVoucher string
|
defaultVoucher string
|
||||||
balances map[string]int
|
balances map[string]int
|
||||||
txs []string
|
txs []tx
|
||||||
}
|
}
|
||||||
|
|
||||||
type voucher struct {
|
type voucher struct {
|
||||||
@ -50,15 +47,11 @@ type voucher struct {
|
|||||||
var (
|
var (
|
||||||
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)
|
|
||||||
txsTrack = make(map[string]string)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
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
|
|
||||||
toAutoCreate bool
|
|
||||||
// accountsSession map[string]string
|
// accountsSession map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,7 +59,7 @@ 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),
|
//accountsSession: make(map[string]string),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,7 +98,6 @@ func (das *DevAccountService) CreateAccount(ctx context.Context) (*models.Accoun
|
|||||||
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,
|
|
||||||
}
|
}
|
||||||
das.accountsTrack[uid.String()] = pubKey
|
das.accountsTrack[uid.String()] = pubKey
|
||||||
return &models.AccountResult{
|
return &models.AccountResult{
|
||||||
@ -153,21 +145,20 @@ func (das *DevAccountService) FetchTransactions(ctx context.Context, publicKey s
|
|||||||
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 := txs[v]
|
|
||||||
if i == 10 {
|
if i == 10 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
voucher, ok := vouchers[mytx.voucher]
|
voucher, ok := vouchers[v.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", v.voucher)
|
||||||
}
|
}
|
||||||
lasttx = append(lasttx, dataserviceapi.Last10TxResponse{
|
lasttx = append(lasttx, dataserviceapi.Last10TxResponse{
|
||||||
Sender: mytx.from,
|
Sender: v.from,
|
||||||
Recipient: mytx.to,
|
Recipient: v.to,
|
||||||
TransferValue: strconv.Itoa(mytx.value),
|
TransferValue: strconv.Itoa(v.value),
|
||||||
ContractAddress: voucher.address,
|
ContractAddress: voucher.address,
|
||||||
TxHash: mytx.hsh,
|
TxHash: v.hsh,
|
||||||
DateBlock: mytx.when,
|
DateBlock: v.when,
|
||||||
TokenSymbol: voucher.symbol,
|
TokenSymbol: voucher.symbol,
|
||||||
TokenDecimals: strconv.Itoa(voucher.decimals),
|
TokenDecimals: strconv.Itoa(voucher.decimals),
|
||||||
})
|
})
|
||||||
@ -194,69 +185,3 @@ func (das *DevAccountService) VoucherData(ctx context.Context, address string) (
|
|||||||
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (das *DevAccountService) TokenTransfer(ctx context.Context, amount, from, to, tokenAddress string) (*models.TokenTransferResponse, error) {
|
|
||||||
var b [hashLen]byte
|
|
||||||
value, err := strconv.Atoi(amount)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
accFrom, ok := das.accounts[from]
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("sender account %v not found", from)
|
|
||||||
}
|
|
||||||
accTo, ok := das.accounts[from]
|
|
||||||
if !ok {
|
|
||||||
if !das.toAutoCreate {
|
|
||||||
return nil, fmt.Errorf("recipient account %v not found, and not creating", from)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sym, ok := vouchersAddress[tokenAddress]
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("voucher address %v not found", tokenAddress)
|
|
||||||
}
|
|
||||||
voucher, ok := vouchers[sym]
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("voucher address %v found but does not resolve", tokenAddress)
|
|
||||||
}
|
|
||||||
|
|
||||||
uid, err := uuid.NewV4()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
c, err := rand.Read(b[:])
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if c != hashLen {
|
|
||||||
return nil, fmt.Errorf("tx hash short read: %d", c)
|
|
||||||
}
|
|
||||||
hsh := fmt.Sprintf("0x%x", b)
|
|
||||||
txs[hsh] = tx{
|
|
||||||
hsh: hsh,
|
|
||||||
to: accTo.address,
|
|
||||||
from: accFrom.address,
|
|
||||||
voucher: voucher.symbol,
|
|
||||||
value: value,
|
|
||||||
track: uid.String(),
|
|
||||||
when: time.Now(),
|
|
||||||
}
|
|
||||||
return &models.TokenTransferResponse{
|
|
||||||
TrackingId: uid.String(),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (das *DevAccountService) CheckAliasAddress(ctx context.Context, alias string) (*dataserviceapi.AliasAddress, error) {
|
|
||||||
addr, ok := das.accountsAlias[alias]
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("alias %s not found", alias)
|
|
||||||
}
|
|
||||||
acc, ok := das.accounts[addr]
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("alias %s found but does not resolve", alias)
|
|
||||||
}
|
|
||||||
return &dataserviceapi.AliasAddress{
|
|
||||||
Address: acc.address,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user