juicebox-technologies / urchin-sdk

Simple Playa API integration for NodeJS
1 stars 0 forks source link

Blockhash shared at .process() #22

Open mjzuppe opened 1 year ago

mjzuppe commented 1 year ago

In updating the SDK to have the option to return the Transaction data (for front end to process instead of back) we have an issue related to Blockhash.

what is blockhash? Blockhash is a sort of ID which denotes which Block of the blockchain is in progress. It updates a little more then every minute. For a transaction to be eligible for processing with the blockchain, it needs to closely reference the current block, else it's rejected as expired.

What's our problem? We currently query and generate the hash for each transaction ( tx.recentBlockhash = (await this.sdk.provider.connection.getLatestBlockhash()).blockhash;) which can lead to mismatched blockhashes leading to rejection - See example console.log below for recentBlockhash:

r {
  completed: true,
  taxonomy: [
    Transaction {
      signatures: [],
      feePayer: [PublicKey [PublicKey(5SKNwTC2Svdd7AbynWTSwPdyZitDcLVcFeQrkqQ137Hd)]],
      instructions: [Array],
      recentBlockhash: 'G7Q68GU5mgx9EigQk2yBgcQyG2XAnXf86iVQg68KwQwG',
      lastValidBlockHeight: undefined,
      nonceInfo: undefined,
      minNonceContextSlot: undefined,
      _message: undefined,
      _json: undefined
    },
    Transaction {
      signatures: [],
      feePayer: [PublicKey [PublicKey(5SKNwTC2Svdd7AbynWTSwPdyZitDcLVcFeQrkqQ137Hd)]],
      instructions: [Array],
      recentBlockhash: 'DV7rdscBAyQBvU2RmUPHxpJ6okxCUUcdQFeapUND49Lh',
      lastValidBlockHeight: undefined,
      nonceInfo: undefined,
      minNonceContextSlot: undefined,
      _message: undefined,
      _json: undefined
    }
  ]
}

When initializing urchin() we should have an optional parameter for blockhash if a user wants to supply. If not provided we should generate once, then when calling .process() we should reuse across all transactions