Documentation ¶
Index ¶
- Constants
- Variables
- func InstructionIDToName(id int32) string
- func PeekAccount(data []byte) uint32
- type AccountHeader
- type AttrsMap
- func (a AttrsMap) BinaryLen() (size int)
- func (a AttrsMap) KVs() map[string]string
- func (a AttrsMap) MarshalBinary() ([]byte, error)
- func (a *AttrsMap) MarshalJSON() ([]byte, error)
- func (a AttrsMap) Sort()
- func (a *AttrsMap) UnmarshalBinary(data []byte) (err error)
- func (a *AttrsMap) UnmarshalJSON(data []byte) (err error)
- type CallbackHandle
- type Client
- func (c *Client) GetAllPriceAccounts(ctx context.Context, commitment rpc.CommitmentType) ([]PriceAccountEntry, error)
- func (c *Client) GetAllProductAccounts(ctx context.Context, commitment rpc.CommitmentType) ([]ProductAccountEntry, error)
- func (c *Client) GetAllProductKeys(ctx context.Context, commitment rpc.CommitmentType) ([]solana.PublicKey, error)
- func (c *Client) GetMappingAccount(ctx context.Context, mappingKey solana.PublicKey, ...) (MappingAccountEntry, error)
- func (c *Client) GetPriceAccount(ctx context.Context, priceKey solana.PublicKey, commitment rpc.CommitmentType) (PriceAccountEntry, error)
- func (c *Client) GetPriceAccountsRecursive(ctx context.Context, commitment rpc.CommitmentType, ...) ([]PriceAccountEntry, error)
- func (c *Client) GetProductAccount(ctx context.Context, productKey solana.PublicKey, ...) (ProductAccountEntry, error)
- func (c *Client) StreamPriceAccounts() *PriceAccountStream
- type CommandAddPrice
- type CommandAddPublisher
- type CommandDelPublisher
- type CommandHeader
- type CommandInitPrice
- type CommandSetMinPub
- type CommandUpdPrice
- type CommandUpdProduct
- type CommandUpdTest
- type Ema
- type Env
- type Instruction
- type InstructionBuilder
- func (i *InstructionBuilder) AddMapping(fundingKey solana.PublicKey, tailMappingKey solana.PublicKey, ...) *Instruction
- func (i *InstructionBuilder) AddPrice(fundingKey solana.PublicKey, productKey solana.PublicKey, ...) *Instruction
- func (i *InstructionBuilder) AddProduct(fundingKey solana.PublicKey, mappingKey solana.PublicKey, ...) *Instruction
- func (i *InstructionBuilder) AddPublisher(fundingKey solana.PublicKey, priceKey solana.PublicKey, ...) *Instruction
- func (i *InstructionBuilder) AggPrice(fundingKey solana.PublicKey, priceKey solana.PublicKey) *Instruction
- func (i *InstructionBuilder) DelPublisher(fundingKey solana.PublicKey, priceKey solana.PublicKey, ...) *Instruction
- func (i *InstructionBuilder) InitMapping(fundingKey solana.PublicKey, mappingKey solana.PublicKey) *Instruction
- func (i *InstructionBuilder) InitPrice(fundingKey solana.PublicKey, priceKey solana.PublicKey, ...) *Instruction
- func (i *InstructionBuilder) InitTest(fundingKey solana.PublicKey, testKey solana.PublicKey) *Instruction
- func (i *InstructionBuilder) SetMinPub(fundingKey solana.PublicKey, priceKey solana.PublicKey, ...) *Instruction
- func (i *InstructionBuilder) UpdPrice(fundingKey solana.PublicKey, priceKey solana.PublicKey, ...) *Instruction
- func (i *InstructionBuilder) UpdPriceNoFailOnError(fundingKey solana.PublicKey, priceKey solana.PublicKey, ...) *Instruction
- func (i *InstructionBuilder) UpdProduct(fundingKey solana.PublicKey, productKey solana.PublicKey, ...) *Instruction
- func (i *InstructionBuilder) UpdTest(fundingKey solana.PublicKey, testKey solana.PublicKey, payload CommandUpdTest) *Instruction
- type MappingAccount
- type MappingAccountEntry
- type PriceAccount
- type PriceAccountEntry
- type PriceAccountStream
- type PriceComp
- type PriceEventHandler
- type PriceInfo
- type PriceUpdate
- type ProductAccount
- type ProductAccountEntry
- type ProductAccountHeader
- type RawProductAccount
Examples ¶
Constants ¶
const ( AccountTypeUnknown = uint32(iota) AccountTypeMapping AccountTypeProduct AccountTypePrice )
The Account type enum identifies what each Pyth account stores.
const ( PriceStatusUnknown = uint32(iota) PriceStatusTrading PriceStatusHalted PriceStatusAuction )
Price status.
const ( Instruction_InitMapping = int32(iota) Instruction_AddMapping Instruction_AddProduct Instruction_UpdProduct Instruction_AddPrice Instruction_AddPublisher Instruction_DelPublisher Instruction_UpdPrice Instruction_AggPrice Instruction_InitPrice Instruction_InitTest Instruction_UpdTest Instruction_SetMinPub Instruction_UpdPriceNoFailOnError )
Pyth program instructions.
const Magic = uint32(0xa1b2c3d4)
Magic is the 32-bit number prefixed on each account.
const ProductAccountHeaderLen = 48
ProductAccountHeaderLen is the binary offset of the AttrsData field within RawProductAccount.
const V2 = uint32(2)
V2 identifies the version 2 data format stored in an account.
Variables ¶
var Devnet = Env{
Program: solana.MustPublicKeyFromBase58("gSbePebfvPy7tRqimPoVecS2UsBvYv46ynrzWocc92s"),
Mapping: solana.MustPublicKeyFromBase58("BmA9Z6FjioHJPpjT39QazZyhDRUdZy2ezwx4GiDdE2u2"),
}
Devnet is the Pyth program on the Solana devnet cluster.
var Mainnet = Env{
Program: solana.MustPublicKeyFromBase58("FsJ3A3u2vn5cTVofAjvy6y5kwABJAqYWpe4975bi2epH"),
Mapping: solana.MustPublicKeyFromBase58("AHtgzX45WTKfkPG53L6WYhGEXwQkN1BVknET3sVsLL8J"),
}
Mainnet is the Pyth program on the Solana mainnet cluster.
var Testnet = Env{
Program: solana.MustPublicKeyFromBase58("8tfDNiaEyrV6Q1U4DEXrEigs9DoDtkugzFbybENEbCDz"),
Mapping: solana.MustPublicKeyFromBase58("AFmdnt9ng1uVxqCmqwQJDAYC5cKTkw8gJKSM5PnzuF6z"),
}
Testnet is the Pyth program on the Solana testnet cluster.
Functions ¶
func InstructionIDToName ¶ added in v0.2.0
InstructionIDToName returns a human-readable name of a Pyth instruction type.
func PeekAccount ¶
PeekAccount determines the account type given the account's data bytes.
Types ¶
type AccountHeader ¶
type AccountHeader struct { Magic uint32 // set exactly to 0xa1b2c3d4 Version uint32 // currently V2 AccountType uint32 // account type following the header Size uint32 // size of the account including the header }
AccountHeader is a 16-byte header at the beginning of each account type.
func (AccountHeader) Valid ¶
func (h AccountHeader) Valid() bool
Valid performs basic checks on an account.
type AttrsMap ¶ added in v0.2.0
type AttrsMap struct {
Pairs [][2]string
}
AttrsMap is a list of string key-value pairs with stable order.
func NewAttrsMap ¶ added in v0.2.0
NewAttrsMap returns a new attribute map with an initial arbitrary order.
The provided Go map may be nil.
func ReadAttrsMapFromBinary ¶ added in v0.2.0
ReadAttrsMapFromBinary consumes all bytes from a binary reader, returning an AttrsMap and the number of bytes read.
func (AttrsMap) BinaryLen ¶ added in v0.3.0
BinaryLen returns this AttrsMap's length in binary encoding.
func (AttrsMap) MarshalBinary ¶ added in v0.2.0
MarshalBinary marshals AttrsMap to its on-chain format.
func (*AttrsMap) MarshalJSON ¶ added in v0.3.0
MarshalJSON returns a JSON string map.
func (AttrsMap) Sort ¶ added in v0.3.0
func (a AttrsMap) Sort()
Sort sorts the keys of an AttrsMap by lexicographic order.
func (*AttrsMap) UnmarshalBinary ¶ added in v0.2.0
UnmarshalBinary unmarshals AttrsMap from its on-chain format.
Will return an error if it fails to consume the entire provided byte slice.
func (*AttrsMap) UnmarshalJSON ¶ added in v0.3.0
UnmarshalJSON loads a JSON string map.
type CallbackHandle ¶ added in v0.3.1
type CallbackHandle struct {
// contains filtered or unexported fields
}
CallbackHandle tracks the lifetime of a callback registration.
func (CallbackHandle) Unsubscribe ¶ added in v0.3.1
func (c CallbackHandle) Unsubscribe()
Unsubscribe de-registers a callback from the handler.
Calling Unsubscribe is optional. The handler calls it automatically when the underlying stream closes.
type Client ¶
type Client struct { Env Env RPC *rpc.Client WebSocketURL string Log *zap.Logger AccountsBatchSize int // number of accounts to get with getMultipleAccounts() }
Client interacts with Pyth via Solana's JSON-RPC API.
Do not instantiate Client directly, use NewClient instead.
func (*Client) GetAllPriceAccounts ¶ added in v0.3.2
func (c *Client) GetAllPriceAccounts(ctx context.Context, commitment rpc.CommitmentType) ([]PriceAccountEntry, error)
GetAllPriceAccounts returns all price accounts.
Aborts and returns an error if any product account failed to fetch.
func (*Client) GetAllProductAccounts ¶ added in v0.3.2
func (c *Client) GetAllProductAccounts(ctx context.Context, commitment rpc.CommitmentType) ([]ProductAccountEntry, error)
GetAllProductAccounts returns all product accounts.
Aborts and returns an error if any product account failed to fetch.
Example ¶
client := NewClient(Devnet, testRPC, testWS) products, _ := client.GetAllProductAccounts(context.TODO(), rpc.CommitmentProcessed) // Print first product as JSON. products[0].Slot = 1234 jsonData, _ := json.MarshalIndent(&products[0], "", " ") fmt.Println(string(jsonData))
Output: { "first_price": "4EQrNZYk5KR1RnjyzbaaRbHsv8VqZWzSUtvx58wLsZbj", "attrs": { "asset_type": "Crypto", "base": "BCH", "description": "BCH/USD", "generic_symbol": "BCHUSD", "quote_currency": "USD", "symbol": "Crypto.BCH/USD" }, "pubkey": "89GseEmvNkzAMMEXcW9oTYzqRPXTsJ3BmNerXmgA1osV", "slot": 1234 }
func (*Client) GetAllProductKeys ¶ added in v0.3.0
func (c *Client) GetAllProductKeys(ctx context.Context, commitment rpc.CommitmentType) ([]solana.PublicKey, error)
GetAllProductKeys lists all mapping accounts for product account pubkeys.
Example ¶
client := NewClient(Devnet, testRPC, testWS) products, _ := client.GetAllProductKeys(context.TODO(), rpc.CommitmentProcessed) // Print first 5 product account pubkeys. for _, key := range products[:5] { fmt.Println(key) }
Output: 89GseEmvNkzAMMEXcW9oTYzqRPXTsJ3BmNerXmgA1osV JCnD5WiurZfoeVPEi2AXVgacg73Wd2iRDDjZDbSwdr9D G89jkM5wFLpmnbvRbeePUumxsJyzoXaRfgBVjyx2CPzQ GaBJpKtnyUbyKe34XuyegR7W98a9PT5cg985G974NY8R Fwosgw2ikRvdzgKcQJwMacyczk3nXgoW3AtVtyVvXSAb
func (*Client) GetMappingAccount ¶ added in v0.3.0
func (c *Client) GetMappingAccount(ctx context.Context, mappingKey solana.PublicKey, commitment rpc.CommitmentType) (MappingAccountEntry, error)
GetMappingAccount retrieves a single mapping account from the blockchain.
func (*Client) GetPriceAccount ¶ added in v0.1.1
func (c *Client) GetPriceAccount(ctx context.Context, priceKey solana.PublicKey, commitment rpc.CommitmentType) (PriceAccountEntry, error)
GetPriceAccount retrieves a price account from the blockchain.
func (*Client) GetPriceAccountsRecursive ¶ added in v0.3.2
func (c *Client) GetPriceAccountsRecursive(ctx context.Context, commitment rpc.CommitmentType, priceKeys ...solana.PublicKey) ([]PriceAccountEntry, error)
GetPriceAccountsRecursive retrieves the price accounts of the given public keys.
If these price accounts have successors, their contents will be fetched as well, recursively. When called with the ProductAccountHeader.FirstPrice, it will fetch all price accounts of a product.
func (*Client) GetProductAccount ¶ added in v0.1.1
func (c *Client) GetProductAccount(ctx context.Context, productKey solana.PublicKey, commitment rpc.CommitmentType) (ProductAccountEntry, error)
GetProductAccount retrieves a product account from the blockchain.
Example ¶
client := NewClient(Devnet, testRPC, testWS) productPubkey := solana.MustPublicKeyFromBase58("EWxGfxoPQSNA2744AYdAKmsQZ8F9o9M7oKkvL3VM1dko") product, _ := client.GetProductAccount(context.TODO(), productPubkey, rpc.CommitmentProcessed) product.Slot = 1234 // Print first product as JSON. jsonData, _ := json.MarshalIndent(product, "", " ") fmt.Println(string(jsonData))
Output: { "first_price": "E36MyBbavhYKHVLWR79GiReNNnBDiHj6nWA7htbkNZbh", "attrs": { "asset_type": "FX", "base": "EUR", "description": "EUR/USD", "generic_symbol": "EURUSD", "quote_currency": "USD", "symbol": "FX.EUR/USD", "tenor": "Spot" }, "pubkey": "EWxGfxoPQSNA2744AYdAKmsQZ8F9o9M7oKkvL3VM1dko", "slot": 1234 }
func (*Client) StreamPriceAccounts ¶
func (c *Client) StreamPriceAccounts() *PriceAccountStream
StreamPriceAccounts creates a new stream of price account updates.
It will reconnect automatically if the WebSocket connection breaks or stalls.
Example ¶
client := NewClient(Devnet, testRPC, testWS) stream := client.StreamPriceAccounts() // Close stream after a while. go func() { <-time.After(3 * time.Second) stream.Close() }() // Print updates. for update := range stream.Updates() { fmt.Println(update.Agg.Price) }
Output:
type CommandAddPrice ¶ added in v0.2.0
CommandAddPrice is the payload of Instruction_AddPrice.
type CommandAddPublisher ¶ added in v0.2.0
type CommandAddPublisher struct {
Publisher solana.PublicKey
}
CommandAddPublisher is the payload of Instruction_AddPublisher.
type CommandDelPublisher ¶ added in v0.2.0
type CommandDelPublisher struct {
Publisher solana.PublicKey
}
CommandDelPublisher is the payload of Instruction_DelPublisher.
type CommandHeader ¶ added in v0.2.0
CommandHeader is an 8-byte header at the beginning any instruction data.
func (*CommandHeader) Valid ¶ added in v0.2.0
func (h *CommandHeader) Valid() bool
Valid performs basic checks on instruction data.
type CommandInitPrice ¶ added in v0.2.0
CommandInitPrice is the payload of Instruction_InitPrice.
type CommandSetMinPub ¶ added in v0.2.0
CommandSetMinPub is the payload of Instruction_SetMinPub.
type CommandUpdPrice ¶ added in v0.2.0
CommandUpdPrice is the payload of Instruction_UpdPrice or Instruction_UpdPriceNoFailOnError.
type CommandUpdProduct ¶ added in v0.2.0
type CommandUpdProduct struct {
AttrsMap
}
CommandUpdProduct is the payload of Instruction_UpdProduct.
type CommandUpdTest ¶ added in v0.2.0
CommandUpdTest is the payload Instruction_UpdTest.
type Env ¶ added in v0.3.0
type Env struct { Program solana.PublicKey // Program ID Mapping solana.PublicKey // Root mapping key }
Env identifies deployment of the Pyth on-chain program.
type Instruction ¶ added in v0.2.0
type Instruction struct { Header CommandHeader Payload interface{} // contains filtered or unexported fields }
func DecodeInstruction ¶ added in v0.2.0
func DecodeInstruction( programKey solana.PublicKey, accounts []*solana.AccountMeta, data []byte, ) (*Instruction, error)
DecodeInstruction attempts to reconstruct a Pyth command from an on-chain instruction.
Security ¶
Please note that this function may behave differently than the Pyth on-chain program. Especially edge cases and invalid input is handled according to "best effort".
This function also performs no account ownership nor permission checks.
It is best to only use this instruction on successful program executions.
func (*Instruction) Accounts ¶ added in v0.2.0
func (inst *Instruction) Accounts() []*solana.AccountMeta
func (*Instruction) Data ¶ added in v0.2.0
func (inst *Instruction) Data() ([]byte, error)
func (*Instruction) ProgramID ¶ added in v0.2.0
func (inst *Instruction) ProgramID() solana.PublicKey
type InstructionBuilder ¶ added in v0.2.0
type InstructionBuilder struct {
// contains filtered or unexported fields
}
InstructionBuilder creates new instructions to interact with the Pyth on-chain program.
func NewInstructionBuilder ¶ added in v0.2.0
func NewInstructionBuilder(programKey solana.PublicKey) *InstructionBuilder
NewInstructionBuilder creates a new InstructionBuilder targeting the given Pyth program.
func (*InstructionBuilder) AddMapping ¶ added in v0.2.0
func (i *InstructionBuilder) AddMapping( fundingKey solana.PublicKey, tailMappingKey solana.PublicKey, newMappingKey solana.PublicKey, ) *Instruction
AddMapping initializes and adds new mapping account to list.
func (*InstructionBuilder) AddPrice ¶ added in v0.2.0
func (i *InstructionBuilder) AddPrice( fundingKey solana.PublicKey, productKey solana.PublicKey, priceKey solana.PublicKey, payload CommandAddPrice, ) *Instruction
AddPrice adds a new price account to a product account.
func (*InstructionBuilder) AddProduct ¶ added in v0.2.0
func (i *InstructionBuilder) AddProduct( fundingKey solana.PublicKey, mappingKey solana.PublicKey, productKey solana.PublicKey, ) *Instruction
AddProduct initializes and adds new product reference data account.
func (*InstructionBuilder) AddPublisher ¶ added in v0.2.0
func (i *InstructionBuilder) AddPublisher( fundingKey solana.PublicKey, priceKey solana.PublicKey, payload CommandAddPublisher, ) *Instruction
AddPublisher adds a publisher to a price account.
func (*InstructionBuilder) AggPrice ¶ added in v0.2.0
func (i *InstructionBuilder) AggPrice( fundingKey solana.PublicKey, priceKey solana.PublicKey, ) *Instruction
AggPrice computes the aggregate price for a product account.
func (*InstructionBuilder) DelPublisher ¶ added in v0.2.0
func (i *InstructionBuilder) DelPublisher( fundingKey solana.PublicKey, priceKey solana.PublicKey, payload CommandDelPublisher, ) *Instruction
DelPublisher deletes a publisher from a price account.
func (*InstructionBuilder) InitMapping ¶ added in v0.2.0
func (i *InstructionBuilder) InitMapping( fundingKey solana.PublicKey, mappingKey solana.PublicKey, ) *Instruction
InitMapping initializes the first mapping list account.
func (*InstructionBuilder) InitPrice ¶ added in v0.2.0
func (i *InstructionBuilder) InitPrice( fundingKey solana.PublicKey, priceKey solana.PublicKey, payload CommandInitPrice, ) *Instruction
InitPrice (re)initializes a price account.
func (*InstructionBuilder) InitTest ¶ added in v0.2.0
func (i *InstructionBuilder) InitTest( fundingKey solana.PublicKey, testKey solana.PublicKey, ) *Instruction
InitTest initializes a test account.
func (*InstructionBuilder) SetMinPub ¶ added in v0.2.0
func (i *InstructionBuilder) SetMinPub( fundingKey solana.PublicKey, priceKey solana.PublicKey, payload CommandSetMinPub, ) *Instruction
SetMinPub sets the minimum publishers of a price account.
func (*InstructionBuilder) UpdPrice ¶ added in v0.2.0
func (i *InstructionBuilder) UpdPrice( fundingKey solana.PublicKey, priceKey solana.PublicKey, payload CommandUpdPrice, ) *Instruction
UpdPrice publishes a new component price to a price account.
func (*InstructionBuilder) UpdPriceNoFailOnError ¶ added in v0.3.7
func (i *InstructionBuilder) UpdPriceNoFailOnError( fundingKey solana.PublicKey, priceKey solana.PublicKey, payload CommandUpdPrice, ) *Instruction
UpdPriceNoFailOnError publishes a new component price to a price account, ignoring errors.
func (*InstructionBuilder) UpdProduct ¶ added in v0.2.0
func (i *InstructionBuilder) UpdProduct( fundingKey solana.PublicKey, productKey solana.PublicKey, payload CommandUpdProduct, ) *Instruction
UpdProduct updates a product account.
func (*InstructionBuilder) UpdTest ¶ added in v0.2.0
func (i *InstructionBuilder) UpdTest( fundingKey solana.PublicKey, testKey solana.PublicKey, payload CommandUpdTest, ) *Instruction
UpdTest runs an aggregate price test.
type MappingAccount ¶
type MappingAccount struct { AccountHeader Num uint32 // number of keys Pad1 uint32 // reserved field Next solana.PublicKey // pubkey of next mapping account Products [640]solana.PublicKey }
MappingAccount is a piece of a singly linked-list of all products on Pyth.
func (*MappingAccount) ProductKeys ¶ added in v0.3.0
func (m *MappingAccount) ProductKeys() []solana.PublicKey
ProductKeys returns the slice of product keys referenced by this mapping, excluding empty entries.
func (*MappingAccount) UnmarshalBinary ¶
func (m *MappingAccount) UnmarshalBinary(buf []byte) error
UnmarshalBinary decodes a mapping account from the on-chain format.
type MappingAccountEntry ¶ added in v0.3.2
type MappingAccountEntry struct { *MappingAccount Pubkey solana.PublicKey `json:"pubkey"` Slot uint64 `json:"slot"` }
MappingAccountEntry is a versioned mapping account and its pubkey.
type PriceAccount ¶
type PriceAccount struct { AccountHeader PriceType uint32 // price or calculation type Exponent int32 // price exponent Num uint32 // number of component prices NumQt uint32 // number of quoters that make up aggregate LastSlot uint64 // slot of last valid (not unknown) aggregate price ValidSlot uint64 // valid slot of aggregate price Twap Ema // exponential moving average price Twac Ema // exponential moving confidence interval Drv1, Drv2 int64 // reserved for future use Product solana.PublicKey // ProductAccount key Next solana.PublicKey // next PriceAccount key in linked list PrevSlot uint64 // valid slot of previous update PrevPrice int64 // aggregate price of previous update PrevConf uint64 // confidence interval of previous update Drv3 int64 // reserved for future use Agg PriceInfo // aggregate price info Components [32]PriceComp // price components for each quoter }
PriceAccount represents a continuously-updating price feed for a product.
func (*PriceAccount) GetComponent ¶
func (p *PriceAccount) GetComponent(publisher *solana.PublicKey) *PriceComp
GetComponent returns the first price component with the given publisher key. Might return nil.
func (*PriceAccount) UnmarshalBinary ¶
func (p *PriceAccount) UnmarshalBinary(buf []byte) error
UnmarshalBinary decodes the price account from the on-chain format.
type PriceAccountEntry ¶ added in v0.3.2
type PriceAccountEntry struct { *PriceAccount Pubkey solana.PublicKey `json:"pubkey"` Slot uint64 `json:"slot"` }
PriceAccountEntry is a versioned price account and its pubkey.
type PriceAccountStream ¶ added in v0.3.0
type PriceAccountStream struct {
// contains filtered or unexported fields
}
PriceAccountStream is an ongoing stream of on-chain price account updates.
func (*PriceAccountStream) Close ¶ added in v0.3.0
func (p *PriceAccountStream) Close()
Close must be called when no more updates are needed.
func (*PriceAccountStream) Err ¶ added in v0.3.0
func (p *PriceAccountStream) Err() error
Err returns the reason why the price account stream is closed. Will block until the stream has actually closed. Returns nil if closure was expected.
func (*PriceAccountStream) Updates ¶ added in v0.3.0
func (p *PriceAccountStream) Updates() <-chan PriceAccountEntry
Updates returns a channel with new price account updates.
type PriceComp ¶
type PriceComp struct { Publisher solana.PublicKey // key of contributing publisher Agg PriceInfo // price used to compute the current aggregate price Latest PriceInfo // latest price of publisher }
PriceComp contains the price and confidence contributed by a specific publisher.
type PriceEventHandler ¶ added in v0.3.1
type PriceEventHandler struct {
// contains filtered or unexported fields
}
PriceEventHandler provides a callback-style interface to Pyth updates.
Example ¶
// Connect to Pyth on Solana devnet. client := NewClient(Devnet, testRPC, testWS) // Open new event stream. stream := client.StreamPriceAccounts() handler := NewPriceEventHandler(stream) // Subscribe to price account changes. priceKey := solana.MustPublicKeyFromBase58("J83w4HKfqxwcq3BEMMkPFSppX3gqekLyLJBexebFVkix") handler.OnPriceChange(priceKey, func(info PriceUpdate) { price, conf, ok := info.Current() if ok { log.Printf("Price change: $%s ± $%s", price, conf) } }) // Close stream after a while. <-time.After(10 * time.Second) stream.Close()
Output:
func NewPriceEventHandler ¶ added in v0.3.1
func NewPriceEventHandler(stream *PriceAccountStream) *PriceEventHandler
NewPriceEventHandler creates a new event handler over the stream.
A stream must not be re-used between event handlers.
func (*PriceEventHandler) Err ¶ added in v0.3.1
func (p *PriceEventHandler) Err() error
Err returns the reason why the underlying price account stream is closed.
Will block until the stream has actually closed. Returns nil if closure was expected.
After this function returns the event handler will not send any more callbacks. You could use this function as a barrier for any cleanup tasks relating to callbacks.
func (*PriceEventHandler) OnComponentChange ¶ added in v0.3.1
func (p *PriceEventHandler) OnComponentChange(priceKey solana.PublicKey, publisher solana.PublicKey, callback func(PriceUpdate)) CallbackHandle
OnComponentChange registers a callback function to be called whenever the price component of the given (price account, publisher account) pair changes.
func (*PriceEventHandler) OnPriceChange ¶ added in v0.3.1
func (p *PriceEventHandler) OnPriceChange(priceKey solana.PublicKey, callback func(PriceUpdate)) CallbackHandle
OnPriceChange registers a callback function to be called whenever the aggregate price of the provided price account changes.
type PriceInfo ¶
type PriceInfo struct { Price int64 // current price Conf uint64 // confidence interval around the price Status uint32 // status of price CorpAct uint32 PubSlot uint64 // valid publishing slot }
PriceInfo contains a price and confidence at a specific slot.
This struct can represent either a publisher's contribution or the outcome of price aggregation.
func (*PriceInfo) HasChanged ¶ added in v0.3.1
HasChanged returns whether there was a change between this and another price info.
type PriceUpdate ¶ added in v0.3.1
type PriceUpdate struct { Account *PriceAccount PreviousInfo *PriceInfo CurrentInfo *PriceInfo }
PriceUpdate is returned to callbacks when an aggregate or component price has been updated.
type ProductAccount ¶ added in v0.1.1
type ProductAccount struct { ProductAccountHeader Attrs AttrsMap `json:"attrs"` // key-value string pairs of additional data }
ProductAccount contains metadata for a single product, such as its symbol and its base/quote currencies.
func (*ProductAccount) UnmarshalBinary ¶ added in v0.1.1
func (p *ProductAccount) UnmarshalBinary(buf []byte) error
UnmarshalBinary decodes the product account from the on-chain format.
func (*ProductAccount) UnmarshalJSON ¶ added in v0.3.0
func (p *ProductAccount) UnmarshalJSON(buf []byte) error
UnmarshalJSON decodes the product account contents from JSON.
type ProductAccountEntry ¶ added in v0.3.0
type ProductAccountEntry struct { *ProductAccount Pubkey solana.PublicKey `json:"pubkey"` Slot uint64 `json:"slot"` }
ProductAccountEntry is a versioned product account and its pubkey.
type ProductAccountHeader ¶ added in v0.3.0
type ProductAccountHeader struct { AccountHeader `json:"-"` FirstPrice solana.PublicKey `json:"first_price"` // first price account in list }
type RawProductAccount ¶ added in v0.3.0
type RawProductAccount struct { ProductAccountHeader AttrsData [464]byte }