From f723e0aa45cc49d4fb39878897793584d33d9948 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Fri, 7 Mar 2025 08:09:41 +0300 Subject: [PATCH 1/4] add sample test data --- dev/data/swap_from.json | 32 ++++++++++++++++++++++++++++++ dev/data/swap_to.json | 14 ++++++++++++++ dev/data/top_pools.json | 43 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 dev/data/swap_from.json create mode 100644 dev/data/swap_to.json create mode 100644 dev/data/top_pools.json diff --git a/dev/data/swap_from.json b/dev/data/swap_from.json new file mode 100644 index 0000000..e523972 --- /dev/null +++ b/dev/data/swap_from.json @@ -0,0 +1,32 @@ +{ + "ok": true, + "description": "Swap from list", + "result": { + "filtered": [ + { + "contractAddress": "0xc7B78Ac9ACB9E025C8234621FC515bC58179dEAe", + "tokenSymbol": "AMANI", + "tokenDecimals": "6", + "balance": "" + }, + { + "contractAddress": "0xF0C3C7581b8b96B59a97daEc8Bd48247cE078674", + "tokenSymbol": "AMUA", + "tokenDecimals": "6", + "balance": "" + }, + { + "contractAddress": "0x371455a30fc62736145Bd8429Fcc6481186f235F", + "tokenSymbol": "BAHARI", + "tokenDecimals": "6", + "balance": "" + }, + { + "contractAddress": "0x7cA6113b59c24a880F382C7E12d609a6Eb05246b", + "tokenSymbol": "BANGLA", + "tokenDecimals": "6", + "balance": "" + } + ] + } +} \ No newline at end of file diff --git a/dev/data/swap_to.json b/dev/data/swap_to.json new file mode 100644 index 0000000..c8906b6 --- /dev/null +++ b/dev/data/swap_to.json @@ -0,0 +1,14 @@ +{ + "ok": true, + "description": "Swap to list", + "result": { + "filtered": [ + { + "contractAddress": "0x765DE816845861e75A25fCA122bb6898B8B1282a", + "tokenSymbol": "cUSD", + "tokenDecimals": "18", + "balance": "" + } + ] + } +} \ No newline at end of file diff --git a/dev/data/top_pools.json b/dev/data/top_pools.json new file mode 100644 index 0000000..85e120c --- /dev/null +++ b/dev/data/top_pools.json @@ -0,0 +1,43 @@ +{ + "ok": true, + "description": "Top 5 pools sorted by swaps", + "result": { + "topPools": [ + { + "poolName": "Kenya ROLA Pool", + "poolSymbol": "ROLA", + "poolContractAddress": "0x48a953cA5cf5298bc6f6Af3C608351f537AAcb9e", + "limiterAddress": "", + "voucherRegistry": "" + }, + { + "poolName": "Nairobi ROLA Pool", + "poolSymbol": "NAIROBI", + "poolContractAddress": "0xB0660Ac1Ee3d32ea35bc728D7CA1705Fa5A37528", + "limiterAddress": "", + "voucherRegistry": "" + }, + { + "poolName": "Friends of Kiriba Ecosystem ", + "poolSymbol": "FRIENDS", + "poolContractAddress": "0xC4848263821FA02baB2181910A2eFb9CECb2c21C", + "limiterAddress": "", + "voucherRegistry": "" + }, + { + "poolName": "Resilient Community Waqfs", + "poolSymbol": "REZILIENS", + "poolContractAddress": "0x1e40951d7a28147D8B4A554C60c42766C92e2Fc6", + "limiterAddress": "", + "voucherRegistry": "" + }, + { + "poolName": "GrE Tech", + "poolSymbol": "GRET", + "poolContractAddress": "0xb7B9d0A264eD1a8E2418571B7AC5933C79C9c2B8", + "limiterAddress": "", + "voucherRegistry": "" + } + ] + } +} \ No newline at end of file From b05734f976cc8dc4d06fb8d189c3917f46e51e91 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Fri, 7 Mar 2025 08:11:04 +0300 Subject: [PATCH 2/4] feat: add fetch pools,swappable pool vouchers --- dev/api.go | 155 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) diff --git a/dev/api.go b/dev/api.go index fc68c29..1ebbe7c 100644 --- a/dev/api.go +++ b/dev/api.go @@ -6,7 +6,9 @@ import ( "crypto/sha1" "encoding/json" "fmt" + "log" "math/rand" + "os" "regexp" "strconv" "strings" @@ -91,6 +93,11 @@ type Voucher struct { Location string `json: "location"` } +type Pool struct { + vouchers []Voucher + poolLimit map[string]string +} + type DevAccountService struct { db db.Db accounts map[string]Account @@ -106,6 +113,7 @@ type DevAccountService struct { defaultAccount string emitterFunc event.EmitterFunc pfx []byte + pool Pool } func NewDevAccountService(ctx context.Context, ss storage.StorageService) *DevAccountService { @@ -118,6 +126,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{}, defaultAccount: zeroAddress, pfx: []byte("__"), } @@ -171,6 +180,15 @@ func (das *DevAccountService) loadAccount(ctx context.Context, pubKey string, v return nil } +func (p *Pool) hasVoucher(voucherAddress string) bool { + for _, value := range p.vouchers { + if value.Address == voucherAddress { + return true + } + } + return false +} + func (das *DevAccountService) loadTx(ctx context.Context, hsh string, v []byte) error { var mytx Tx @@ -415,6 +433,74 @@ func (das *DevAccountService) CreateAccount(ctx context.Context) (*models.Accoun }, nil } +func (das *DevAccountService) PoolDeposit(ctx context.Context, amount, from, poolAddress, tokenAddress string) (*models.PoolDepositResult, error) { + sym, ok := das.vouchersAddress[tokenAddress] + if !ok { + return nil, fmt.Errorf("voucher address %v not found", tokenAddress) + } + uid, err := uuid.NewV4() + if err != nil { + return nil, err + } + + voucher, ok := das.vouchers[sym] + if !ok { + return nil, fmt.Errorf("voucher address %v found but does not resolve", tokenAddress) + } + + das.pool = Pool{ + vouchers: append(das.pool.vouchers, voucher), + poolLimit: map[string]string{tokenAddress: amount}, + } + return &models.PoolDepositResult{ + TrackingId: uid.String(), + }, nil +} + +func (das *DevAccountService) GetPoolSwapQuote(ctx context.Context, amount, from, fromTokenAddress, poolAddress, toTokenAddress string) (*models.PoolSwapQuoteResult, error) { + _, ok := das.accounts[from] + if !ok { + return nil, fmt.Errorf("account not found (publickey): %v", from) + } + //resolve the token address you are trying to swap from(fromTokenAddress) + _, ok = das.vouchersAddress[fromTokenAddress] + if !ok { + return nil, fmt.Errorf("voucher address %v not found", fromTokenAddress) + } + p := das.pool + + //check if pool has voucher to swap to + if !p.hasVoucher(toTokenAddress) { + return nil, fmt.Errorf("Voucher with address: %v not found in the pool", toTokenAddress) + } + //Return a a quote that is equal to the amount enter + return &models.PoolSwapQuoteResult{IncludesFeesDeduction: false, OutValue: amount}, nil +} + +func (das *DevAccountService) PoolSwap(ctx context.Context, amount, from, fromTokenAddress, poolAddress, toTokenAddress string) (*models.PoolSwapResult, error) { + uid, err := uuid.NewV4() + if err != nil { + return nil, err + } + + _, ok := das.accounts[from] + if !ok { + return nil, fmt.Errorf("account not found (publickey): %v", from) + } + _, ok = das.vouchersAddress[fromTokenAddress] + if !ok { + return nil, fmt.Errorf("voucher address %v not found", fromTokenAddress) + } + p := das.pool + + //check if pool has voucher to swap to + if !p.hasVoucher(toTokenAddress) { + return nil, fmt.Errorf("Voucher with address: %v not found in the pool", toTokenAddress) + } + + return &models.PoolSwapResult{TrackingId: uid.String()}, nil +} + func (das *DevAccountService) TrackAccountStatus(ctx context.Context, publicKey string) (*models.TrackStatusResult, error) { var ok bool _, ok = das.accounts[publicKey] @@ -653,3 +739,72 @@ func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string Alias: alias, }, nil } + +func (das *DevAccountService) FetchTopPools(ctx context.Context) ([]models.Pool, error) { + var ( + r struct { + OK bool `json:"ok"` + Description string `json:"description"` + Result struct { + Pools []models.Pool `json:"topPools"` + } `json:"result"` + } + ) + data, err := os.ReadFile("./data/top_pools.json") + if err != nil { + log.Fatal(err) + } + + err = json.Unmarshal(data, &r) + if err != nil { + log.Fatal(err) + } + + return r.Result.Pools, nil +} + +func (das *DevAccountService) GetPoolSwappableFromVouchers(ctx context.Context) ([]models.SwappableVoucher, error) { + var ( + r struct { + OK bool `json:"ok"` + Description string `json:"description"` + Result struct { + SwappableVouchers []models.SwappableVoucher `json:"filtered"` + } `json:"result"` + } + ) + data, err := os.ReadFile("./data/swap_from.json") + if err != nil { + log.Fatal(err) + } + + err = json.Unmarshal(data, &r) + if err != nil { + log.Fatal(err) + } + + return r.Result.SwappableVouchers, nil +} + +func (das *DevAccountService) GetPoolSwappableVouchers(ctx context.Context) ([]models.SwappableVoucher, error) { + var ( + r struct { + OK bool `json:"ok"` + Description string `json:"description"` + Result struct { + SwappableVouchers []models.SwappableVoucher `json:"filtered"` + } `json:"result"` + } + ) + data, err := os.ReadFile("./data/swap_to.json") + if err != nil { + log.Fatal(err) + } + + err = json.Unmarshal(data, &r) + if err != nil { + log.Fatal(err) + } + + return r.Result.SwappableVouchers, nil +} From 67d7ec156728ead0166d0f83b616aa98e2d8d592 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Fri, 7 Mar 2025 08:11:33 +0300 Subject: [PATCH 3/4] add pool structs --- models/pool.go | 10 ++++++++++ models/voucher_data_result.go | 7 +++++++ 2 files changed, 17 insertions(+) create mode 100644 models/pool.go diff --git a/models/pool.go b/models/pool.go new file mode 100644 index 0000000..9e784b3 --- /dev/null +++ b/models/pool.go @@ -0,0 +1,10 @@ +package models + + +type Pool struct { + PoolName string `json:"poolName"` + PoolSymbol string `json:"poolSymbol"` + PoolContractAddress string `json:"poolContractAddress"` + LimiterAddress string `json:"limiterAddress"` + VoucherRegistry string `json:"voucherRegistry"` +} diff --git a/models/voucher_data_result.go b/models/voucher_data_result.go index 9a10831..1311482 100644 --- a/models/voucher_data_result.go +++ b/models/voucher_data_result.go @@ -8,3 +8,10 @@ type VoucherDataResult struct { TokenCommodity string `json:"tokenCommodity"` TokenLocation string `json:"tokenLocation"` } + +type SwappableVoucher struct { + ContractAddress string `json:"contractAddress"` + TokenSymbol string `json:"tokenSymbol"` + TokenDecimals string `json:"tokenDecimals"` + Balance string `json:"balance"` +} From e8c58a8f3308ea724ad14eb4618d700c63d6cc52 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Fri, 7 Mar 2025 08:13:15 +0300 Subject: [PATCH 4/4] feat: implement fetch pools,pool swappable vouchers with dummy data --- remote/http/service.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/remote/http/service.go b/remote/http/service.go index 86a856a..7bad334 100644 --- a/remote/http/service.go +++ b/remote/http/service.go @@ -239,6 +239,11 @@ func resolveAliasAddress(ctx context.Context, alias string) (*models.AliasAddres return &r, err } +func (as *HTTPAccountService) FetchTopPools(ctx context.Context) ([]models.Pool, error) { + svc := dev.NewDevAccountService(ctx, as.SS) + return svc.FetchTopPools(ctx) +} + func (as *HTTPAccountService) PoolDeposit(ctx context.Context, amount, from, poolAddress, tokenAddress string) (*models.PoolDepositResult, error) { var r models.PoolDepositResult @@ -292,6 +297,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) 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