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

fix: use utf-8 string length calculation #83

Merged
merged 2 commits into from
Feb 6, 2025
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.22.0",
"version": "2.22.1",
"description": "Client for consuming Pyth price data",
"homepage": "https://pyth.network",
"main": "lib/index.js",
Expand Down
15 changes: 15 additions & 0 deletions src/__tests__/Anchor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ test('Anchor', (done) => {
expect(decoded?.data.generic_symbol).toBe('ETHUSD')
})

pythOracle.methods
.addProduct({
'😆': 'ÉTH',
})
.accounts({
fundingAccount: PublicKey.unique(),
productAccount: PublicKey.unique(),
tailMappingAccount: PublicKey.unique(),
})
.instruction()
.then((instruction) => {
const decoded = pythOracleCoder().instruction.decode(instruction.data)
expect(decoded?.data['😆']).toBe('ÉTH')
})

pythOracle.methods
.updProduct({
asset_type: 'Crypto',
Expand Down
8 changes: 4 additions & 4 deletions src/__tests__/Price.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ test('Handle price getting stale', (done) => {
expect(price.magic).toBe(Magic)
expect(price.version).toBe(Version)
expect(price.status).toBe(PriceStatus.Trading)
expect(price.flags.accumulatorV2).toBe(false);
expect(price.flags.messageBufferCleared).toBe(false);
expect(price.flags.accumulatorV2).toBe(false)
expect(price.flags.messageBufferCleared).toBe(false)
expect(price.feedIndex).toBe(0)

expect(parsePriceData(data, price.aggregate.publishSlot + MAX_SLOT_DIFFERENCE).status).toBe(PriceStatus.Trading)
Expand Down Expand Up @@ -143,8 +143,8 @@ test('Handle flags', (done) => {
const data = Buffer.from(b64_data, 'base64')
const price = parsePriceData(data)

expect(price.flags.accumulatorV2).toBe(true);
expect(price.flags.messageBufferCleared).toBe(true);
expect(price.flags.accumulatorV2).toBe(true)
expect(price.flags.messageBufferCleared).toBe(true)

done()
})
Expand Down
4 changes: 2 additions & 2 deletions src/anchor/coder/instructions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ export class PythOracleInstructionCoder implements InstructionCoder {
if (methodName === 'updProduct' || methodName === 'addProduct') {
let offset = 0
for (const key of Object.keys(ix.productMetadata)) {
offset += buffer.subarray(offset).writeUInt8(key.length)
offset += buffer.subarray(offset).writeUInt8(Buffer.byteLength(key))
offset += buffer.subarray(offset).write(key)
offset += buffer.subarray(offset).writeUInt8(ix.productMetadata[key].length)
offset += buffer.subarray(offset).writeUInt8(Buffer.byteLength(ix.productMetadata[key]))
offset += buffer.subarray(offset).write(ix.productMetadata[key])
}
if (offset > MAX_METADATA_SIZE) {
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export enum AccountType {
}

export type Flags = {
accumulatorV2: boolean,
messageBufferCleared: boolean,
accumulatorV2: boolean
messageBufferCleared: boolean
}

const empty32Buffer = Buffer.alloc(32)
Expand Down Expand Up @@ -110,7 +110,7 @@ export interface PriceData extends Base {
minPublishers: number
messageSent: number
maxLatency: number
flags: Flags,
flags: Flags
feedIndex: number
productAccountKey: PublicKey
nextPriceAccountKey: PublicKey | null
Expand Down Expand Up @@ -300,8 +300,8 @@ export const parsePriceData = (data: Buffer, currentSlot?: number): PriceData =>

/* tslint:disable:no-bitwise */
const flags = {
accumulatorV2: (flagBits & (1<<0)) !== 0,
messageBufferCleared: (flagBits & (1<<1)) !== 0,
accumulatorV2: (flagBits & (1 << 0)) !== 0,
messageBufferCleared: (flagBits & (1 << 1)) !== 0,
}
/* tslint:enable:no-bitwise */

Expand Down