Compare commits
25 Commits
lash/alias
...
pool-swap-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e8c58a8f33
|
||
|
|
67d7ec1567
|
||
|
|
b05734f976
|
||
|
|
f723e0aa45
|
||
|
|
aef8efa2bf
|
||
|
|
a1fe416f51
|
||
|
|
3473a4413b
|
||
|
|
31eb30de0f
|
||
| 2181388f5b | |||
|
|
448fdffbd0
|
||
|
|
4ae0a484f4
|
||
|
|
3f9dc89a40
|
||
|
|
bc7afa50a4
|
||
|
|
9c75109b75
|
||
|
|
ee574908d4
|
||
|
|
e0b5398098 | ||
|
|
b7e53609a9
|
||
| a4cc7d2a98 | |||
|
|
a63164bcf5
|
||
|
|
fa355e3729
|
||
|
|
99c704f6ff
|
||
|
|
ed549cba70
|
||
|
|
8a47d1d674
|
||
|
|
23b4180e50
|
||
| b22ff1e7f0 |
@@ -15,7 +15,10 @@ const (
|
||||
voucherHoldingsPathPrefix = "/api/v1/holdings"
|
||||
voucherTransfersPathPrefix = "/api/v1/transfers/last10"
|
||||
voucherDataPathPrefix = "/api/v1/token"
|
||||
AliasPrefix = "api/v1/alias"
|
||||
aliasPrefix = "api/v1/alias"
|
||||
poolDepositPrefix = "/api/v2/pool/deposit"
|
||||
poolSwapQoutePrefix = "/api/v2/pool/quote"
|
||||
poolSwapPrefix = "/api/v2/pool/swap"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -34,6 +37,9 @@ var (
|
||||
VoucherTransfersURL string
|
||||
VoucherDataURL string
|
||||
CheckAliasURL string
|
||||
PoolDepositURL string
|
||||
PoolSwapQuoteURL string
|
||||
PoolSwapURL string
|
||||
)
|
||||
|
||||
func setBase() error {
|
||||
@@ -68,7 +74,10 @@ func LoadConfig() error {
|
||||
VoucherHoldingsURL, _ = url.JoinPath(dataURLBase, voucherHoldingsPathPrefix)
|
||||
VoucherTransfersURL, _ = url.JoinPath(dataURLBase, voucherTransfersPathPrefix)
|
||||
VoucherDataURL, _ = url.JoinPath(dataURLBase, voucherDataPathPrefix)
|
||||
CheckAliasURL, _ = url.JoinPath(dataURLBase, AliasPrefix)
|
||||
CheckAliasURL, _ = url.JoinPath(dataURLBase, aliasPrefix)
|
||||
PoolDepositURL, _ = url.JoinPath(custodialURLBase, poolDepositPrefix)
|
||||
PoolSwapQuoteURL, _ = url.JoinPath(custodialURLBase, poolSwapQoutePrefix)
|
||||
PoolSwapURL, _ = url.JoinPath(custodialURLBase, poolSwapPrefix)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
417
dev/api.go
417
dev/api.go
@@ -6,71 +6,75 @@ import (
|
||||
"crypto/sha1"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
"git.defalsify.org/vise.git/logging"
|
||||
"git.defalsify.org/vise.git/db"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/event"
|
||||
"git.defalsify.org/vise.git/logging"
|
||||
"git.grassecon.net/grassrootseconomics/common/phone"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/event"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
||||
"git.grassecon.net/grassrootseconomics/visedriver/storage"
|
||||
"github.com/gofrs/uuid"
|
||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||
)
|
||||
|
||||
var (
|
||||
logg = logging.NewVanilla().WithDomain("sarafu-api.devapi")
|
||||
aliasRegex = regexp.MustCompile("^\\+?[a-zA-Z0-9\\-_]+$")
|
||||
logg = logging.NewVanilla().WithDomain("sarafu-api.devapi")
|
||||
aliasRegex = regexp.MustCompile("^\\+?[a-zA-Z0-9\\-_]+$")
|
||||
searchDomain = ".sarafu.local"
|
||||
)
|
||||
|
||||
const (
|
||||
pubKeyLen int = 20
|
||||
hashLen int = 32
|
||||
defaultDecimals = 6
|
||||
zeroAddress string = "0x0000000000000000000000000000000000000000"
|
||||
pubKeyLen int = 20
|
||||
hashLen int = 32
|
||||
defaultDecimals = 6
|
||||
zeroAddress string = "0x0000000000000000000000000000000000000000"
|
||||
defaultVoucherBalance float64 = 500.00
|
||||
)
|
||||
|
||||
type Tx struct {
|
||||
Track string `json: "track"`
|
||||
Hsh string `json:"hash"`
|
||||
To string `json:"to"`
|
||||
From string `json: "from"`
|
||||
Voucher string `json: "voucher"`
|
||||
Value int `json: "value"`
|
||||
When time.Time `json: "when"`
|
||||
Track string `json: "track"`
|
||||
Hsh string `json:"hash"`
|
||||
To string `json:"to"`
|
||||
From string `json: "from"`
|
||||
Voucher string `json: "voucher"`
|
||||
Value int `json: "value"`
|
||||
When time.Time `json: "when"`
|
||||
}
|
||||
|
||||
func (t *Tx) ToTransferEvent() event.EventTokenTransfer {
|
||||
return event.EventTokenTransfer{
|
||||
To: t.To,
|
||||
Value: t.Value,
|
||||
To: t.To,
|
||||
Value: t.Value,
|
||||
VoucherAddress: t.Voucher,
|
||||
TxHash: t.Hsh,
|
||||
From: t.From,
|
||||
TxHash: t.Hsh,
|
||||
From: t.From,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Tx) ToMintEvent() event.EventTokenMint {
|
||||
return event.EventTokenMint{
|
||||
To: t.To,
|
||||
Value: t.Value,
|
||||
To: t.To,
|
||||
Value: t.Value,
|
||||
VoucherAddress: t.Voucher,
|
||||
TxHash: t.Hsh,
|
||||
TxHash: t.Hsh,
|
||||
}
|
||||
}
|
||||
|
||||
type Account struct {
|
||||
Track string `json: "track"`
|
||||
Address string `json: "address"`
|
||||
Nonce int `json: "nonce"`
|
||||
DefaultVoucher string `json: "defaultVoucher"`
|
||||
Balances map[string]int `json: "balances"`
|
||||
Alias string
|
||||
Txs []string `json: "txs"`
|
||||
Track string `json: "track"`
|
||||
Address string `json: "address"`
|
||||
Nonce int `json: "nonce"`
|
||||
DefaultVoucher string `json: "defaultVoucher"`
|
||||
Balances map[string]int `json: "balances"`
|
||||
Alias string
|
||||
Txs []string `json: "txs"`
|
||||
}
|
||||
|
||||
func (a *Account) ToRegistrationEvent() event.EventCustodialRegistration {
|
||||
@@ -80,45 +84,51 @@ func (a *Account) ToRegistrationEvent() event.EventCustodialRegistration {
|
||||
}
|
||||
|
||||
type Voucher struct {
|
||||
Name string `json: "name"`
|
||||
Address string `json: "address"`
|
||||
Symbol string `json: "symbol"`
|
||||
Decimals int `json: "decimals"`
|
||||
Sink string `json: "sink"`
|
||||
Name string `json: "name"`
|
||||
Address string `json: "address"`
|
||||
Symbol string `json: "symbol"`
|
||||
Decimals int `json: "decimals"`
|
||||
Sink string `json: "sink"`
|
||||
Commodity string `json: "commodity"`
|
||||
Location string `json: "location"`
|
||||
Location string `json: "location"`
|
||||
}
|
||||
|
||||
type Pool struct {
|
||||
vouchers []Voucher
|
||||
poolLimit map[string]string
|
||||
}
|
||||
|
||||
type DevAccountService struct {
|
||||
db db.Db
|
||||
accounts map[string]Account
|
||||
accountsTrack map[string]string
|
||||
accountsAlias map[string]string
|
||||
vouchers map[string]Voucher
|
||||
vouchersAddress map[string]string
|
||||
txs map[string]Tx
|
||||
txsTrack map[string]string
|
||||
toAutoCreate bool
|
||||
autoVouchers []string
|
||||
db db.Db
|
||||
accounts map[string]Account
|
||||
accountsTrack map[string]string
|
||||
accountsAlias map[string]string
|
||||
vouchers map[string]Voucher
|
||||
vouchersAddress map[string]string
|
||||
txs map[string]Tx
|
||||
txsTrack map[string]string
|
||||
toAutoCreate bool
|
||||
autoVouchers []string
|
||||
autoVoucherValue map[string]int
|
||||
defaultAccount string
|
||||
emitterFunc event.EmitterFunc
|
||||
pfx []byte
|
||||
// accountsSession map[string]string
|
||||
defaultAccount string
|
||||
emitterFunc event.EmitterFunc
|
||||
pfx []byte
|
||||
pool Pool
|
||||
}
|
||||
|
||||
func NewDevAccountService(ctx context.Context, ss storage.StorageService) *DevAccountService {
|
||||
svc := &DevAccountService{
|
||||
accounts: make(map[string]Account),
|
||||
accountsTrack: make(map[string]string),
|
||||
accountsAlias: make(map[string]string),
|
||||
vouchers: make(map[string]Voucher),
|
||||
vouchersAddress: make(map[string]string),
|
||||
txs: make(map[string]Tx),
|
||||
txsTrack: make(map[string]string),
|
||||
accounts: make(map[string]Account),
|
||||
accountsTrack: make(map[string]string),
|
||||
accountsAlias: make(map[string]string),
|
||||
vouchers: make(map[string]Voucher),
|
||||
vouchersAddress: make(map[string]string),
|
||||
txs: make(map[string]Tx),
|
||||
txsTrack: make(map[string]string),
|
||||
autoVoucherValue: make(map[string]int),
|
||||
defaultAccount: zeroAddress,
|
||||
pfx: []byte("__"),
|
||||
pool: Pool{},
|
||||
defaultAccount: zeroAddress,
|
||||
pfx: []byte("__"),
|
||||
}
|
||||
if ss != nil {
|
||||
var err error
|
||||
@@ -151,7 +161,7 @@ func (das *DevAccountService) WithPrefix(pfx []byte) *DevAccountService {
|
||||
}
|
||||
|
||||
func (das *DevAccountService) prefixKeyFor(k string, v string) []byte {
|
||||
return append(das.pfx, []byte(k + "_" + v)...)
|
||||
return append(das.pfx, []byte(k+"_"+v)...)
|
||||
}
|
||||
|
||||
func (das *DevAccountService) loadAccount(ctx context.Context, pubKey string, v []byte) error {
|
||||
@@ -170,6 +180,15 @@ func (das *DevAccountService) loadAccount(ctx context.Context, pubKey string, v
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Pool) hasVoucher(voucherAddress string) bool {
|
||||
for _, value := range p.vouchers {
|
||||
if value.Address == voucherAddress {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (das *DevAccountService) loadTx(ctx context.Context, hsh string, v []byte) error {
|
||||
var mytx Tx
|
||||
|
||||
@@ -183,6 +202,15 @@ func (das *DevAccountService) loadTx(ctx context.Context, hsh string, v []byte)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (das *DevAccountService) loadAlias(ctx context.Context, alias string, key []byte) error {
|
||||
result, err := das.db.Get(ctx, key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
das.accountsAlias[alias] = strings.ReplaceAll(string(result), `"`, "")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (das *DevAccountService) loadItem(ctx context.Context, k []byte, v []byte) error {
|
||||
var err error
|
||||
s := string(k)
|
||||
@@ -192,8 +220,13 @@ func (das *DevAccountService) loadItem(ctx context.Context, k []byte, v []byte)
|
||||
}
|
||||
if ss[0] == "account" {
|
||||
err = das.loadAccount(ctx, ss[1], v)
|
||||
logg.ErrorCtxf(ctx, "loading saved account failed", "error_load_account", err)
|
||||
} else if ss[0] == "tx" {
|
||||
err = das.loadTx(ctx, ss[1], v)
|
||||
logg.ErrorCtxf(ctx, "loading transactions failed", "error_load_txs", err)
|
||||
} else if ss[0] == "alias" {
|
||||
err = das.loadAlias(ctx, ss[1], k)
|
||||
logg.ErrorCtxf(ctx, "loading aliases failed", "error_load_aliases", err)
|
||||
} else {
|
||||
logg.ErrorCtxf(ctx, "unknown double underscore key", "key", ss[0])
|
||||
}
|
||||
@@ -225,7 +258,7 @@ func (das *DevAccountService) loadAll(ctx context.Context) error {
|
||||
}
|
||||
|
||||
func (das *DevAccountService) indexAll(ctx context.Context) error {
|
||||
for k, v := range(das.txs) {
|
||||
for k, v := range das.txs {
|
||||
acc := das.accounts[v.From]
|
||||
acc.Txs = append(acc.Txs, k)
|
||||
logg.TraceCtxf(ctx, "add tx to sender index", "from", v.From, "tx", k)
|
||||
@@ -265,8 +298,8 @@ func (das *DevAccountService) AddVoucher(ctx context.Context, symbol string) err
|
||||
z := h.Sum(nil)
|
||||
address := fmt.Sprintf("0x%x", z)
|
||||
das.vouchers[symbol] = Voucher{
|
||||
Name: symbol,
|
||||
Symbol: symbol,
|
||||
Name: symbol,
|
||||
Symbol: symbol,
|
||||
Address: address,
|
||||
}
|
||||
das.vouchersAddress[address] = symbol
|
||||
@@ -288,14 +321,14 @@ func (das *DevAccountService) CheckBalance(ctx context.Context, publicKey string
|
||||
if !ok {
|
||||
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),
|
||||
Nonce: json.Number(strconv.Itoa(acc.Nonce)),
|
||||
Nonce: json.Number(strconv.Itoa(acc.Nonce)),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (das *DevAccountService) balanceAuto(ctx context.Context, pubKey string) error {
|
||||
for _, v := range(das.autoVouchers) {
|
||||
for _, v := range das.autoVouchers {
|
||||
voucher, ok := das.vouchers[v]
|
||||
if !ok {
|
||||
return fmt.Errorf("balance auto voucher set but not resolved: %s", v)
|
||||
@@ -312,6 +345,10 @@ func (das *DevAccountService) balanceAuto(ctx context.Context, pubKey string) er
|
||||
return nil
|
||||
}
|
||||
|
||||
func (das *DevAccountService) GetAliases(ctx context.Context) map[string]string {
|
||||
return das.accountsAlias
|
||||
}
|
||||
|
||||
func (das *DevAccountService) saveAccount(ctx context.Context, acc Account) error {
|
||||
if das.db == nil {
|
||||
return nil
|
||||
@@ -326,6 +363,23 @@ func (das *DevAccountService) saveAccount(ctx context.Context, acc Account) erro
|
||||
return das.db.Put(ctx, []byte(k), v)
|
||||
}
|
||||
|
||||
func (das *DevAccountService) saveAlias(ctx context.Context, alias map[string]string) error {
|
||||
if das.db == nil {
|
||||
return fmt.Errorf("Db cannot be nil")
|
||||
}
|
||||
for k, v := range alias {
|
||||
k_ := das.prefixKeyFor("alias", k)
|
||||
v_, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
das.db.SetSession("")
|
||||
das.db.SetPrefix(db.DATATYPE_USERDATA)
|
||||
return das.db.Put(ctx, []byte(k_), v_)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (das *DevAccountService) CreateAccount(ctx context.Context) (*models.AccountResult, error) {
|
||||
var b [pubKeyLen]byte
|
||||
uid, err := uuid.NewV4()
|
||||
@@ -341,7 +395,7 @@ func (das *DevAccountService) CreateAccount(ctx context.Context) (*models.Accoun
|
||||
}
|
||||
pubKey := fmt.Sprintf("0x%x", b)
|
||||
acc := Account{
|
||||
Track: uid.String(),
|
||||
Track: uid.String(),
|
||||
Address: pubKey,
|
||||
}
|
||||
|
||||
@@ -363,7 +417,7 @@ func (das *DevAccountService) CreateAccount(ctx context.Context) (*models.Accoun
|
||||
|
||||
if das.emitterFunc != nil {
|
||||
msg := event.Msg{
|
||||
Typ: event.EventRegistrationTag,
|
||||
Typ: event.EventRegistrationTag,
|
||||
Item: acc,
|
||||
}
|
||||
err = das.emitterFunc(ctx, msg)
|
||||
@@ -374,11 +428,79 @@ func (das *DevAccountService) CreateAccount(ctx context.Context) (*models.Accoun
|
||||
logg.TraceCtxf(ctx, "account created", "account", acc)
|
||||
|
||||
return &models.AccountResult{
|
||||
PublicKey: pubKey,
|
||||
PublicKey: pubKey,
|
||||
TrackingId: uid.String(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (das *DevAccountService) PoolDeposit(ctx context.Context, amount, from, poolAddress, tokenAddress string) (*models.PoolDepositResult, error) {
|
||||
sym, ok := das.vouchersAddress[tokenAddress]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("voucher address %v not found", tokenAddress)
|
||||
}
|
||||
uid, err := uuid.NewV4()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
voucher, ok := das.vouchers[sym]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("voucher address %v found but does not resolve", tokenAddress)
|
||||
}
|
||||
|
||||
das.pool = Pool{
|
||||
vouchers: append(das.pool.vouchers, voucher),
|
||||
poolLimit: map[string]string{tokenAddress: amount},
|
||||
}
|
||||
return &models.PoolDepositResult{
|
||||
TrackingId: uid.String(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (das *DevAccountService) GetPoolSwapQuote(ctx context.Context, amount, from, fromTokenAddress, poolAddress, toTokenAddress string) (*models.PoolSwapQuoteResult, error) {
|
||||
_, ok := das.accounts[from]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("account not found (publickey): %v", from)
|
||||
}
|
||||
//resolve the token address you are trying to swap from(fromTokenAddress)
|
||||
_, ok = das.vouchersAddress[fromTokenAddress]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("voucher address %v not found", fromTokenAddress)
|
||||
}
|
||||
p := das.pool
|
||||
|
||||
//check if pool has voucher to swap to
|
||||
if !p.hasVoucher(toTokenAddress) {
|
||||
return nil, fmt.Errorf("Voucher with address: %v not found in the pool", toTokenAddress)
|
||||
}
|
||||
//Return a a quote that is equal to the amount enter
|
||||
return &models.PoolSwapQuoteResult{IncludesFeesDeduction: false, OutValue: amount}, nil
|
||||
}
|
||||
|
||||
func (das *DevAccountService) PoolSwap(ctx context.Context, amount, from, fromTokenAddress, poolAddress, toTokenAddress string) (*models.PoolSwapResult, error) {
|
||||
uid, err := uuid.NewV4()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, ok := das.accounts[from]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("account not found (publickey): %v", from)
|
||||
}
|
||||
_, ok = das.vouchersAddress[fromTokenAddress]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("voucher address %v not found", fromTokenAddress)
|
||||
}
|
||||
p := das.pool
|
||||
|
||||
//check if pool has voucher to swap to
|
||||
if !p.hasVoucher(toTokenAddress) {
|
||||
return nil, fmt.Errorf("Voucher with address: %v not found in the pool", toTokenAddress)
|
||||
}
|
||||
|
||||
return &models.PoolSwapResult{TrackingId: uid.String()}, nil
|
||||
}
|
||||
|
||||
func (das *DevAccountService) TrackAccountStatus(ctx context.Context, publicKey string) (*models.TrackStatusResult, error) {
|
||||
var ok bool
|
||||
_, ok = das.accounts[publicKey]
|
||||
@@ -392,22 +514,20 @@ func (das *DevAccountService) TrackAccountStatus(ctx context.Context, publicKey
|
||||
|
||||
func (das *DevAccountService) FetchVouchers(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
|
||||
var holdings []dataserviceapi.TokenHoldings
|
||||
acc, ok := das.accounts[publicKey]
|
||||
_, ok := das.accounts[publicKey]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("account not found (publickey): %v", publicKey)
|
||||
}
|
||||
for k, v := range(acc.Balances) {
|
||||
voucher, ok := das.vouchers[k]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("voucher has balance but object not found: %v", k)
|
||||
}
|
||||
//TODO: Iterate over the account acc.Balances object
|
||||
for _, voucher := range das.vouchers {
|
||||
holdings = append(holdings, dataserviceapi.TokenHoldings{
|
||||
ContractAddress: voucher.Address,
|
||||
TokenSymbol: voucher.Symbol,
|
||||
TokenDecimals: strconv.Itoa(voucher.Decimals),
|
||||
Balance: strconv.Itoa(v),
|
||||
TokenSymbol: voucher.Symbol,
|
||||
TokenDecimals: strconv.Itoa(voucher.Decimals),
|
||||
Balance: strconv.Itoa(int(defaultVoucherBalance)),
|
||||
})
|
||||
}
|
||||
|
||||
return holdings, nil
|
||||
}
|
||||
|
||||
@@ -417,24 +537,24 @@ func (das *DevAccountService) FetchTransactions(ctx context.Context, publicKey s
|
||||
if !ok {
|
||||
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]
|
||||
if i == 10 {
|
||||
break
|
||||
break
|
||||
}
|
||||
voucher, ok := das.vouchers[mytx.Voucher]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("voucher %s in tx list but not found in voucher list", mytx.Voucher)
|
||||
}
|
||||
lasttx = append(lasttx, dataserviceapi.Last10TxResponse{
|
||||
Sender: mytx.From,
|
||||
Recipient: mytx.To,
|
||||
TransferValue: strconv.Itoa(mytx.Value),
|
||||
Sender: mytx.From,
|
||||
Recipient: mytx.To,
|
||||
TransferValue: strconv.Itoa(mytx.Value),
|
||||
ContractAddress: voucher.Address,
|
||||
TxHash: mytx.Hsh,
|
||||
DateBlock: mytx.When,
|
||||
TokenSymbol: voucher.Symbol,
|
||||
TokenDecimals: strconv.Itoa(voucher.Decimals),
|
||||
TxHash: mytx.Hsh,
|
||||
DateBlock: mytx.When,
|
||||
TokenSymbol: voucher.Symbol,
|
||||
TokenDecimals: strconv.Itoa(voucher.Decimals),
|
||||
})
|
||||
}
|
||||
return lasttx, nil
|
||||
@@ -450,13 +570,12 @@ func (das *DevAccountService) VoucherData(ctx context.Context, address string) (
|
||||
return nil, fmt.Errorf("voucher address %v found but does not resolve", address)
|
||||
}
|
||||
return &models.VoucherDataResult{
|
||||
TokenName: voucher.Name,
|
||||
TokenSymbol: voucher.Symbol,
|
||||
TokenDecimals: voucher.Decimals,
|
||||
SinkAddress: voucher.Sink,
|
||||
TokenName: voucher.Name,
|
||||
TokenSymbol: voucher.Symbol,
|
||||
TokenDecimals: voucher.Decimals,
|
||||
SinkAddress: voucher.Sink,
|
||||
TokenCommodity: voucher.Commodity,
|
||||
TokenLocation: voucher.Location,
|
||||
|
||||
TokenLocation: voucher.Location,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -481,12 +600,12 @@ func (das *DevAccountService) TokenTransfer(ctx context.Context, amount, from, t
|
||||
}
|
||||
accFrom, ok := das.accounts[from]
|
||||
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[to]
|
||||
if !ok {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -512,13 +631,13 @@ func (das *DevAccountService) TokenTransfer(ctx context.Context, amount, from, t
|
||||
}
|
||||
hsh := fmt.Sprintf("0x%x", b)
|
||||
mytx := Tx{
|
||||
Hsh: hsh,
|
||||
To: accTo.Address,
|
||||
From: accFrom.Address,
|
||||
Hsh: hsh,
|
||||
To: accTo.Address,
|
||||
From: accFrom.Address,
|
||||
Voucher: voucher.Symbol,
|
||||
Value: value,
|
||||
Track: uid.String(),
|
||||
When: time.Now(),
|
||||
Value: value,
|
||||
Track: uid.String(),
|
||||
When: time.Now(),
|
||||
}
|
||||
err = das.saveTokenTransfer(ctx, mytx)
|
||||
if err != nil {
|
||||
@@ -527,7 +646,7 @@ func (das *DevAccountService) TokenTransfer(ctx context.Context, amount, from, t
|
||||
das.txs[hsh] = mytx
|
||||
if das.emitterFunc != nil {
|
||||
msg := event.Msg{
|
||||
Typ: event.EventTokenTransferTag,
|
||||
Typ: event.EventTokenTransferTag,
|
||||
Item: mytx,
|
||||
}
|
||||
err = das.emitterFunc(ctx, msg)
|
||||
@@ -544,10 +663,12 @@ func (das *DevAccountService) TokenTransfer(ctx context.Context, amount, from, t
|
||||
func (das *DevAccountService) CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
|
||||
addr, ok := das.accountsAlias[alias]
|
||||
if !ok {
|
||||
logg.ErrorCtxf(ctx, "alias check failed", "alias", alias)
|
||||
return nil, fmt.Errorf("alias %s not found", alias)
|
||||
}
|
||||
acc, ok := das.accounts[addr]
|
||||
if !ok {
|
||||
logg.ErrorCtxf(ctx, "failed to resolve alias", "alias", alias)
|
||||
return nil, fmt.Errorf("alias %s found but does not resolve", alias)
|
||||
}
|
||||
return &models.AliasAddress{
|
||||
@@ -568,16 +689,29 @@ func (das *DevAccountService) applyPhoneAlias(ctx context.Context, publicKey str
|
||||
|
||||
func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
|
||||
var alias string
|
||||
uid, err := uuid.NewV4()
|
||||
if !aliasRegex.MatchString(hint) {
|
||||
logg.ErrorCtxf(ctx, "alias hint does not match", "key", publicKey, "hint", hint)
|
||||
return nil, fmt.Errorf("alias hint does not match: %s", publicKey)
|
||||
}
|
||||
acc, ok := das.accounts[publicKey]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("address %s not found", publicKey)
|
||||
//Handle accounts created via the api
|
||||
acc = Account{
|
||||
Track: uid.String(),
|
||||
Address: publicKey,
|
||||
}
|
||||
err = das.saveAccount(ctx, acc)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "account save failed with", "account", acc, "account_save_error", err)
|
||||
return nil, err
|
||||
}
|
||||
das.accounts[publicKey] = acc
|
||||
}
|
||||
alias = hint
|
||||
isPhone, err := das.applyPhoneAlias(ctx, publicKey, alias)
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "failed to apply phone alias", "public key", publicKey, "alias", alias, "error", err)
|
||||
return nil, fmt.Errorf("phone parser error: %v", err)
|
||||
}
|
||||
if !isPhone {
|
||||
@@ -592,10 +726,85 @@ func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string
|
||||
alias += "x"
|
||||
}
|
||||
acc.Alias = alias
|
||||
alias = alias + searchDomain
|
||||
das.accountsAlias[alias] = publicKey
|
||||
err := das.saveAlias(ctx, map[string]string{alias: publicKey})
|
||||
if err != nil {
|
||||
logg.ErrorCtxf(ctx, "account save error", "public key", publicKey, "alias", alias, "alias_save_error", err)
|
||||
return nil, fmt.Errorf("Failed to save the account alias with error: %s", err.Error())
|
||||
}
|
||||
}
|
||||
logg.DebugCtxf(ctx, "set alias", "addr", publicKey, "alias", alias)
|
||||
return &models.RequestAliasResult{
|
||||
Alias: alias,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (das *DevAccountService) FetchTopPools(ctx context.Context) ([]models.Pool, error) {
|
||||
var (
|
||||
r struct {
|
||||
OK bool `json:"ok"`
|
||||
Description string `json:"description"`
|
||||
Result struct {
|
||||
Pools []models.Pool `json:"topPools"`
|
||||
} `json:"result"`
|
||||
}
|
||||
)
|
||||
data, err := os.ReadFile("./data/top_pools.json")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &r)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return r.Result.Pools, nil
|
||||
}
|
||||
|
||||
func (das *DevAccountService) GetPoolSwappableFromVouchers(ctx context.Context) ([]models.SwappableVoucher, error) {
|
||||
var (
|
||||
r struct {
|
||||
OK bool `json:"ok"`
|
||||
Description string `json:"description"`
|
||||
Result struct {
|
||||
SwappableVouchers []models.SwappableVoucher `json:"filtered"`
|
||||
} `json:"result"`
|
||||
}
|
||||
)
|
||||
data, err := os.ReadFile("./data/swap_from.json")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &r)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return r.Result.SwappableVouchers, nil
|
||||
}
|
||||
|
||||
func (das *DevAccountService) GetPoolSwappableVouchers(ctx context.Context) ([]models.SwappableVoucher, error) {
|
||||
var (
|
||||
r struct {
|
||||
OK bool `json:"ok"`
|
||||
Description string `json:"description"`
|
||||
Result struct {
|
||||
SwappableVouchers []models.SwappableVoucher `json:"filtered"`
|
||||
} `json:"result"`
|
||||
}
|
||||
)
|
||||
data, err := os.ReadFile("./data/swap_to.json")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &r)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return r.Result.SwappableVouchers, nil
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
func TestApiRequestAlias(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, "SessionId", "+25471234565")
|
||||
storageService := mocks.NewMemStorageService(ctx)
|
||||
svc := NewDevAccountService(ctx, storageService)
|
||||
ra, err := svc.CreateAccount(ctx)
|
||||
@@ -16,10 +17,10 @@ func TestApiRequestAlias(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
addr := ra.PublicKey
|
||||
|
||||
|
||||
_, err = svc.RequestAlias(ctx, addr, "+254f00")
|
||||
if err == nil {
|
||||
t.Fatalf("expected error")
|
||||
t.Fatalf("expected error")
|
||||
}
|
||||
alias := "+254712345678"
|
||||
rb, err := svc.RequestAlias(ctx, addr, alias)
|
||||
@@ -39,6 +40,7 @@ func TestApiRequestAlias(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
alias = "foo.sarafu.local"
|
||||
if rb.Alias != alias {
|
||||
t.Fatalf("expected '%s', got '%s'", alias, rb.Alias)
|
||||
}
|
||||
@@ -56,12 +58,12 @@ func TestApiRequestAlias(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
addr = ra.PublicKey
|
||||
|
||||
alias = "foox"
|
||||
rb, err = svc.RequestAlias(ctx, addr, alias)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
alias = "foox"
|
||||
alias = "foox.sarafu.local"
|
||||
if rb.Alias != alias {
|
||||
t.Fatalf("expected '%s', got '%s'", alias, rb.Alias)
|
||||
}
|
||||
|
||||
32
dev/data/swap_from.json
Normal file
32
dev/data/swap_from.json
Normal file
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"ok": true,
|
||||
"description": "Swap from list",
|
||||
"result": {
|
||||
"filtered": [
|
||||
{
|
||||
"contractAddress": "0xc7B78Ac9ACB9E025C8234621FC515bC58179dEAe",
|
||||
"tokenSymbol": "AMANI",
|
||||
"tokenDecimals": "6",
|
||||
"balance": ""
|
||||
},
|
||||
{
|
||||
"contractAddress": "0xF0C3C7581b8b96B59a97daEc8Bd48247cE078674",
|
||||
"tokenSymbol": "AMUA",
|
||||
"tokenDecimals": "6",
|
||||
"balance": ""
|
||||
},
|
||||
{
|
||||
"contractAddress": "0x371455a30fc62736145Bd8429Fcc6481186f235F",
|
||||
"tokenSymbol": "BAHARI",
|
||||
"tokenDecimals": "6",
|
||||
"balance": ""
|
||||
},
|
||||
{
|
||||
"contractAddress": "0x7cA6113b59c24a880F382C7E12d609a6Eb05246b",
|
||||
"tokenSymbol": "BANGLA",
|
||||
"tokenDecimals": "6",
|
||||
"balance": ""
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
14
dev/data/swap_to.json
Normal file
14
dev/data/swap_to.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"ok": true,
|
||||
"description": "Swap to list",
|
||||
"result": {
|
||||
"filtered": [
|
||||
{
|
||||
"contractAddress": "0x765DE816845861e75A25fCA122bb6898B8B1282a",
|
||||
"tokenSymbol": "cUSD",
|
||||
"tokenDecimals": "18",
|
||||
"balance": ""
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
43
dev/data/top_pools.json
Normal file
43
dev/data/top_pools.json
Normal file
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"ok": true,
|
||||
"description": "Top 5 pools sorted by swaps",
|
||||
"result": {
|
||||
"topPools": [
|
||||
{
|
||||
"poolName": "Kenya ROLA Pool",
|
||||
"poolSymbol": "ROLA",
|
||||
"poolContractAddress": "0x48a953cA5cf5298bc6f6Af3C608351f537AAcb9e",
|
||||
"limiterAddress": "",
|
||||
"voucherRegistry": ""
|
||||
},
|
||||
{
|
||||
"poolName": "Nairobi ROLA Pool",
|
||||
"poolSymbol": "NAIROBI",
|
||||
"poolContractAddress": "0xB0660Ac1Ee3d32ea35bc728D7CA1705Fa5A37528",
|
||||
"limiterAddress": "",
|
||||
"voucherRegistry": ""
|
||||
},
|
||||
{
|
||||
"poolName": "Friends of Kiriba Ecosystem ",
|
||||
"poolSymbol": "FRIENDS",
|
||||
"poolContractAddress": "0xC4848263821FA02baB2181910A2eFb9CECb2c21C",
|
||||
"limiterAddress": "",
|
||||
"voucherRegistry": ""
|
||||
},
|
||||
{
|
||||
"poolName": "Resilient Community Waqfs",
|
||||
"poolSymbol": "REZILIENS",
|
||||
"poolContractAddress": "0x1e40951d7a28147D8B4A554C60c42766C92e2Fc6",
|
||||
"limiterAddress": "",
|
||||
"voucherRegistry": ""
|
||||
},
|
||||
{
|
||||
"poolName": "GrE Tech",
|
||||
"poolSymbol": "GRET",
|
||||
"poolContractAddress": "0xb7B9d0A264eD1a8E2418571B7AC5933C79C9c2B8",
|
||||
"limiterAddress": "",
|
||||
"voucherRegistry": ""
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
20
event/msg.go
20
event/msg.go
@@ -8,12 +8,12 @@ import (
|
||||
const (
|
||||
// TODO: integrate with sarafu-vise-events
|
||||
EventTokenTransferTag = "TOKEN_TRANSFER"
|
||||
EventTokenMintTag = "TOKEN_MINT"
|
||||
EventRegistrationTag = "CUSTODIAL_REGISTRATION"
|
||||
EventTokenMintTag = "TOKEN_MINT"
|
||||
EventRegistrationTag = "CUSTODIAL_REGISTRATION"
|
||||
)
|
||||
|
||||
type Msg struct {
|
||||
Typ string
|
||||
Typ string
|
||||
Item any
|
||||
}
|
||||
|
||||
@@ -26,17 +26,17 @@ type EventCustodialRegistration struct {
|
||||
|
||||
// fields used for handling token transfer event.
|
||||
type EventTokenTransfer struct {
|
||||
To string
|
||||
Value int
|
||||
To string
|
||||
Value int
|
||||
VoucherAddress string
|
||||
TxHash string
|
||||
From string
|
||||
TxHash string
|
||||
From string
|
||||
}
|
||||
|
||||
type EventTokenMint struct {
|
||||
To string
|
||||
Value int
|
||||
TxHash string
|
||||
To string
|
||||
Value int
|
||||
TxHash string
|
||||
VoucherAddress string
|
||||
}
|
||||
|
||||
|
||||
6
go.mod
6
go.mod
@@ -3,9 +3,9 @@ module git.grassecon.net/grassrootseconomics/sarafu-api
|
||||
go 1.23.4
|
||||
|
||||
require (
|
||||
git.defalsify.org/vise.git v0.2.3-0.20250114225117-3b5fc85b650b
|
||||
git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05
|
||||
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115003256-c0534ede1b63
|
||||
git.defalsify.org/vise.git v0.2.3-0.20250120121301-10739fb4a8c9
|
||||
git.grassecon.net/grassrootseconomics/common v0.0.0-20250121134736-ba8cbbccea7d
|
||||
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250122123424-6749c632b0a2
|
||||
github.com/gofrs/uuid v4.4.0+incompatible
|
||||
github.com/grassrootseconomics/eth-custodial v1.3.0-beta
|
||||
github.com/grassrootseconomics/ussd-data-service v1.2.0-beta
|
||||
|
||||
12
go.sum
12
go.sum
@@ -1,9 +1,9 @@
|
||||
git.defalsify.org/vise.git v0.2.3-0.20250114225117-3b5fc85b650b h1:rwWXMtNSn7aqhb4p1oVZkCA1vC7pVdohwW61QQM8fUs=
|
||||
git.defalsify.org/vise.git v0.2.3-0.20250114225117-3b5fc85b650b/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck=
|
||||
git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05 h1:BenzGx6aDHKDwE23/mWIFD2InYIXyzHroZWV3MF5WUk=
|
||||
git.grassecon.net/grassrootseconomics/common v0.0.0-20250113174703-6afccefd1f05/go.mod h1:wgQJZGIS6QuNLHqDhcsvehsbn5PvgV7aziRebMnJi60=
|
||||
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115003256-c0534ede1b63 h1:bX7klKZpX+ZZu1LKbtOXDAhV/KK0YwExehiIi0jusAM=
|
||||
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250115003256-c0534ede1b63/go.mod h1:Syw9TZyigPAM7t9FvicOm36iUnidt45f0SxzT2JniQ8=
|
||||
git.defalsify.org/vise.git v0.2.3-0.20250120121301-10739fb4a8c9 h1:sPcqXQcywxA8W3W+9qQncLPmsrgqTIlec7vmD4/7vyA=
|
||||
git.defalsify.org/vise.git v0.2.3-0.20250120121301-10739fb4a8c9/go.mod h1:jyBMe1qTYUz3mmuoC9JQ/TvFeW0vTanCUcPu3H8p4Ck=
|
||||
git.grassecon.net/grassrootseconomics/common v0.0.0-20250121134736-ba8cbbccea7d h1:5mzLas+jxTUtusOKx4XvU+n2QvrV/mH17MnJRy46siQ=
|
||||
git.grassecon.net/grassrootseconomics/common v0.0.0-20250121134736-ba8cbbccea7d/go.mod h1:wgQJZGIS6QuNLHqDhcsvehsbn5PvgV7aziRebMnJi60=
|
||||
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250122123424-6749c632b0a2 h1:ON77G5K0JNuwPb5JT/hRfF6G6+xstlBQgEIEzWydnhg=
|
||||
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250122123424-6749c632b0a2/go.mod h1:pjKp9L/ZsWW3kMB0UoIl1yv9TBIuU33mn9Aghxp7vGk=
|
||||
github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c h1:H9Nm+I7Cg/YVPpEV1RzU3Wq2pjamPc/UtHDgItcb7lE=
|
||||
github.com/barbashov/iso639-3 v0.0.0-20211020172741-1f4ffb2d8d1c/go.mod h1:rGod7o6KPeJ+hyBpHfhi4v7blx9sf+QsHsA7KAsdN6U=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
|
||||
10
models/pool.go
Normal file
10
models/pool.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package models
|
||||
|
||||
|
||||
type Pool struct {
|
||||
PoolName string `json:"poolName"`
|
||||
PoolSymbol string `json:"poolSymbol"`
|
||||
PoolContractAddress string `json:"poolContractAddress"`
|
||||
LimiterAddress string `json:"limiterAddress"`
|
||||
VoucherRegistry string `json:"voucherRegistry"`
|
||||
}
|
||||
14
models/pool_swap_response.go
Normal file
14
models/pool_swap_response.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package models
|
||||
|
||||
type PoolDepositResult struct {
|
||||
TrackingId string `json:"trackingId"`
|
||||
}
|
||||
|
||||
type PoolSwapQuoteResult struct {
|
||||
IncludesFeesDeduction bool `json:"includesFeesDeduction"`
|
||||
OutValue string `json:"outValue"`
|
||||
}
|
||||
|
||||
type PoolSwapResult struct {
|
||||
TrackingId string `json:"trackingId"`
|
||||
}
|
||||
@@ -8,3 +8,10 @@ type VoucherDataResult struct {
|
||||
TokenCommodity string `json:"tokenCommodity"`
|
||||
TokenLocation string `json:"tokenLocation"`
|
||||
}
|
||||
|
||||
type SwappableVoucher struct {
|
||||
ContractAddress string `json:"contractAddress"`
|
||||
TokenSymbol string `json:"tokenSymbol"`
|
||||
TokenDecimals string `json:"tokenDecimals"`
|
||||
Balance string `json:"balance"`
|
||||
}
|
||||
|
||||
@@ -3,21 +3,30 @@ package http
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/config"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/dev"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
||||
"git.grassecon.net/grassrootseconomics/visedriver/storage"
|
||||
"github.com/grassrootseconomics/eth-custodial/pkg/api"
|
||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||
)
|
||||
|
||||
var (
|
||||
aliasRegex = regexp.MustCompile("^\\+?[a-zA-Z0-9\\-_]+$")
|
||||
)
|
||||
|
||||
type HTTPAccountService struct {
|
||||
SS storage.StorageService
|
||||
UseApi bool
|
||||
}
|
||||
|
||||
// Parameters:
|
||||
@@ -204,24 +213,132 @@ func (as *HTTPAccountService) TokenTransfer(ctx context.Context, amount, from, t
|
||||
// Parameters:
|
||||
// - alias: The alias of the user.
|
||||
func (as *HTTPAccountService) CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
|
||||
if as.SS == nil {
|
||||
return nil, fmt.Errorf("The storage service cannot be nil")
|
||||
}
|
||||
svc := dev.NewDevAccountService(ctx, as.SS)
|
||||
if as.UseApi {
|
||||
return resolveAliasAddress(ctx, alias)
|
||||
} else {
|
||||
return svc.CheckAliasAddress(ctx, alias)
|
||||
}
|
||||
}
|
||||
|
||||
func resolveAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
|
||||
var r models.AliasAddress
|
||||
|
||||
ep, err := url.JoinPath(config.CheckAliasURL, alias)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", ep, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = doRequest(ctx, req, &r)
|
||||
return &r, err
|
||||
}
|
||||
|
||||
func (as *HTTPAccountService) FetchTopPools(ctx context.Context) ([]models.Pool, error) {
|
||||
svc := dev.NewDevAccountService(ctx, as.SS)
|
||||
return svc.FetchTopPools(ctx)
|
||||
}
|
||||
|
||||
func (as *HTTPAccountService) PoolDeposit(ctx context.Context, amount, from, poolAddress, tokenAddress string) (*models.PoolDepositResult, error) {
|
||||
var r models.PoolDepositResult
|
||||
|
||||
//pool deposit payload
|
||||
payload := map[string]string{
|
||||
"amount": amount,
|
||||
"from": from,
|
||||
"poolAddress": poolAddress,
|
||||
"tokenAddress": tokenAddress,
|
||||
}
|
||||
|
||||
payloadBytes, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req, err := http.NewRequest("POST", config.TokenTransferURL, bytes.NewBuffer(payloadBytes))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = doRequest(ctx, req, &r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
func (as *HTTPAccountService) GetPoolSwapQuote(ctx context.Context, amount, from, fromTokenAddress, poolAddress, toTokenAddress string) (*models.PoolSwapQuoteResult, error) {
|
||||
var r models.PoolSwapQuoteResult
|
||||
|
||||
//pool swap quote payload
|
||||
payload := map[string]string{
|
||||
"amount": amount,
|
||||
"from": from,
|
||||
"fromTokenAddress": fromTokenAddress,
|
||||
"poolAddress": poolAddress,
|
||||
"toTokenAddress": toTokenAddress,
|
||||
}
|
||||
|
||||
payloadBytes, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req, err := http.NewRequest("POST", config.PoolSwapQuoteURL, bytes.NewBuffer(payloadBytes))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = doRequest(ctx, req, &r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
func (as *HTTPAccountService) GetPoolSwappableFromVouchers(ctx context.Context) ([]models.SwappableVoucher, error) {
|
||||
return as.GetPoolSwappableFromVouchers(ctx)
|
||||
}
|
||||
|
||||
func (as *HTTPAccountService) GetPoolSwappableVouchers(ctx context.Context) ([]models.SwappableVoucher, error) {
|
||||
return as.GetPoolSwappableVouchers(ctx)
|
||||
}
|
||||
|
||||
func (as *HTTPAccountService) PoolSwap(ctx context.Context, amount, from, fromTokenAddress, poolAddress, toTokenAddress string) (*models.PoolSwapResult, error) {
|
||||
var r models.PoolSwapResult
|
||||
|
||||
//swap payload
|
||||
payload := map[string]string{
|
||||
"amount": amount,
|
||||
"from": from,
|
||||
"fromTokenAddress": fromTokenAddress,
|
||||
"poolAddress": poolAddress,
|
||||
"toTokenAddress": toTokenAddress,
|
||||
}
|
||||
|
||||
payloadBytes, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req, err := http.NewRequest("POST", config.PoolSwapQuoteURL, bytes.NewBuffer(payloadBytes))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = doRequest(ctx, req, &r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
// TODO: Use actual custodial api to request available alias
|
||||
func (as *HTTPAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
|
||||
return nil, fmt.Errorf("not yet implemented")
|
||||
if as.SS == nil {
|
||||
return nil, fmt.Errorf("The storage service cannot be nil")
|
||||
}
|
||||
svc := dev.NewDevAccountService(ctx, as.SS)
|
||||
return svc.RequestAlias(ctx, publicKey, hint)
|
||||
}
|
||||
|
||||
// TODO: remove eth-custodial api dependency
|
||||
|
||||
@@ -4,8 +4,8 @@ import (
|
||||
"context"
|
||||
|
||||
"git.defalsify.org/vise.git/logging"
|
||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
||||
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -14,52 +14,50 @@ var (
|
||||
|
||||
const (
|
||||
AliceChecksum = "0xeae046BF396e91f5A8D74f863dC57c107c8a4a70"
|
||||
BobChecksum = "0xB3117202371853e24B725d4169D87616A7dDb127"
|
||||
AliceSession = "5553425"
|
||||
BobChecksum = "0xB3117202371853e24B725d4169D87616A7dDb127"
|
||||
AliceSession = "5553425"
|
||||
)
|
||||
|
||||
type MockApi struct {
|
||||
TransactionsContent []dataserviceapi.Last10TxResponse
|
||||
VouchersContent []dataserviceapi.TokenHoldings
|
||||
VoucherDataContent *models.VoucherDataResult
|
||||
VouchersContent []dataserviceapi.TokenHoldings
|
||||
VoucherDataContent *models.VoucherDataResult
|
||||
}
|
||||
|
||||
func(m MockApi) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResult, error) {
|
||||
func (m MockApi) CheckBalance(ctx context.Context, publicKey string) (*models.BalanceResult, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func(m MockApi) CreateAccount(ctx context.Context) (*models.AccountResult, error) {
|
||||
func (m MockApi) CreateAccount(ctx context.Context) (*models.AccountResult, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func(m MockApi) TrackAccountStatus(ctx context.Context, publicKey string) (*models.TrackStatusResult, error) {
|
||||
func (m MockApi) TrackAccountStatus(ctx context.Context, publicKey string) (*models.TrackStatusResult, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func(m MockApi) FetchVouchers(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
|
||||
func (m MockApi) FetchVouchers(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
|
||||
logg.DebugCtxf(ctx, "mockapi fetchvouchers", "key", publicKey)
|
||||
return m.VouchersContent, nil
|
||||
}
|
||||
|
||||
func(m MockApi) FetchTransactions(ctx context.Context, publicKey string) ([]dataserviceapi.Last10TxResponse, error) {
|
||||
func (m MockApi) FetchTransactions(ctx context.Context, publicKey string) ([]dataserviceapi.Last10TxResponse, error) {
|
||||
logg.DebugCtxf(ctx, "mockapi fetchtransactions", "key", publicKey)
|
||||
return m.TransactionsContent, nil
|
||||
}
|
||||
|
||||
func(m MockApi) VoucherData(ctx context.Context, address string) (*models.VoucherDataResult, error) {
|
||||
func (m MockApi) VoucherData(ctx context.Context, address string) (*models.VoucherDataResult, error) {
|
||||
return m.VoucherDataContent, nil
|
||||
}
|
||||
|
||||
func(m MockApi) CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
|
||||
func (m MockApi) CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func(m MockApi) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
|
||||
func (m MockApi) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func(m MockApi) TokenTransfer(ctx context.Context, amount, from, to, tokenAddress string) (*models.TokenTransferResponse, error) {
|
||||
func (m MockApi) TokenTransfer(ctx context.Context, amount, from, to, tokenAddress string) (*models.TokenTransferResponse, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ func (m *MockAccountService) CheckAliasAddress(ctx context.Context, alias string
|
||||
return args.Get(0).(*models.AliasAddress), args.Error(1)
|
||||
}
|
||||
|
||||
func(m MockAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
|
||||
func (m MockAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
|
||||
args := m.Called(publicKey, hint)
|
||||
return args.Get(0).(*models.RequestAliasResult), args.Error(1)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user