Compare commits
	
		
			No commits in common. "master" and "pool-swap-endpoints" have entirely different histories.
		
	
	
		
			master
			...
			pool-swap-
		
	
		
@ -25,7 +25,6 @@ const (
 | 
			
		||||
	AliasRegistrationPrefix     = "/api/v1/internal/register"
 | 
			
		||||
	AliasResolverPrefix         = "/api/v1/resolve"
 | 
			
		||||
	ExternalSMSPrefix           = "/api/v1/external"
 | 
			
		||||
	AliasUpdatePrefix           = "/api/v1/internal/update"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
@ -56,7 +55,6 @@ var (
 | 
			
		||||
	AliasRegistrationURL     string
 | 
			
		||||
	AliasResolverURL         string
 | 
			
		||||
	ExternalSMSURL           string
 | 
			
		||||
	AliasUpdateURL           string
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func setBase() error {
 | 
			
		||||
@ -104,7 +102,6 @@ func LoadConfig() error {
 | 
			
		||||
	AliasRegistrationURL, _ = url.JoinPath(aliasEnsURLBase, AliasRegistrationPrefix)
 | 
			
		||||
	AliasResolverURL, _ = url.JoinPath(aliasEnsURLBase, AliasResolverPrefix)
 | 
			
		||||
	ExternalSMSURL, _ = url.JoinPath(externalSMSBase, ExternalSMSPrefix)
 | 
			
		||||
	AliasUpdateURL, _ = url.JoinPath(aliasEnsURLBase, AliasUpdatePrefix)
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -806,13 +806,6 @@ func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (das *DevAccountService) UpdateAlias(ctx context.Context, publicKey string, name string) (*models.RequestAliasResult, error) {
 | 
			
		||||
	logg.DebugCtxf(ctx, "Updated the alias", "address", publicKey, "name", name)
 | 
			
		||||
	return &models.RequestAliasResult{
 | 
			
		||||
		Alias: name,
 | 
			
		||||
	}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (das *DevAccountService) SendUpsellSMS(ctx context.Context, inviterPhone, inviteePhone string) (*models.SendSMSResponse, error) {
 | 
			
		||||
	logg.DebugCtxf(ctx, "sent an SMS", "inviterPhone", inviterPhone, "inviteePhone", inviteePhone)
 | 
			
		||||
	return &models.SendSMSResponse{
 | 
			
		||||
 | 
			
		||||
@ -17,7 +17,6 @@ type AccountService interface {
 | 
			
		||||
	TokenTransfer(ctx context.Context, amount, from, to, tokenAddress string) (*models.TokenTransferResponse, error)
 | 
			
		||||
	CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error)
 | 
			
		||||
	RequestAlias(ctx context.Context, hint string, publicKey string) (*models.RequestAliasResult, error)
 | 
			
		||||
	UpdateAlias(ctx context.Context, name 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
 | 
			
		||||
 | 
			
		||||
@ -27,36 +27,11 @@ var (
 | 
			
		||||
	logg       = logging.NewVanilla().WithDomain("sarafu-api.devapi")
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type APIError struct {
 | 
			
		||||
	Code        string
 | 
			
		||||
	Description string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (e *APIError) Error() string {
 | 
			
		||||
	if e.Code != "" {
 | 
			
		||||
		return fmt.Sprintf("[%s] %s", e.Code, e.Description)
 | 
			
		||||
	}
 | 
			
		||||
	return e.Description
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type HTTPAccountService struct {
 | 
			
		||||
	SS     storage.StorageService
 | 
			
		||||
	UseApi bool
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// symbolReplacements holds mappings of invalid symbols → valid ones
 | 
			
		||||
var symbolReplacements = map[string]string{
 | 
			
		||||
	"USD₮": "USDT",
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// sanitizeSymbol replaces known invalid token symbols with normalized ones
 | 
			
		||||
func sanitizeSymbol(symbol string) string {
 | 
			
		||||
	if replacement, ok := symbolReplacements[symbol]; ok {
 | 
			
		||||
		return replacement
 | 
			
		||||
	}
 | 
			
		||||
	return symbol
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Parameters:
 | 
			
		||||
//   - trackingId: A unique identifier for the account.This should be obtained from a previous call to
 | 
			
		||||
//     CreateAccount or a similar function that returns an AccountResponse. The `trackingId` field in the
 | 
			
		||||
@ -155,11 +130,6 @@ func (as *HTTPAccountService) FetchVouchers(ctx context.Context, publicKey strin
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Normalize symbols before returning
 | 
			
		||||
	for i := range r.Holdings {
 | 
			
		||||
		r.Holdings[i].TokenSymbol = sanitizeSymbol(r.Holdings[i].TokenSymbol)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return r.Holdings, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -186,11 +156,6 @@ func (as *HTTPAccountService) FetchTransactions(ctx context.Context, publicKey s
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Normalize symbols before returning
 | 
			
		||||
	for i := range r.Transfers {
 | 
			
		||||
		r.Transfers[i].TokenSymbol = sanitizeSymbol(r.Transfers[i].TokenSymbol)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return r.Transfers, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -212,9 +177,6 @@ func (as *HTTPAccountService) VoucherData(ctx context.Context, address string) (
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Normalize symbols before returning
 | 
			
		||||
	r.TokenDetails.TokenSymbol = sanitizeSymbol(r.TokenDetails.TokenSymbol)
 | 
			
		||||
 | 
			
		||||
	_, err = doRequest(ctx, req, &r)
 | 
			
		||||
	return &r.TokenDetails, err
 | 
			
		||||
}
 | 
			
		||||
@ -405,6 +367,7 @@ func (as *HTTPAccountService) GetPoolSwappableFromVouchers(ctx context.Context,
 | 
			
		||||
		svc := dev.NewDevAccountService(ctx, as.SS)
 | 
			
		||||
		return svc.GetPoolSwappableFromVouchers(ctx, poolAddress, publicKey)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (as *HTTPAccountService) getPoolSwappableFromVouchers(ctx context.Context, poolAddress, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
 | 
			
		||||
@ -420,14 +383,6 @@ func (as *HTTPAccountService) getPoolSwappableFromVouchers(ctx context.Context,
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	_, err = doRequest(ctx, req, &r)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Normalize symbols before returning
 | 
			
		||||
	for i := range r.PoolSwappableVouchers {
 | 
			
		||||
		r.PoolSwappableVouchers[i].TokenSymbol = sanitizeSymbol(r.PoolSwappableVouchers[i].TokenSymbol)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return r.PoolSwappableVouchers, nil
 | 
			
		||||
}
 | 
			
		||||
@ -468,15 +423,6 @@ func (as HTTPAccountService) getPoolSwappableVouchers(ctx context.Context, poolA
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	_, err = doRequest(ctx, req, &r)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Normalize symbols before returning
 | 
			
		||||
	for i := range r.PoolSwappableVouchers {
 | 
			
		||||
		r.PoolSwappableVouchers[i].TokenSymbol = sanitizeSymbol(r.PoolSwappableVouchers[i].TokenSymbol)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return r.PoolSwappableVouchers, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -613,54 +559,6 @@ func requestEnsAlias(ctx context.Context, publicKey string, hint string) (*model
 | 
			
		||||
	return &r, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (as *HTTPAccountService) UpdateAlias(ctx context.Context, name string, publicKey string) (*models.RequestAliasResult, error) {
 | 
			
		||||
	if as.SS == nil {
 | 
			
		||||
		return nil, fmt.Errorf("The storage service cannot be nil")
 | 
			
		||||
	}
 | 
			
		||||
	if as.UseApi {
 | 
			
		||||
		if !strings.Contains(name, ".") {
 | 
			
		||||
			name = as.ToFqdn(name)
 | 
			
		||||
		}
 | 
			
		||||
		enr, err := updateEnsAlias(ctx, name, publicKey)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
		return &models.RequestAliasResult{Alias: enr.Name}, nil
 | 
			
		||||
	} else {
 | 
			
		||||
		svc := dev.NewDevAccountService(ctx, as.SS)
 | 
			
		||||
		return svc.RequestAlias(ctx, publicKey, name)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func updateEnsAlias(ctx context.Context, name string, publicKey string) (*models.AliasEnsResult, error) {
 | 
			
		||||
	var r models.AliasEnsResult
 | 
			
		||||
 | 
			
		||||
	endpoint := config.AliasUpdateURL
 | 
			
		||||
 | 
			
		||||
	logg.InfoCtxf(ctx, "updating alias", "endpoint", endpoint, "name", name)
 | 
			
		||||
 | 
			
		||||
	payload := map[string]string{
 | 
			
		||||
		"name":    name,
 | 
			
		||||
		"address": publicKey,
 | 
			
		||||
	}
 | 
			
		||||
	payloadBytes, err := json.Marshal(payload)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
	req, err := http.NewRequest("PUT", 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 updated", "alias", r.Name)
 | 
			
		||||
	return &r, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// SendSMS calls the API to send out an SMS.
 | 
			
		||||
// Parameters:
 | 
			
		||||
//   - inviterPhone: The user initiating the SMS.
 | 
			
		||||
@ -774,11 +672,7 @@ func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKRespons
 | 
			
		||||
		if err := json.Unmarshal(body, &errResponse); err != nil {
 | 
			
		||||
			return nil, err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		return nil, &APIError{
 | 
			
		||||
			Code:        errResponse.ErrCode,
 | 
			
		||||
			Description: errResponse.Description,
 | 
			
		||||
		}
 | 
			
		||||
		return nil, errors.New(errResponse.Description)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := json.Unmarshal(body, &okResponse); err != nil {
 | 
			
		||||
@ -786,7 +680,7 @@ func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKRespons
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(okResponse.Result) == 0 {
 | 
			
		||||
		return nil, errors.New("empty api result")
 | 
			
		||||
		return nil, errors.New("Empty api result")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	v, err := json.Marshal(okResponse.Result)
 | 
			
		||||
 | 
			
		||||
@ -58,10 +58,6 @@ func (m MockApi) RequestAlias(ctx context.Context, publicKey string, hint string
 | 
			
		||||
	return nil, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (m MockApi) UpdateAlias(ctx context.Context, publicKey string, name string) (*models.RequestAliasResult, error) {
 | 
			
		||||
	return nil, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (m MockApi) TokenTransfer(ctx context.Context, amount, from, to, tokenAddress string) (*models.TokenTransferResponse, error) {
 | 
			
		||||
	return nil, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -58,11 +58,6 @@ func (m *MockAccountService) RequestAlias(ctx context.Context, publicKey string,
 | 
			
		||||
	return args.Get(0).(*models.RequestAliasResult), args.Error(1)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (m *MockAccountService) UpdateAlias(ctx context.Context, publicKey string, name string) (*models.RequestAliasResult, error) {
 | 
			
		||||
	args := m.Called(publicKey, name)
 | 
			
		||||
	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)
 | 
			
		||||
 | 
			
		||||
@ -69,10 +69,6 @@ func (m *TestAccountService) RequestAlias(ctx context.Context, publicKey string,
 | 
			
		||||
	return &models.RequestAliasResult{}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (m *TestAccountService) UpdateAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
 | 
			
		||||
	return &models.RequestAliasResult{}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (m *TestAccountService) SendUpsellSMS(ctx context.Context, inviterPhone, inviteePhone string) (*models.SendSMSResponse, error) {
 | 
			
		||||
	return &models.SendSMSResponse{}, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user