Compare commits
17 Commits
pool-swap-
...
sms-api
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6597d6b6a2
|
||
|
|
441e289854
|
||
|
|
5b41c8dc64
|
||
|
|
417e7f0179
|
||
|
|
2eed990921
|
||
|
|
8145b4bd00
|
||
|
|
dda02852bf
|
||
|
|
6e437cb8e0
|
||
|
|
4af0e9709d
|
||
|
|
9c8a3df971
|
||
|
|
62c33e2587 | ||
|
|
4a5de68a8c
|
||
|
|
6c4702e2ba
|
||
|
|
7280784ee1
|
||
|
|
1f3ac220d1
|
||
|
|
e64eb265a5
|
||
|
|
d5dc792dce
|
@@ -15,16 +15,16 @@ 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"
|
AliasPrefix = "api/v1/alias"
|
||||||
poolDepositPrefix = "/api/v2/pool/deposit"
|
SendSMSPrefix = "api/v1/external/upsell"
|
||||||
poolSwapQoutePrefix = "/api/v2/pool/quote"
|
AliasEnsPrefix = "/api/v1/bypass"
|
||||||
poolSwapPrefix = "/api/v2/pool/swap"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
custodialURLBase string
|
custodialURLBase string
|
||||||
dataURLBase string
|
dataURLBase string
|
||||||
BearerToken string
|
BearerToken string
|
||||||
|
aliasEnsURLBase string
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -37,9 +37,8 @@ var (
|
|||||||
VoucherTransfersURL string
|
VoucherTransfersURL string
|
||||||
VoucherDataURL string
|
VoucherDataURL string
|
||||||
CheckAliasURL string
|
CheckAliasURL string
|
||||||
PoolDepositURL string
|
SendSMSURL string
|
||||||
PoolSwapQuoteURL string
|
AliasEnsURL string
|
||||||
PoolSwapURL string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func setBase() error {
|
func setBase() error {
|
||||||
@@ -47,6 +46,7 @@ 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")
|
||||||
BearerToken = env.GetEnv("BEARER_TOKEN", "")
|
BearerToken = env.GetEnv("BEARER_TOKEN", "")
|
||||||
|
|
||||||
_, err = url.Parse(custodialURLBase)
|
_, err = url.Parse(custodialURLBase)
|
||||||
@@ -74,10 +74,8 @@ 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)
|
CheckAliasURL, _ = url.JoinPath(dataURLBase, AliasPrefix)
|
||||||
PoolDepositURL, _ = url.JoinPath(custodialURLBase, poolDepositPrefix)
|
SendSMSURL, _ = url.JoinPath(dataURLBase, SendSMSPrefix)
|
||||||
PoolSwapQuoteURL, _ = url.JoinPath(custodialURLBase, poolSwapQoutePrefix)
|
AliasEnsURL, _ = url.JoinPath(aliasEnsURLBase, AliasEnsPrefix)
|
||||||
PoolSwapURL, _ = url.JoinPath(custodialURLBase, poolSwapPrefix)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
158
dev/api.go
158
dev/api.go
@@ -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"
|
||||||
@@ -93,11 +91,6 @@ type Voucher struct {
|
|||||||
Location string `json: "location"`
|
Location string `json: "location"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Pool struct {
|
|
||||||
vouchers []Voucher
|
|
||||||
poolLimit map[string]string
|
|
||||||
}
|
|
||||||
|
|
||||||
type DevAccountService struct {
|
type DevAccountService struct {
|
||||||
db db.Db
|
db db.Db
|
||||||
accounts map[string]Account
|
accounts map[string]Account
|
||||||
@@ -113,7 +106,6 @@ type DevAccountService struct {
|
|||||||
defaultAccount string
|
defaultAccount string
|
||||||
emitterFunc event.EmitterFunc
|
emitterFunc event.EmitterFunc
|
||||||
pfx []byte
|
pfx []byte
|
||||||
pool Pool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDevAccountService(ctx context.Context, ss storage.StorageService) *DevAccountService {
|
func NewDevAccountService(ctx context.Context, ss storage.StorageService) *DevAccountService {
|
||||||
@@ -126,7 +118,6 @@ 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{},
|
|
||||||
defaultAccount: zeroAddress,
|
defaultAccount: zeroAddress,
|
||||||
pfx: []byte("__"),
|
pfx: []byte("__"),
|
||||||
}
|
}
|
||||||
@@ -180,15 +171,6 @@ func (das *DevAccountService) loadAccount(ctx context.Context, pubKey string, v
|
|||||||
return nil
|
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 {
|
func (das *DevAccountService) loadTx(ctx context.Context, hsh string, v []byte) error {
|
||||||
var mytx Tx
|
var mytx Tx
|
||||||
|
|
||||||
@@ -433,74 +415,6 @@ func (das *DevAccountService) CreateAccount(ctx context.Context) (*models.Accoun
|
|||||||
}, nil
|
}, 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) {
|
func (das *DevAccountService) TrackAccountStatus(ctx context.Context, publicKey string) (*models.TrackStatusResult, error) {
|
||||||
var ok bool
|
var ok bool
|
||||||
_, ok = das.accounts[publicKey]
|
_, ok = das.accounts[publicKey]
|
||||||
@@ -740,71 +654,9 @@ 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)
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,32 +0,0 @@
|
|||||||
{
|
|
||||||
"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": ""
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
{
|
|
||||||
"ok": true,
|
|
||||||
"description": "Swap to list",
|
|
||||||
"result": {
|
|
||||||
"filtered": [
|
|
||||||
{
|
|
||||||
"contractAddress": "0x765DE816845861e75A25fCA122bb6898B8B1282a",
|
|
||||||
"tokenSymbol": "cUSD",
|
|
||||||
"tokenDecimals": "18",
|
|
||||||
"balance": ""
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
{
|
|
||||||
"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": ""
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -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"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -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"`
|
|
||||||
}
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
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"`
|
|
||||||
}
|
|
||||||
5
models/send_sms_response.go
Normal file
5
models/send_sms_response.go
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
type SendSMSResponse struct {
|
||||||
|
Invitee string `json:"invitee"`
|
||||||
|
}
|
||||||
@@ -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"`
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -17,4 +17,5 @@ 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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,120 +234,114 @@ 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)
|
ep, err := url.JoinPath(config.AliasEnsURL, "/resolve")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
req, err := http.NewRequest("GET", ep, nil)
|
|
||||||
|
u, err := url.Parse(ep)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
_, err = doRequest(ctx, req, &r)
|
|
||||||
return &r, err
|
query := u.Query()
|
||||||
|
query.Set("name", alias)
|
||||||
|
u.RawQuery = query.Encode()
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", u.String(), nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
_, err = doRequest(ctx, req, &aliasEnsResult)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &models.AliasAddress{Address: aliasEnsResult.Address}, 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) {
|
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")
|
||||||
}
|
}
|
||||||
svc := dev.NewDevAccountService(ctx, as.SS)
|
if as.UseApi {
|
||||||
return svc.RequestAlias(ctx, publicKey, hint)
|
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)
|
||||||
|
return svc.RequestAlias(ctx, publicKey, hint)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func requestEnsAlias(ctx context.Context, publicKey string, hint string) (*models.AliasEnsResult, error) {
|
||||||
|
var r models.AliasEnsResult
|
||||||
|
|
||||||
|
ep, err := url.JoinPath(config.AliasEnsURL, "/register")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
logg.InfoCtxf(ctx, "requesting alias", "endpoint", ep, "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", ep, 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
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: remove eth-custodial api dependency
|
// TODO: remove eth-custodial api dependency
|
||||||
|
|||||||
@@ -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
|
||||||
|
}
|
||||||
|
|||||||
@@ -53,7 +53,12 @@ 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)
|
||||||
|
}
|
||||||
|
|||||||
@@ -64,3 +64,7 @@ func (m TestAccountService) CheckAliasAddress(ctx context.Context, alias string)
|
|||||||
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
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user