Compare commits

...

80 Commits

Author SHA1 Message Date
d177942bd2 Merge pull request 'pool-swap-endpoints' (#11) from pool-swap-endpoints into master
Reviewed-on: #11
2025-06-26 07:58:53 +02:00
339ba854c9
fix: correctly unmarshal nested pool details response in retrievePoolDeta 2025-06-24 12:07:44 +03:00
5aa032400c
update the alias endpoints 2025-06-24 10:48:30 +03:00
7b42d509e6
log the request and response body 2025-06-23 10:50:57 +03:00
d945964b0b
use the TokenAddress instead of ContractAddress 2025-06-23 10:00:26 +03:00
c1797e7a32
use the TokenHoldings instead of TokenDetails 2025-06-23 09:32:34 +03:00
54dfe037b4
updated the ussd-data-service to tag v1.5.0-beta 2025-06-23 09:31:53 +03:00
b5ccaea575
add the RetrievePoolDetails to the testutil and dev account service 2025-06-06 22:42:35 +03:00
bf830e92de
add the RetrievePoolDetails to the account_service 2025-06-05 20:41:08 +03:00
24224e553d
use the correct PoolSwapURL when performing the actual swap 2025-05-22 15:31:08 +03:00
6c3719e3b6
Fix: use the correct TokenDetails in the GetPoolSwappableVouchers 2025-05-21 17:12:46 +03:00
62cc5eed89
use the updated ussd-data-service 2025-05-21 17:10:25 +03:00
343d30a2f2
rename the variables and read the stables param from the config 2025-05-21 15:42:06 +03:00
a354371a94
add the IncludeStablesParam config 2025-05-21 15:40:56 +03:00
857f564aae
Fix: remove the unused publicKey from the GetPoolSwappableVouchers 2025-05-21 15:22:31 +03:00
f234d51104
Fix: update the getPoolSwappableVouchers to pass a 'stable' query parameter 2025-05-21 15:21:27 +03:00
81fbc2574c
Fix: use the correct getPoolSwappableVouchers 2025-05-20 15:50:35 +03:00
334aa07f9f
Fix: Correct JSON mapping for checkTokenInPool and getSwapFromTokenMaxLimit API responses 2025-05-20 15:48:13 +03:00
e6677015d9
use the latest ussd-data-service 2025-05-19 10:36:47 +03:00
050998ff82
added CheckTokenInPool to the account_service and test utils 2025-05-17 14:45:12 +03:00
ee434dba69
added CheckTokenInPool to check whether a token can be swapped from in a pool 2025-05-17 14:37:06 +03:00
f101ffd4c9
added a model for TokenInPoolResult 2025-05-17 14:36:26 +03:00
8b2bd72143
format document 2025-05-17 14:35:53 +03:00
3b85167ad8
Merge branch 'master' into pool-swap-endpoints 2025-05-16 12:43:26 +03:00
5d221b8d56 Merge pull request 'sms-address-pin-reset' (#14) from sms-address-pin-reset into master
Reviewed-on: #14
2025-04-28 10:27:11 +02:00
341901ec83
match new external sms endpoint 2025-04-28 11:26:13 +03:00
ed416c57c7
chore: rename EXTRA_SMS_BASE to EXTERNAL_SMS_BASE 2025-04-28 11:15:11 +03:00
6fc99661f7
chore: remove extra underscore in EXTRA_SMS_BASE env identifier 2025-04-28 11:08:39 +03:00
d509477de3
remove accidentallypushed bearer token 2025-04-28 11:03:34 +03:00
4ea8fd1f04
chore: rename extrasms prefixes to external sms 2025-04-28 10:56:05 +03:00
5c0ed48e67
add required sms methods 2025-04-17 23:14:42 +03:00
5ff139b649
add expected accountservice methods 2025-04-17 23:07:33 +03:00
86cfb9c020
match expected method signature 2025-04-17 22:18:30 +03:00
58366274c3
add required AccountService methods 2025-04-17 18:58:12 +03:00
b85511e436
implement SendPINResetSMS and SendAddressSMS 2025-04-17 18:57:24 +03:00
a243fe8c29
add SendAddressSMS and SendPINResetSMS 2025-04-17 18:57:02 +03:00
492b101aa4
setup extra sms api endpoints 2025-04-17 18:56:20 +03:00
34957e5b6f Merge pull request 'sms-upsell-api' (#10) from sms-api into master
Reviewed-on: #10
2025-04-11 10:06:08 +02:00
6597d6b6a2
added the SendUpsellSMS to DevAccountService and MockApi 2025-04-11 11:02:23 +03:00
441e289854
implement SendUpsellSMs 2025-04-01 15:25:10 +03:00
5b41c8dc64
chore: rename SendSMS to SendUpsellSMS 2025-04-01 14:55:03 +03:00
417e7f0179
fix: correct sendSms response 2025-04-01 14:53:30 +03:00
2eed990921
Merge branch 'master' into sms-api 2025-04-01 14:18:04 +03:00
56ed9b2439
Merge branch 'multi-pool' into pool-swap-endpoints 2025-03-20 08:35:22 +03:00
064e749b04
Merge branch 'master' into pool-swap-endpoints 2025-03-19 15:22:30 +03:00
80c2493e80
implement get token pool limit,get all pool swappable vouchers 2025-03-19 12:09:52 +03:00
4444ac0255
add pool swap endpoints 2025-03-19 12:07:11 +03:00
720fa94b56
register pool endpoints 2025-03-17 20:40:56 +03:00
ee04552cbb
feat: implement fetch top pools,retrieve pool details using a pool symbol 2025-03-17 20:40:19 +03:00
c09cb77e19
implement pool limit 2025-03-17 12:08:17 +03:00
e053d561c9
feat(pool): add multiple pools 2025-03-14 10:04:04 +03:00
b9cef2b578
Merge branch 'pool-swap-endpoints' into multi-pool 2025-03-14 09:03:09 +03:00
06a7ddea6f
read persisted vouchers for a particular pool 2025-03-13 10:35:21 +03:00
ba43610ff0
Added pool swap functions to the MockAccountService 2025-03-13 10:14:11 +03:00
3c2889ac72
Added pool swap functions to the TestAccountService 2025-03-13 10:05:49 +03:00
5432365874
add logs,use same pool instance 2025-03-12 23:20:01 +03:00
3244c717cb
Added the MaxLimitResult struct model 2025-03-11 15:46:51 +03:00
17e89e0b88
Added the GetSwapFromTokenMaxLimit 2025-03-11 15:46:17 +03:00
3b39b86d09
pass the correct arguments to the swap functions 2025-03-10 17:12:05 +03:00
37a2006a0b
feat: persist and load pool info 2025-03-10 16:50:29 +03:00
d4f27af7a7
add check for account when doing a pool deposit 2025-03-10 16:50:29 +03:00
ba8d2a19c2
Use the DevAccountService functions to retrieve the data 2025-03-10 16:39:37 +03:00
e3b6c25792
Added the swap functions to the accountService interface 2025-03-10 14:17:03 +03:00
a04f7ee66c
Remove unused model structs 2025-03-10 14:03:30 +03:00
52e1be1104
Return an array of data instead of reading from the file 2025-03-10 14:00:16 +03:00
d7ae1cc096
Include the account's public key 2025-03-10 13:59:05 +03:00
8145b4bd00
fix: remove formatting to fqdn 2025-03-10 12:39:12 +03:00
dda02852bf
add alias resolve logs 2025-03-10 11:49:49 +03:00
6e437cb8e0
feat: sanitize alias hint before resolving 2025-03-07 16:10:40 +03:00
4af0e9709d
fix: format alias to fqdn before request 2025-03-07 16:04:15 +03:00
9c8a3df971
add logs 2025-03-07 09:45:09 +03:00
62c33e2587 Merge pull request 'ens-names' (#8) from ens-names into master
Reviewed-on: #8
2025-03-07 07:27:29 +01:00
4783e2dcb3
Merge branch 'pool-swap-endpoints-issue#9' into pool-swap-endpoints 2025-03-07 08:16:49 +03:00
35f24b5183
Use the latest ussd-data-service 2025-03-06 18:14:34 +03:00
4a5de68a8c
added the SendSMS func to the httpAccount service 2025-03-06 09:06:45 +03:00
6c4702e2ba
added the send sms response model 2025-03-06 09:05:39 +03:00
7280784ee1
added the send sms endpoint configs 2025-03-06 09:05:01 +03:00
1f3ac220d1
feat: request and resolve aliases. 2025-02-24 14:49:58 +03:00
e64eb265a5
add alias responses 2025-02-24 14:48:00 +03:00
d5dc792dce
add alias ens endpoint 2025-02-24 14:45:45 +03:00
14 changed files with 726 additions and 170 deletions

View File

@ -15,16 +15,25 @@ const (
voucherHoldingsPathPrefix = "/api/v1/holdings" voucherHoldingsPathPrefix = "/api/v1/holdings"
voucherTransfersPathPrefix = "/api/v1/transfers/last10" voucherTransfersPathPrefix = "/api/v1/transfers/last10"
voucherDataPathPrefix = "/api/v1/token" voucherDataPathPrefix = "/api/v1/token"
aliasPrefix = "api/v1/alias" SendSMSPrefix = "api/v1/external/upsell"
poolDepositPrefix = "/api/v2/pool/deposit" poolDepositPrefix = "/api/v2/pool/deposit"
poolSwapQoutePrefix = "/api/v2/pool/quote" poolSwapQoutePrefix = "/api/v2/pool/quote"
poolSwapPrefix = "/api/v2/pool/swap" poolSwapPrefix = "/api/v2/pool/swap"
topPoolsPrefix = "/api/v1/pool/top"
retrievePoolDetailsPrefix = "/api/v1/pool/reverse"
poolSwappableVouchersPrefix = "/api/v1/pool"
AliasRegistrationPrefix = "/api/v1/internal/register"
AliasResolverPrefix = "/api/v1/resolve"
ExternalSMSPrefix = "/api/v1/external"
) )
var ( var (
custodialURLBase string custodialURLBase string
dataURLBase string dataURLBase string
BearerToken string BearerToken string
aliasEnsURLBase string
externalSMSBase string
IncludeStablesParam string
) )
var ( var (
@ -36,10 +45,16 @@ var (
VoucherHoldingsURL string VoucherHoldingsURL string
VoucherTransfersURL string VoucherTransfersURL string
VoucherDataURL string VoucherDataURL string
CheckAliasURL string
PoolDepositURL string PoolDepositURL string
PoolSwapQuoteURL string PoolSwapQuoteURL string
PoolSwapURL string PoolSwapURL string
TopPoolsURL string
RetrievePoolDetailsURL string
PoolSwappableVouchersURL string
SendSMSURL string
AliasRegistrationURL string
AliasResolverURL string
ExternalSMSURL string
) )
func setBase() error { func setBase() error {
@ -47,7 +62,10 @@ func setBase() error {
custodialURLBase = env.GetEnv("CUSTODIAL_URL_BASE", "http://localhost:5003") custodialURLBase = env.GetEnv("CUSTODIAL_URL_BASE", "http://localhost:5003")
dataURLBase = env.GetEnv("DATA_URL_BASE", "http://localhost:5006") dataURLBase = env.GetEnv("DATA_URL_BASE", "http://localhost:5006")
aliasEnsURLBase = env.GetEnv("ALIAS_ENS_BASE", "http://localhost:5015")
externalSMSBase = env.GetEnv("EXTERNAL_SMS_BASE", "http://localhost:5035")
BearerToken = env.GetEnv("BEARER_TOKEN", "") BearerToken = env.GetEnv("BEARER_TOKEN", "")
IncludeStablesParam = env.GetEnv("INCLUDE_STABLES_PARAM", "false")
_, err = url.Parse(custodialURLBase) _, err = url.Parse(custodialURLBase)
if err != nil { if err != nil {
@ -74,10 +92,16 @@ func LoadConfig() error {
VoucherHoldingsURL, _ = url.JoinPath(dataURLBase, voucherHoldingsPathPrefix) VoucherHoldingsURL, _ = url.JoinPath(dataURLBase, voucherHoldingsPathPrefix)
VoucherTransfersURL, _ = url.JoinPath(dataURLBase, voucherTransfersPathPrefix) VoucherTransfersURL, _ = url.JoinPath(dataURLBase, voucherTransfersPathPrefix)
VoucherDataURL, _ = url.JoinPath(dataURLBase, voucherDataPathPrefix) VoucherDataURL, _ = url.JoinPath(dataURLBase, voucherDataPathPrefix)
CheckAliasURL, _ = url.JoinPath(dataURLBase, aliasPrefix) SendSMSURL, _ = url.JoinPath(dataURLBase, SendSMSPrefix)
PoolDepositURL, _ = url.JoinPath(custodialURLBase, poolDepositPrefix) PoolDepositURL, _ = url.JoinPath(custodialURLBase, poolDepositPrefix)
PoolSwapQuoteURL, _ = url.JoinPath(custodialURLBase, poolSwapQoutePrefix) PoolSwapQuoteURL, _ = url.JoinPath(custodialURLBase, poolSwapQoutePrefix)
PoolSwapURL, _ = url.JoinPath(custodialURLBase, poolSwapPrefix) PoolSwapURL, _ = url.JoinPath(custodialURLBase, poolSwapPrefix)
TopPoolsURL, _ = url.JoinPath(dataURLBase, topPoolsPrefix)
RetrievePoolDetailsURL, _ = url.JoinPath(dataURLBase, retrievePoolDetailsPrefix)
PoolSwappableVouchersURL, _ = url.JoinPath(dataURLBase, poolSwappableVouchersPrefix)
AliasRegistrationURL, _ = url.JoinPath(aliasEnsURLBase, AliasRegistrationPrefix)
AliasResolverURL, _ = url.JoinPath(aliasEnsURLBase, AliasResolverPrefix)
ExternalSMSURL, _ = url.JoinPath(externalSMSBase, ExternalSMSPrefix)
return nil return nil
} }

View File

@ -6,9 +6,7 @@ import (
"crypto/sha1" "crypto/sha1"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"math/rand" "math/rand"
"os"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -36,6 +34,9 @@ const (
defaultDecimals = 6 defaultDecimals = 6
zeroAddress string = "0x0000000000000000000000000000000000000000" zeroAddress string = "0x0000000000000000000000000000000000000000"
defaultVoucherBalance float64 = 500.00 defaultVoucherBalance float64 = 500.00
cityPoolAddress string = "0x3b517308D858a47458aD5C8E699697C5dc91Da0F"
poolName string = "citypool"
PoolSymbol string = "CTY"
) )
type Tx struct { type Tx struct {
@ -94,8 +95,11 @@ type Voucher struct {
} }
type Pool struct { type Pool struct {
vouchers []Voucher Name string `json: "name"`
poolLimit map[string]string Symbol string `json: "symbol"`
Address string `json: "address"`
Vouchers []Voucher `json: "voucher"`
PoolLimit map[string]string `json: "poollimit"`
} }
type DevAccountService struct { type DevAccountService struct {
@ -113,7 +117,7 @@ type DevAccountService struct {
defaultAccount string defaultAccount string
emitterFunc event.EmitterFunc emitterFunc event.EmitterFunc
pfx []byte pfx []byte
pool Pool pools map[string]Pool
} }
func NewDevAccountService(ctx context.Context, ss storage.StorageService) *DevAccountService { func NewDevAccountService(ctx context.Context, ss storage.StorageService) *DevAccountService {
@ -126,7 +130,7 @@ func NewDevAccountService(ctx context.Context, ss storage.StorageService) *DevAc
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),
pool: Pool{}, pools: make(map[string]Pool),
defaultAccount: zeroAddress, defaultAccount: zeroAddress,
pfx: []byte("__"), pfx: []byte("__"),
} }
@ -181,7 +185,7 @@ func (das *DevAccountService) loadAccount(ctx context.Context, pubKey string, v
} }
func (p *Pool) hasVoucher(voucherAddress string) bool { func (p *Pool) hasVoucher(voucherAddress string) bool {
for _, value := range p.vouchers { for _, value := range p.Vouchers {
if value.Address == voucherAddress { if value.Address == voucherAddress {
return true return true
} }
@ -211,6 +215,17 @@ func (das *DevAccountService) loadAlias(ctx context.Context, alias string, key [
return nil return nil
} }
func (das *DevAccountService) loadPoolInfo(ctx context.Context, name string, v []byte) error {
var pool Pool
err := json.Unmarshal(v, &pool)
if err != nil {
return fmt.Errorf("failed to unmarshall pool info: %v", err)
}
das.pools[name] = pool
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)
@ -227,6 +242,8 @@ func (das *DevAccountService) loadItem(ctx context.Context, k []byte, v []byte)
} else if ss[0] == "alias" { } else if ss[0] == "alias" {
err = das.loadAlias(ctx, ss[1], k) err = das.loadAlias(ctx, ss[1], k)
logg.ErrorCtxf(ctx, "loading aliases failed", "error_load_aliases", err) logg.ErrorCtxf(ctx, "loading aliases failed", "error_load_aliases", err)
} else if ss[0] == "pool" {
err = das.loadPoolInfo(ctx, ss[1], v)
} else { } else {
logg.ErrorCtxf(ctx, "unknown double underscore key", "key", ss[0]) logg.ErrorCtxf(ctx, "unknown double underscore key", "key", ss[0])
} }
@ -242,13 +259,13 @@ func (das *DevAccountService) loadAll(ctx context.Context) error {
} }
for true { for true {
k, v := dumper.Next(ctx) k, v := dumper.Next(ctx)
logg.InfoCtxf(ctx, "loading all", "key", string(k), "value", string(v))
if k == nil { if k == nil {
break break
} }
if !bytes.HasPrefix(k, das.pfx) { if !bytes.HasPrefix(k, das.pfx) {
continue continue
} }
err = das.loadItem(ctx, k, v) err = das.loadItem(ctx, k, v)
if err != nil { if err != nil {
return err return err
@ -283,6 +300,35 @@ func (das *DevAccountService) WithAutoVoucher(ctx context.Context, symbol string
return das return das
} }
func (das *DevAccountService) RegisterPool(ctx context.Context, name string, sm string) error {
var seedVouchers []Voucher
h := sha1.New()
h.Write([]byte(sm))
z := h.Sum(nil)
pooladdr := fmt.Sprintf("0x%x", z)
p := Pool{
Name: name,
Symbol: sm,
Address: pooladdr,
PoolLimit: make(map[string]string),
}
for _, v := range das.vouchers {
//pre-load vouchers with vouchers when a pool is registered
seedVouchers = append(seedVouchers, v)
p.PoolLimit[v.Address] = fmt.Sprintf("%f", defaultVoucherBalance)
}
p.Vouchers = append(p.Vouchers, seedVouchers...)
err := das.savePoolInfo(ctx, p)
if err != nil {
return err
}
return nil
}
// TODO: add persistence for vouchers // TODO: add persistence for vouchers
// TODO: set max balance for 0x00 address // TODO: set max balance for 0x00 address
func (das *DevAccountService) AddVoucher(ctx context.Context, symbol string) error { func (das *DevAccountService) AddVoucher(ctx context.Context, symbol string) error {
@ -363,6 +409,20 @@ func (das *DevAccountService) saveAccount(ctx context.Context, acc Account) erro
return das.db.Put(ctx, []byte(k), v) return das.db.Put(ctx, []byte(k), v)
} }
func (das *DevAccountService) savePoolInfo(ctx context.Context, pool Pool) error {
if das.db == nil {
return nil
}
k := das.prefixKeyFor("pool", pool.Address)
v, err := json.Marshal(pool)
if err != nil {
return err
}
das.db.SetSession("")
das.db.SetPrefix(db.DATATYPE_USERDATA)
return das.db.Put(ctx, []byte(k), v)
}
func (das *DevAccountService) saveAlias(ctx context.Context, alias map[string]string) error { func (das *DevAccountService) saveAlias(ctx context.Context, alias map[string]string) error {
if das.db == nil { if das.db == nil {
return fmt.Errorf("Db cannot be nil") return fmt.Errorf("Db cannot be nil")
@ -434,6 +494,10 @@ func (das *DevAccountService) CreateAccount(ctx context.Context) (*models.Accoun
} }
func (das *DevAccountService) PoolDeposit(ctx context.Context, amount, from, poolAddress, tokenAddress string) (*models.PoolDepositResult, error) { func (das *DevAccountService) PoolDeposit(ctx context.Context, amount, from, poolAddress, tokenAddress string) (*models.PoolDepositResult, error) {
_, ok := das.accounts[from]
if !ok {
return nil, fmt.Errorf("account not found (publickey): %v", from)
}
sym, ok := das.vouchersAddress[tokenAddress] sym, ok := das.vouchersAddress[tokenAddress]
if !ok { if !ok {
return nil, fmt.Errorf("voucher address %v not found", tokenAddress) return nil, fmt.Errorf("voucher address %v not found", tokenAddress)
@ -443,14 +507,13 @@ func (das *DevAccountService) PoolDeposit(ctx context.Context, amount, from, poo
return nil, err return nil, err
} }
voucher, ok := das.vouchers[sym] _, ok = das.vouchers[sym]
if !ok { if !ok {
return nil, fmt.Errorf("voucher address %v found but does not resolve", tokenAddress) return nil, fmt.Errorf("voucher address %v found but does not resolve", tokenAddress)
} }
das.pool = Pool{ if err != nil {
vouchers: append(das.pool.vouchers, voucher), return nil, err
poolLimit: map[string]string{tokenAddress: amount},
} }
return &models.PoolDepositResult{ return &models.PoolDepositResult{
TrackingId: uid.String(), TrackingId: uid.String(),
@ -462,18 +525,18 @@ func (das *DevAccountService) GetPoolSwapQuote(ctx context.Context, amount, from
if !ok { if !ok {
return nil, fmt.Errorf("account not found (publickey): %v", from) return nil, fmt.Errorf("account not found (publickey): %v", from)
} }
//resolve the token address you are trying to swap from(fromTokenAddress) p, ok := das.pools[poolAddress]
_, ok = das.vouchersAddress[fromTokenAddress]
if !ok { if !ok {
return nil, fmt.Errorf("voucher address %v not found", fromTokenAddress) return nil, fmt.Errorf("pool address %v not found", poolAddress)
} }
p := das.pool
//check if pool has voucher to swap to //resolve the token address you are trying to swap from(fromTokenAddress)
if !p.hasVoucher(toTokenAddress) { ok = p.hasVoucher(fromTokenAddress)
return nil, fmt.Errorf("Voucher with address: %v not found in the pool", toTokenAddress) if !ok {
return nil, fmt.Errorf("voucher with address %v not found in the pool", fromTokenAddress)
} }
//Return a a quote that is equal to the amount enter
//Return a a quote that is equal to the amount entered
return &models.PoolSwapQuoteResult{IncludesFeesDeduction: false, OutValue: amount}, nil return &models.PoolSwapQuoteResult{IncludesFeesDeduction: false, OutValue: amount}, nil
} }
@ -483,19 +546,22 @@ func (das *DevAccountService) PoolSwap(ctx context.Context, amount, from, fromTo
return nil, err return nil, err
} }
_, ok := das.accounts[from] p, ok := das.pools[poolAddress]
if !ok {
return nil, fmt.Errorf("pool address %v not found", toTokenAddress)
}
_, ok = das.accounts[from]
if !ok { if !ok {
return nil, fmt.Errorf("account not found (publickey): %v", from) return nil, fmt.Errorf("account not found (publickey): %v", from)
} }
_, ok = das.vouchersAddress[fromTokenAddress] ok = p.hasVoucher(fromTokenAddress)
if !ok { if !ok {
return nil, fmt.Errorf("voucher address %v not found", fromTokenAddress) return nil, fmt.Errorf("token %v not found in the pool", fromTokenAddress)
} }
p := das.pool
//check if pool has voucher to swap to ok = p.hasVoucher(toTokenAddress)
if !p.hasVoucher(toTokenAddress) { if !ok {
return nil, fmt.Errorf("Voucher with address: %v not found in the pool", toTokenAddress) return nil, fmt.Errorf("token %v not found in the pool", toTokenAddress)
} }
return &models.PoolSwapResult{TrackingId: uid.String()}, nil return &models.PoolSwapResult{TrackingId: uid.String()}, nil
@ -521,7 +587,7 @@ func (das *DevAccountService) FetchVouchers(ctx context.Context, publicKey strin
//TODO: Iterate over the account acc.Balances object //TODO: Iterate over the account acc.Balances object
for _, voucher := range das.vouchers { for _, voucher := range das.vouchers {
holdings = append(holdings, dataserviceapi.TokenHoldings{ holdings = append(holdings, dataserviceapi.TokenHoldings{
ContractAddress: voucher.Address, TokenAddress: voucher.Address,
TokenSymbol: voucher.Symbol, TokenSymbol: voucher.Symbol,
TokenDecimals: strconv.Itoa(voucher.Decimals), TokenDecimals: strconv.Itoa(voucher.Decimals),
Balance: strconv.Itoa(int(defaultVoucherBalance)), Balance: strconv.Itoa(int(defaultVoucherBalance)),
@ -740,71 +806,97 @@ func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string
}, nil }, nil
} }
func (das *DevAccountService) FetchTopPools(ctx context.Context) ([]models.Pool, error) { func (das *DevAccountService) SendUpsellSMS(ctx context.Context, inviterPhone, inviteePhone string) (*models.SendSMSResponse, error) {
var ( logg.DebugCtxf(ctx, "sent an SMS", "inviterPhone", inviterPhone, "inviteePhone", inviteePhone)
r struct { return &models.SendSMSResponse{
OK bool `json:"ok"` Invitee: inviteePhone,
Description string `json:"description"` }, nil
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) func (das *DevAccountService) SendPINResetSMS(ctx context.Context, admin, phone string) error {
if err != nil { return fmt.Errorf("unimplemented")
log.Fatal(err)
} }
return r.Result.Pools, nil func (das *DevAccountService) SendAddressSMS(ctx context.Context, publicKey, originPhone string) error {
return fmt.Errorf("unimplemented")
} }
func (das *DevAccountService) GetPoolSwappableFromVouchers(ctx context.Context) ([]models.SwappableVoucher, error) { func (das *DevAccountService) FetchTopPools(ctx context.Context) ([]dataserviceapi.PoolDetails, error) {
var ( var topPools []dataserviceapi.PoolDetails
r struct { for _, p := range das.pools {
OK bool `json:"ok"` topPools = append(topPools, dataserviceapi.PoolDetails{
Description string `json:"description"` PoolName: p.Name,
Result struct { PoolSymbol: p.Symbol,
SwappableVouchers []models.SwappableVoucher `json:"filtered"` PoolContractAdrress: p.Address,
} `json:"result"` })
} }
) return topPools, nil
data, err := os.ReadFile("./data/swap_from.json")
if err != nil {
log.Fatal(err)
} }
err = json.Unmarshal(data, &r) func (das *DevAccountService) RetrievePoolDetails(ctx context.Context, sym string) (*dataserviceapi.PoolDetails, error) {
if err != nil { testPool := &dataserviceapi.PoolDetails{
log.Fatal(err) PoolName: "DevTest",
PoolSymbol: "DEVT",
PoolContractAdrress: "0x145F87d6198dEDD45C614FFD8b70E9a2fCCc5cc9",
LimiterAddress: "",
VoucherRegistry: "",
} }
return r.Result.SwappableVouchers, nil return testPool, nil
} }
func (das *DevAccountService) GetPoolSwappableVouchers(ctx context.Context) ([]models.SwappableVoucher, error) { func (das *DevAccountService) GetPoolSwappableFromVouchers(ctx context.Context, poolAddress, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
var ( var swapFromList []dataserviceapi.TokenHoldings
r struct {
OK bool `json:"ok"` p, ok := das.pools[poolAddress]
Description string `json:"description"` if !ok {
Result struct { return nil, fmt.Errorf("Invalid pool address: %v", poolAddress)
SwappableVouchers []models.SwappableVoucher `json:"filtered"`
} `json:"result"`
} }
) for _, v := range p.Vouchers {
data, err := os.ReadFile("./data/swap_to.json") swapFromList = append(swapFromList, dataserviceapi.TokenHoldings{
if err != nil { TokenAddress: v.Address,
log.Fatal(err) TokenSymbol: v.Symbol,
TokenDecimals: string(defaultDecimals),
Balance: fmt.Sprintf("%f", defaultVoucherBalance),
})
} }
err = json.Unmarshal(data, &r) return swapFromList, nil
if err != nil {
log.Fatal(err)
} }
return r.Result.SwappableVouchers, nil func (das *DevAccountService) GetPoolSwappableVouchers(ctx context.Context, poolAddress string) ([]dataserviceapi.TokenHoldings, error) {
var swapToList []dataserviceapi.TokenHoldings
_, ok := das.pools[poolAddress]
if !ok {
return nil, fmt.Errorf("Invalid pool address: %v", poolAddress)
}
for _, voucher := range das.vouchers {
swapToList = append(swapToList, dataserviceapi.TokenHoldings{
TokenAddress: voucher.Address,
TokenSymbol: voucher.Symbol,
TokenDecimals: strconv.Itoa(voucher.Decimals),
})
}
return swapToList, nil
}
func (das *DevAccountService) GetSwapFromTokenMaxLimit(ctx context.Context, poolAddress, fromTokenAddress, toTokenAddress, publicKey string) (*models.MaxLimitResult, error) {
p, ok := das.pools[poolAddress]
if !ok {
return nil, fmt.Errorf("Pool address: %v not found ", poolAddress)
}
limit, ok := p.PoolLimit[fromTokenAddress]
if !ok {
return nil, fmt.Errorf("Token address: %v not found in the pool", fromTokenAddress)
}
return &models.MaxLimitResult{
Max: limit,
}, nil
}
func (das *DevAccountService) CheckTokenInPool(ctx context.Context, poolAddress, tokenAddress string) (*models.TokenInPoolResult, error) {
return &models.TokenInPoolResult{
CanSwapFrom: true,
}, nil
} }

2
go.mod
View File

@ -8,7 +8,7 @@ require (
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250122123424-6749c632b0a2 git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250122123424-6749c632b0a2
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
github.com/grassrootseconomics/ussd-data-service v1.2.0-beta github.com/grassrootseconomics/ussd-data-service v1.6.0-beta
github.com/stretchr/testify v1.9.0 github.com/stretchr/testify v1.9.0
) )

10
go.sum
View File

@ -18,6 +18,16 @@ github.com/grassrootseconomics/eth-custodial v1.3.0-beta h1:twrMBhl89GqDUL9PlkzQ
github.com/grassrootseconomics/eth-custodial v1.3.0-beta/go.mod h1:7uhRcdnJplX4t6GKCEFkbeDhhjlcaGJeJqevbcvGLZo= github.com/grassrootseconomics/eth-custodial v1.3.0-beta/go.mod h1:7uhRcdnJplX4t6GKCEFkbeDhhjlcaGJeJqevbcvGLZo=
github.com/grassrootseconomics/ussd-data-service v1.2.0-beta h1:fn1gwbWIwHVEBtUC2zi5OqTlfI/5gU1SMk0fgGixIXk= github.com/grassrootseconomics/ussd-data-service v1.2.0-beta h1:fn1gwbWIwHVEBtUC2zi5OqTlfI/5gU1SMk0fgGixIXk=
github.com/grassrootseconomics/ussd-data-service v1.2.0-beta/go.mod h1:omfI0QtUwIdpu9gMcUqLMCG8O1XWjqJGBx1qUMiGWC0= github.com/grassrootseconomics/ussd-data-service v1.2.0-beta/go.mod h1:omfI0QtUwIdpu9gMcUqLMCG8O1XWjqJGBx1qUMiGWC0=
github.com/grassrootseconomics/ussd-data-service v1.4.0-beta h1:4fMd/3h2ZIhRg4GdHQmRw5FfD3MpJvFNNJQo+Q27f5M=
github.com/grassrootseconomics/ussd-data-service v1.4.0-beta/go.mod h1:9sGnorpKaK76FmOGXoh/xv7x5siSFNYdXxQo9BKW4DI=
github.com/grassrootseconomics/ussd-data-service v1.4.4-beta h1:turlyo0i3OLj29mWpWNoB/3Qao8qEngT/5d1jDWTZE4=
github.com/grassrootseconomics/ussd-data-service v1.4.4-beta/go.mod h1:9sGnorpKaK76FmOGXoh/xv7x5siSFNYdXxQo9BKW4DI=
github.com/grassrootseconomics/ussd-data-service v1.4.7-beta h1:yAe1YaOBsdxW2m20jnVU4F0kLmFr+mK/gHCWEdHmE90=
github.com/grassrootseconomics/ussd-data-service v1.4.7-beta/go.mod h1:9sGnorpKaK76FmOGXoh/xv7x5siSFNYdXxQo9BKW4DI=
github.com/grassrootseconomics/ussd-data-service v1.5.0-beta h1:BSSQL/yPEvTVku9ja/ENZyZdwZkEaiTzzOUfg72bTy4=
github.com/grassrootseconomics/ussd-data-service v1.5.0-beta/go.mod h1:9sGnorpKaK76FmOGXoh/xv7x5siSFNYdXxQo9BKW4DI=
github.com/grassrootseconomics/ussd-data-service v1.6.0-beta h1:pY6zns6ifXyClRxP+JJaWrged3oRE7tTS2xaftF9clA=
github.com/grassrootseconomics/ussd-data-service v1.6.0-beta/go.mod h1:9sGnorpKaK76FmOGXoh/xv7x5siSFNYdXxQo9BKW4DI=
github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4 h1:U4kkNYryi/qfbBF8gh7Vsbuz+cVmhf5kt6pE9bYYyLo= github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4 h1:U4kkNYryi/qfbBF8gh7Vsbuz+cVmhf5kt6pE9bYYyLo=
github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4/go.mod h1:zpZDgZFzeq9s0MIeB1P50NIEWDFFHSFBohI/NbaTD/Y= github.com/graygnuorg/go-gdbm v0.0.0-20220711140707-71387d66dce4/go.mod h1:zpZDgZFzeq9s0MIeB1P50NIEWDFFHSFBohI/NbaTD/Y=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=

View File

@ -7,3 +7,13 @@ type RequestAliasResult struct {
type AliasAddress struct { type AliasAddress struct {
Address string Address string
} }
type AliasEnsResult struct {
Address string `json:"address"`
AutoChoose bool `json:"autoChoose"`
Name string `json:"name"`
}
type AliasEnsAddressResult struct {
Address string `json:"address"`
}

View File

@ -1,10 +0,0 @@
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"`
}

View File

@ -12,3 +12,11 @@ type PoolSwapQuoteResult struct {
type PoolSwapResult struct { type PoolSwapResult struct {
TrackingId string `json:"trackingId"` TrackingId string `json:"trackingId"`
} }
type MaxLimitResult struct {
Max string `json:"max"`
}
type TokenInPoolResult struct {
CanSwapFrom bool `json:"canSwapFrom"`
}

View File

@ -0,0 +1,5 @@
package models
type SendSMSResponse struct {
Invitee string `json:"invitee"`
}

View File

@ -8,10 +8,3 @@ type VoucherDataResult struct {
TokenCommodity string `json:"tokenCommodity"` TokenCommodity string `json:"tokenCommodity"`
TokenLocation string `json:"tokenLocation"` TokenLocation string `json:"tokenLocation"`
} }
type SwappableVoucher struct {
ContractAddress string `json:"contractAddress"`
TokenSymbol string `json:"tokenSymbol"`
TokenDecimals string `json:"tokenDecimals"`
Balance string `json:"balance"`
}

View File

@ -17,4 +17,16 @@ type AccountService interface {
TokenTransfer(ctx context.Context, amount, from, to, tokenAddress string) (*models.TokenTransferResponse, error) TokenTransfer(ctx context.Context, amount, from, to, tokenAddress string) (*models.TokenTransferResponse, error)
CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error)
RequestAlias(ctx context.Context, hint string, publicKey string) (*models.RequestAliasResult, error) RequestAlias(ctx context.Context, hint string, publicKey string) (*models.RequestAliasResult, error)
SendUpsellSMS(ctx context.Context, inviterPhone, inviteePhone string) (*models.SendSMSResponse, error)
SendAddressSMS(ctx context.Context, publicKey, originPhone string) error
SendPINResetSMS(ctx context.Context, admin, phone string) error
PoolDeposit(ctx context.Context, amount, from, poolAddress, tokenAddress string) (*models.PoolDepositResult, error)
FetchTopPools(ctx context.Context) ([]dataserviceapi.PoolDetails, error)
RetrievePoolDetails(ctx context.Context, sym string) (*dataserviceapi.PoolDetails, error)
GetPoolSwappableFromVouchers(ctx context.Context, poolAddress, publicKey string) ([]dataserviceapi.TokenHoldings, error)
GetPoolSwappableVouchers(ctx context.Context, poolAddress string) ([]dataserviceapi.TokenHoldings, error)
GetPoolSwapQuote(ctx context.Context, amount, from, fromTokenAddress, poolAddress, toTokenAddress string) (*models.PoolSwapQuoteResult, error)
PoolSwap(ctx context.Context, amount, from, fromTokenAddress, poolAddress, toTokenAddress string) (*models.PoolSwapResult, error)
GetSwapFromTokenMaxLimit(ctx context.Context, poolAddress, fromTokenAddress, toTokenAddress, publicKey string) (*models.MaxLimitResult, error)
CheckTokenInPool(ctx context.Context, poolAddress, tokenAddress string) (*models.TokenInPoolResult, error)
} }

View File

@ -11,7 +11,9 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"regexp" "regexp"
"strings"
"git.defalsify.org/vise.git/logging"
"git.grassecon.net/grassrootseconomics/sarafu-api/config" "git.grassecon.net/grassrootseconomics/sarafu-api/config"
"git.grassecon.net/grassrootseconomics/sarafu-api/dev" "git.grassecon.net/grassrootseconomics/sarafu-api/dev"
"git.grassecon.net/grassrootseconomics/sarafu-api/models" "git.grassecon.net/grassrootseconomics/sarafu-api/models"
@ -22,6 +24,7 @@ import (
var ( var (
aliasRegex = regexp.MustCompile("^\\+?[a-zA-Z0-9\\-_]+$") aliasRegex = regexp.MustCompile("^\\+?[a-zA-Z0-9\\-_]+$")
logg = logging.NewVanilla().WithDomain("sarafu-api.devapi")
) )
type HTTPAccountService struct { type HTTPAccountService struct {
@ -59,6 +62,10 @@ func (as *HTTPAccountService) TrackAccountStatus(ctx context.Context, publicKey
return &r, nil return &r, nil
} }
func (as *HTTPAccountService) ToFqdn(alias string) string {
return alias + ".sarafu.eth"
}
// CheckBalance retrieves the balance for a given public key from the custodial balance API endpoint. // CheckBalance retrieves the balance for a given public key from the custodial balance API endpoint.
// Parameters: // Parameters:
// - publicKey: The public key associated with the account whose balance needs to be checked. // - publicKey: The public key associated with the account whose balance needs to be checked.
@ -216,8 +223,10 @@ func (as *HTTPAccountService) CheckAliasAddress(ctx context.Context, alias strin
if as.SS == nil { if as.SS == nil {
return nil, fmt.Errorf("The storage service cannot be nil") return nil, fmt.Errorf("The storage service cannot be nil")
} }
logg.InfoCtxf(ctx, "resolving alias before formatting", "alias", alias)
svc := dev.NewDevAccountService(ctx, as.SS) svc := dev.NewDevAccountService(ctx, as.SS)
if as.UseApi { if as.UseApi {
logg.InfoCtxf(ctx, "resolving alias to address", "alias", alias)
return resolveAliasAddress(ctx, alias) return resolveAliasAddress(ctx, alias)
} else { } else {
return svc.CheckAliasAddress(ctx, alias) return svc.CheckAliasAddress(ctx, alias)
@ -225,9 +234,63 @@ func (as *HTTPAccountService) CheckAliasAddress(ctx context.Context, alias strin
} }
func resolveAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) { func resolveAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
var r models.AliasAddress var aliasEnsResult models.AliasEnsAddressResult
ep, err := url.JoinPath(config.CheckAliasURL, alias) fullURL, err := url.JoinPath(config.AliasResolverURL, alias)
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", fullURL, nil)
if err != nil {
return nil, err
}
_, err = doRequest(ctx, req, &aliasEnsResult)
if err != nil {
return nil, err
}
return &models.AliasAddress{Address: aliasEnsResult.Address}, nil
}
func (as *HTTPAccountService) FetchTopPools(ctx context.Context) ([]dataserviceapi.PoolDetails, error) {
svc := dev.NewDevAccountService(ctx, as.SS)
if as.UseApi {
return fetchCustodialTopPools(ctx)
} else {
return svc.FetchTopPools(ctx)
}
}
func fetchCustodialTopPools(ctx context.Context) ([]dataserviceapi.PoolDetails, error) {
var r struct {
TopPools []dataserviceapi.PoolDetails `json:"topPools"`
}
req, err := http.NewRequest("GET", config.TopPoolsURL, nil)
if err != nil {
return nil, err
}
_, err = doRequest(ctx, req, &r)
return r.TopPools, nil
}
func (as *HTTPAccountService) RetrievePoolDetails(ctx context.Context, sym string) (*dataserviceapi.PoolDetails, error) {
if as.UseApi {
return retrievePoolDetails(ctx, sym)
} else {
return nil, nil
}
}
func retrievePoolDetails(ctx context.Context, sym string) (*dataserviceapi.PoolDetails, error) {
var r struct {
PoolDetails dataserviceapi.PoolDetails `json:"poolDetails"`
}
ep, err := url.JoinPath(config.RetrievePoolDetailsURL, sym)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -236,12 +299,11 @@ func resolveAliasAddress(ctx context.Context, alias string) (*models.AliasAddres
return nil, err return nil, err
} }
_, err = doRequest(ctx, req, &r) _, err = doRequest(ctx, req, &r)
return &r, err if err != nil {
return nil, err
} }
func (as *HTTPAccountService) FetchTopPools(ctx context.Context) ([]models.Pool, error) { return &r.PoolDetails, nil
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) { func (as *HTTPAccountService) PoolDeposit(ctx context.Context, amount, from, poolAddress, tokenAddress string) (*models.PoolDepositResult, error) {
@ -286,6 +348,7 @@ func (as *HTTPAccountService) GetPoolSwapQuote(ctx context.Context, amount, from
if err != nil { if err != nil {
return nil, err return nil, err
} }
req, err := http.NewRequest("POST", config.PoolSwapQuoteURL, bytes.NewBuffer(payloadBytes)) req, err := http.NewRequest("POST", config.PoolSwapQuoteURL, bytes.NewBuffer(payloadBytes))
if err != nil { if err != nil {
return nil, err return nil, err
@ -297,12 +360,70 @@ func (as *HTTPAccountService) GetPoolSwapQuote(ctx context.Context, amount, from
return &r, nil return &r, nil
} }
func (as *HTTPAccountService) GetPoolSwappableFromVouchers(ctx context.Context) ([]models.SwappableVoucher, error) { func (as *HTTPAccountService) GetPoolSwappableFromVouchers(ctx context.Context, poolAddress, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
return as.GetPoolSwappableFromVouchers(ctx) if as.UseApi {
return as.getPoolSwappableFromVouchers(ctx, poolAddress, publicKey)
} else {
svc := dev.NewDevAccountService(ctx, as.SS)
return svc.GetPoolSwappableFromVouchers(ctx, poolAddress, publicKey)
} }
func (as *HTTPAccountService) GetPoolSwappableVouchers(ctx context.Context) ([]models.SwappableVoucher, error) { }
return as.GetPoolSwappableVouchers(ctx)
func (as *HTTPAccountService) getPoolSwappableFromVouchers(ctx context.Context, poolAddress, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
var r struct {
PoolSwappableVouchers []dataserviceapi.TokenHoldings `json:"filtered"`
}
ep, err := url.JoinPath(config.PoolSwappableVouchersURL, poolAddress, "from", publicKey)
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.PoolSwappableVouchers, nil
}
func (as *HTTPAccountService) GetPoolSwappableVouchers(ctx context.Context, poolAddress string) ([]dataserviceapi.TokenHoldings, error) {
svc := dev.NewDevAccountService(ctx, as.SS)
if as.UseApi {
return as.getPoolSwappableVouchers(ctx, poolAddress)
} else {
return svc.GetPoolSwappableVouchers(ctx, poolAddress)
}
}
func (as HTTPAccountService) getPoolSwappableVouchers(ctx context.Context, poolAddress string) ([]dataserviceapi.TokenHoldings, error) {
var r struct {
PoolSwappableVouchers []dataserviceapi.TokenHoldings `json:"filtered"`
}
basePath, err := url.JoinPath(config.PoolSwappableVouchersURL, poolAddress, "to")
if err != nil {
return nil, err
}
parsedURL, err := url.Parse(basePath)
if err != nil {
return nil, err
}
query := parsedURL.Query()
if config.IncludeStablesParam != "" {
query.Set("stables", config.IncludeStablesParam)
}
parsedURL.RawQuery = query.Encode()
req, err := http.NewRequest("GET", parsedURL.String(), nil)
if err != nil {
return nil, err
}
_, err = doRequest(ctx, req, &r)
return r.PoolSwappableVouchers, nil
} }
func (as *HTTPAccountService) PoolSwap(ctx context.Context, amount, from, fromTokenAddress, poolAddress, toTokenAddress string) (*models.PoolSwapResult, error) { func (as *HTTPAccountService) PoolSwap(ctx context.Context, amount, from, fromTokenAddress, poolAddress, toTokenAddress string) (*models.PoolSwapResult, error) {
@ -321,7 +442,8 @@ func (as *HTTPAccountService) PoolSwap(ctx context.Context, amount, from, fromTo
if err != nil { if err != nil {
return nil, err return nil, err
} }
req, err := http.NewRequest("POST", config.PoolSwapQuoteURL, bytes.NewBuffer(payloadBytes))
req, err := http.NewRequest("POST", config.PoolSwapURL, bytes.NewBuffer(payloadBytes))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -332,14 +454,191 @@ func (as *HTTPAccountService) PoolSwap(ctx context.Context, amount, from, fromTo
return &r, nil return &r, nil
} }
func (as *HTTPAccountService) GetSwapFromTokenMaxLimit(ctx context.Context, poolAddress, fromTokenAddress, toTokenAddress, publicKey string) (*models.MaxLimitResult, error) {
if as.UseApi {
return as.getSwapFromTokenMaxLimit(ctx, poolAddress, fromTokenAddress, toTokenAddress, publicKey)
} else {
svc := dev.NewDevAccountService(ctx, as.SS)
return svc.GetSwapFromTokenMaxLimit(ctx, poolAddress, fromTokenAddress, toTokenAddress, publicKey)
}
}
func (as *HTTPAccountService) getSwapFromTokenMaxLimit(ctx context.Context, poolAddress, fromTokenAddress, toTokeAddress, publicKey string) (*models.MaxLimitResult, error) {
var r models.MaxLimitResult
ep, err := url.JoinPath(config.PoolSwappableVouchersURL, poolAddress, "limit", fromTokenAddress, toTokeAddress, publicKey)
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", ep, nil)
if err != nil {
return nil, err
}
_, err = doRequest(ctx, req, &r)
if err != nil {
return nil, err
}
return &r, nil
}
func (as *HTTPAccountService) CheckTokenInPool(ctx context.Context, poolAddress, tokenAddress string) (*models.TokenInPoolResult, error) {
if as.UseApi {
return as.checkTokenInPool(ctx, poolAddress, tokenAddress)
} else {
svc := dev.NewDevAccountService(ctx, as.SS)
return svc.CheckTokenInPool(ctx, poolAddress, tokenAddress)
}
}
func (as *HTTPAccountService) checkTokenInPool(ctx context.Context, poolAddress, tokenAddress string) (*models.TokenInPoolResult, error) {
var r models.TokenInPoolResult
ep, err := url.JoinPath(config.PoolSwappableVouchersURL, poolAddress, "check", tokenAddress)
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", ep, nil)
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 // TODO: Use actual custodial api to request available alias
func (as *HTTPAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) { func (as *HTTPAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
if as.SS == nil { if as.SS == nil {
return nil, fmt.Errorf("The storage service cannot be nil") return nil, fmt.Errorf("The storage service cannot be nil")
} }
if as.UseApi {
if !strings.Contains(hint, ".") {
hint = as.ToFqdn(hint)
}
enr, err := requestEnsAlias(ctx, publicKey, hint)
if err != nil {
return nil, err
}
return &models.RequestAliasResult{Alias: enr.Name}, nil
} else {
svc := dev.NewDevAccountService(ctx, as.SS) svc := dev.NewDevAccountService(ctx, as.SS)
return svc.RequestAlias(ctx, publicKey, hint) return svc.RequestAlias(ctx, publicKey, hint)
} }
}
func requestEnsAlias(ctx context.Context, publicKey string, hint string) (*models.AliasEnsResult, error) {
var r models.AliasEnsResult
endpoint := config.AliasRegistrationURL
logg.InfoCtxf(ctx, "requesting alias", "endpoint", endpoint, "hint", hint)
//Payload with the address and hint to derive an ENS name
payload := map[string]string{
"address": publicKey,
"hint": hint,
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(payloadBytes))
if err != nil {
return nil, err
}
// Log the request body
logg.InfoCtxf(ctx, "request body", "payload", string(payloadBytes))
_, err = doRequest(ctx, req, &r)
if err != nil {
return nil, err
}
logg.InfoCtxf(ctx, "alias successfully assigned", "alias", r.Name)
return &r, nil
}
// SendSMS calls the API to send out an SMS.
// Parameters:
// - inviterPhone: The user initiating the SMS.
// - inviteePhone: The number being invited to Sarafu.
func (as *HTTPAccountService) SendUpsellSMS(ctx context.Context, inviterPhone, inviteePhone string) (*models.SendSMSResponse, error) {
var r models.SendSMSResponse
// Create request payload
payload := map[string]string{
"inviterPhone": inviterPhone,
"inviteePhone": inviteePhone,
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return nil, err
}
// Create a new request
req, err := http.NewRequest("POST", config.SendSMSURL, 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) SendAddressSMS(ctx context.Context, publicKey, originPhone string) error {
ep, err := url.JoinPath(config.ExternalSMSURL, "address")
if err != nil {
return err
}
logg.InfoCtxf(ctx, "sending an address sms", "endpoint", ep, "address", publicKey, "origin-phone", originPhone)
payload := map[string]string{
"address": publicKey,
"originPhone": originPhone,
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return err
}
req, err := http.NewRequest("POST", ep, bytes.NewBuffer(payloadBytes))
if err != nil {
return err
}
_, err = doRequest(ctx, req, nil)
if err != nil {
return err
}
return nil
}
func (as *HTTPAccountService) SendPINResetSMS(ctx context.Context, admin, phone string) error {
ep, err := url.JoinPath(config.ExternalSMSURL, "pinreset")
if err != nil {
return err
}
logg.InfoCtxf(ctx, "sending pin reset sms", "endpoint", ep, "admin", admin, "phone", phone)
payload := map[string]string{
"admin": admin,
"phone": phone,
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return err
}
req, err := http.NewRequest("POST", ep, bytes.NewBuffer(payloadBytes))
if err != nil {
return err
}
_, err = doRequest(ctx, req, nil)
if err != nil {
return err
}
return nil
}
// TODO: remove eth-custodial api dependency // TODO: remove eth-custodial api dependency
func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) { func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) {
@ -349,6 +648,7 @@ func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKRespons
req.Header.Set("Authorization", "Bearer "+config.BearerToken) req.Header.Set("Authorization", "Bearer "+config.BearerToken)
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
// Log request
logRequestDetails(req) logRequestDetails(req)
resp, err := http.DefaultClient.Do(req) resp, err := http.DefaultClient.Do(req)
@ -359,22 +659,26 @@ func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKRespons
} }
defer resp.Body.Close() defer resp.Body.Close()
log.Printf("Received response for %s: Status Code: %d | Content-Type: %s", req.URL, resp.StatusCode, resp.Header.Get("Content-Type")) // Read and log response body
body, err := io.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }
log.Printf("Received response for %s: Status Code: %d | Content-Type: %s | Body: %s",
req.URL, resp.StatusCode, resp.Header.Get("Content-Type"), string(body))
if resp.StatusCode >= http.StatusBadRequest { if resp.StatusCode >= http.StatusBadRequest {
err := json.Unmarshal([]byte(body), &errResponse) if err := json.Unmarshal(body, &errResponse); err != nil {
if err != nil {
return nil, err return nil, err
} }
return nil, errors.New(errResponse.Description) return nil, errors.New(errResponse.Description)
} }
err = json.Unmarshal([]byte(body), &okResponse)
if err != nil { if err := json.Unmarshal(body, &okResponse); err != nil {
return nil, err return nil, err
} }
if len(okResponse.Result) == 0 { if len(okResponse.Result) == 0 {
return nil, errors.New("Empty api result") return nil, errors.New("Empty api result")
} }
@ -391,16 +695,14 @@ func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKRespons
func logRequestDetails(req *http.Request) { func logRequestDetails(req *http.Request) {
var bodyBytes []byte var bodyBytes []byte
contentType := req.Header.Get("Content-Type") contentType := req.Header.Get("Content-Type")
if req.Body != nil { if req.Body != nil {
bodyBytes, err := io.ReadAll(req.Body) bodyBytes, _ = io.ReadAll(req.Body)
if err != nil { req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes)) // Restore body
log.Printf("Error reading request body: %s", err)
return
}
req.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
} else { } else {
bodyBytes = []byte("-") bodyBytes = []byte("-")
} }
log.Printf("URL: %s | Content-Type: %s | Method: %s| Request Body: %s", req.URL, contentType, req.Method, string(bodyBytes)) log.Printf("Outgoing Request -> URL: %s | Method: %s | Content-Type: %s | Body: %s",
req.URL.String(), req.Method, contentType, string(bodyBytes))
} }

View File

@ -61,3 +61,7 @@ func (m MockApi) RequestAlias(ctx context.Context, publicKey string, hint string
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 return nil, nil
} }
func (m MockApi) SendUpsellSMS(ctx context.Context, inviterPhone, inviteePhone string) (*models.SendSMSResponse, error) {
return nil, nil
}

View File

@ -53,7 +53,65 @@ func (m *MockAccountService) CheckAliasAddress(ctx context.Context, alias string
return args.Get(0).(*models.AliasAddress), args.Error(1) 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) args := m.Called(publicKey, hint)
return args.Get(0).(*models.RequestAliasResult), args.Error(1) return args.Get(0).(*models.RequestAliasResult), args.Error(1)
} }
func (m *MockAccountService) SendUpsellSMS(ctx context.Context, inviterPhone, inviteePhone string) (*models.SendSMSResponse, error) {
args := m.Called(inviterPhone, inviteePhone)
return args.Get(0).(*models.SendSMSResponse), args.Error(1)
}
func (m *MockAccountService) SendPINResetSMS(ctx context.Context, admin, phone string) error {
return nil
}
func (m *MockAccountService) SendAddressSMS(ctx context.Context, publicKey, originPhone string) error {
return nil
}
func (m MockAccountService) PoolDeposit(ctx context.Context, amount, from, poolAddress, tokenAddress string) (*models.PoolDepositResult, error) {
args := m.Called(amount, from, poolAddress, tokenAddress)
return args.Get(0).(*models.PoolDepositResult), args.Error(1)
}
func (m MockAccountService) FetchTopPools(ctx context.Context) ([]dataserviceapi.PoolDetails, error) {
args := m.Called()
return args.Get(0).([]dataserviceapi.PoolDetails), args.Error(1)
}
func (m MockAccountService) RetrievePoolDetails(ctx context.Context, sym string) (*dataserviceapi.PoolDetails, error) {
args := m.Called()
return args.Get(0).(*dataserviceapi.PoolDetails), args.Error(1)
}
func (m MockAccountService) GetPoolSwappableFromVouchers(ctx context.Context, poolAddress, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
args := m.Called(poolAddress, publicKey)
return args.Get(0).([]dataserviceapi.TokenHoldings), args.Error(1)
}
func (m MockAccountService) GetPoolSwappableVouchers(ctx context.Context, poolAddress string) ([]dataserviceapi.TokenHoldings, error) {
args := m.Called(poolAddress)
return args.Get(0).([]dataserviceapi.TokenHoldings), args.Error(1)
}
func (m MockAccountService) GetPoolSwapQuote(ctx context.Context, amount, from, fromTokenAddress, poolAddress, toTokenAddress string) (*models.PoolSwapQuoteResult, error) {
args := m.Called(amount, from, fromTokenAddress, poolAddress, toTokenAddress)
return args.Get(0).(*models.PoolSwapQuoteResult), args.Error(1)
}
func (m MockAccountService) PoolSwap(ctx context.Context, amount, from, fromTokenAddress, poolAddress, toTokenAddress string) (*models.PoolSwapResult, error) {
args := m.Called(amount, from, fromTokenAddress, poolAddress, toTokenAddress)
return args.Get(0).(*models.PoolSwapResult), args.Error(1)
}
func (m MockAccountService) GetSwapFromTokenMaxLimit(ctx context.Context, poolAddress, fromTokenAddress, toTokenAddress, publicKey string) (*models.MaxLimitResult, error) {
args := m.Called(poolAddress, fromTokenAddress, toTokenAddress, publicKey)
return args.Get(0).(*models.MaxLimitResult), args.Error(1)
}
func (m MockAccountService) CheckTokenInPool(ctx context.Context, poolAddress, tokenAddress string) (*models.TokenInPoolResult, error) {
args := m.Called(poolAddress, tokenAddress)
return args.Get(0).(*models.TokenInPoolResult), args.Error(1)
}

View File

@ -35,7 +35,7 @@ func (tas *TestAccountService) TrackAccountStatus(ctx context.Context, publicKey
func (tas *TestAccountService) FetchVouchers(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error) { func (tas *TestAccountService) FetchVouchers(ctx context.Context, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
return []dataserviceapi.TokenHoldings{ return []dataserviceapi.TokenHoldings{
dataserviceapi.TokenHoldings{ dataserviceapi.TokenHoldings{
ContractAddress: "0x6CC75A06ac72eB4Db2eE22F781F5D100d8ec03ee", TokenAddress: "0x6CC75A06ac72eB4Db2eE22F781F5D100d8ec03ee",
TokenSymbol: "SRF", TokenSymbol: "SRF",
TokenDecimals: "6", TokenDecimals: "6",
Balance: "2745987", Balance: "2745987",
@ -57,10 +57,58 @@ func (tas *TestAccountService) TokenTransfer(ctx context.Context, amount, from,
}, nil }, nil
} }
func (m TestAccountService) CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) { func (m TestAccountService) PoolDeposit(ctx context.Context, amount, from, poolAddress, tokenAddress string) (*models.PoolDepositResult, error) {
return &models.PoolDepositResult{}, nil
}
func (m *TestAccountService) CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
return &models.AliasAddress{}, nil return &models.AliasAddress{}, nil
} }
func (m TestAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) { func (m *TestAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
return &models.RequestAliasResult{}, nil return &models.RequestAliasResult{}, nil
} }
func (m *TestAccountService) SendUpsellSMS(ctx context.Context, inviterPhone, inviteePhone string) (*models.SendSMSResponse, error) {
return &models.SendSMSResponse{}, nil
}
func (m *TestAccountService) SendAddressSMS(ctx context.Context, publicKey, originPhone string) error {
return nil
}
func (m *TestAccountService) SendPINResetSMS(ctx context.Context, admin, phone string) error {
return nil
}
func (m TestAccountService) FetchTopPools(ctx context.Context) ([]dataserviceapi.PoolDetails, error) {
return []dataserviceapi.PoolDetails{}, nil
}
func (m TestAccountService) RetrievePoolDetails(ctx context.Context, sym string) (*dataserviceapi.PoolDetails, error) {
return &dataserviceapi.PoolDetails{}, nil
}
func (m TestAccountService) GetPoolSwappableFromVouchers(ctx context.Context, poolAddress, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
return []dataserviceapi.TokenHoldings{}, nil
}
func (m TestAccountService) GetPoolSwappableVouchers(ctx context.Context, poolAddress string) ([]dataserviceapi.TokenHoldings, error) {
return []dataserviceapi.TokenHoldings{}, nil
}
func (m TestAccountService) GetPoolSwapQuote(ctx context.Context, amount, from, fromTokenAddress, poolAddress, toTokenAddress string) (*models.PoolSwapQuoteResult, error) {
return &models.PoolSwapQuoteResult{}, nil
}
func (m TestAccountService) PoolSwap(ctx context.Context, amount, from, fromTokenAddress, poolAddress, toTokenAddress string) (*models.PoolSwapResult, error) {
return &models.PoolSwapResult{}, nil
}
func (m TestAccountService) GetSwapFromTokenMaxLimit(ctx context.Context, poolAddress, fromTokenAddress, toTokenAddress, publicKey string) (*models.MaxLimitResult, error) {
return &models.MaxLimitResult{}, nil
}
func (m TestAccountService) CheckTokenInPool(ctx context.Context, poolAddress, tokenAddress string) (*models.TokenInPoolResult, error) {
return &models.TokenInPoolResult{}, nil
}