Merge branch 'pool-swap-endpoints-issue#9' into pool-swap-endpoints
This commit is contained in:
commit
4783e2dcb3
155
dev/api.go
155
dev/api.go
@ -6,7 +6,9 @@ import (
|
|||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -91,6 +93,11 @@ 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
|
||||||
@ -106,6 +113,7 @@ 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 {
|
||||||
@ -118,6 +126,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{},
|
||||||
defaultAccount: zeroAddress,
|
defaultAccount: zeroAddress,
|
||||||
pfx: []byte("__"),
|
pfx: []byte("__"),
|
||||||
}
|
}
|
||||||
@ -171,6 +180,15 @@ 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
|
||||||
|
|
||||||
@ -415,6 +433,74 @@ 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]
|
||||||
@ -653,3 +739,72 @@ func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string
|
|||||||
Alias: alias,
|
Alias: alias,
|
||||||
}, nil
|
}, 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
|
||||||
|
}
|
||||||
|
32
dev/data/swap_from.json
Normal file
32
dev/data/swap_from.json
Normal file
@ -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": ""
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
14
dev/data/swap_to.json
Normal file
14
dev/data/swap_to.json
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"ok": true,
|
||||||
|
"description": "Swap to list",
|
||||||
|
"result": {
|
||||||
|
"filtered": [
|
||||||
|
{
|
||||||
|
"contractAddress": "0x765DE816845861e75A25fCA122bb6898B8B1282a",
|
||||||
|
"tokenSymbol": "cUSD",
|
||||||
|
"tokenDecimals": "18",
|
||||||
|
"balance": ""
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
43
dev/data/top_pools.json
Normal file
43
dev/data/top_pools.json
Normal file
@ -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": ""
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
10
models/pool.go
Normal file
10
models/pool.go
Normal file
@ -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"`
|
||||||
|
}
|
@ -8,3 +8,10 @@ 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"`
|
||||||
|
}
|
||||||
|
@ -239,6 +239,11 @@ func resolveAliasAddress(ctx context.Context, alias string) (*models.AliasAddres
|
|||||||
return &r, err
|
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) {
|
func (as *HTTPAccountService) PoolDeposit(ctx context.Context, amount, from, poolAddress, tokenAddress string) (*models.PoolDepositResult, error) {
|
||||||
var r models.PoolDepositResult
|
var r models.PoolDepositResult
|
||||||
|
|
||||||
@ -292,6 +297,14 @@ 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) {
|
||||||
|
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) {
|
func (as *HTTPAccountService) PoolSwap(ctx context.Context, amount, from, fromTokenAddress, poolAddress, toTokenAddress string) (*models.PoolSwapResult, error) {
|
||||||
var r models.PoolSwapResult
|
var r models.PoolSwapResult
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user