// GetInstant emulates an order in the current state of the Instant Exchange of CryptoMarket
// Returns an Instant struct holding the data.
//
// List of accepted Arguments:
//   - required: Market (string), Type (string), Amount (string)
//   - optional: none
// https://developers.cryptomkt.com/#obtener-cantidad
GetInstant emulates an order in the current state of the Instant Exchange of CryptoMarket
Returns an Instant struct holding the data.


// GetInstant emulates an order in the current state of the Instant Exchange of CryptoMarket // Returns an Instant struct holding the data . // // List of accepted Arguments : // - required : Market ( string ) Type ( string ) Amount ( string ) // - optional : none // https : // developers . cryptomkt . com / #obtener - cantidad
func (client *Client) GetInstant(arguments ...args.Argument) (*Instant, error) {
	required := []string{"market", "type", "amount"}
	req, err := makeReq(required, arguments...)
	if err != nil {
		return nil, fmt.Errorf("Error in GetInstant: %s", err)
	}
	bookArguments := req.GetArguments()
	totalAmount, _ := strconv.ParseFloat(bookArguments["amount"], 64)
	bookType := bookArguments["type"]
	if bookType == "sell" {
		bookType = "buy"
	} else {
		bookType = "sell"
	}
	var amountRequired float64
	var amountObtained float64
	for page, rest := 0, totalAmount; rest > 0; page++ {
		bookReq, err := makeReq(
			nil,
			args.Market(bookArguments["market"]),
			args.Type(bookType),
			args.Page(page),
			args.Limit(100),
		)
		if err != nil {
			return nil, fmt.Errorf("Error in GetInstant: %s", err)
		}
		resp, err := client.getPublic("book", bookReq)
		if err != nil {
			return nil, fmt.Errorf("error making the request: %s", err)
		}
		var bResp BookResponse
		json.Unmarshal(resp, &bResp)
		if bResp.Status == "error" {
			return nil, fmt.Errorf("error from the server side: %s", bResp.Message)
		}
		book := bResp.Data
		for i := 0; i < len(book); i++ {
			price, _ := strconv.ParseFloat(book[i].Price, 64)
			amount, _ := strconv.ParseFloat(book[i].Amount, 64)
			if (rest - amount) < 0 {
				amountObtained += rest * price
				amountRequired += rest
				rest = 0
				break
			} else { 
				amountObtained += amount * price
				amountRequired += amount
				rest -= amount
			}
		}
		if bResp.Pagination.Next == nil {
			break
		}
		time.Sleep(2 * time.Second)
	}
	if bookType == "sell" {
		amountRequired, amountObtained = amountObtained, amountRequired
	}
	instant := Instant{
		Obtained: amountObtained,
		Required: amountRequired,
	}
	return &instant, nil
}
func ( client * Client ) GetInstant ( arguments ... args . Argument ) ( * Instant , error ) { required := [ ] string { " " , " " , " " } req , err := makeReq ( required , arguments ... ) if err != nil { return nil , fmt . Errorf ( " " , err ) } bookArguments := req . GetArguments ( ) totalAmount , _ := strconv . ParseFloat ( bookArguments [ " " ] , 64 ) bookType := bookArguments [ " " ] if bookType == " " { bookType = " " } else { bookType = " " } var amountRequired float64 var amountObtained float64 for page , rest := 0 , totalAmount ; rest > 0 ; page ++ { bookReq , err := makeReq ( nil , args . Market ( bookArguments [ " " ] ) , args . Type ( bookType ) , args . Page ( page ) , args . Limit ( 100 ) , ) if err != nil { return nil , fmt . Errorf ( " " , err ) } resp , err := client . getPublic ( " " , bookReq ) if err != nil { return nil , fmt . Errorf ( " " , err ) } var bResp BookResponse json . Unmarshal ( resp , & bResp ) if bResp . Status == " " { return nil , fmt . Errorf ( " " , bResp . Message ) } book := bResp . Data for i := 0 ; i < len ( book ) ; i ++ { price , _ := strconv . ParseFloat ( book [ i ] . Price , 64 ) amount , _ := strconv . ParseFloat ( book [ i ] . Amount , 64 ) if ( rest - amount ) < 0 { amountObtained += rest * price amountRequired += rest rest = 0 break } else { amountObtained += amount * price amountRequired += amount rest -= amount } } if bResp . Pagination . Next == nil { break } time . Sleep ( 2 * time . Second ) } if bookType == " " { amountRequired , amountObtained = amountObtained , amountRequired } instant := Instant { Obtained : amountObtained , Required : amountRequired , } return & instant , nil }
GetInstant emulates an order in the current state of the Instant Exchange of CryptoMarket
Returns an Instant struct holding the data.
GetInstant emulates an order in the current state of the Instant Exchange of CryptoMarket Returns an Instant struct holding the data .
// si quiero vender, necesito el libro de los que compran //si quiero comprar, necesito el libro de los que venden // make a book request // rest - amount >= 0 // si quiero comprar crypto, el amountRequired es la cantidad en fiat, // y el amount obtained es la cantidad en crypto, lo que corresponde // al caso inverso del bookType, si bookType es sell, entonces yo quiero comprar.