exchanges: Initial context propagation (#744)

* gct: phase one context awareness pass

* exchanges: context propagation pass

* common/requester: force context requirement

* gctcli/exchanges: linter fix

* rpcserver: fix test using dummy rpc server

* backtester: fix comments

* grpc: add correct cancel and timeout for commands

* rpcserver_test: add comment on dummy server

* common: deprecated SendHTTPGetRequest

* linter: fix

* linter: turn on no context check

* apichecker: fix context linter issue

* binance: use param context

* common: remove checks as this gets executed before main

* common: change mutex to RW as clients can be used by multiple go routines.

* common: remove init and JIT default client. Unexport global variables and add protection.

* common: Add comments

* bithumb: after dinner mints fix
This commit is contained in:
Ryan O'Hara-Reid
2021-09-11 13:52:07 +10:00
committed by GitHub
parent 72516f7268
commit d636049fb2
168 changed files with 8085 additions and 6996 deletions

View File

@@ -116,20 +116,20 @@ type LocalBitcoins struct {
// GetAccountInformation lets you retrieve the public user information on a
// LocalBitcoins user. The response contains the same information that is found
// on an account's public profile page.
func (l *LocalBitcoins) GetAccountInformation(username string, self bool) (AccountInfo, error) {
func (l *LocalBitcoins) GetAccountInformation(ctx context.Context, username string, self bool) (AccountInfo, error) {
type response struct {
Data AccountInfo `json:"data"`
}
resp := response{}
if self {
err := l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodGet, localbitcoinsAPIMyself, nil, &resp)
err := l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodGet, localbitcoinsAPIMyself, nil, &resp)
if err != nil {
return resp.Data, err
}
} else {
path := fmt.Sprintf("/%s/%s/", localbitcoinsAPIAccountInfo, username)
err := l.SendHTTPRequest(exchange.RestSpot, path, &resp, request.Unset)
err := l.SendHTTPRequest(ctx, exchange.RestSpot, path, &resp, request.Unset)
if err != nil {
return resp.Data, err
}
@@ -142,7 +142,7 @@ func (l *LocalBitcoins) GetAccountInformation(username string, self bool) (Accou
// adID omitted.
//
// adID - [optional] string if omitted returns all ads
func (l *LocalBitcoins) Getads(args ...string) (AdData, error) {
func (l *LocalBitcoins) Getads(ctx context.Context, args ...string) (AdData, error) {
var resp struct {
Data AdData `json:"data"`
Error struct {
@@ -153,14 +153,13 @@ func (l *LocalBitcoins) Getads(args ...string) (AdData, error) {
var err error
if len(args) == 0 {
err = l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodGet,
err = l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodGet,
localbitcoinsAPIAds,
nil,
&resp)
} else {
params := url.Values{"ads": {strings.Join(args, ",")}}
err = l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodGet,
err = l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodGet,
localbitcoinsAPIAdGet,
params,
&resp)
@@ -181,7 +180,7 @@ func (l *LocalBitcoins) Getads(args ...string) (AdData, error) {
// params - see localbitcoins_types.go AdEdit for reference
// adID - string for the ad you already created
// TODO
func (l *LocalBitcoins) EditAd(_ *AdEdit, adID string) error {
func (l *LocalBitcoins) EditAd(ctx context.Context, _ *AdEdit, adID string) error {
resp := struct {
Data AdData `json:"data"`
Error struct {
@@ -190,7 +189,9 @@ func (l *LocalBitcoins) EditAd(_ *AdEdit, adID string) error {
}
}{}
err := l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost,
err := l.SendAuthenticatedHTTPRequest(ctx,
exchange.RestSpot,
http.MethodPost,
localbitcoinsAPIAdEdit+adID+"/",
nil,
&resp)
@@ -209,8 +210,8 @@ func (l *LocalBitcoins) EditAd(_ *AdEdit, adID string) error {
//
// params - see localbitcoins_types.go AdCreate for reference
// TODO
func (l *LocalBitcoins) CreateAd(_ *AdCreate) error {
return l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPIAdCreate, nil, nil)
func (l *LocalBitcoins) CreateAd(ctx context.Context, _ *AdCreate) error {
return l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPIAdCreate, nil, nil)
}
// UpdatePriceEquation updates price equation of an advertisement. If there are
@@ -220,15 +221,15 @@ func (l *LocalBitcoins) CreateAd(_ *AdCreate) error {
// equation - string of equation
// adID - string of specific ad identification
// TODO
func (l *LocalBitcoins) UpdatePriceEquation(adID string) error {
return l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPIUpdateEquation+adID, nil, nil)
func (l *LocalBitcoins) UpdatePriceEquation(ctx context.Context, adID string) error {
return l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPIUpdateEquation+adID, nil, nil)
}
// DeleteAd deletes the advertisement by adID.
//
// adID - string of specific ad identification
// TODO
func (l *LocalBitcoins) DeleteAd(adID string) error {
func (l *LocalBitcoins) DeleteAd(ctx context.Context, adID string) error {
resp := struct {
Error struct {
Message string `json:"message"`
@@ -236,7 +237,9 @@ func (l *LocalBitcoins) DeleteAd(adID string) error {
} `json:"error"`
}{}
err := l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost,
err := l.SendAuthenticatedHTTPRequest(ctx,
exchange.RestSpot,
http.MethodPost,
localbitcoinsAPIDeleteAd+adID+"/",
nil,
&resp)
@@ -253,39 +256,39 @@ func (l *LocalBitcoins) DeleteAd(adID string) error {
// ReleaseFunds releases Bitcoin trades specified by ID {contact_id}. If the
// release was successful a message is returned on the data key.
func (l *LocalBitcoins) ReleaseFunds(contactID string) error {
return l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPIRelease+contactID, nil, nil)
func (l *LocalBitcoins) ReleaseFunds(ctx context.Context, contactID string) error {
return l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPIRelease+contactID, nil, nil)
}
// ReleaseFundsByPin releases Bitcoin trades specified by ID {contact_id}. if
// the current pincode is provided. If the release was successful a message is
// returned on the data key.
// TODO
func (l *LocalBitcoins) ReleaseFundsByPin(contactID string) error {
return l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPIReleaseByPin+contactID, nil, nil)
func (l *LocalBitcoins) ReleaseFundsByPin(ctx context.Context, contactID string) error {
return l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPIReleaseByPin+contactID, nil, nil)
}
// MarkAsPaid marks a trade as paid.
func (l *LocalBitcoins) MarkAsPaid(contactID string) error {
return l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPIMarkAsPaid+contactID, nil, nil)
func (l *LocalBitcoins) MarkAsPaid(ctx context.Context, contactID string) error {
return l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPIMarkAsPaid+contactID, nil, nil)
}
// GetMessages returns all chat messages from the trade. Messages are on the message_list key.
func (l *LocalBitcoins) GetMessages(contactID string) (Message, error) {
func (l *LocalBitcoins) GetMessages(ctx context.Context, contactID string) (Message, error) {
type response struct {
MessageList Message `json:"message_list"`
}
resp := response{}
return resp.MessageList,
l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPIMessages+contactID, nil, &resp)
l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPIMessages+contactID, nil, &resp)
}
// SendMessage posts a message and/or uploads an image to the trade. Encode
// images with multipart/form-data encoding.
// TODO
func (l *LocalBitcoins) SendMessage(contactID string) error {
return l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPISendMessage+contactID, nil, nil)
func (l *LocalBitcoins) SendMessage(ctx context.Context, contactID string) error {
return l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPISendMessage+contactID, nil, nil)
}
// Dispute starts a dispute on the specified trade ID if the requirements for
@@ -293,57 +296,57 @@ func (l *LocalBitcoins) SendMessage(contactID string) error {
//
// topic - [optional] String Short description of issue to LocalBitcoins customer support.
// TODO
func (l *LocalBitcoins) Dispute(_, contactID string) error {
return l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPIDispute+contactID, nil, nil)
func (l *LocalBitcoins) Dispute(ctx context.Context, _, contactID string) error {
return l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPIDispute+contactID, nil, nil)
}
// CancelTrade cancels the trade if the token owner is the Bitcoin buyer.
// Bitcoin sellers cannot cancel trades.
func (l *LocalBitcoins) CancelTrade(contactID string) error {
return l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPICancelTrade+contactID, nil, nil)
func (l *LocalBitcoins) CancelTrade(ctx context.Context, contactID string) error {
return l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPICancelTrade+contactID, nil, nil)
}
// FundTrade attempts to fund an unfunded local trade from the token owners
// wallet. Works only if the token owner is the Bitcoin seller in the trade.
func (l *LocalBitcoins) FundTrade(contactID string) error {
return l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPIFundTrade+contactID, nil, nil)
func (l *LocalBitcoins) FundTrade(ctx context.Context, contactID string) error {
return l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPIFundTrade+contactID, nil, nil)
}
// ConfirmRealName creates or updates real name confirmation.
func (l *LocalBitcoins) ConfirmRealName(contactID string) error {
return l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPIConfirmRealName+contactID, nil, nil)
func (l *LocalBitcoins) ConfirmRealName(ctx context.Context, contactID string) error {
return l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPIConfirmRealName+contactID, nil, nil)
}
// VerifyIdentity marks the identity of trade partner as verified. You must be
// the advertiser in this trade.
func (l *LocalBitcoins) VerifyIdentity(contactID string) error {
return l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPIVerifyIdentity+contactID, nil, nil)
func (l *LocalBitcoins) VerifyIdentity(ctx context.Context, contactID string) error {
return l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPIVerifyIdentity+contactID, nil, nil)
}
// InitiateTrade sttempts to start a Bitcoin trade from the specified
// advertisement ID.
// TODO
func (l *LocalBitcoins) InitiateTrade(adID string) error {
return l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPIInitiateTrade+adID, nil, nil)
func (l *LocalBitcoins) InitiateTrade(ctx context.Context, adID string) error {
return l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPIInitiateTrade+adID, nil, nil)
}
// GetTradeInfo returns information about a single trade that the token owner is
// part in.
func (l *LocalBitcoins) GetTradeInfo(contactID string) (dbi DashBoardInfo, err error) {
err = l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodGet, localbitcoinsAPITradeInfo+contactID+"/", nil, &dbi)
func (l *LocalBitcoins) GetTradeInfo(ctx context.Context, contactID string) (dbi DashBoardInfo, err error) {
err = l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodGet, localbitcoinsAPITradeInfo+contactID+"/", nil, &dbi)
return
}
// GetCountryCodes returns a list of valid and recognized countrycodes
func (l *LocalBitcoins) GetCountryCodes() error {
return l.SendHTTPRequest(exchange.RestSpot, localbitcoinsAPICountryCodes, nil, request.Unset)
func (l *LocalBitcoins) GetCountryCodes(ctx context.Context) error {
return l.SendHTTPRequest(ctx, exchange.RestSpot, localbitcoinsAPICountryCodes, nil, request.Unset)
}
// GetCurrencies returns a list of valid and recognized fiat currencies. Also
// contains human readable name for every currency and boolean that tells if
// currency is an altcoin.
func (l *LocalBitcoins) GetCurrencies() error {
return l.SendHTTPRequest(exchange.RestSpot, localbitcoinsAPICurrencies, nil, request.Unset)
func (l *LocalBitcoins) GetCurrencies(ctx context.Context) error {
return l.SendHTTPRequest(ctx, exchange.RestSpot, localbitcoinsAPICurrencies, nil, request.Unset)
}
// GetDashboardInfo returns a list of trades on the data key contact_list. This
@@ -353,7 +356,7 @@ func (l *LocalBitcoins) GetCurrencies() error {
// view contacts where the token owner is either buying or selling, respectively.
// E.g. /api/dashboard/buyer/. All contacts where the token owner is
// participating are returned.
func (l *LocalBitcoins) GetDashboardInfo() ([]DashBoardInfo, error) {
func (l *LocalBitcoins) GetDashboardInfo(ctx context.Context) ([]DashBoardInfo, error) {
var resp struct {
Data struct {
ContactList []DashBoardInfo `json:"contact_list"`
@@ -362,12 +365,12 @@ func (l *LocalBitcoins) GetDashboardInfo() ([]DashBoardInfo, error) {
}
return resp.Data.ContactList,
l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodGet, localbitcoinsAPIDashboard, nil, &resp)
l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodGet, localbitcoinsAPIDashboard, nil, &resp)
}
// GetDashboardReleasedTrades returns a list of all released trades where the
// token owner is either a buyer or seller.
func (l *LocalBitcoins) GetDashboardReleasedTrades() ([]DashBoardInfo, error) {
func (l *LocalBitcoins) GetDashboardReleasedTrades(ctx context.Context) ([]DashBoardInfo, error) {
var resp struct {
Data struct {
ContactList []DashBoardInfo `json:"contact_list"`
@@ -376,12 +379,12 @@ func (l *LocalBitcoins) GetDashboardReleasedTrades() ([]DashBoardInfo, error) {
}
return resp.Data.ContactList,
l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodGet, localbitcoinsAPIDashboardReleased, nil, &resp)
l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodGet, localbitcoinsAPIDashboardReleased, nil, &resp)
}
// GetDashboardCancelledTrades returns a list of all canceled trades where the
// token owner is either a buyer or seller.
func (l *LocalBitcoins) GetDashboardCancelledTrades() ([]DashBoardInfo, error) {
func (l *LocalBitcoins) GetDashboardCancelledTrades(ctx context.Context) ([]DashBoardInfo, error) {
var resp struct {
Data struct {
ContactList []DashBoardInfo `json:"contact_list"`
@@ -390,12 +393,12 @@ func (l *LocalBitcoins) GetDashboardCancelledTrades() ([]DashBoardInfo, error) {
}
return resp.Data.ContactList,
l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodGet, localbitcoinsAPIDashboardCancelled, nil, &resp)
l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodGet, localbitcoinsAPIDashboardCancelled, nil, &resp)
}
// GetDashboardClosedTrades returns a list of all closed trades where the token
// owner is either a buyer or seller.
func (l *LocalBitcoins) GetDashboardClosedTrades() ([]DashBoardInfo, error) {
func (l *LocalBitcoins) GetDashboardClosedTrades(ctx context.Context) ([]DashBoardInfo, error) {
var resp struct {
Data struct {
ContactList []DashBoardInfo `json:"contact_list"`
@@ -404,7 +407,7 @@ func (l *LocalBitcoins) GetDashboardClosedTrades() ([]DashBoardInfo, error) {
}
return resp.Data.ContactList,
l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodGet, localbitcoinsAPIDashboardClosed, nil, &resp)
l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodGet, localbitcoinsAPIDashboardClosed, nil, &resp)
}
// SetFeedback gives feedback to user. Possible feedback values are: trust,
@@ -420,29 +423,29 @@ func (l *LocalBitcoins) GetDashboardClosedTrades() ([]DashBoardInfo, error) {
// profile page.
// username - username of trade contact
// TODO
func (l *LocalBitcoins) SetFeedback() error {
return l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPIFeedback, nil, nil)
func (l *LocalBitcoins) SetFeedback(ctx context.Context) error {
return l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPIFeedback, nil, nil)
}
// Logout expires the current access token immediately. To get a new token
// afterwards, public apps will need to re-authenticate, confidential apps can
// turn in a refresh token.
func (l *LocalBitcoins) Logout() error {
return l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPILogout, nil, nil)
func (l *LocalBitcoins) Logout(ctx context.Context) error {
return l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPILogout, nil, nil)
}
// CreateNewInvoice creates a new invoice.
// TODO
func (l *LocalBitcoins) CreateNewInvoice() error {
return l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPICreateInvoice, nil, nil)
func (l *LocalBitcoins) CreateNewInvoice(ctx context.Context) error {
return l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPICreateInvoice, nil, nil)
}
// GetInvoice returns information about a specific invoice created by the token
// owner.
// TODO
func (l *LocalBitcoins) GetInvoice() (Invoice, error) {
func (l *LocalBitcoins) GetInvoice(ctx context.Context) (Invoice, error) {
resp := Invoice{}
return resp, l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPICreateInvoice, nil, &resp)
return resp, l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPICreateInvoice, nil, &resp)
}
// DeleteInvoice deletes a specific invoice. Deleting invoices is possible when
@@ -450,34 +453,34 @@ func (l *LocalBitcoins) GetInvoice() (Invoice, error) {
// as the merchant is deleting it. You can use the API request
// /api/merchant/invoice/{invoice_id}/ to check if deleting is possible.
// TODO
func (l *LocalBitcoins) DeleteInvoice() (Invoice, error) {
func (l *LocalBitcoins) DeleteInvoice(ctx context.Context) (Invoice, error) {
resp := Invoice{}
return resp, l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPICreateInvoice, nil, &resp)
return resp, l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPICreateInvoice, nil, &resp)
}
// GetNotifications returns recent notifications.
func (l *LocalBitcoins) GetNotifications() ([]NotificationInfo, error) {
func (l *LocalBitcoins) GetNotifications(ctx context.Context) ([]NotificationInfo, error) {
var resp []NotificationInfo
return resp, l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPIGetNotification, nil, &resp)
return resp, l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPIGetNotification, nil, &resp)
}
// MarkNotifications marks a specific notification as read.
// TODO
func (l *LocalBitcoins) MarkNotifications() error {
return l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPIMarkNotification, nil, nil)
func (l *LocalBitcoins) MarkNotifications(ctx context.Context) error {
return l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPIMarkNotification, nil, nil)
}
// GetPaymentMethods returns a list of valid payment methods. Also contains name
// and code for payment methods, and possible limitations in currencies and bank
// name choices.
func (l *LocalBitcoins) GetPaymentMethods() error {
return l.SendHTTPRequest(exchange.RestSpot, localbitcoinsAPIPaymentMethods, nil, request.Unset)
func (l *LocalBitcoins) GetPaymentMethods(ctx context.Context) error {
return l.SendHTTPRequest(ctx, exchange.RestSpot, localbitcoinsAPIPaymentMethods, nil, request.Unset)
}
// GetPaymentMethodsByCountry returns a list of valid payment methods filtered
// by countrycodes.
func (l *LocalBitcoins) GetPaymentMethodsByCountry(countryCode string) error {
return l.SendHTTPRequest(exchange.RestSpot, localbitcoinsAPIPaymentMethods+countryCode, nil, request.Unset)
func (l *LocalBitcoins) GetPaymentMethodsByCountry(ctx context.Context, countryCode string) error {
return l.SendHTTPRequest(ctx, exchange.RestSpot, localbitcoinsAPIPaymentMethods+countryCode, nil, request.Unset)
}
// CheckPincode checks the given PIN code against the token owners currently
@@ -486,7 +489,7 @@ func (l *LocalBitcoins) GetPaymentMethodsByCountry(countryCode string) error {
// Due to only requiring the read scope, the user is not guaranteed to have set
// a PIN code. If you protect your application using this request, please make
// the user has set a PIN code for his account.
func (l *LocalBitcoins) CheckPincode(pin int) (bool, error) {
func (l *LocalBitcoins) CheckPincode(ctx context.Context, pin int) (bool, error) {
type response struct {
Data struct {
PinOK bool `json:"pincode_ok"`
@@ -495,7 +498,7 @@ func (l *LocalBitcoins) CheckPincode(pin int) (bool, error) {
resp := response{}
values := url.Values{}
values.Set("pincode", strconv.Itoa(pin))
err := l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPIPinCode, values, &resp)
err := l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPIPinCode, values, &resp)
if err != nil {
return false, err
@@ -511,31 +514,31 @@ func (l *LocalBitcoins) CheckPincode(pin int) (bool, error) {
// GetPlaces Looks up places near lat, lon and provides full URLs to buy and
// sell listings for each.
// TODO
func (l *LocalBitcoins) GetPlaces() error {
return l.SendHTTPRequest(exchange.RestSpot, localbitcoinsAPIPlaces, nil, request.Unset)
func (l *LocalBitcoins) GetPlaces(ctx context.Context) error {
return l.SendHTTPRequest(ctx, exchange.RestSpot, localbitcoinsAPIPlaces, nil, request.Unset)
}
// VerifyUsername returns list of real name verifiers for the user. Returns a
// list only when you have a trade with the user where you are the seller.
func (l *LocalBitcoins) VerifyUsername() error {
return l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPIVerifyUsername, nil, nil)
func (l *LocalBitcoins) VerifyUsername(ctx context.Context) error {
return l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPIVerifyUsername, nil, nil)
}
// GetRecentMessages returns maximum of 25 newest trade messages. Does not
// return messages older than one month. Messages are ordered by sending time,
// and the newest one is first.
// TODO
func (l *LocalBitcoins) GetRecentMessages() error {
return l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPIVerifyUsername, nil, nil)
func (l *LocalBitcoins) GetRecentMessages(ctx context.Context) error {
return l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPIVerifyUsername, nil, nil)
}
// GetWalletInfo gets information about the token owner's wallet balance.
func (l *LocalBitcoins) GetWalletInfo() (WalletInfo, error) {
func (l *LocalBitcoins) GetWalletInfo(ctx context.Context) (WalletInfo, error) {
type response struct {
Data WalletInfo `json:"data"`
}
resp := response{}
err := l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodGet, localbitcoinsAPIWallet, nil, &resp)
err := l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodGet, localbitcoinsAPIWallet, nil, &resp)
if err != nil {
return WalletInfo{}, err
@@ -551,12 +554,12 @@ func (l *LocalBitcoins) GetWalletInfo() (WalletInfo, error) {
// GetWalletBalance Same as GetWalletInfo(), but only returns the message,
// receiving_address and total fields.
// Use this instead if you don't care about transactions at the moment.
func (l *LocalBitcoins) GetWalletBalance() (WalletBalanceInfo, error) {
func (l *LocalBitcoins) GetWalletBalance(ctx context.Context) (WalletBalanceInfo, error) {
type response struct {
Data WalletBalanceInfo `json:"data"`
}
resp := response{}
err := l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodGet, localbitcoinsAPIWalletBalance, nil, &resp)
err := l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodGet, localbitcoinsAPIWalletBalance, nil, &resp)
if err != nil {
return WalletBalanceInfo{}, err
@@ -573,7 +576,7 @@ func (l *LocalBitcoins) GetWalletBalance() (WalletBalanceInfo, error) {
// On success, the response returns a message indicating success. It is highly
// recommended to minimize the lifetime of access tokens with the money
// permission. Use Logout() to make the current token expire instantly.
func (l *LocalBitcoins) WalletSend(address string, amount float64, pin int64) error {
func (l *LocalBitcoins) WalletSend(ctx context.Context, address string, amount float64, pin int64) error {
values := url.Values{}
values.Set("address", address)
values.Set("amount", strconv.FormatFloat(amount, 'f', -1, 64))
@@ -595,7 +598,7 @@ func (l *LocalBitcoins) WalletSend(address string, amount float64, pin int64) er
} `json:"data"`
}{}
err := l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, path, values, &resp)
err := l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, path, values, &resp)
if err != nil {
return err
}
@@ -617,7 +620,7 @@ func (l *LocalBitcoins) WalletSend(address string, amount float64, pin int64) er
// GetWalletAddress returns an unused receiving address from the token owner's
// wallet. The address is returned in the address key of the response. Note that
// this API may keep returning the same (unused) address if requested repeatedly.
func (l *LocalBitcoins) GetWalletAddress() (string, error) {
func (l *LocalBitcoins) GetWalletAddress(ctx context.Context) (string, error) {
type response struct {
Data struct {
Message string `json:"message"`
@@ -625,7 +628,7 @@ func (l *LocalBitcoins) GetWalletAddress() (string, error) {
}
}
resp := response{}
err := l.SendAuthenticatedHTTPRequest(exchange.RestSpot, http.MethodPost, localbitcoinsAPIWalletAddress, nil, &resp)
err := l.SendAuthenticatedHTTPRequest(ctx, exchange.RestSpot, http.MethodPost, localbitcoinsAPIWalletAddress, nil, &resp)
if err != nil {
return "", err
}
@@ -639,25 +642,25 @@ func (l *LocalBitcoins) GetWalletAddress() (string, error) {
// GetBitcoinsWithCashAd returns buy or sell as cash local advertisements.
// TODO
func (l *LocalBitcoins) GetBitcoinsWithCashAd() error {
return l.SendHTTPRequest(exchange.RestSpot, localbitcoinsAPICashBuy, nil, request.Unset)
func (l *LocalBitcoins) GetBitcoinsWithCashAd(ctx context.Context) error {
return l.SendHTTPRequest(ctx, exchange.RestSpot, localbitcoinsAPICashBuy, nil, request.Unset)
}
// GetBitcoinsOnlineAd this API returns buy or sell Bitcoin online ads.
// TODO
func (l *LocalBitcoins) GetBitcoinsOnlineAd() error {
return l.SendHTTPRequest(exchange.RestSpot, localbitcoinsAPIOnlineBuy, nil, request.Unset)
func (l *LocalBitcoins) GetBitcoinsOnlineAd(ctx context.Context) error {
return l.SendHTTPRequest(ctx, exchange.RestSpot, localbitcoinsAPIOnlineBuy, nil, request.Unset)
}
// GetTicker returns list of all completed trades.
func (l *LocalBitcoins) GetTicker() (map[string]Ticker, error) {
func (l *LocalBitcoins) GetTicker(ctx context.Context) (map[string]Ticker, error) {
result := make(map[string]Ticker)
return result, l.SendHTTPRequest(exchange.RestSpot, localbitcoinsAPITicker, &result, tickerLimiter)
return result, l.SendHTTPRequest(ctx, exchange.RestSpot, localbitcoinsAPITicker, &result, tickerLimiter)
}
// GetTradableCurrencies returns a list of tradable fiat currencies
func (l *LocalBitcoins) GetTradableCurrencies() ([]string, error) {
resp, err := l.GetTicker()
func (l *LocalBitcoins) GetTradableCurrencies(ctx context.Context) ([]string, error) {
resp, err := l.GetTicker(ctx)
if err != nil {
return nil, err
}
@@ -672,18 +675,18 @@ func (l *LocalBitcoins) GetTradableCurrencies() ([]string, error) {
// GetTrades returns all closed trades in online buy and online sell categories,
// updated every 15 minutes.
func (l *LocalBitcoins) GetTrades(currency string, values url.Values) ([]Trade, error) {
func (l *LocalBitcoins) GetTrades(ctx context.Context, currency string, values url.Values) ([]Trade, error) {
endpoint := localbitcoinsAPIBitcoincharts + currency + localbitcoinsAPITrades
path := common.EncodeURLValues(endpoint, values)
var result []Trade
return result, l.SendHTTPRequest(exchange.RestSpot, path, &result, request.Unset)
return result, l.SendHTTPRequest(ctx, exchange.RestSpot, path, &result, request.Unset)
}
// GetOrderbook returns buy and sell bitcoin online advertisements. Amount is
// the maximum amount available for the trade request. Price is the hourly
// updated price. The price is based on the price equation and commission %
// entered by the ad author.
func (l *LocalBitcoins) GetOrderbook(currency string) (Orderbook, error) {
func (l *LocalBitcoins) GetOrderbook(ctx context.Context, currency string) (Orderbook, error) {
type response struct {
Bids [][2]string `json:"bids"`
Asks [][2]string `json:"asks"`
@@ -692,7 +695,7 @@ func (l *LocalBitcoins) GetOrderbook(currency string) (Orderbook, error) {
path := localbitcoinsAPIBitcoincharts + currency + localbitcoinsAPIOrderbook
resp := response{}
var ob Orderbook
if err := l.SendHTTPRequest(exchange.RestSpot, path, &resp, orderBookLimiter); err != nil {
if err := l.SendHTTPRequest(ctx, exchange.RestSpot, path, &resp, orderBookLimiter); err != nil {
return ob, err
}
@@ -724,7 +727,7 @@ func (l *LocalBitcoins) GetOrderbook(currency string) (Orderbook, error) {
}
// SendHTTPRequest sends an unauthenticated HTTP request
func (l *LocalBitcoins) SendHTTPRequest(endpoint exchange.URL, path string, result interface{}, ep request.EndpointLimit) error {
func (l *LocalBitcoins) SendHTTPRequest(ctx context.Context, endpoint exchange.URL, path string, result interface{}, ep request.EndpointLimit) error {
ePoint, err := l.API.Endpoints.GetURL(endpoint)
if err != nil {
return err
@@ -739,14 +742,14 @@ func (l *LocalBitcoins) SendHTTPRequest(endpoint exchange.URL, path string, resu
HTTPRecording: l.HTTPRecording,
}
return l.SendPayload(context.Background(), ep, func() (*request.Item, error) {
return l.SendPayload(ctx, ep, func() (*request.Item, error) {
return item, nil
})
}
// SendAuthenticatedHTTPRequest sends an authenticated HTTP request to
// localbitcoins
func (l *LocalBitcoins) SendAuthenticatedHTTPRequest(ep exchange.URL, method, path string, params url.Values, result interface{}) (err error) {
func (l *LocalBitcoins) SendAuthenticatedHTTPRequest(ctx context.Context, ep exchange.URL, method, path string, params url.Values, result interface{}) (err error) {
if !l.AllowAuthenticatedRequest() {
return fmt.Errorf("%s %w", l.Name, exchange.ErrAuthenticatedRequestWithoutCredentialsSet)
}
@@ -755,7 +758,7 @@ func (l *LocalBitcoins) SendAuthenticatedHTTPRequest(ep exchange.URL, method, pa
return err
}
return l.SendPayload(context.Background(), request.Unset, func() (*request.Item, error) {
return l.SendPayload(ctx, request.Unset, func() (*request.Item, error) {
n := l.Requester.GetNonce(true).String()
fullPath := "/api/" + path

View File

@@ -1,6 +1,7 @@
package localbitcoins
import (
"context"
"testing"
"time"
@@ -26,7 +27,7 @@ var l LocalBitcoins
func TestGetTicker(t *testing.T) {
t.Parallel()
_, err := l.GetTicker()
_, err := l.GetTicker(context.Background())
if err != nil {
t.Errorf("GetTicker() returned: %s", err)
}
@@ -35,7 +36,7 @@ func TestGetTicker(t *testing.T) {
func TestGetTradableCurrencies(t *testing.T) {
t.Parallel()
_, err := l.GetTradableCurrencies()
_, err := l.GetTradableCurrencies(context.Background())
if err != nil {
t.Errorf("GetTradableCurrencies() returned: %s", err)
}
@@ -43,7 +44,7 @@ func TestGetTradableCurrencies(t *testing.T) {
func TestGetAccountInfo(t *testing.T) {
t.Parallel()
_, err := l.GetAccountInformation("", true)
_, err := l.GetAccountInformation(context.Background(), "", true)
switch {
case areTestAPIKeysSet() && err != nil && !mockTests:
t.Errorf("Could not get AccountInformation: %s", err)
@@ -56,7 +57,7 @@ func TestGetAccountInfo(t *testing.T) {
func TestGetads(t *testing.T) {
t.Parallel()
_, err := l.Getads("")
_, err := l.Getads(context.Background(), "")
switch {
case areTestAPIKeysSet() && err != nil && !mockTests:
t.Errorf("Could not get ads: %s", err)
@@ -71,7 +72,7 @@ func TestEditAd(t *testing.T) {
t.Parallel()
var edit AdEdit
err := l.EditAd(&edit, "1337")
err := l.EditAd(context.Background(), &edit, "1337")
switch {
case areTestAPIKeysSet() && err != nil && !mockTests:
t.Errorf("Could not edit order: %s", err)
@@ -97,7 +98,7 @@ func setFeeBuilder() *exchange.FeeBuilder {
func TestGetTrades(t *testing.T) {
t.Parallel()
_, err := l.GetTrades("LTC", nil)
_, err := l.GetTrades(context.Background(), "LTC", nil)
if err != nil {
t.Error(err)
}
@@ -105,7 +106,7 @@ func TestGetTrades(t *testing.T) {
func TestGetOrderbook(t *testing.T) {
t.Parallel()
ob, err := l.GetOrderbook("AUD")
ob, err := l.GetOrderbook(context.Background(), "AUD")
if err != nil {
t.Fatal(err)
}
@@ -121,7 +122,7 @@ func TestGetOrderbook(t *testing.T) {
func TestGetFeeByTypeOfflineTradeFee(t *testing.T) {
t.Parallel()
var feeBuilder = setFeeBuilder()
_, err := l.GetFeeByType(feeBuilder)
_, err := l.GetFeeByType(context.Background(), feeBuilder)
if err != nil {
t.Fatal(err)
}
@@ -222,7 +223,7 @@ func TestGetActiveOrders(t *testing.T) {
AssetType: asset.Spot,
}
_, err := l.GetActiveOrders(&getOrdersRequest)
_, err := l.GetActiveOrders(context.Background(), &getOrdersRequest)
switch {
case areTestAPIKeysSet() && err != nil && !mockTests:
t.Errorf("Could not get active orders: %s", err)
@@ -241,7 +242,7 @@ func TestGetOrderHistory(t *testing.T) {
AssetType: asset.Spot,
}
_, err := l.GetOrderHistory(&getOrdersRequest)
_, err := l.GetOrderHistory(context.Background(), &getOrdersRequest)
switch {
case areTestAPIKeysSet() && err != nil && !mockTests:
t.Errorf("Could not get order history: %s", err)
@@ -277,7 +278,7 @@ func TestSubmitOrder(t *testing.T) {
ClientID: "meowOrder",
AssetType: asset.Spot,
}
response, err := l.SubmitOrder(orderSubmission)
response, err := l.SubmitOrder(context.Background(), orderSubmission)
switch {
case areTestAPIKeysSet() && (err != nil || !response.IsOrderPlaced) && !mockTests:
t.Errorf("Order failed to be placed: %v", err)
@@ -302,7 +303,7 @@ func TestCancelExchangeOrder(t *testing.T) {
AssetType: asset.Spot,
}
err := l.CancelOrder(orderCancellation)
err := l.CancelOrder(context.Background(), orderCancellation)
switch {
case !areTestAPIKeysSet() && err == nil && !mockTests:
t.Error("Expecting an error when no keys are set")
@@ -327,7 +328,7 @@ func TestCancelAllExchangeOrders(t *testing.T) {
AssetType: asset.Spot,
}
resp, err := l.CancelAllOrders(orderCancellation)
resp, err := l.CancelAllOrders(context.Background(), orderCancellation)
switch {
case !areTestAPIKeysSet() && err == nil && !mockTests:
t.Error("Expecting an error when no keys are set")
@@ -345,7 +346,8 @@ func TestCancelAllExchangeOrders(t *testing.T) {
func TestModifyOrder(t *testing.T) {
t.Parallel()
_, err := l.ModifyOrder(&order.Modify{AssetType: asset.Spot})
_, err := l.ModifyOrder(context.Background(),
&order.Modify{AssetType: asset.Spot})
if err != common.ErrFunctionNotSupported {
t.Error("ModifyOrder() error", err)
}
@@ -367,7 +369,8 @@ func TestWithdraw(t *testing.T) {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}
_, err := l.WithdrawCryptocurrencyFunds(&withdrawCryptoRequest)
_, err := l.WithdrawCryptocurrencyFunds(context.Background(),
&withdrawCryptoRequest)
switch {
case !areTestAPIKeysSet() && err == nil && !mockTests:
t.Error("Expecting an error when no keys are set")
@@ -382,7 +385,7 @@ func TestWithdrawFiat(t *testing.T) {
t.Parallel()
var withdrawFiatRequest = withdraw.Request{}
_, err := l.WithdrawFiatFunds(&withdrawFiatRequest)
_, err := l.WithdrawFiatFunds(context.Background(), &withdrawFiatRequest)
if err != common.ErrFunctionNotSupported {
t.Errorf("Expected '%v', received: '%v'",
common.ErrFunctionNotSupported,
@@ -394,7 +397,7 @@ func TestWithdrawInternationalBank(t *testing.T) {
t.Parallel()
var withdrawFiatRequest = withdraw.Request{}
_, err := l.WithdrawFiatFundsToInternationalBank(&withdrawFiatRequest)
_, err := l.WithdrawFiatFundsToInternationalBank(context.Background(), &withdrawFiatRequest)
if err != common.ErrFunctionNotSupported {
t.Errorf("Expected '%v', received: '%v'",
common.ErrFunctionNotSupported,
@@ -405,7 +408,7 @@ func TestWithdrawInternationalBank(t *testing.T) {
func TestGetDepositAddress(t *testing.T) {
t.Parallel()
_, err := l.GetDepositAddress(currency.BTC, "")
_, err := l.GetDepositAddress(context.Background(), currency.BTC, "")
switch {
case areTestAPIKeysSet() && err != nil && !mockTests:
t.Error("GetDepositAddress() error", err)
@@ -422,7 +425,7 @@ func TestGetRecentTrades(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = l.GetRecentTrades(currencyPair, asset.Spot)
_, err = l.GetRecentTrades(context.Background(), currencyPair, asset.Spot)
if err != nil {
t.Error(err)
}
@@ -434,7 +437,8 @@ func TestGetHistoricTrades(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = l.GetHistoricTrades(currencyPair, asset.Spot, time.Now().Add(-time.Minute*15), time.Now())
_, err = l.GetHistoricTrades(context.Background(),
currencyPair, asset.Spot, time.Now().Add(-time.Minute*15), time.Now())
if err != nil && err != common.ErrFunctionNotSupported {
t.Error(err)
}
@@ -446,7 +450,7 @@ func TestUpdateTicker(t *testing.T) {
if err != nil {
t.Fatal(err)
}
_, err = l.UpdateTicker(cp, asset.Spot)
_, err = l.UpdateTicker(context.Background(), cp, asset.Spot)
if err != nil {
t.Error(err)
}
@@ -454,7 +458,7 @@ func TestUpdateTicker(t *testing.T) {
func TestUpdateTickers(t *testing.T) {
t.Parallel()
err := l.UpdateTickers(asset.Spot)
err := l.UpdateTickers(context.Background(), asset.Spot)
if err != nil {
t.Error(err)
}

View File

@@ -1,6 +1,7 @@
package localbitcoins
import (
"context"
"errors"
"fmt"
"math"
@@ -41,7 +42,7 @@ func (l *LocalBitcoins) GetDefaultConfig() (*config.ExchangeConfig, error) {
}
if l.Features.Supports.RESTCapabilities.AutoPairUpdates {
err = l.UpdateTradablePairs(true)
err = l.UpdateTradablePairs(context.TODO(), true)
if err != nil {
return nil, err
}
@@ -130,15 +131,15 @@ func (l *LocalBitcoins) Run() {
return
}
err := l.UpdateTradablePairs(false)
err := l.UpdateTradablePairs(context.TODO(), false)
if err != nil {
log.Errorf(log.ExchangeSys, "%s failed to update tradable pairs. Err: %s", l.Name, err)
}
}
// FetchTradablePairs returns a list of the exchanges tradable pairs
func (l *LocalBitcoins) FetchTradablePairs(asset asset.Item) ([]string, error) {
currencies, err := l.GetTradableCurrencies()
func (l *LocalBitcoins) FetchTradablePairs(ctx context.Context, asset asset.Item) ([]string, error) {
currencies, err := l.GetTradableCurrencies(ctx)
if err != nil {
return nil, err
}
@@ -153,8 +154,8 @@ func (l *LocalBitcoins) FetchTradablePairs(asset asset.Item) ([]string, error) {
// UpdateTradablePairs updates the exchanges available pairs and stores
// them in the exchanges config
func (l *LocalBitcoins) UpdateTradablePairs(forceUpdate bool) error {
pairs, err := l.FetchTradablePairs(asset.Spot)
func (l *LocalBitcoins) UpdateTradablePairs(ctx context.Context, forceUpdate bool) error {
pairs, err := l.FetchTradablePairs(ctx, asset.Spot)
if err != nil {
return err
}
@@ -166,8 +167,8 @@ func (l *LocalBitcoins) UpdateTradablePairs(forceUpdate bool) error {
}
// UpdateTickers updates the ticker for all currency pairs of a given asset type
func (l *LocalBitcoins) UpdateTickers(a asset.Item) error {
tick, err := l.GetTicker()
func (l *LocalBitcoins) UpdateTickers(ctx context.Context, a asset.Item) error {
tick, err := l.GetTicker(ctx)
if err != nil {
return err
}
@@ -197,8 +198,8 @@ func (l *LocalBitcoins) UpdateTickers(a asset.Item) error {
}
// UpdateTicker updates and returns the ticker for a currency pair
func (l *LocalBitcoins) UpdateTicker(p currency.Pair, a asset.Item) (*ticker.Price, error) {
err := l.UpdateTickers(a)
func (l *LocalBitcoins) UpdateTicker(ctx context.Context, p currency.Pair, a asset.Item) (*ticker.Price, error) {
err := l.UpdateTickers(ctx, a)
if err != nil {
return nil, err
}
@@ -206,25 +207,25 @@ func (l *LocalBitcoins) UpdateTicker(p currency.Pair, a asset.Item) (*ticker.Pri
}
// FetchTicker returns the ticker for a currency pair
func (l *LocalBitcoins) FetchTicker(p currency.Pair, assetType asset.Item) (*ticker.Price, error) {
func (l *LocalBitcoins) FetchTicker(ctx context.Context, p currency.Pair, assetType asset.Item) (*ticker.Price, error) {
tickerNew, err := ticker.GetTicker(l.Name, p, assetType)
if err != nil {
return l.UpdateTicker(p, assetType)
return l.UpdateTicker(ctx, p, assetType)
}
return tickerNew, nil
}
// FetchOrderbook returns orderbook base on the currency pair
func (l *LocalBitcoins) FetchOrderbook(p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
func (l *LocalBitcoins) FetchOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
ob, err := orderbook.Get(l.Name, p, assetType)
if err != nil {
return l.UpdateOrderbook(p, assetType)
return l.UpdateOrderbook(ctx, p, assetType)
}
return ob, nil
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (l *LocalBitcoins) UpdateOrderbook(p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
func (l *LocalBitcoins) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Base, error) {
book := &orderbook.Base{
Exchange: l.Name,
Pair: p,
@@ -232,7 +233,7 @@ func (l *LocalBitcoins) UpdateOrderbook(p currency.Pair, assetType asset.Item) (
VerifyOrderbook: l.CanVerifyOrderbook,
}
orderbookNew, err := l.GetOrderbook(p.Quote.String())
orderbookNew, err := l.GetOrderbook(ctx, p.Quote.String())
if err != nil {
return book, err
}
@@ -262,10 +263,10 @@ func (l *LocalBitcoins) UpdateOrderbook(p currency.Pair, assetType asset.Item) (
// UpdateAccountInfo retrieves balances for all enabled currencies for the
// LocalBitcoins exchange
func (l *LocalBitcoins) UpdateAccountInfo(assetType asset.Item) (account.Holdings, error) {
func (l *LocalBitcoins) UpdateAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) {
var response account.Holdings
response.Exchange = l.Name
accountBalance, err := l.GetWalletBalance()
accountBalance, err := l.GetWalletBalance(ctx)
if err != nil {
return response, err
}
@@ -286,10 +287,10 @@ func (l *LocalBitcoins) UpdateAccountInfo(assetType asset.Item) (account.Holding
}
// FetchAccountInfo retrieves balances for all enabled currencies
func (l *LocalBitcoins) FetchAccountInfo(assetType asset.Item) (account.Holdings, error) {
func (l *LocalBitcoins) FetchAccountInfo(ctx context.Context, assetType asset.Item) (account.Holdings, error) {
acc, err := account.GetHoldings(l.Name, assetType)
if err != nil {
return l.UpdateAccountInfo(assetType)
return l.UpdateAccountInfo(ctx, assetType)
}
return acc, nil
@@ -297,24 +298,24 @@ func (l *LocalBitcoins) FetchAccountInfo(assetType asset.Item) (account.Holdings
// GetFundingHistory returns funding history, deposits and
// withdrawals
func (l *LocalBitcoins) GetFundingHistory() ([]exchange.FundHistory, error) {
func (l *LocalBitcoins) GetFundingHistory(ctx context.Context) ([]exchange.FundHistory, error) {
return nil, common.ErrFunctionNotSupported
}
// GetWithdrawalsHistory returns previous withdrawals data
func (l *LocalBitcoins) GetWithdrawalsHistory(c currency.Code) (resp []exchange.WithdrawalHistory, err error) {
func (l *LocalBitcoins) GetWithdrawalsHistory(ctx context.Context, c currency.Code) (resp []exchange.WithdrawalHistory, err error) {
return nil, common.ErrNotYetImplemented
}
// GetRecentTrades returns the most recent trades for a currency and asset
func (l *LocalBitcoins) GetRecentTrades(p currency.Pair, assetType asset.Item) ([]trade.Data, error) {
func (l *LocalBitcoins) GetRecentTrades(ctx context.Context, p currency.Pair, assetType asset.Item) ([]trade.Data, error) {
var err error
p, err = l.FormatExchangeCurrency(p, assetType)
if err != nil {
return nil, err
}
var tradeData []Trade
tradeData, err = l.GetTrades(p.Quote.String(), nil)
tradeData, err = l.GetTrades(ctx, p.Quote.String(), nil)
if err != nil {
return nil, err
}
@@ -341,12 +342,12 @@ func (l *LocalBitcoins) GetRecentTrades(p currency.Pair, assetType asset.Item) (
}
// GetHistoricTrades returns historic trade data within the timeframe provided
func (l *LocalBitcoins) GetHistoricTrades(_ currency.Pair, _ asset.Item, _, _ time.Time) ([]trade.Data, error) {
func (l *LocalBitcoins) GetHistoricTrades(_ context.Context, _ currency.Pair, _ asset.Item, _, _ time.Time) ([]trade.Data, error) {
return nil, common.ErrFunctionNotSupported
}
// SubmitOrder submits a new order
func (l *LocalBitcoins) SubmitOrder(s *order.Submit) (order.SubmitResponse, error) {
func (l *LocalBitcoins) SubmitOrder(ctx context.Context, s *order.Submit) (order.SubmitResponse, error) {
var submitOrderResponse order.SubmitResponse
if err := s.Validate(); err != nil {
return submitOrderResponse, err
@@ -380,7 +381,7 @@ func (l *LocalBitcoins) SubmitOrder(s *order.Submit) (order.SubmitResponse, erro
}
// Does not return any orderID, so create the add, then get the order
err = l.CreateAd(&params)
err = l.CreateAd(ctx, &params)
if err != nil {
return submitOrderResponse, err
}
@@ -390,7 +391,7 @@ func (l *LocalBitcoins) SubmitOrder(s *order.Submit) (order.SubmitResponse, erro
// Now to figure out what ad we just submitted
// The only details we have are the params above
var adID string
ads, err := l.Getads()
ads, err := l.Getads(ctx)
for i := range ads.AdList {
if ads.AdList[i].Data.PriceEquation == params.PriceEquation &&
ads.AdList[i].Data.Lat == float64(params.Latitude) &&
@@ -422,36 +423,36 @@ func (l *LocalBitcoins) SubmitOrder(s *order.Submit) (order.SubmitResponse, erro
// ModifyOrder will allow of changing orderbook placement and limit to
// market conversion
func (l *LocalBitcoins) ModifyOrder(action *order.Modify) (order.Modify, error) {
func (l *LocalBitcoins) ModifyOrder(ctx context.Context, action *order.Modify) (order.Modify, error) {
return order.Modify{}, common.ErrFunctionNotSupported
}
// CancelOrder cancels an order by its corresponding ID number
func (l *LocalBitcoins) CancelOrder(o *order.Cancel) error {
func (l *LocalBitcoins) CancelOrder(ctx context.Context, o *order.Cancel) error {
if err := o.Validate(o.StandardCancel()); err != nil {
return err
}
return l.DeleteAd(o.ID)
return l.DeleteAd(ctx, o.ID)
}
// CancelBatchOrders cancels an orders by their corresponding ID numbers
func (l *LocalBitcoins) CancelBatchOrders(o []order.Cancel) (order.CancelBatchResponse, error) {
func (l *LocalBitcoins) CancelBatchOrders(ctx context.Context, o []order.Cancel) (order.CancelBatchResponse, error) {
return order.CancelBatchResponse{}, common.ErrNotYetImplemented
}
// CancelAllOrders cancels all orders associated with a currency pair
func (l *LocalBitcoins) CancelAllOrders(_ *order.Cancel) (order.CancelAllResponse, error) {
func (l *LocalBitcoins) CancelAllOrders(ctx context.Context, _ *order.Cancel) (order.CancelAllResponse, error) {
cancelAllOrdersResponse := order.CancelAllResponse{
Status: make(map[string]string),
}
ads, err := l.Getads()
ads, err := l.Getads(ctx)
if err != nil {
return cancelAllOrdersResponse, err
}
for i := range ads.AdList {
adIDString := strconv.FormatInt(ads.AdList[i].Data.AdID, 10)
err = l.DeleteAd(adIDString)
err = l.DeleteAd(ctx, adIDString)
if err != nil {
cancelAllOrdersResponse.Status[adIDString] = err.Error()
}
@@ -461,28 +462,29 @@ func (l *LocalBitcoins) CancelAllOrders(_ *order.Cancel) (order.CancelAllRespons
}
// GetOrderInfo returns order information based on order ID
func (l *LocalBitcoins) GetOrderInfo(orderID string, pair currency.Pair, assetType asset.Item) (order.Detail, error) {
func (l *LocalBitcoins) GetOrderInfo(ctx context.Context, orderID string, pair currency.Pair, assetType asset.Item) (order.Detail, error) {
var orderDetail order.Detail
return orderDetail, common.ErrNotYetImplemented
}
// GetDepositAddress returns a deposit address for a specified currency
func (l *LocalBitcoins) GetDepositAddress(cryptocurrency currency.Code, _ string) (string, error) {
func (l *LocalBitcoins) GetDepositAddress(ctx context.Context, cryptocurrency currency.Code, _ string) (string, error) {
if !strings.EqualFold(currency.BTC.String(), cryptocurrency.String()) {
return "", fmt.Errorf("%s does not have support for currency %s, it only supports bitcoin",
l.Name, cryptocurrency)
}
return l.GetWalletAddress()
return l.GetWalletAddress(ctx)
}
// WithdrawCryptocurrencyFunds returns a withdrawal ID when a withdrawal is
// submitted
func (l *LocalBitcoins) WithdrawCryptocurrencyFunds(withdrawRequest *withdraw.Request) (*withdraw.ExchangeResponse, error) {
func (l *LocalBitcoins) WithdrawCryptocurrencyFunds(ctx context.Context, withdrawRequest *withdraw.Request) (*withdraw.ExchangeResponse, error) {
if err := withdrawRequest.Validate(); err != nil {
return nil, err
}
err := l.WalletSend(withdrawRequest.Crypto.Address,
err := l.WalletSend(ctx,
withdrawRequest.Crypto.Address,
withdrawRequest.Amount,
withdrawRequest.PIN)
if err != nil {
@@ -493,18 +495,18 @@ func (l *LocalBitcoins) WithdrawCryptocurrencyFunds(withdrawRequest *withdraw.Re
// WithdrawFiatFunds returns a withdrawal ID when a
// withdrawal is submitted
func (l *LocalBitcoins) WithdrawFiatFunds(_ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
func (l *LocalBitcoins) WithdrawFiatFunds(_ context.Context, _ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
return nil, common.ErrFunctionNotSupported
}
// WithdrawFiatFundsToInternationalBank returns a withdrawal ID when a
// withdrawal is submitted
func (l *LocalBitcoins) WithdrawFiatFundsToInternationalBank(_ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
func (l *LocalBitcoins) WithdrawFiatFundsToInternationalBank(_ context.Context, _ *withdraw.Request) (*withdraw.ExchangeResponse, error) {
return nil, common.ErrFunctionNotSupported
}
// GetFeeByType returns an estimate of fee based on type of transaction
func (l *LocalBitcoins) GetFeeByType(feeBuilder *exchange.FeeBuilder) (float64, error) {
func (l *LocalBitcoins) GetFeeByType(ctx context.Context, feeBuilder *exchange.FeeBuilder) (float64, error) {
if (!l.AllowAuthenticatedRequest() || l.SkipAuthCheck) && // Todo check connection status
feeBuilder.FeeType == exchange.CryptocurrencyTradeFee {
feeBuilder.FeeType = exchange.OfflineTradeFee
@@ -513,12 +515,12 @@ func (l *LocalBitcoins) GetFeeByType(feeBuilder *exchange.FeeBuilder) (float64,
}
// GetActiveOrders retrieves any orders that are active/open
func (l *LocalBitcoins) GetActiveOrders(getOrdersRequest *order.GetOrdersRequest) ([]order.Detail, error) {
func (l *LocalBitcoins) GetActiveOrders(ctx context.Context, getOrdersRequest *order.GetOrdersRequest) ([]order.Detail, error) {
if err := getOrdersRequest.Validate(); err != nil {
return nil, err
}
resp, err := l.GetDashboardInfo()
resp, err := l.GetDashboardInfo(ctx)
if err != nil {
return nil, err
}
@@ -569,25 +571,25 @@ func (l *LocalBitcoins) GetActiveOrders(getOrdersRequest *order.GetOrdersRequest
// GetOrderHistory retrieves account order information
// Can Limit response to specific order status
func (l *LocalBitcoins) GetOrderHistory(getOrdersRequest *order.GetOrdersRequest) ([]order.Detail, error) {
func (l *LocalBitcoins) GetOrderHistory(ctx context.Context, getOrdersRequest *order.GetOrdersRequest) ([]order.Detail, error) {
if err := getOrdersRequest.Validate(); err != nil {
return nil, err
}
var allTrades []DashBoardInfo
resp, err := l.GetDashboardCancelledTrades()
resp, err := l.GetDashboardCancelledTrades(ctx)
if err != nil {
return nil, err
}
allTrades = append(allTrades, resp...)
resp, err = l.GetDashboardClosedTrades()
resp, err = l.GetDashboardClosedTrades(ctx)
if err != nil {
return nil, err
}
allTrades = append(allTrades, resp...)
resp, err = l.GetDashboardReleasedTrades()
resp, err = l.GetDashboardReleasedTrades(ctx)
if err != nil {
return nil, err
}
@@ -655,17 +657,17 @@ func (l *LocalBitcoins) GetOrderHistory(getOrdersRequest *order.GetOrdersRequest
// ValidateCredentials validates current credentials used for wrapper
// functionality
func (l *LocalBitcoins) ValidateCredentials(assetType asset.Item) error {
_, err := l.UpdateAccountInfo(assetType)
func (l *LocalBitcoins) ValidateCredentials(ctx context.Context, assetType asset.Item) error {
_, err := l.UpdateAccountInfo(ctx, assetType)
return l.CheckTransientError(err)
}
// GetHistoricCandles returns candles between a time period for a set time interval
func (l *LocalBitcoins) GetHistoricCandles(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
func (l *LocalBitcoins) GetHistoricCandles(ctx context.Context, pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
return kline.Item{}, common.ErrFunctionNotSupported
}
// GetHistoricCandlesExtended returns candles between a time period for a set time interval
func (l *LocalBitcoins) GetHistoricCandlesExtended(pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
func (l *LocalBitcoins) GetHistoricCandlesExtended(ctx context.Context, pair currency.Pair, a asset.Item, start, end time.Time, interval kline.Interval) (kline.Item, error) {
return kline.Item{}, common.ErrFunctionNotSupported
}