xmrto

package module
v0.0.0-...-c261958 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2020 License: MIT Imports: 7 Imported by: 1

README

GO XMR.TO Client

GoDoc

Logo

A client implementation for the xmr.to service written in go.

Installation
go get -u github.com/monero-ecosystem/go-xmrto-client
Example code:
package main

import (
	"encoding/json"
	"fmt"
	"log"
	"os"
	"time"

	"github.com/monero-ecosystem/go-xmrto-client"
)

func main() {
	// initiate a new client for the testnet
	client := xmrto.New(&xmrto.Config{Testnet: true})

	// here we check if we run the programm with a check parameter.
	// in case we already got a secret-key from xmr.to we can track the order.
	if len(os.Args) > 2 {
		if os.Args[1] == "check" {
			checkorder, err := client.GetOrderStatus(&xmrto.RequestGetOrderStatus{UUID: os.Args[2]})
			checkerr(err)

			log.Println(string(prettyPrintJSON(checkorder)))
			fmt.Printf("Your order has state: %s\n", checkorder.State)

			return
		}
	}
	// otherwise

	// get order parameters:
	// - how much bitcoin can we send?
	// - what is the upper limit? lower limit?
	// - the current exchange rate (price) of the btc amount we want to send
	// - etc.
	getorder, err := client.GetOrderParameters()
	checkerr(err)

	// pretty print.
	log.Println(string(prettyPrintJSON(getorder)))

	// let's create an order with 0.001 btc.
	createorder, err := client.CreateOrder(&xmrto.RequestCreateOrder{
		BTCAmount:      .001,
		BTCDestAddress: "2N5AYGnYKM7zgTe1n8P7mjUE3DavD1ub7Zs", // this is the testnet btc address of xmr.to itself.
	})
	checkerr(err)

	log.Println(string(prettyPrintJSON(createorder)))

	// we got a secret-key from xmr.to (for later).
	fmt.Printf("UUID (secret key) is: %s\n", createorder.UUID)

	// important: give it time to let "CreateOrder" to settle down
	// before we query our order.
	time.Sleep(time.Second * 1)

	// now check the order state with the secret-key
	// we received from xmr.to for this particular order.
	orderstatus, err := client.GetOrderStatus(&xmrto.RequestGetOrderStatus{UUID: createorder.UUID})
	checkerr(err)

	log.Println(string(prettyPrintJSON(orderstatus)))

	// print a nice message to the user
	// how much xmr to deposit to which xmr address.
	fmt.Printf("Please deposit %f to Monero address: %s\n", orderstatus.XMRAmountTotal, orderstatus.XMRReceivingSubAddress)

	return
}

/* helper functions */
func prettyPrintJSON(data interface{}) (pretty []byte) {
	pretty, _ = json.MarshalIndent(data, "", "    ")
	return
}

