Skip to content

Commit

Permalink
Complete wizardswap (#14)
Browse files Browse the repository at this point in the history
* add GetExchangeRateInfo, GetCurrencies and GetCurrenciesToPair for stealthes

* add wizardswap instant exchange

* wizardswap is leaved behide temporary

* complete support for wizardswap
  • Loading branch information
vibros68 authored Jan 16, 2024
1 parent 4a3c569 commit c949559
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 19 deletions.
6 changes: 0 additions & 6 deletions instantswap/exchange/stealthex/stealthex.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,6 @@ func (s *stealthex) EstimateAmount(vars interface{}) (res instantswap.EstimateAm
}

func parseResponseData(data []byte, obj interface{}) error {
var err Error
if json.Unmarshal(data, &err) == nil {
if err.Code > 0 {
return fmt.Errorf(err.Message)
}
}
return json.Unmarshal(data, obj)
}

Expand Down
5 changes: 0 additions & 5 deletions instantswap/exchange/stealthex/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,3 @@ type Order struct {
RefundAddress string `json:"refund_address"`
RefundExtraId string `json:"refund_extra_id"`
}

type Error struct {
Code int `json:"code"`
Message string `json:"message"`
}
56 changes: 56 additions & 0 deletions instantswap/exchange/wizardswap/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,59 @@ type OrderRequest struct {
RefundExtraId string `json:"refund_extra_id"`
ApiKey string `json:"api_key"`
}

type Exchange struct {
Id string `json:"id"`
Type string `json:"type"`
Timestamp string `json:"timestamp"`
UpdatedAt string `json:"updated_at"`
CurrencyFrom string `json:"currency_from"`
CurrencyTo string `json:"currency_to"`
AmountFrom float64 `json:"amount_from,string"`
ExpectedAmount float64 `json:"expected_amount,string"`
AmountTo float64 `json:"amount_to,string"`
AddressFrom string `json:"address_from"`
AddressTo string `json:"address_to"`
ExtraIdFrom string `json:"extra_id_from"`
ExtraIdTo string `json:"extra_id_to"`
TxFrom string `json:"tx_from"`
TxTo string `json:"tx_to"`
Status string `json:"status"`
Currencies map[string][]string `json:"currencies"`
RefundAddress string `json:"refund_address"`
RefundExtraId string `json:"refund_extra_id"`
}

type ExchangeInfo struct {
Id string `json:"id"`
Timestamp string `json:"timestamp"`
UpdatedAt string `json:"updated_at"`
CurrencyFrom string `json:"currency_from"`
CurrencyTo string `json:"currency_to"`
AmountFrom float64 `json:"amount_from,string"`
AmountTo float64 `json:"amount_to,string"`
ExpectedAmount float64 `json:"expected_amount,string"`
AddressFrom string `json:"address_from"`
AddressTo string `json:"address_to"`
ExtraIdFrom string `json:"extra_id_from"`
ExtraIdTo string `json:"extra_id_to"`
TxFrom string `json:"tx_from"`
TxTo string `json:"tx_to"`
Status string `json:"status"`
RefundAddress string `json:"refund_address"`
RefundExtraId string `json:"refund_extra_id"`
Currencies []CurrencyExchange `json:"currencies"`
}

type CurrencyExchange struct {
Symbol string `json:"symbol"`
HasExtraId bool `json:"has_extra_id"`
Name string `json:"name"`
WarningsFrom []interface{} `json:"warnings_from"`
WarningsTo []interface{} `json:"warnings_to"`
ValidationAddress string `json:"validation_address"`
ValidationExtra interface{} `json:"validation_extra"`
AddressExplorer interface{} `json:"address_explorer"`
TxExplorer string `json:"tx_explorer"`
Image string `json:"image"`
}
60 changes: 52 additions & 8 deletions instantswap/exchange/wizardswap/wizardswap.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
// the work on wizardswap is incompleted.
// I tried to create an order(exchange in wizardswap) but the server
// was always return "/tfalse" in the response body.
// I tried to contact support but they do not response yet
// wizardswap is leaved behide temporary

package wizardswap

import (
Expand Down Expand Up @@ -123,7 +117,39 @@ func (w *wizardswap) QueryLimits(fromCurr, toCurr string) (res instantswap.Query
}

func (w *wizardswap) CreateOrder(vars instantswap.CreateOrder) (res instantswap.CreateResultInfo, err error) {
return res, fmt.Errorf("not supported yet")
f := map[string]string{
"currency_from": strings.ToLower(vars.FromCurrency),
"currency_to": strings.ToLower(vars.ToCurrency),
"amount_from": fmt.Sprintf("%.8f", vars.InvoicedAmount),
"api_key": w.conf.ApiKey,
"address_to": vars.Destination,
"refund_address": vars.RefundAddress,
}
data, _ := json.Marshal(f)
r, err := w.client.Do(API_BASE, http.MethodPost, "exchange", string(data), false)
if err != nil {
return res, err
}
var order Exchange
err = parseResponseData(r, &order)
if err != nil {
return res, err
}
res = instantswap.CreateResultInfo{
ChargedFee: 0,
Destination: order.AddressTo,
ExchangeRate: order.AmountFrom / order.AmountTo,
FromCurrency: order.CurrencyFrom,
InvoicedAmount: order.AmountFrom,
OrderedAmount: order.AmountTo,
ToCurrency: order.CurrencyTo,
UUID: order.Id,
DepositAddress: order.AddressFrom,
Expires: 0,
ExtraID: "",
PayoutExtraID: "",
}
return res, nil
}

func (w *wizardswap) UpdateOrder(vars interface{}) (res instantswap.UpdateOrderResultInfo, err error) {
Expand All @@ -134,7 +160,25 @@ func (w *wizardswap) CancelOrder(orderID string) (res string, err error) {
}

func (w *wizardswap) OrderInfo(orderID string, extraIds ...string) (res instantswap.OrderInfoResult, err error) {
return res, fmt.Errorf("not supported yet")
r, err := w.client.Do(API_BASE, http.MethodGet, fmt.Sprintf("exchange/%s", orderID), "", false)
if err != nil {
return res, err
}
var order ExchangeInfo
err = parseResponseData(r, &order)
if err != nil {
return res, err
}
res = instantswap.OrderInfoResult{
Expires: 0,
LastUpdate: "",
ReceiveAmount: order.AmountTo,
TxID: order.TxTo,
Status: order.Status,
InternalStatus: parseStatus(order.Status),
Confirmations: "",
}
return res, nil
}

func (w *wizardswap) EstimateAmount(vars interface{}) (res instantswap.EstimateAmount, err error) {
Expand Down

0 comments on commit c949559

Please sign in to comment.