Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AddProduct creates the Product metadata #67

Merged
merged 3 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pythnetwork/client",
"version": "2.14.0",
"version": "2.15.0",
"description": "Client for consuming Pyth price data",
"homepage": "https://pyth.network",
"main": "lib/index.js",
Expand Down
24 changes: 14 additions & 10 deletions src/PythConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ export class PythConnection {
/** Create a PythConnection that reads its data from an underlying solana web3 connection.
* pythProgramKey is the public key of the Pyth program running on the chosen solana cluster.
*/
constructor(connection: Connection, pythProgramKey: PublicKey, commitment: Commitment = 'finalized', feedIds?: PublicKey[]) {
constructor(
connection: Connection,
pythProgramKey: PublicKey,
commitment: Commitment = 'finalized',
feedIds?: PublicKey[],
) {
this.connection = connection
this.pythProgramKey = pythProgramKey
this.commitment = commitment
Expand All @@ -124,21 +129,20 @@ export class PythConnection {
this.handleAccount(account.pubkey, account.account, true, currentSlot)
}

if(this.feedIds) {
if (this.feedIds) {
// Filter down to only the feeds we want
const rawIDs = this.feedIds.map((feed) => feed.toString())
accounts = accounts.filter((feed) => rawIDs.includes(feed.pubkey.toString()))
for (const account of accounts){
for (const account of accounts) {
this.connection.onAccountChange(
account.pubkey,
account.pubkey,

(accountInfo, context) => {
this.handleAccount(account.pubkey, accountInfo, false, context.slot)
},
this.commitment,
)
(accountInfo, context) => {
this.handleAccount(account.pubkey, accountInfo, false, context.slot)
},
this.commitment,
)
}

} else {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive by format

this.connection.onProgramAccountChange(
this.pythProgramKey,
Expand Down
26 changes: 26 additions & 0 deletions src/__tests__/Anchor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,32 @@ test('Anchor', (done) => {
expect(decoded?.name).toBe('addMapping')
expect(decoded?.data).toStrictEqual({})
})
pythOracle.methods
.addProduct({
asset_type: 'Crypto',
base: 'ETH',
description: 'ETH/USD',
quote_currency: 'USD',
symbol: 'Crypto.ETH/USD',
generic_symbol: 'ETHUSD',
})
.accounts({
fundingAccount: PublicKey.unique(),
productAccount: PublicKey.unique(),
tailMappingAccount: PublicKey.unique(),
})
.instruction()
.then((instruction) => {
const decoded = pythOracleCoder().instruction.decode(instruction.data)
expect(decoded?.name).toBe('addProduct')
expect(decoded?.data.asset_type).toBe('Crypto')
expect(decoded?.data.base).toBe('ETH')
expect(decoded?.data.description).toBe('ETH/USD')
expect(decoded?.data.quote_currency).toBe('USD')
expect(decoded?.data.symbol).toBe('Crypto.ETH/USD')
expect(decoded?.data.generic_symbol).toBe('ETHUSD')
})

pythOracle.methods
.updProduct({
asset_type: 'Crypto',
Expand Down
12 changes: 7 additions & 5 deletions src/anchor/coder/instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { IdlCoder } from './idl'
import { InstructionCoder } from '@coral-xyz/anchor'
import { Product } from '../..'

const MAX_METADATA_SIZE: number = 464

export type PythIdlInstruction = {
name: string
docs?: string[]
Expand Down Expand Up @@ -87,16 +89,16 @@ export class PythOracleInstructionCoder implements InstructionCoder {
throw new Error(`Unknown method: ${methodName}`)
}

/// updProduct has its own format
if (methodName === 'updProduct') {
/// updProduct and addProduct have their own format
if (methodName === 'updProduct' || methodName === 'addProduct') {
let offset = 0
for (const key of Object.keys(ix.productMetadata)) {
offset += buffer.subarray(offset).writeInt8(key.length)
offset += buffer.subarray(offset).write(key)
offset += buffer.subarray(offset).writeInt8(ix.productMetadata[key].length)
offset += buffer.subarray(offset).write(ix.productMetadata[key])
}
if (offset > 464) {
if (offset > MAX_METADATA_SIZE) {
throw new Error('The metadata is too long')
}
const data = buffer.subarray(0, offset)
Expand Down Expand Up @@ -134,8 +136,8 @@ export class PythOracleInstructionCoder implements InstructionCoder {
return null
}

/// updProduct has its own format
if (decoder.name === 'updProduct') {
/// updProduct and addProduct have their own format
if (decoder.name === 'updProduct' || decoder.name === 'addProduct') {
const product: Product = {}
let idx = 0
while (idx < data.length) {
Expand Down
9 changes: 8 additions & 1 deletion src/anchor/idl.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,14 @@
}
}
],
"args": []
"args": [
{
"name": "productMetadata",
"type": {
"defined": "ProductMetadata"
}
}
]
},
{
"name": "updProduct",
Expand Down
9 changes: 8 additions & 1 deletion src/anchor/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,14 @@ export type PythOracle = {
}
},
]
args: []
args: [
{
name: 'productMetadata'
type: {
defined: 'ProductMetadata'
}
},
]
},
{
name: 'updProduct'
Expand Down
2 changes: 1 addition & 1 deletion src/example_ws_single_feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const pythPublicKey = getPythProgramKeyForCluster(PYTHNET_CLUSTER_NAME)
// This example shows Crypto.SOL/USD
const feeds = [new PublicKey('H6ARHf6YXhGYeQfUzQNGk6rDNnLBQKrenN712K4AQJEG')]

const pythConnection = new PythConnection(connection, pythPublicKey, "confirmed", feeds)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drive by formatting

const pythConnection = new PythConnection(connection, pythPublicKey, 'confirmed', feeds)
pythConnection.onPriceChangeVerbose((productAccount, priceAccount) => {
// The arguments to the callback include solana account information / the update slot if you need it.
const product = productAccount.accountInfo.data.product
Expand Down