project-serum / swap-ui

React Component for Swapping on the Serum DEX
Apache License 2.0
113 stars 117 forks source link

fix: 'Trying to access beyond buffer length' error #84

Closed secretshardul closed 3 years ago

secretshardul commented 3 years ago

Sometimes connection.onAccountChange() receives an empty buffer, causing an error

←→1 of 2 errors on the page
Unhandled Rejection (RangeError): Trying to access beyond buffer length
▶ 4 stack frames were collapsed.
parseTokenAccountData
src/utils/tokens.ts:73
  70 | 
  71 | export function parseTokenAccountData(data: Buffer): TokenAccount {
  72 |   // @ts-ignore
> 73 |   let { mint, owner, amount } = ACCOUNT_LAYOUT.decode(data);
  74 |   // @ts-ignore
  75 |   return {
  76 |     mint: new PublicKey(mint),

Try-catch exception handling is added and decodes are not attempted on empty buffers.