Compare commits
28 Commits
pool-swap-
...
multi-pool
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c09cb77e19
|
||
|
|
e053d561c9
|
||
|
|
b9cef2b578
|
||
|
|
06a7ddea6f
|
||
|
|
ba43610ff0
|
||
|
|
3c2889ac72
|
||
|
|
5432365874
|
||
|
|
3244c717cb
|
||
|
|
17e89e0b88
|
||
|
|
3b39b86d09
|
||
|
|
37a2006a0b
|
||
|
|
d4f27af7a7
|
||
|
|
ba8d2a19c2
|
||
|
|
e3b6c25792
|
||
|
|
a04f7ee66c
|
||
|
|
52e1be1104
|
||
|
|
d7ae1cc096
|
||
|
|
8145b4bd00
|
||
|
|
dda02852bf
|
||
|
|
6e437cb8e0
|
||
|
|
4af0e9709d
|
||
|
|
9c8a3df971
|
||
|
|
62c33e2587 | ||
|
|
4783e2dcb3
|
||
|
|
35f24b5183
|
||
|
|
1f3ac220d1
|
||
|
|
e64eb265a5
|
||
|
|
d5dc792dce
|
@@ -19,12 +19,14 @@ const (
|
||||
poolDepositPrefix = "/api/v2/pool/deposit"
|
||||
poolSwapQoutePrefix = "/api/v2/pool/quote"
|
||||
poolSwapPrefix = "/api/v2/pool/swap"
|
||||
AliasEnsPrefix = "/api/v1/bypass"
|
||||
)
|
||||
|
||||
var (
|
||||
custodialURLBase string
|
||||
dataURLBase string
|
||||
BearerToken string
|
||||
aliasEnsURLBase string
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -40,6 +42,7 @@ var (
|
||||
PoolDepositURL string
|
||||
PoolSwapQuoteURL string
|
||||
PoolSwapURL string
|
||||
AliasEnsURL string
|
||||
)
|
||||
|
||||
func setBase() error {
|
||||
@@ -47,6 +50,7 @@ func setBase() error {
|
||||
|
||||
custodialURLBase = env.GetEnv("CUSTODIAL_URL_BASE", "http://localhost:5003")
|
||||
dataURLBase = env.GetEnv("DATA_URL_BASE", "http://localhost:5006")
|
||||
aliasEnsURLBase = env.GetEnv("ALIAS_ENS_BASE", "http://localhost:5015")
|
||||
BearerToken = env.GetEnv("BEARER_TOKEN", "")
|
||||
|
||||
_, err = url.Parse(custodialURLBase)
|
||||
@@ -79,5 +83,6 @@ func LoadConfig() error {
|
||||
PoolSwapQuoteURL, _ = url.JoinPath(custodialURLBase, poolSwapQoutePrefix)
|
||||
PoolSwapURL, _ = url.JoinPath(custodialURLBase, poolSwapPrefix)
|
||||
|
||||
AliasEnsURL, _ = url.JoinPath(aliasEnsURLBase, AliasEnsPrefix)
|
||||
return nil
|
||||
}
|
||||
|
||||
232
dev/api.go
232
dev/api.go
@@ -6,9 +6,7 @@ import (
|
||||
"crypto/sha1"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -36,6 +34,9 @@ const (
|
||||
defaultDecimals = 6
|
||||
zeroAddress string = "0x0000000000000000000000000000000000000000"
|
||||
defaultVoucherBalance float64 = 500.00
|
||||
cityPoolAddress string = "0x3b517308D858a47458aD5C8E699697C5dc91Da0F"
|
||||
poolName string = "citypool"
|
||||
PoolSymbol string = "CTY"
|
||||
)
|
||||
|
||||
type Tx struct {
|
||||
@@ -94,8 +95,11 @@ type Voucher struct {
|
||||
}
|
||||
|
||||
type Pool struct {
|
||||
vouchers []Voucher
|
||||
poolLimit map[string]string
|
||||
Name string `json: "name"`
|
||||
Symbol string `json: "symbol"`
|
||||
Address string `json: "address"`
|
||||
Vouchers []Voucher `json: "voucher"`
|
||||
PoolLimit map[string]string `json: "poollimit"`
|
||||
}
|
||||
|
||||
type DevAccountService struct {
|
||||
@@ -113,7 +117,7 @@ type DevAccountService struct {
|
||||
defaultAccount string
|
||||
emitterFunc event.EmitterFunc
|
||||
pfx []byte
|
||||
pool Pool
|
||||
pools map[string]Pool
|
||||
}
|
||||
|
||||
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),
|
||||
txsTrack: make(map[string]string),
|
||||
autoVoucherValue: make(map[string]int),
|
||||
pool: Pool{},
|
||||
pools: make(map[string]Pool),
|
||||
defaultAccount: zeroAddress,
|
||||
pfx: []byte("__"),
|
||||
}
|
||||
@@ -181,7 +185,7 @@ func (das *DevAccountService) loadAccount(ctx context.Context, pubKey string, v
|
||||
}
|
||||
|
||||
func (p *Pool) hasVoucher(voucherAddress string) bool {
|
||||
for _, value := range p.vouchers {
|
||||
for _, value := range p.Vouchers {
|
||||
if value.Address == voucherAddress {
|
||||
return true
|
||||
}
|
||||
@@ -211,6 +215,17 @@ func (das *DevAccountService) loadAlias(ctx context.Context, alias string, key [
|
||||
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 {
|
||||
var err error
|
||||
s := string(k)
|
||||
@@ -218,6 +233,7 @@ func (das *DevAccountService) loadItem(ctx context.Context, k []byte, v []byte)
|
||||
if len(ss) != 2 {
|
||||
return fmt.Errorf("malformed key: %s", s)
|
||||
}
|
||||
fmt.Println("S1", ss[0])
|
||||
if ss[0] == "account" {
|
||||
err = das.loadAccount(ctx, ss[1], v)
|
||||
logg.ErrorCtxf(ctx, "loading saved account failed", "error_load_account", err)
|
||||
@@ -227,6 +243,8 @@ func (das *DevAccountService) loadItem(ctx context.Context, k []byte, v []byte)
|
||||
} else if ss[0] == "alias" {
|
||||
err = das.loadAlias(ctx, ss[1], k)
|
||||
logg.ErrorCtxf(ctx, "loading aliases failed", "error_load_aliases", err)
|
||||
} else if ss[0] == "pool" {
|
||||
err = das.loadPoolInfo(ctx, ss[1], v)
|
||||
} else {
|
||||
logg.ErrorCtxf(ctx, "unknown double underscore key", "key", ss[0])
|
||||
}
|
||||
@@ -242,13 +260,13 @@ func (das *DevAccountService) loadAll(ctx context.Context) error {
|
||||
}
|
||||
for true {
|
||||
k, v := dumper.Next(ctx)
|
||||
logg.InfoCtxf(ctx, "loading all", "key", string(k), "value", string(v))
|
||||
if k == nil {
|
||||
break
|
||||
}
|
||||
if !bytes.HasPrefix(k, das.pfx) {
|
||||
continue
|
||||
}
|
||||
|
||||
err = das.loadItem(ctx, k, v)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -283,6 +301,35 @@ func (das *DevAccountService) WithAutoVoucher(ctx context.Context, symbol string
|
||||
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: set max balance for 0x00 address
|
||||
func (das *DevAccountService) AddVoucher(ctx context.Context, symbol string) error {
|
||||
@@ -363,6 +410,20 @@ func (das *DevAccountService) saveAccount(ctx context.Context, acc Account) erro
|
||||
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 {
|
||||
if das.db == nil {
|
||||
return fmt.Errorf("Db cannot be nil")
|
||||
@@ -434,6 +495,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) {
|
||||
_, ok := das.accounts[from]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("account not found (publickey): %v", from)
|
||||
}
|
||||
sym, ok := das.vouchersAddress[tokenAddress]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("voucher address %v not found", tokenAddress)
|
||||
@@ -443,14 +508,13 @@ func (das *DevAccountService) PoolDeposit(ctx context.Context, amount, from, poo
|
||||
return nil, err
|
||||
}
|
||||
|
||||
voucher, ok := das.vouchers[sym]
|
||||
_, 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},
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &models.PoolDepositResult{
|
||||
TrackingId: uid.String(),
|
||||
@@ -462,18 +526,18 @@ func (das *DevAccountService) GetPoolSwapQuote(ctx context.Context, amount, 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]
|
||||
p, ok := das.pools[poolAddress]
|
||||
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
|
||||
if !p.hasVoucher(toTokenAddress) {
|
||||
return nil, fmt.Errorf("Voucher with address: %v not found in the pool", toTokenAddress)
|
||||
//resolve the token address you are trying to swap from(fromTokenAddress)
|
||||
ok = p.hasVoucher(fromTokenAddress)
|
||||
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
|
||||
}
|
||||
|
||||
@@ -483,19 +547,22 @@ func (das *DevAccountService) PoolSwap(ctx context.Context, amount, from, fromTo
|
||||
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 {
|
||||
return nil, fmt.Errorf("account not found (publickey): %v", from)
|
||||
}
|
||||
_, ok = das.vouchersAddress[fromTokenAddress]
|
||||
ok = p.hasVoucher(fromTokenAddress)
|
||||
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
|
||||
if !p.hasVoucher(toTokenAddress) {
|
||||
return nil, fmt.Errorf("Voucher with address: %v not found in the pool", toTokenAddress)
|
||||
ok = p.hasVoucher(toTokenAddress)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("token %v not found in the pool", toTokenAddress)
|
||||
}
|
||||
|
||||
return &models.PoolSwapResult{TrackingId: uid.String()}, nil
|
||||
@@ -740,71 +807,66 @@ func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (das *DevAccountService) FetchTopPools(ctx context.Context) ([]models.Pool, error) {
|
||||
var (
|
||||
r struct {
|
||||
OK bool `json:"ok"`
|
||||
Description string `json:"description"`
|
||||
Result struct {
|
||||
Pools []models.Pool `json:"topPools"`
|
||||
} `json:"result"`
|
||||
}
|
||||
)
|
||||
data, err := os.ReadFile("./data/top_pools.json")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
func (das *DevAccountService) FetchTopPools(ctx context.Context) ([]dataserviceapi.PoolDetails, error) {
|
||||
var topPools []dataserviceapi.PoolDetails
|
||||
for _, p := range das.pools {
|
||||
topPools = append(topPools, dataserviceapi.PoolDetails{
|
||||
PoolName: p.Name,
|
||||
PoolSymbol: p.Symbol,
|
||||
PoolContractAdrress: p.Address,
|
||||
})
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &r)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return r.Result.Pools, nil
|
||||
return topPools, 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)
|
||||
func (das *DevAccountService) GetPoolSwappableFromVouchers(ctx context.Context, poolAddress, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
|
||||
var swapFromList []dataserviceapi.TokenHoldings
|
||||
|
||||
p, ok := das.pools[poolAddress]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Invalid pool address: %v", poolAddress)
|
||||
}
|
||||
for _, v := range p.Vouchers {
|
||||
swapFromList = append(swapFromList, dataserviceapi.TokenHoldings{
|
||||
ContractAddress: v.Address,
|
||||
TokenSymbol: v.Symbol,
|
||||
TokenDecimals: string(defaultDecimals),
|
||||
Balance: fmt.Sprintf("%f", defaultVoucherBalance),
|
||||
})
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &r)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return r.Result.SwappableVouchers, nil
|
||||
return swapFromList, 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)
|
||||
func (das *DevAccountService) GetPoolSwappableVouchers(ctx context.Context, poolAddress, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
|
||||
var swapToList []dataserviceapi.TokenHoldings
|
||||
_, ok := das.pools[poolAddress]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Invalid pool address: %v", poolAddress)
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &r)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
for _, voucher := range das.vouchers {
|
||||
swapToList = append(swapToList, dataserviceapi.TokenHoldings{
|
||||
ContractAddress: voucher.Address,
|
||||
TokenSymbol: voucher.Symbol,
|
||||
TokenDecimals: string(defaultDecimals),
|
||||
Balance: fmt.Sprintf("%f", defaultVoucherBalance),
|
||||
})
|
||||
}
|
||||
|
||||
return r.Result.SwappableVouchers, nil
|
||||
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
|
||||
}
|
||||
|
||||
2
go.mod
2
go.mod
@@ -8,7 +8,7 @@ require (
|
||||
git.grassecon.net/grassrootseconomics/visedriver v0.8.0-beta.10.0.20250122123424-6749c632b0a2
|
||||
github.com/gofrs/uuid v4.4.0+incompatible
|
||||
github.com/grassrootseconomics/eth-custodial v1.3.0-beta
|
||||
github.com/grassrootseconomics/ussd-data-service v1.2.0-beta
|
||||
github.com/grassrootseconomics/ussd-data-service v1.4.0-beta
|
||||
github.com/stretchr/testify v1.9.0
|
||||
)
|
||||
|
||||
|
||||
2
go.sum
2
go.sum
@@ -18,6 +18,8 @@ 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/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.4.0-beta h1:4fMd/3h2ZIhRg4GdHQmRw5FfD3MpJvFNNJQo+Q27f5M=
|
||||
github.com/grassrootseconomics/ussd-data-service v1.4.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/go.mod h1:zpZDgZFzeq9s0MIeB1P50NIEWDFFHSFBohI/NbaTD/Y=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
|
||||
@@ -7,3 +7,13 @@ type RequestAliasResult struct {
|
||||
type AliasAddress struct {
|
||||
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"`
|
||||
}
|
||||
@@ -12,3 +12,7 @@ type PoolSwapQuoteResult struct {
|
||||
type PoolSwapResult struct {
|
||||
TrackingId string `json:"trackingId"`
|
||||
}
|
||||
|
||||
type MaxLimitResult struct {
|
||||
Max string `json:"max"`
|
||||
}
|
||||
|
||||
@@ -8,10 +8,3 @@ type VoucherDataResult struct {
|
||||
TokenCommodity string `json:"tokenCommodity"`
|
||||
TokenLocation string `json:"tokenLocation"`
|
||||
}
|
||||
|
||||
type SwappableVoucher struct {
|
||||
ContractAddress string `json:"contractAddress"`
|
||||
TokenSymbol string `json:"tokenSymbol"`
|
||||
TokenDecimals string `json:"tokenDecimals"`
|
||||
Balance string `json:"balance"`
|
||||
}
|
||||
|
||||
@@ -17,4 +17,11 @@ 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)
|
||||
PoolDeposit(ctx context.Context, amount, from, poolAddress, tokenAddress string) (*models.PoolDepositResult, error)
|
||||
FetchTopPools(ctx context.Context) ([]dataserviceapi.PoolDetails, error)
|
||||
GetPoolSwappableFromVouchers(ctx context.Context, poolAddress, publicKey string) ([]dataserviceapi.TokenHoldings, error)
|
||||
GetPoolSwappableVouchers(ctx context.Context, poolAddress, publicKey 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)
|
||||
}
|
||||
|
||||
@@ -11,7 +11,9 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"git.defalsify.org/vise.git/logging"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/config"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/dev"
|
||||
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
||||
@@ -22,6 +24,7 @@ import (
|
||||
|
||||
var (
|
||||
aliasRegex = regexp.MustCompile("^\\+?[a-zA-Z0-9\\-_]+$")
|
||||
logg = logging.NewVanilla().WithDomain("sarafu-api.devapi")
|
||||
)
|
||||
|
||||
type HTTPAccountService struct {
|
||||
@@ -59,6 +62,10 @@ func (as *HTTPAccountService) TrackAccountStatus(ctx context.Context, publicKey
|
||||
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.
|
||||
// Parameters:
|
||||
// - 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 {
|
||||
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)
|
||||
if as.UseApi {
|
||||
logg.InfoCtxf(ctx, "resolving alias to address", "alias", alias)
|
||||
return resolveAliasAddress(ctx, alias)
|
||||
} else {
|
||||
return svc.CheckAliasAddress(ctx, alias)
|
||||
@@ -225,21 +234,36 @@ func (as *HTTPAccountService) CheckAliasAddress(ctx context.Context, alias strin
|
||||
}
|
||||
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
req, err := http.NewRequest("GET", ep, nil)
|
||||
|
||||
u, err := url.Parse(ep)
|
||||
if err != nil {
|
||||
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) {
|
||||
func (as *HTTPAccountService) FetchTopPools(ctx context.Context) ([]dataserviceapi.PoolDetails, error) {
|
||||
svc := dev.NewDevAccountService(ctx, as.SS)
|
||||
return svc.FetchTopPools(ctx)
|
||||
}
|
||||
@@ -297,12 +321,14 @@ func (as *HTTPAccountService) GetPoolSwapQuote(ctx context.Context, amount, from
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
func (as *HTTPAccountService) GetPoolSwappableFromVouchers(ctx context.Context) ([]models.SwappableVoucher, error) {
|
||||
return as.GetPoolSwappableFromVouchers(ctx)
|
||||
func (as *HTTPAccountService) GetPoolSwappableFromVouchers(ctx context.Context, poolAddress, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
|
||||
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) GetPoolSwappableVouchers(ctx context.Context, poolAddress, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
|
||||
svc := dev.NewDevAccountService(ctx, as.SS)
|
||||
return svc.GetPoolSwappableVouchers(ctx, poolAddress, publicKey)
|
||||
}
|
||||
|
||||
func (as *HTTPAccountService) PoolSwap(ctx context.Context, amount, from, fromTokenAddress, poolAddress, toTokenAddress string) (*models.PoolSwapResult, error) {
|
||||
@@ -332,13 +358,59 @@ func (as *HTTPAccountService) PoolSwap(ctx context.Context, amount, from, fromTo
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
// TODO: Use actual custodial api to request available alias
|
||||
func (as *HTTPAccountService) GetSwapFromTokenMaxLimit(ctx context.Context, poolAddress, fromTokenAddress, toTokenAddress, publicKey string) (*models.MaxLimitResult, error) {
|
||||
svc := dev.NewDevAccountService(ctx, as.SS)
|
||||
return svc.GetSwapFromTokenMaxLimit(ctx, poolAddress, fromTokenAddress, toTokenAddress, publicKey)
|
||||
}
|
||||
|
||||
func (as *HTTPAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
|
||||
if as.SS == nil {
|
||||
return nil, fmt.Errorf("The storage service cannot be nil")
|
||||
}
|
||||
svc := dev.NewDevAccountService(ctx, as.SS)
|
||||
return svc.RequestAlias(ctx, publicKey, hint)
|
||||
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)
|
||||
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
|
||||
}
|
||||
|
||||
// TODO: remove eth-custodial api dependency
|
||||
|
||||
@@ -57,3 +57,38 @@ func (m MockAccountService) RequestAlias(ctx context.Context, publicKey string,
|
||||
args := m.Called(publicKey, hint)
|
||||
return args.Get(0).(*models.RequestAliasResult), args.Error(1)
|
||||
}
|
||||
|
||||
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) 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, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
|
||||
args := m.Called(poolAddress, publicKey)
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -57,6 +57,10 @@ func (tas *TestAccountService) TokenTransfer(ctx context.Context, amount, from,
|
||||
}, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@@ -64,3 +68,27 @@ func (m TestAccountService) CheckAliasAddress(ctx context.Context, alias string)
|
||||
func (m TestAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
|
||||
return &models.RequestAliasResult{}, nil
|
||||
}
|
||||
|
||||
func (m TestAccountService) FetchTopPools(ctx context.Context) ([]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, publicKey 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user