func checkerr(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

Run the code

go run main.go

to create an order, or:

go run main.go check <your secret-key>

to check an order about its state

Contribution

  • You can fork this, extend it and contribute back.
  • You can contribute with pull requests.

Donations

I love Monero (XMR) and building applications for and on top of Monero.

You can make me happy by donating Monero to the following address:

89woiq9b5byQ89SsUL4Bd66MNfReBrTwNEDk9GoacgESjfiGnLSZjTD5x7CcUZba4PBbE3gUJRQyLWD4Akz8554DR4Lcyoj

LICENSE

MIT License

Documentation

Index

Constants

View Source
const APIBaseAddress = "https://xmr.to/api"

APIBaseAddress is the base URL of the xmr.to API

View Source
const APIConversionDirection = "xmr2btc"

APIConversionDirection is the API conversion direction (currently only xmr2btc)

View Source
const APITestnetBaseAddress = "https://test.xmr.to/api/"

APITestnetBaseAddress is the base URL of the xmr.to TESTNET API

View Source
const APIVersion = "v3"

APIVersion is the API version identifier (currently v3)

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError

type APIError struct {
	APIError        string `json:"error"`
	APIErrorMessage string `json:"error_msg"`
}

APIError represents an error message by the xmr.to API

func (*APIError) Error

func (we *APIError) Error() string

type Client

type Client interface {
	// The order parameter endpoint supplies information about whether new orders can be created.
	// In this case, this endpoint provides the current price, order limits, etc. for newly created orders.
	GetOrderParameters() (*ResponseGetOrderParameters, error)
	// The order creation endpoint allows to create a new order at the current price.
	// The user has to supply a bitcoin destination address and amount to create the order.
	CreateOrder(*RequestCreateOrder) (*ResponseCreateOrder, error)
	// The order status endpoint allows users to query the status of an order, thereby obtaining payment details and order processing progress.
	GetOrderStatus(*RequestGetOrderStatus) (*ResponseGetOrderStatus, error)
	// The order status endpoint allows users to query the recent price of an order by displaying the convertion price of XMR to BTC
	GetOrderPrice(*RequestGetOrderPrice) (*ResponseGetOrderPrice, error)
}

Client is a xmr.to API client

func New

func New(config *Config) Client

New returns a new xmr.to API client

type Config

type Config struct {
	APIBaseAddress         string
	APIVersion             string
	APIConversionDirection string
	Testnet                bool
}

Config holds the configuration of a xmr.to API client.

type ErrorCode

type ErrorCode int

ErrorCode is a xmr.to error code table

type RequestCreateOrder

type RequestCreateOrder struct {
	Amount         float64 `json:"amount"`
	AmountCurrency string  `json:"amount_currency"`
	BTCDestAddress string  `json:"btc_dest_address"`
}

RequestCreateOrder is for CreateOrder()

type RequestGetOrderPrice

type RequestGetOrderPrice struct {
	Amount         float64 `json:"amount"`
	AmountCurrency string  `json:"amount_currency"`
}

RequestGetOrderPrice is for GetOrderPrice()

type RequestGetOrderStatus

type RequestGetOrderStatus struct {
	UUID string `json:"uuid"`
}

RequestGetOrderStatus is for GetOrderStatus()

type ResponseCreateOrder

type ResponseCreateOrder struct {
	State          string `json:"state"`
	BTCAmount      string `json:"btc_amount"`
	BTCDestAddress string `json:"btc_dest_address"`
	UUID           string `json:"uuid"`
}

ResponseCreateOrder is for CreateOrder()

type ResponseGetOrderParameters

type ResponseGetOrderParameters struct {
	LowerLimit        string `json:"lower_limit"`
	Price             string `json:"price"`
	UpperLimit        string `json:"upper_limit"`
	ZeroConfEnabled   bool   `json:"zero_conf_enabled"`
	ZeroConfMaxAmount string `json:"zero_conf_max_amount"`
}

ResponseGetOrderParameters is for GetOrderParameters()

type ResponseGetOrderPrice

type ResponseGetOrderPrice struct {
	BTCAmount                    string `json:"btc_amount"`
	XMRAmountTotal               string `json:"incoming_amount_total"`
	XMRNumConfirmationsRemaining int64  `json:"incoming_num_confirmations_remaining"`
	XMRPriceBTC                  string `json:"incoming_price_btc"`
}

ResponseGetOrderPrice is for GetOrderPrice()

type ResponseGetOrderStatus

type ResponseGetOrderStatus struct {
	State                        string `json:"state"`
	BTCAmount                    string `json:"btc_amount"`
	BTCAmountPartial             string `json:"btc_amount_partial"`
	BTCDestAddress               string `json:"btc_dest_address"`
	UUID                         string `json:"uuid"`
	BTCNumConfirmationsThreshold int64  `json:"btc_num_confirmations_threshold"`
	CreatedAT                    string `json:"created_at"`
	ExpiresAT                    string `json:"expires_at"`
	SecondsTillTimeout           int64  `json:"seconds_till_timeout"`
	BTCTransactionID             string `json:"btc_transaction_id"`
	XMRAmountTotal               string `json:"incoming_amount_total"`
	XMRAmountRemaining           string `json:"remaining_amount_incoming"`
	XMRNumConfirmationsRemaining int64  `json:"incoming_num_confirmations_remaining"`
	XMRPriceBTC                  string `json:"incoming_price_btc"`
	XMRReceivingSubAddress       string `json:"receiving_subaddress"`
	XMRRecommendedMixin          int64  `json:"recommended_mixin"`
}

ResponseGetOrderStatus is for GetOrderStatus()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL