rpc

package
v0.0.0-...-b1afd28 Latest Latest
Warning

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

Go to latest
Published: May 9, 2023 License: MIT Imports: 12 Imported by: 0

README

RPC

RPC is a go package meant for interacting with the Chia Blockchain RPC API.

This package is currently a work in progress, and does not yet support much more of the API than is necessary to mint an NFT. This will improve as time allows!

Example Usage
Sync Status
r := &rpc.SyncStatusRequest{}
status, err := r.Send(rpc.Wallet)
if err != nil {
    // Sync Status Request failed; handle error.
}
if !status.Success {
    // Sync Status Request was unsuccessful; handle case.
}
if !status.Synced {
    // Check to see if wallet is actively synchronizing.
    if !status.Syncing {
        // Wallet is not actively synchronizing.
    }
}
Wallet Balance
r := &rpc.WalletBalanceRequest{WalletId: 1}
balance, err := r.Send(rpc.Wallet)
if err != nil {
    // Wallet Balance Request failed; handle error
}
if !balance.Success {
    // Sync Status Request was unsuccessful; handle error.
}
// Got wallet balance.
fmt.Printf("Wallet Balance: %v", balance.WalletBalance)

Documentation

Overview

Package rpc provides types and methods for use in communicating with the Chia RPC

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultPath     = ".chia"
	DefaultCertPath = "mainnet/config/ssl/"
	HomeDir         = ""
)

Functions

func Call

func Call(c Caller, p string, d interface{}) ([]byte, error)

Call is a general endpoint call function for convenience. It takes a Caller, procedure name string, and an any data type to marshal into JSON. @TODO: Expand definition for use with interfaces. May require some renaming of Endpoint and Procedure functions as well as package interfaces rework.

func PercentageToRoyalty

func PercentageToRoyalty(p float64) uint

PercentageToRoyalty takes a percentage p%, and returns the royalty percentage uint as chia expects it. (Ex: 5%; p=5, returns 500)

Types

type Caller

type Caller interface {
	Call(Procedure, []byte) ([]byte, error)
}

Caller is implemented by any type that calls a Chia RPC endpoint.

type Coin

type Coin struct {
	Amount         uint   `json:"amount"`
	ParentCoinInfo string `json:"parent_coin_info"`
	PuzzleHash     string `json:"puzzle_hash"`
}

Coin contains details about a specific coin.

type CoinRecord

type CoinRecord struct {
	Coin                *Coin `json:"coin"`
	Coinbase            bool  `json:"coinbase"`
	ConfirmedBlockIndex uint  `json:"confirmed_block_index"`
	Spent               bool  `json:"spent"`
	SpentBlockIndex     uint  `json:"spent_block_index"`
	Timestamp           uint  `json:"timestamp"`
}

CoinRecord contains details about a coin record.

type CoinRecordRequest

type CoinRecordRequest struct {
	Name string `json:"name"`
}

CoinRecordRequest is a type for making a request for a single CoinRecord by name.

func (*CoinRecordRequest) Procedure

func (c *CoinRecordRequest) Procedure() Procedure

Procedure returns the Procedure which this request will use.

func (*CoinRecordRequest) Send

Sends the request via an Endpoint, and returns the response, and an error. If successful, error returns nil.

func (*CoinRecordRequest) String

func (c *CoinRecordRequest) String() string

String implements the fmt.Stringer interface.

type CoinRecordResponse

type CoinRecordResponse struct {
	CoinRecord *CoinRecord `json:"coin_record"`
	Success    bool        `json:"success"`
	Error      string      `json:"error"`
}

CoinRecordResponse represents the Chia RPC API's response to a CoinRecordRequest.

type CoinRecordsByNameRequest

type CoinRecordsByNameRequest struct {
	Name         []string `json:"parent_ids"`
	StartHeight  uint     `json:"start_height,omitempty"`
	EndHeight    uint     `json:"end_height,omitempty"`
	IncludeSpent bool     `json:"include_spent_coins,omitempty"`
}

CoinRecordsByNameRequest is a type for making a request for a multiple CoinRecords by coin name.

func (*CoinRecordsByNameRequest) Procedure

func (c *CoinRecordsByNameRequest) Procedure() Procedure

Procedure returns the Procedure which this request will use.

func (*CoinRecordsByNameRequest) Send

Sends the request via an Endpoint, and returns the response, and an error. If successful, error returns nil.

func (*CoinRecordsByNameRequest) String

func (c *CoinRecordsByNameRequest) String() string

String implements the fmt.Stringer interface.

type CoinRecordsByParentIdsRequest

type CoinRecordsByParentIdsRequest struct {
	ParentIds    []string `json:"parent_ids"`
	StartHeight  uint     `json:"start_height,omitempty"`
	EndHeight    uint     `json:"end_height,omitempty"`
	IncludeSpent bool     `json:"include_spent_coins,omitempty"`
}

CoinRecordsByParentIdsRequest is a type for making a request for a multiple CoinRecords by parent ids.

func (*CoinRecordsByParentIdsRequest) Procedure

Procedure returns the Procedure which this request will use.

func (*CoinRecordsByParentIdsRequest) Send

Sends the request via an Endpoint, and returns the response, and an error. If successful, error returns nil.

func (*CoinRecordsByParentIdsRequest) String

String implements the fmt.Stringer interface.

type CoinRecordsRequest

type CoinRecordsRequest struct {
	Names        []string `json:"names"`
	ParentIds    []string `json:"parent_ids"`
	Hints        []string `json:"hints"`
	StartHeight  uint     `json:"start_height,omitempty"`
	EndHeight    uint     `json:"end_height,omitempty"`
	IncludeSpent bool     `json:"include_spent_coins,omitempty"`
}

CoinRecordsRequest is a type for making at least one request for a multiple CoinRecords by coin names, parent ids, and/or hints.

func (*CoinRecordsRequest) Procedure

func (c *CoinRecordsRequest) Procedure() Procedure

Procedure returns the Procedure which this request will use. Though this type is designed to call multiple procedures, this method will always return FullNodeCoinRecordByNames.

func (*CoinRecordsRequest) Send

Sends at least one request, of at least one CoinRecordsByN procedure, via an Endpoint, and returns the response, and any error. If successful, error returns nil.

func (*CoinRecordsRequest) String

func (c *CoinRecordsRequest) String() string

String implements the fmt.Stringer interface.

type CoinRecordsResponse

type CoinRecordsResponse struct {
	CoinRecords []*CoinRecord `json:"coin_records"`
	Success     bool          `json:"success"`
	Error       string        `json:"error"`
}

CoinRecordsResponse represents the Chia RPC API's response to a CoinRecordRequest.

type Endpoint

type Endpoint struct {
	Name string
	Host string
	Port uint
	*http.Transport
	*http.Client
}

An Endpoint represents a Chia RPC endpoint. It implements Caller.

var (
	Daemon *Endpoint = &Endpoint{Name: "daemon", Host: defaultHost, Port: 55400}
)
var (
	Farmer *Endpoint = &Endpoint{Name: "farmer", Host: defaultHost, Port: 8559}
)
var (
	FullNode *Endpoint = &Endpoint{Name: "full_node", Host: defaultHost, Port: 8555}
)
var (
	Harvester *Endpoint = &Endpoint{Name: "harvester", Host: defaultHost, Port: 8560}
)
var (
	Wallet *Endpoint = &Endpoint{Name: "wallet", Host: defaultHost, Port: 9256}
)

func NewEndpoint

func NewEndpoint(n string, h string, p uint) (*Endpoint, error)

NewEndpoint returns and initializes a new *Endpoint. It returns an error on initialization faliure.

func (*Endpoint) Call

func (e *Endpoint) Call(p Procedure, j []byte) ([]byte, error)

Call makes a call to the Chia RPC endpoint and returns the response as a byte slice. Takes a Procedure, p, and a payload as a JSON byte slice, j.

func (*Endpoint) Init

func (e *Endpoint) Init() error

Initializes the Endpoint's HTTP Transport and Client properties.

func (*Endpoint) Post

func (e *Endpoint) Post(p Procedure, b io.Reader) (*http.Response, error)

Post wraps the embedded *http.Client method. Used by Endpoint.Call() to make a POST request to a Chia RPC endpoint.

func (*Endpoint) String

func (e *Endpoint) String() string

Returns the Endpoint URI. Implements the fmt.Stringer interface.

type Errors

type Errors []error

Errors is a slice of error, and itself implements the built-in error interface.

func NewErrors

func NewErrors(e ...error) Errors

func (Errors) Error

func (e Errors) Error() string

Error implements the built-in error interface.

type MetadataListItem

type MetadataListItem struct {
	Uris          []string `json:"uris"`
	MetaUris      []string `json:"meta_uris,omitempty"`
	LicenseUris   []string `json:"license_uris,omitempty"`
	Hash          string   `json:"hash"`
	MetaHash      string   `json:"meta_hash,omitempty"`
	LicenseHash   string   `json:"license_hash,omitempty"`
	EditionNumber int      `json:"edition_number,omitempty"` // Not in CHIP-0007, but in chia 1.4.0 as "series_total", rather than "edition_total", due to bug. This was fixed in chia 1.5.0.
	EditionTotal  int      `json:"edition_total,omitempty"`  // Not in CHIP-0007, but in chia 1.4.0 as "series_number", rather than "edition_total", due to bug. This was fixed in chia 1.5.0.
}

type MintBulkRequest

type MintBulkRequest struct {
	WalletId            int                    `json:"wallet_id"`
	MetadataList        []*MetadataListItem    `json:"metadata_list"`
	RoyaltyPercentage   int                    `json:"royalty_percentage,omitempty"`
	RoyaltyAddress      string                 `json:"royalty_address,omitempty"`
	TargetAddressList   []string               `json:"target_address_list,omitempty"`
	MintNumberStart     int                    `json:"mint_number_start,omitempty"`
	MintTotal           int                    `json:"mint_total,omitempty"`
	XchCoinList         []string               `json:"xch_coin_list,omitempty"`
	XchChangeTarget     string                 `json:"xch_change_target,omitempty"`
	NewInnerPuzHash     string                 `json:"new_innerpuzhash,omitempty"`
	NewP2PuzHash        string                 `json:"new_p2_puzhash,omitempty"`
	DidCoinDict         map[string]interface{} `json:"did_coin_dict,omitempty"`
	DidLineageParentHex string                 `json:"did_lineage_parent_hex,omitempty"`
	MintFromDid         bool                   `json:"mint_from_did,omitempty"`
	Fee                 float64                `json:"fee,omitempty"`
	ReusePuzHash        bool                   `json:"reuse_puzhash,omitempty"`
}

func (*MintBulkRequest) Procedure

func (m *MintBulkRequest) Procedure() Procedure

func (MintBulkRequest) Send

func (*MintBulkRequest) String

func (m *MintBulkRequest) String() string

type MintBulkResponse

type MintBulkResponse struct {
	NftIdList   []string     `json:"nft_id_list"`
	SpendBundle *SpendBundle `json:"spend_bundle"`
	Success     bool         `json:"success"`
	Error       string       `json:"error"`
}

type MintRequest

type MintRequest struct {
	WalletId          int      `json:"wallet_id"`
	Uris              []string `json:"uris"`
	Hash              string   `json:"hash"`
	DidId             string   `json:"did_id,omitempty"`
	MetaUris          []string `json:"meta_uris,omitempty"`
	MetaHash          string   `json:"meta_hash,omitempty"`
	LicenseUris       []string `json:"license_uris,omitempty"`
	LicenseHash       string   `json:"license_hash,omitempty"`
	RoyaltyAddress    string   `json:"royalty_address,omitempty"`
	RoyaltyPercentage int      `json:"royalty_percentage,omitempty"`
	TargetAddress     string   `json:"target_address,omitempty"`
	Fee               float64  `json:"fee,omitempty"`
	SeriesNumber      int      `json:"series_number,omitempty"`  // In CHIP-0007, but not in chia 1.4.0 as "series_number", due to bug. This has been deprecated as of chia 1.5.0, likely until NFT2.
	SeriesTotal       int      `json:"series_total,omitempty"`   // In CHIP-0007, but not in chia 1.4.0 as "series_total", due to bug.. This has been deprecated as of chia 1.5.0, likely until NFT2.
	EditionNumber     int      `json:"edition_number,omitempty"` // Not in CHIP-0007, but in chia 1.4.0 as "series_total", rather than "edition_total", due to bug. This was fixed in chia 1.5.0.
	EditionTotal      int      `json:"edition_total,omitempty"`  // Not in CHIP-0007, but in chia 1.4.0 as "series_number", rather than "edition_total", due to bug. This was fixed in chia 1.5.0.
}

func (*MintRequest) Procedure

func (m *MintRequest) Procedure() Procedure

func (MintRequest) Send

func (m MintRequest) Send(e *Endpoint) (*MintResponse, error)

func (*MintRequest) String

func (m *MintRequest) String() string

type MintResponse

type MintResponse struct {
	Spend_bundle *SpendBundle
	Success      bool   `json:"success"`
	WalletId     uint   `json:"wallet_id"`
	Error        string `json:"error"`
}

type NftWalletGetDidRequest

type NftWalletGetDidRequest struct {
	WalletId uint `json:"wallet_id"`
}

func (*NftWalletGetDidRequest) Procedure

func (w *NftWalletGetDidRequest) Procedure() Procedure

func (*NftWalletGetDidRequest) Send

func (*NftWalletGetDidRequest) String

func (n *NftWalletGetDidRequest) String() string

type NftWalletGetDidResponse

type NftWalletGetDidResponse struct {
	DidId   int    `json:"did_id"`
	Success bool   `json:"success"`
	Error   string `json:"error"`
}

type Procedure

type Procedure string

Procedure represents procedure, and its value is its name.

const (
	FullNodeCoinRecordByName      Procedure = "get_coin_record_by_name"
	FullNodeCoinRecordByNames     Procedure = "get_coin_record_by_names"
	FullNodeCoinRecordByParentIds Procedure = "get_coin_record_by_parent_ids"
	FullNodeCoinRecordByHints     Procedure = "get_coin_record_by_hints"
	FullNodePushTx                Procedure = "push_tx"
)
const (
	WalletNFTMint         Procedure = "nft_mint_nft"
	WalletNFTMintBulk     Procedure = "nft_mint_bulk"
	WalletNFTGetWalletDID Procedure = "nft_get_wallet_did"
	WalletSyncStatus      Procedure = "get_sync_status"
	WalletGetBalance      Procedure = `get_wallet_balance`
	WalletPushTx          Procedure = `push_tx`
)

func (Procedure) String

func (p Procedure) String() string

String implements the fmt.Stringer interface.

type PushTxRequest

type PushTxRequest struct {
	SpendBundle *SpendBundle `json:"spend_bundle"`
}

PushTxRequest is a type for making a request to submit a SpendBundle to the blockchain.

func (*PushTxRequest) Procedure

func (c *PushTxRequest) Procedure() Procedure

Procedure returns the Procedure which this request will use.

func (*PushTxRequest) Send

func (p *PushTxRequest) Send(e *Endpoint) (*PushTxResponse, error)

Sends the request via an Endpoint, and returns the response, and an error. If successful, error returns nil.

func (*PushTxRequest) String

func (p *PushTxRequest) String() string

String implements the fmt.Stringer interface.

type PushTxResponse

type PushTxResponse struct {
	Status  string `json:"status"`
	Success bool   `json:"success"`
	Error   string `json:"error"`
}

PushTxResponse represents the Chia RPC API's response to a PushTxRequest.

type Sender

type Sender interface {
	Send(e *Endpoint) (interface{}, error)
	Procedure() Procedure
}

Sender is implemented by any type which can make a request via an Endpoint.

type Solution

type Solution struct {
	Coin         *Coin  `json:"coin"`
	PuzzleReveal string `json:"puzzle_reveal"`
	Solution     string `json:"solution"`
}

type SpendBundle

type SpendBundle struct {
	AggregatedSignature string      `json:"aggregated_signature"`
	CoinSolutions       []*Solution `json:"coin_solutions"`
}

type SyncStatusRequest

type SyncStatusRequest struct{}

func (*SyncStatusRequest) Procedure

func (s *SyncStatusRequest) Procedure() Procedure

func (*SyncStatusRequest) Send

func (*SyncStatusRequest) String

func (s *SyncStatusRequest) String() string

type SyncStatusResponse

type SyncStatusResponse struct {
	GenesisInitialized bool   `json:"genesis_initialized"`
	Success            bool   `json:"success"`
	Synced             bool   `json:"synced"`
	Syncing            bool   `json:"syncing"`
	Error              string `json:"error"`
}

type UntypedRequest

type UntypedRequest struct {
	Proc Procedure
	Data map[string]interface{}
}

Generic request type for use when other types won't do, or you don't want to write your own. Implements Sender.

func NewUntypedRequest

func NewUntypedRequest(p Procedure) *UntypedRequest

NewUntypedRequest takes a Procedure and returns an UntypedRequest.

func (*UntypedRequest) Procedure

func (u *UntypedRequest) Procedure() Procedure

Procedure returns the procedure currently assigned to this generic request.

func (*UntypedRequest) Send

Send sends the request via an Endpoint, and returns the result as an UntypedResponse, or nil, and an error. If successful, error returns nil.

func (*UntypedRequest) String

func (u *UntypedRequest) String() string

String implements the fmt.Stringer interface.

type UntypedResponse

type UntypedResponse map[string]interface{}

Generic response type for use in conjunction with UntypedRequest.

func NewUntypedResponse

func NewUntypedResponse() UntypedResponse

Convenience function. May be deprecated.

type WalletBalance

type WalletBalance struct {
	ConfirmedWalletBalance   uint `json:"confirmed_wallet_balance"`
	Fingerprint              uint `json:"fingerprint"`
	MaxSendAmount            uint `json:"max_send_amount"`
	PendingChange            uint `json:"pending_change"`
	PendingCoinRemovalCount  uint `json:"pending_coin_removal_count"`
	SpendableBalance         uint `json:"spendable_balance"`
	UnconfirmedWalletBalance uint `json:"unconfirmed_wallet_balance"`
	UnspentCoinCount         uint `json:"unspent_coin_amount"`
	WalletId                 uint `json:"wallet_id"`
}

type WalletBalanceRequest

type WalletBalanceRequest struct {
	WalletId uint `json:"wallet_id"`
}

func (*WalletBalanceRequest) Procedure

func (w *WalletBalanceRequest) Procedure() Procedure

func (*WalletBalanceRequest) Send

func (*WalletBalanceRequest) String

func (w *WalletBalanceRequest) String() string

type WalletBalanceResponse

type WalletBalanceResponse struct {
	WalletBalance *WalletBalance `json:"wallet_balance"`
	Success       bool           `json:"success"`
	Error         string         `json:"error"`
}

Jump to

Keyboard shortcuts

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