Created
August 23, 2024 21:50
-
-
Save rubpy/59a75d9ead42f78af1fc3fdb354d4270 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import web3 from "@solana/web3.js"; | |
import * as BufferLayout from "@solana/buffer-layout"; | |
////////////////////////////////////////////////// | |
export class BorshBoolean extends BufferLayout.Layout<boolean> { | |
constructor(property?: string) { | |
super(1, property); | |
} | |
decode(b: Buffer, offset = 0): boolean { | |
return !!Buffer.from(b.buffer, b.byteOffset, b.length)[offset]; | |
} | |
encode(src: boolean, b: Buffer, offset = 0): number { | |
Buffer.from(b.buffer, b.byteOffset, b.length).writeUInt8(src ? 1 : 0, offset); | |
return this.span; | |
} | |
} | |
export class BorshUInt64LE extends BufferLayout.Layout<bigint> { | |
constructor(property?: string) { | |
super(8, property); | |
} | |
decode(b: Buffer, offset = 0): bigint { | |
return Buffer.from(b.buffer, b.byteOffset, b.length).readBigUInt64LE(offset); | |
} | |
encode(src: bigint, b: Buffer, offset = 0): number { | |
Buffer.from(b.buffer, b.byteOffset, b.length).writeBigUInt64LE(BigInt(src), offset); | |
return this.span; | |
} | |
} | |
export class BorshInt64LE extends BufferLayout.Layout<bigint> { | |
constructor(property?: string) { | |
super(8, property); | |
} | |
decode(b: Buffer, offset = 0): bigint { | |
return Buffer.from(b.buffer, b.byteOffset, b.length).readBigInt64LE(offset); | |
} | |
encode(src: bigint, b: Buffer, offset = 0): number { | |
Buffer.from(b.buffer, b.byteOffset, b.length).writeBigInt64LE(BigInt(src), offset); | |
return this.span; | |
} | |
} | |
class BorshPublicKey extends BufferLayout.Layout<web3.PublicKey> { | |
constructor(property?: string) { | |
super(32, property); | |
} | |
decode(b: Uint8Array, offset?: number): web3.PublicKey { | |
offset = offset || 0; | |
const span = this.getSpan(b, offset); | |
return new web3.PublicKey( | |
Buffer.from(b.buffer, b.byteOffset, b.length).subarray(offset, offset + span), | |
); | |
} | |
encode(src: web3.PublicKey, b: Uint8Array, offset?: number): number { | |
offset = offset || 0; | |
const dstBuf = Buffer.from(b.buffer, b.byteOffset, b.length); | |
const srcBuf = src.toBuffer(); | |
return srcBuf.copy(dstBuf, offset); | |
} | |
} | |
export type PfTradeEventLayout = { | |
mint: web3.PublicKey; | |
solAmount: bigint; | |
tokenAmount: bigint; | |
isBuy: boolean; | |
user: web3.PublicKey; | |
timestamp: bigint; | |
virtualSolReserves: bigint; | |
virtualTokenReserves: bigint; | |
realSolReserves: bigint; | |
realTokenReserves: bigint; | |
}; | |
export const PfTradeEventLayout = BufferLayout.struct<PfTradeEventLayout>([ | |
new BorshPublicKey("mint"), | |
new BorshUInt64LE("solAmount"), | |
new BorshUInt64LE("tokenAmount"), | |
new BorshBoolean("isBuy"), | |
new BorshPublicKey("user"), | |
new BorshInt64LE("timestamp"), | |
new BorshUInt64LE("virtualSolReserves"), | |
new BorshUInt64LE("virtualTokenReserves"), | |
new BorshUInt64LE("realSolReserves"), | |
new BorshUInt64LE("realTokenReserves"), | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment