Merge branch 'multi-pool' into pool-swap-endpoints
This commit is contained in:
commit
56ed9b2439
@ -22,6 +22,7 @@ const (
|
|||||||
topPoolsPrefix = "/api/v1/pool/top"
|
topPoolsPrefix = "/api/v1/pool/top"
|
||||||
retrievePoolDetailsPrefix = "/api/v1/pool/reverse"
|
retrievePoolDetailsPrefix = "/api/v1/pool/reverse"
|
||||||
poolSwappableVouchersPrefix = "/api/v1/pool"
|
poolSwappableVouchersPrefix = "/api/v1/pool"
|
||||||
|
AliasEnsPrefix = "/api/v1/bypass"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -47,6 +48,7 @@ var (
|
|||||||
TopPoolsURL string
|
TopPoolsURL string
|
||||||
RetrievePoolDetailsURL string
|
RetrievePoolDetailsURL string
|
||||||
PoolSwappableVouchersURL string
|
PoolSwappableVouchersURL string
|
||||||
|
AliasEnsURL string
|
||||||
)
|
)
|
||||||
|
|
||||||
func setBase() error {
|
func setBase() error {
|
||||||
@ -90,5 +92,6 @@ func LoadConfig() error {
|
|||||||
RetrievePoolDetailsURL, _ = url.JoinPath(dataURLBase, retrievePoolDetailsPrefix)
|
RetrievePoolDetailsURL, _ = url.JoinPath(dataURLBase, retrievePoolDetailsPrefix)
|
||||||
PoolSwappableVouchersURL, _ = url.JoinPath(dataURLBase, poolSwappableVouchersPrefix)
|
PoolSwappableVouchersURL, _ = url.JoinPath(dataURLBase, poolSwappableVouchersPrefix)
|
||||||
|
|
||||||
|
AliasEnsURL, _ = url.JoinPath(aliasEnsURLBase, AliasEnsPrefix)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
140
dev/api.go
140
dev/api.go
@ -117,7 +117,7 @@ type DevAccountService struct {
|
|||||||
defaultAccount string
|
defaultAccount string
|
||||||
emitterFunc event.EmitterFunc
|
emitterFunc event.EmitterFunc
|
||||||
pfx []byte
|
pfx []byte
|
||||||
pool Pool
|
pools map[string]Pool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDevAccountService(ctx context.Context, ss storage.StorageService) *DevAccountService {
|
func NewDevAccountService(ctx context.Context, ss storage.StorageService) *DevAccountService {
|
||||||
@ -130,7 +130,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{Address: cityPoolAddress, Name: poolName},
|
pools: make(map[string]Pool),
|
||||||
defaultAccount: zeroAddress,
|
defaultAccount: zeroAddress,
|
||||||
pfx: []byte("__"),
|
pfx: []byte("__"),
|
||||||
}
|
}
|
||||||
@ -222,8 +222,7 @@ func (das *DevAccountService) loadPoolInfo(ctx context.Context, name string, v [
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to unmarshall pool info: %v", err)
|
return fmt.Errorf("failed to unmarshall pool info: %v", err)
|
||||||
}
|
}
|
||||||
das.pool = pool
|
das.pools[name] = pool
|
||||||
logg.InfoCtxf(ctx, "loaded pool info", "name", das.pool.Name, "vouchers", das.pool.Vouchers)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -301,10 +300,33 @@ func (das *DevAccountService) WithAutoVoucher(ctx context.Context, symbol string
|
|||||||
return das
|
return das
|
||||||
}
|
}
|
||||||
|
|
||||||
func (das *DevAccountService) RegisterPool() {
|
func (das *DevAccountService) RegisterPool(ctx context.Context, name string, sm string) error {
|
||||||
das.pool.Name = poolName
|
var seedVouchers []Voucher
|
||||||
das.pool.Address = cityPoolAddress
|
|
||||||
das.pool.Symbol = PoolSymbol
|
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: add persistence for vouchers
|
||||||
@ -391,7 +413,7 @@ func (das *DevAccountService) savePoolInfo(ctx context.Context, pool Pool) error
|
|||||||
if das.db == nil {
|
if das.db == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
k := das.prefixKeyFor("pool", pool.Name)
|
k := das.prefixKeyFor("pool", pool.Address)
|
||||||
v, err := json.Marshal(pool)
|
v, err := json.Marshal(pool)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -485,15 +507,11 @@ func (das *DevAccountService) PoolDeposit(ctx context.Context, amount, from, poo
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
voucher, ok := das.vouchers[sym]
|
_, ok = das.vouchers[sym]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("voucher address %v found but does not resolve", tokenAddress)
|
return nil, fmt.Errorf("voucher address %v found but does not resolve", tokenAddress)
|
||||||
}
|
}
|
||||||
|
|
||||||
das.pool.Vouchers = append(das.pool.Vouchers, voucher)
|
|
||||||
das.pool.PoolLimit = map[string]string{tokenAddress: amount}
|
|
||||||
|
|
||||||
err = das.savePoolInfo(ctx, das.pool)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -507,17 +525,17 @@ func (das *DevAccountService) GetPoolSwapQuote(ctx context.Context, amount, from
|
|||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("account not found (publickey): %v", from)
|
return nil, fmt.Errorf("account not found (publickey): %v", from)
|
||||||
}
|
}
|
||||||
//resolve the token address you are trying to swap from(fromTokenAddress)
|
p, ok := das.pools[poolAddress]
|
||||||
_, ok = das.vouchersAddress[fromTokenAddress]
|
|
||||||
if !ok {
|
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
|
//resolve the token address you are trying to swap from(fromTokenAddress)
|
||||||
if !p.hasVoucher(toTokenAddress) {
|
ok = p.hasVoucher(fromTokenAddress)
|
||||||
return nil, fmt.Errorf("Voucher with address: %v not found in the pool", toTokenAddress)
|
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 entered
|
//Return a a quote that is equal to the amount entered
|
||||||
return &models.PoolSwapQuoteResult{IncludesFeesDeduction: false, OutValue: amount}, nil
|
return &models.PoolSwapQuoteResult{IncludesFeesDeduction: false, OutValue: amount}, nil
|
||||||
}
|
}
|
||||||
@ -528,19 +546,22 @@ func (das *DevAccountService) PoolSwap(ctx context.Context, amount, from, fromTo
|
|||||||
return nil, err
|
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 {
|
if !ok {
|
||||||
return nil, fmt.Errorf("account not found (publickey): %v", from)
|
return nil, fmt.Errorf("account not found (publickey): %v", from)
|
||||||
}
|
}
|
||||||
_, ok = das.vouchersAddress[fromTokenAddress]
|
ok = p.hasVoucher(fromTokenAddress)
|
||||||
if !ok {
|
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
|
ok = p.hasVoucher(toTokenAddress)
|
||||||
if !p.hasVoucher(toTokenAddress) {
|
if !ok {
|
||||||
return nil, fmt.Errorf("Voucher with address: %v not found in the pool", toTokenAddress)
|
return nil, fmt.Errorf("token %v not found in the pool", toTokenAddress)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &models.PoolSwapResult{TrackingId: uid.String()}, nil
|
return &models.PoolSwapResult{TrackingId: uid.String()}, nil
|
||||||
@ -787,47 +808,64 @@ func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string
|
|||||||
|
|
||||||
func (das *DevAccountService) FetchTopPools(ctx context.Context) ([]dataserviceapi.PoolDetails, error) {
|
func (das *DevAccountService) FetchTopPools(ctx context.Context) ([]dataserviceapi.PoolDetails, error) {
|
||||||
var topPools []dataserviceapi.PoolDetails
|
var topPools []dataserviceapi.PoolDetails
|
||||||
|
for _, p := range das.pools {
|
||||||
pool := dataserviceapi.PoolDetails{
|
topPools = append(topPools, dataserviceapi.PoolDetails{
|
||||||
PoolName: das.pool.Name,
|
PoolName: p.Name,
|
||||||
PoolSymbol: das.pool.Symbol,
|
PoolSymbol: p.Symbol,
|
||||||
PoolContractAdrress: das.pool.Address,
|
PoolContractAdrress: p.Address,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the citypool as the main pool
|
|
||||||
topPools = append(topPools, pool)
|
|
||||||
|
|
||||||
return topPools, nil
|
return topPools, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (das *DevAccountService) GetPoolSwappableFromVouchers(ctx context.Context, poolAddress, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
|
func (das *DevAccountService) GetPoolSwappableFromVouchers(ctx context.Context, poolAddress, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
|
||||||
var swapFromList []dataserviceapi.TokenHoldings
|
var swapFromList []dataserviceapi.TokenHoldings
|
||||||
|
|
||||||
for _, voucher := range das.pool.Vouchers {
|
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{
|
swapFromList = append(swapFromList, dataserviceapi.TokenHoldings{
|
||||||
|
ContractAddress: v.Address,
|
||||||
|
TokenSymbol: v.Symbol,
|
||||||
|
TokenDecimals: string(defaultDecimals),
|
||||||
|
Balance: fmt.Sprintf("%f", defaultVoucherBalance),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return swapFromList, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
for _, voucher := range das.vouchers {
|
||||||
|
swapToList = append(swapToList, dataserviceapi.TokenHoldings{
|
||||||
ContractAddress: voucher.Address,
|
ContractAddress: voucher.Address,
|
||||||
TokenSymbol: voucher.Symbol,
|
TokenSymbol: voucher.Symbol,
|
||||||
TokenDecimals: string(defaultDecimals),
|
TokenDecimals: string(defaultDecimals),
|
||||||
Balance: fmt.Sprintf("%f", defaultVoucherBalance),
|
Balance: fmt.Sprintf("%f", defaultVoucherBalance),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return swapFromList, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (das *DevAccountService) GetPoolSwappableVouchers(ctx context.Context, poolAddress, publicKey string) ([]dataserviceapi.TokenHoldings, error) {
|
|
||||||
swapToList := []dataserviceapi.TokenHoldings{
|
|
||||||
{
|
|
||||||
ContractAddress: "0x765DE816845861e75A25fCA122bb6898B8B1282a",
|
|
||||||
TokenSymbol: "cUSD",
|
|
||||||
TokenDecimals: "18",
|
|
||||||
Balance: "",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
return swapToList, nil
|
return swapToList, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (das *DevAccountService) GetSwapFromTokenMaxLimit(ctx context.Context, poolAddress, fromTokenAddress, toTokenAddress, publicKey string) (*models.MaxLimitResult, error) {
|
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{
|
return &models.MaxLimitResult{
|
||||||
Max: "1339482",
|
Max: limit,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user